protected bool AddEdge(int first_vertex, int second_vertex, EdgeT edge) { if (first_vertex < 0 || first_vertex >= Size || second_vertex < 0 || second_vertex >= Size) { throw new ArgumentException(); } if (first_vertex != second_vertex) { Edges.AddEdge(first_vertex, second_vertex, edge); return(true); } return(false); }
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); }