public void GraphSearchTest() { // this is the example graph at http://www.codeproject.com/cs/miscctrl/quickgraph.asp Graph <BasicNode> g = new Graph <BasicNode>(); BasicNode u = new BasicNode("u"); BasicNode v = new BasicNode("v"); BasicNode w = new BasicNode("w"); BasicNode x = new BasicNode("x"); BasicNode y = new BasicNode("y"); BasicNode z = new BasicNode("z"); g.Nodes.Add(u); g.Nodes.Add(v); g.Nodes.Add(w); g.Nodes.Add(x); g.Nodes.Add(y); g.Nodes.Add(z); g.AddEdge(u, v); g.AddEdge(u, x); g.AddEdge(v, y); g.AddEdge(y, x); g.AddEdge(x, v); g.AddEdge(w, u); g.AddEdge(w, y); g.AddEdge(w, z); DepthFirstSearch <BasicNode> dfs = new DepthFirstSearch <BasicNode>(g); dfs.DiscoverNode += delegate(BasicNode node) { Console.WriteLine("discover " + node); }; dfs.FinishNode += delegate(BasicNode node) { Console.WriteLine("finish " + node); }; dfs.DiscoverEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("discover " + edge); }; dfs.TreeEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("tree edge " + edge); }; dfs.FinishTreeEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("finish tree edge " + edge); }; dfs.CrossEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("cross edge " + edge); }; dfs.BackEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("back edge " + edge); }; Console.WriteLine("dfs from u:"); dfs.SearchFrom(u); Console.WriteLine(); Console.WriteLine("dfs from w:"); dfs.SearchFrom(w); Console.WriteLine(); dfs.Clear(); Console.WriteLine("cleared dfs from w:"); dfs.SearchFrom(w); Console.WriteLine(); Console.WriteLine("bfs:"); BreadthFirstSearch <BasicNode> bfs = new BreadthFirstSearch <BasicNode>(g); IndexedProperty <BasicNode, double> distance = g.CreateNodeData <double>(Double.PositiveInfinity); bfs.DiscoverNode += delegate(BasicNode node) { Console.WriteLine("discover " + node); }; bfs.FinishNode += delegate(BasicNode node) { Console.WriteLine("finish " + node); }; bfs.TreeEdge += delegate(Edge <BasicNode> edge) { Console.WriteLine("tree edge " + edge); distance[edge.Target] = distance[edge.Source] + 1; }; // compute distances from w distance[w] = 0; bfs.SearchFrom(w); Console.WriteLine("distances from w:"); foreach (BasicNode node in g.Nodes) { Console.WriteLine("[" + node + "] " + distance[node]); } Assert.Equal(2.0, distance[x]); Console.WriteLine(); Console.WriteLine("distances from w:"); DistanceSearch <BasicNode> dists = new DistanceSearch <BasicNode>(g); dists.SetDistance += delegate(BasicNode node, int dist) { Console.WriteLine("[" + node + "] " + dist); }; dists.SearchFrom(w); Console.WriteLine(); BasicNode start = z, end; (new PseudoPeripheralSearch <BasicNode>(g)).SearchFrom(ref start, out end); Console.WriteLine("pseudo-peripheral nodes: " + start + "," + end); StrongComponents <BasicNode> scc = new StrongComponents <BasicNode>(g); int count = 0; scc.AddNode += delegate(BasicNode node) { Console.Write(" " + node); }; scc.BeginComponent += delegate() { Console.Write("["); count++; }; scc.EndComponent += delegate() { Console.Write("]"); }; Console.Write("strong components reachable from w (topological order): "); scc.SearchFrom(w); Assert.Equal(4, count); Console.WriteLine(); count = 0; StrongComponents2 <BasicNode> scc2 = new StrongComponents2 <BasicNode>(g); scc2.AddNode += delegate(BasicNode node) { Console.Write(" " + node); }; scc2.BeginComponent += delegate() { Console.Write("["); count++; }; scc2.EndComponent += delegate() { Console.Write("]"); }; Console.Write("strong components reachable from w (rev topological order): "); scc2.SearchFrom(w); Assert.Equal(4, count); Console.WriteLine(); #if false Console.WriteLine("CyclicDependencySort:"); List <BasicNode> schedule = CyclicDependencySort <BasicNode> .Schedule(g, delegate(BasicNode node, ICollection <BasicNode> available) { if (node == x) { return(available.Contains(u)); } else { return(false); } }, g.Nodes); foreach (BasicNode node in schedule) { Console.Write(node + " "); } Console.WriteLine(); Assert.Equal <int>(6, schedule.Count); // order should be: w u x v y z (z can be re-ordered) Assert.Equal <BasicNode>(w, schedule[0]); Assert.Equal <BasicNode>(u, schedule[1]); //Assert.Equal<BasicNode>(z,schedule[5]); #endif }