} // the main function of Program.cs has finished executing private static void ShowPerson(string name) // Show the relationships a person is involved in { GraphNode n = rg.GetNode(name); // create a graphnode variable for name if (n != null) // check that the name is valid { Console.Write(n.ToString()); // write the name and its relationships to the console } else // if the name wasn't valid { Console.WriteLine("{0} not found", name); // write that the name wasn't valid to the console } }
// Show the relationships a person is involved in private static void ShowPerson(string name) { GraphNode n = rg.GetNode(name); if (n != null) { Console.Write(n.ToString()); } else { Console.WriteLine("{0} not found", name); } }
// bingo finds and prints the shortest path of relationship between two people private static void Bingo(string name1, string name2) { GraphNode n1 = rg.GetNode(name1); GraphNode n2 = rg.GetNode(name2); if (n1 != null && n2 != null && n1 != n2) { // label all nodes as "unvisited" foreach (GraphNode n in rg.nodes) { n.Label = "Unvisited"; } // create queue for BFS and a reversed (for future output) BFS spanning tree Queue <GraphNode> queue = new Queue <GraphNode>(); RelationshipGraph bfs = new RelationshipGraph(); // starts at root node n1.Label = "Visited"; queue.Enqueue(n1); // BFS while (queue.Count != 0) { GraphNode node = queue.Dequeue(); foreach (GraphEdge e in node.GetEdges()) { GraphNode to = e.ToNode(); if (to.Label != "Visited") { to.Label = "Visited"; queue.Enqueue(to); bfs.AddEdge(e.To(), e.From(), e.Relationship()); } // breaks if successfully found if (to == n2) { queue.Clear(); } } } // output if relationship is not found if (bfs.GetNode(name2) == null) { Console.WriteLine("No relationship found between {0}, {1}", name1, name2); } else // output relationship { // use stack to reverse the order in the tree Stack <string> relationships = new Stack <string>(); GraphNode child = bfs.GetNode(name2); while (child != bfs.GetNode(name1)) { foreach (GraphEdge e in child.GetEdges()) { relationships.Push(e.Relationship()); child = e.ToNode(); } } Console.Write(name2 + " is " + name1); foreach (string s in relationships) { Console.Write("'s " + s.Remove(0, 3)); } } } else { if (n1 == null) { Console.WriteLine("{0} not found ", name1); } if (n2 == null) { Console.WriteLine("{0} not found", name2); } if (n1 == n2) { Console.WriteLine("Please enter two different persons."); } } Console.WriteLine(); }