Пример #1
0
        // Read RelationshipGraph whose filename is passed in as a parameter.
        // Build a RelationshipGraph in RelationshipGraph rg
        private static void ReadRelationshipGraph(string filename)
        {
            rg = new RelationshipGraph();                           // create a new RelationshipGraph object

            string name      = "";                                  // name of person currently being read
            int    numPeople = 0;

            string[] values;
            Console.Write("Reading file " + filename + "\n");
            try
            {
                string input = System.IO.File.ReadAllText(filename); // read file
                input = input.Replace("\r", ";");                    // get rid of nasty carriage returns
                input = input.Replace("\n", ";");                    // get rid of nasty new lines
                string[] inputItems = Regex.Split(input, @";\s*");   // parse out the relationships (separated by ;)
                foreach (string item in inputItems)
                {
                    if (item.Length > 2)                            // don't bother with empty relationships
                    {
                        values = Regex.Split(item, @"\s*:\s*");     // parse out relationship:name
                        if (values[0] == "name")                    // name:[personname] indicates start of new person
                        {
                            name = values[1];                       // remember name for future relationships
                            rg.AddNode(name);                       // create the node
                            numPeople++;
                        }
                        else
                        {
                            rg.AddEdge(name, values[1], values[0]); // add relationship (name1, name2, relationship)

                            // handle symmetric relationships -- add the other way
                            if (values[0] == "spouse" || values[0] == "hasFriend")
                            {
                                rg.AddEdge(values[1], name, values[0]);
                            }

                            // for parent relationships add child as well
                            else if (values[0] == "parent")
                            {
                                rg.AddEdge(values[1], name, "child");
                            }
                            else if (values[0] == "child")
                            {
                                rg.AddEdge(values[1], name, "parent");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write("Unable to read file {0}: {1}\n", filename, e.ToString());
            }
            Console.WriteLine(numPeople + " people read");
        }
Пример #2
0
        // Read RelationshipGraph whose filename is passed in as a parameter.
        // Build a RelationshipGraph in RelationshipGraph rg
        private static void ReadRelationshipGraph(string filename)
        {
            rg = new RelationshipGraph();                            // create a new RelationshipGraph object
            string name      = "";                                   // name of person currently being read
            int    numPeople = 0;                                    // counter of how many people are read

            string[] values;                                         // array of people in file
            Console.Write("Reading file " + filename + "\n");        // prompt user for the file
            try                                                      // begin processing contents of file
            {
                string input = System.IO.File.ReadAllText(filename); // read file into a variable
                input = input.Replace("\r", ";");                    // replace carriage returns with semicolons
                input = input.Replace("\n", ";");                    // replace new lines with semicolons
                string[] inputItems = Regex.Split(input, @";\s*");   // parse out the relationships with semicolons
                foreach (string item in inputItems)                  // begin processing each item in the new array
                {
                    if (item.Length > 2)                             // skip any empty relationships
                    {                                                // begin processing the current item
                        values = Regex.Split(item, @"\s*:\s*");      // parse out relationship from the name
                        if (values[0] == "name")                     // check to see if the current item is a new person
                        {                                            // begin processing the current name
                            name = values[1];                        // remember name for future relationships
                            rg.AddNode(name);                        // create the node in the relationship graph
                            numPeople++;                             // increment the number of people in the graph
                        }
                        else
                        {                                                       // begin processing the relationships of the current name
                            rg.AddEdge(name, values[1], values[0]);             // add the given relationship of the current name to the graph
                            if (values[0] == "spouse" || values[0] == "friend") // check for any symmetric relationships (i.e. friends or spouse)
                            {
                                rg.AddEdge(values[1], name, values[0]);         // handle the symmetric relationship by adding the relationship to graph
                            }
                            else if (values[0] == "parent")                     // check for any parent relationships (i.e. does name have a parent)
                            {
                                rg.AddEdge(values[1], name, "child");           // handle the parent relationship by adding the child relationship to the graph
                            }
                            else if (values[0] == "child")                      // check for children relationships of the current name (i.e. does name have children)
                            {
                                rg.AddEdge(values[1], name, "parent");          // handle the child relationship by adding a parent relationship to the graph
                            }
                        }                                                       // finished processing this item return to the beginning of the foreach loop
                    }
                }
            }
            catch (Exception e)                                                          // if there is an error throw an exception
            {                                                                            // begin handling the exception
                Console.Write("Unable to read file {0}: {1}\n", filename, e.ToString()); // write the error message to the console
            }                                                                            // proceed if there were no exceptions while reading
            Console.WriteLine(numPeople + " people read");                               // write the total number of people in the graph
        }                                                                                // the main function of Program.cs has finished executing
Пример #3
0
        // 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();
        }