public void BfsCountShortestNoChildNodesMatchPath() { BfsNode.Node <string> head = new BfsNode.Node <string>("bob"); var bfs = new BfsCountShortestPath(); var result = bfs.Search(head, "bob"); int expected = 0; Assert.AreEqual(expected, result); }
public void BfsCountShortestPath() { var alex = new BfsNode.Node <string>("Alex"); var rob = new BfsNode.Node <string>("Rob"); var garry = new BfsNode.Node <string>("Garry"); // Khalid is friends with alex, rob and garry var khalid = new BfsNode.Node <string>("Khalid") { Children = new List <BfsNode.Node <string> > { alex, rob, garry } }; // Andrew if friends with Alex and Khalid var andrew = new BfsNode.Node <string>("Andrew") { Children = new List <BfsNode.Node <string> > { khalid, alex } }; // Helen is friends with Andrew var helen = new BfsNode.Node <string>("Helen") { Children = new List <BfsNode.Node <string> > { andrew } }; // Bob is friends with Helen and Khalid // Shortest path to Alex = Bob -> Khalid -> Alex. // Longer path to Alex = Bob -> helen -> Andrew -> Alex. // Even longer path Alex = Bob -> helen -> Andrew -> Khalid -> Alex. BfsNode.Node <string> head = new BfsNode.Node <string>("Bob") { Children = new List <BfsNode.Node <string> > { helen, khalid } }; var bfs = new BfsCountShortestPath(); var result = bfs.Search(head, "Alex"); int expected = 2; Assert.AreEqual(expected, result); }
public void BfsIntegerFindNode() { BfsNode.Node <int> head = new BfsNode.Node <int>(1) { Children = new List <BfsNode.Node <int> > { new BfsNode.Node <int>(200), new BfsNode.Node <int>(500), new BfsNode.Node <int>(1000) { Children = new List <BfsNode.Node <int> > { new BfsNode.Node <int>(99) } } } }; BreadthFirstSearch bfs = new BreadthFirstSearch(); var result = bfs.Search(head, 1000); var foo = result.Children.FirstOrDefault().Name; Assert.AreEqual(99, foo); }
public void BfsStingFindNode() { BfsNode.Node <string> head = new BfsNode.Node <string>("bob") { Children = new List <BfsNode.Node <string> > { new BfsNode.Node <string>("Rob"), new BfsNode.Node <string>("Garry"), new BfsNode.Node <string>("Helen") { Children = new List <BfsNode.Node <string> > { new BfsNode.Node <string>("Andrew") } } } }; BreadthFirstSearch bfs = new BreadthFirstSearch(); var result = bfs.Search(head, "Helen"); var firstChildVal = result.Children.FirstOrDefault().Name; Assert.AreEqual("Andrew", firstChildVal); }