Exemplo n.º 1
0
        /**
         * Removes the edge between the vertices at specified indices.
         *
         * @param first_index   The index of the first vertex.
         * @param second_index  The index of the second vertex.
         *
         * @throws ArgumentException exception if first_index or second_index
         * is negative or greater-or-equal than the number of vertices in the
         * graph.
         */
        public void RemoveEdge(int first_index, int second_index)
        {
            if (first_index < 0 || first_index >= Size || second_index < 0 || second_index >= Size)
            {
                throw new ArgumentException();
            }

            if (first_index != second_index)
            {
                Edges.RemoveEdge(first_index, second_index);
            }
        }
Exemplo n.º 2
0
    static void Test(string[] nodes, IEdgeCollection edges, int edgeCount)
    {
        var       rng       = new Random(edgeCount);
        var       edgeList  = new List <Tuple <string, string> >();
        Stopwatch stopwatch = new Stopwatch();

        // randomly generated edges
        stopwatch.Start();
        for (int i = 0; i < edgeCount; i++)
        {
            string node1 = nodes[rng.Next(nodes.Length)];
            string node2 = nodes[rng.Next(nodes.Length)];
            edges.AddEdge(node1, node2);
            edgeList.Add(new Tuple <string, string>(node1, node2));
        }
        var addElapsed = stopwatch.Elapsed;
        // non random lookups
        int nonRandomFound = 0;

        stopwatch.Start();
        foreach (var edge in edgeList)
        {
            if (edges.ContainsEdge(edge.Item1, edge.Item2))
            {
                nonRandomFound++;
            }
        }
        var nonRandomLookupElapsed = stopwatch.Elapsed;

        if (nonRandomFound != edgeList.Count)
        {
            Console.WriteLine("The edge collection {0} is not working right!", edges.GetType().FullName);
            return;
        }
        // random lookups
        int randomFound = 0;

        stopwatch.Start();
        for (int i = 0; i < edgeCount; i++)
        {
            string node1 = nodes[rng.Next(nodes.Length)];
            string node2 = nodes[rng.Next(nodes.Length)];
            if (edges.ContainsEdge(node1, node2))
            {
                randomFound++;
            }
        }
        var randomLookupElapsed = stopwatch.Elapsed;

        // remove all
        stopwatch.Start();
        foreach (var edge in edgeList)
        {
            edges.RemoveEdge(edge.Item1, edge.Item2);
        }
        var removeElapsed = stopwatch.Elapsed;

        Console.WriteLine("Test: {0} with {1} edges: {2}s addition, {3}s non-random lookup, {4}s random lookup, {5}s removal",
                          edges.GetType().FullName,
                          edgeCount,
                          addElapsed.TotalSeconds,
                          nonRandomLookupElapsed.TotalSeconds,
                          randomLookupElapsed.TotalSeconds,
                          removeElapsed.TotalSeconds);
    }