Example #1
0
        // Show a person's descendants
        private static void ShowDescendants(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n == null)
            {
                Console.WriteLine("{0} not found", name);
                return;
            }

            List <GraphEdge>            childEdges = n.GetEdges("hasChild");
            Dictionary <string, string> output     = FindChildren(new Dictionary <string, string>(), childEdges, 0);

            if (output == null)
            {
                Console.WriteLine("No descendants found");
            }
            else
            {
                foreach (KeyValuePair <string, string> desc in output)
                {
                    Console.WriteLine("{0} is a {1}", desc.Key, desc.Value);
                }
            }
        }
Example #2
0
        /**
         * @param: string, name
         * Descendants returns children of [name], and calls GrandChildren() on each child
         **/
        private static void Descendants(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                int children = 0;
                List <GraphEdge> childEdges = n.GetEdges("hasChild");
                foreach (GraphEdge e in childEdges) // for each child, print its name
                {
                    Console.WriteLine("child: {0} ", e.To());
                    GrandChildren(e.To());
                    children++;
                }
                if (children == 0) // no children
                {
                    Console.WriteLine("{0} has no descendants", name);
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} has no descendants", name);
            }
        }
Example #3
0
        // Recursive function to find descendants
        private static Dictionary <string, string> FindChildren(Dictionary <string, string> dict, List <GraphEdge> lstEdges, int count)
        {
            if (!lstEdges.Any())
            {
                return(null);
            }

            string suffix = "child";

            if (count > 1)
            {
                suffix = "grand" + suffix;
            }
            if (count > 2)
            {
                int i = count;
                while (i > 2)
                {
                    suffix = "great " + suffix;
                    i--;
                }
            }

            foreach (GraphEdge edges in lstEdges)
            {
                dict.TryAdd(edges.To(), suffix);
                GraphNode temp = rg.GetNode(edges.To());
                if (temp != null)
                {
                    FindChildren(dict, temp.GetEdges("hasChild"), count++);
                }
            }
            return(dict);
        }
Example #4
0
        //Finds the children of 1 node and prints them all out. This function is used with
        //ShowDescendents to print off all of descendents.
        private static List <string> descend(string name, int generation)
        {
            List <string> children = new List <string>();
            GraphNode     n        = rg.GetNode(name);

            if (n != null)
            {
                List <GraphEdge> childrenEdges = n.GetEdges("hasChild");
                if (childrenEdges.Count != 0)
                {
                    if (generation == 0) //if its the first generation print Childen, otherwise the outide function will take care of it.
                    {
                        Console.Write("{0}'s children: ", name);
                        Console.WriteLine();
                    }
                    foreach (GraphEdge e in childrenEdges)
                    {
                        children.Add(e.To());
                        Console.Write("{0} ", e.To());
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
            return(children);
        }
Example #5
0
        // Show a person's siblings
        private static void ShowSiblings(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                Console.Write("{0}'s sibling(s): ", name);
                // look for person's parent(s)
                List <GraphEdge> parentEdges = n.GetEdges("hasParent");
                foreach (GraphEdge e in parentEdges)
                {
                    // look for parent's children other than self
                    List <GraphEdge> siblingEdges = e.ToNode().GetEdges("hasChild");
                    foreach (GraphEdge s in siblingEdges)
                    {
                        if (s.To() != name)
                        {
                            Console.Write("{0} ", s.To());
                        }
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
        }
Example #6
0
        // Prints the shortest connection between two people

        /**
         * Algorithm:
         * have a hash table of visited edges
         * have a queue for nodes to be visited
         * add the start node to the queue
         * while the queue is not empty and (&&) the top node is not the ending node:
         *      dequeue the top node and get the list of its edges
         *      for each of the top node's edges:
         *          if the child for the edge is not in the hash table of visited edges, enqueue the edge
         *          add the edge from the child node to the parent node to the hash table of visited edges
         * if the top node is the ending node
         *      current name keeps track of what node we're on, starting with the name of the ending node
         *      make relationship stack
         *      while current name != start
         *          search the hash table of edges for current name and get the value (name of parent node)
         *          search relationship graph to find relationship between current node and parent node, add it to relationship stack
         *          change current name to name of parent node
         *      while relationship stack is not empty
         *          pop and print top relationship
         * else
         *      print no relationship found
         */
        private static void Bingo(string startString, string endString)
        {
            GraphNode startNode = rg.GetNode(startString);
            GraphNode endNode   = rg.GetNode(endString);

            if (startNode != null && endNode != null)
            {
                Hashtable         visitedEdges = new Hashtable();
                Queue <GraphNode> nodeBFS      = new Queue <GraphNode>();
                nodeBFS.Enqueue(startNode);
                while (nodeBFS.Count != 0 && nodeBFS.Peek() != endNode)
                {
                    GraphNode        tempNode   = nodeBFS.Dequeue();
                    List <GraphEdge> childEdges = tempNode.GetEdges();
                    foreach (GraphEdge edge in childEdges)
                    {
                        if (!visitedEdges.Contains(edge.To()))
                        {
                            visitedEdges.Add(edge.To(), tempNode);      // keeps track of the spanning tree via edges
                            nodeBFS.Enqueue(edge.To());
                        }
                    }
                }
                if (nodeBFS.Count >= 1 && nodeBFS.Peek() == endNode)
                {
                    GraphNode      currentNode       = endNode;
                    GraphNode      parentNode        = endNode;
                    Stack <string> relationshipStack = new Stack <string>();
                    while (currentNode != startNode)
                    {
                        parentNode = (GraphNode)visitedEdges[currentNode];
                        List <GraphEdge> parentEdges = parentNode.GetEdges();
                        foreach (GraphEdge graphEdge in parentEdges)
                        {
                            if (graphEdge.To() == currentNode)
                            {
                                relationshipStack.Push(graphEdge.ToString());
                                break;
                            }
                        }
                        currentNode = parentNode;
                    }
                    while (relationshipStack.Count != 0)
                    {
                        Console.Write(relationshipStack.Pop() + "\n");
                    }
                }
                else
                {
                    Console.WriteLine("No relationship found between {0} and {1}", startString, endString);
                }
            }
            else
            {
                Console.WriteLine("{0} and/or {1} were not found", startString, endString);
            }
        }
Example #7
0
        // Show a person's decendents
        private static void ShowDecendents(string name)
        {
            GraphNode        root           = rg.GetNode(name);
            List <GraphNode> DecendentNodes = new List <GraphNode>();

            DecendentNodes.Add(root);
            List <GraphNode> nextDecendentNodes = new List <GraphNode>();
            // marking generations
            int           generation = 0;
            List <string> Children   = new List <string>();

            try
            {
                if (root.GetEdges("hasChild").Count == 0 && root != null)
                {
                    Console.Write(name + " has no decendents");
                }
                else if (root != null)
                {
                    Console.Write(name + "'s decendents\n");
                    while (DecendentNodes.Count > 0)
                    {
                        Children = new List <string>();
                        foreach (GraphNode n in DecendentNodes)
                        {
                            foreach (GraphEdge e in n.GetEdges("hasChild"))
                            {
                                nextDecendentNodes.Add(e.ToNode());
                                Children.Add(e.To());
                            }
                        }
                        if (generation == 0 && Children.Count > 0)
                        {
                            Console.Write("Children: ");
                            Console.WriteLine(String.Join(", ", (String[])Children.ToArray()));
                        }
                        else if (generation > 0 && Children.Count > 0)
                        {
                            for (int i = generation; i > 1; i--)
                            {
                                Console.Write("Great ");
                            }
                            Console.Write("Grandchildren: ");
                            Console.WriteLine(String.Join(", ", (String[])Children.ToArray()));
                        }
                        DecendentNodes     = nextDecendentNodes;
                        nextDecendentNodes = new List <GraphNode>();
                        generation++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write("Unable to read\n");
                Console.WriteLine(e.Message);
            }
        }
Example #8
0
        // Get parent node
        public List <GraphNode> GetParentNodes(string name)
        {
            GraphNode        child       = GetNode(name);
            List <GraphEdge> parentEdges = child.GetEdges("hasParent");

            List <GraphNode> parentNodes = new List <GraphNode>();

            foreach (GraphEdge parentEdge in parentEdges)
            {
                parentNodes.Add(GetNode(parentEdge.To()));
            }
            return(parentNodes);
        }
Example #9
0
        /**
         * @param: string, name
         * Descendants returns children of [name], and calls GreatGrandDescendants() on each grandchild
         **/
        public static void GrandChildren(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                List <GraphEdge> childEdges = n.GetEdges("hasChild");
                foreach (GraphEdge e in childEdges)
                {
                    Console.WriteLine("grandchild: {0} ", e.To()); //for each grandchild, print its name
                    GreatGrandDescendants(e.To(), 1);              //find grandchild's descendants
                }
            }
        }
Example #10
0
        // Get Child Nodes
        public List <GraphNode> GetChildNodes(string name)
        {
            GraphNode        n          = GetNode(name);
            List <GraphEdge> childEdges = n.GetEdges("hasChild");

            List <GraphNode> childNodes = new List <GraphNode>();

            foreach (GraphEdge childEdge in childEdges)
            {
                childNodes.Add(GetNode(childEdge.To()));
            }

            return(childNodes);
        }
Example #11
0
        // Show a person's descendants
        private static void ShowDescendants(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                Console.Write("{0}'s descendant(s): ", name);
                List <GraphEdge> descendantEdges = n.GetEdges("hasChild");
                if (descendantEdges.Count == 0)
                {
                    Console.Write("none");
                }

                int count = 0;
                while (descendantEdges.Count != 0)
                {
                    List <GraphEdge> descendantEdges2 = new List <GraphEdge>();
                    // title of descendants
                    if (count == 0)
                    {
                        Console.Write("\n\tchildren: ");
                    }
                    else if (count == 1)
                    {
                        Console.Write("\n\tgrandchildren: ");
                    }
                    else
                    {
                        Console.Write("\n\t" + string.Concat(Enumerable.Repeat("great ", (count - 1))) + "grandchildren: ");
                    }

                    foreach (GraphEdge e in descendantEdges)
                    {
                        Console.Write("{0} ", e.To());
                        // update descendantEdges with the children
                        foreach (GraphEdge d in e.ToNode().GetEdges("hasChild"))
                        {
                            descendantEdges2.Add(d);
                        }
                    }
                    descendantEdges = descendantEdges2;
                    count++;
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
        }
Example #12
0
        /// <summary>
        ///shows all of a persons descendants, depth first order right now....
        /// </summary>
        /// <param name="name"></param> name of person of whom descendants will be found
        private static void Descendants(string name)
        {
            //start at name
            GraphNode n = rg.GetNode(name);
            //look for children edges
            List <GraphEdge> childEdges = n.GetEdges("child");

            foreach (GraphEdge e in childEdges)
            {
                //recurse through the other end of the child edge using To()
                Descendants(e.To());
            }
            Console.WriteLine(n.Name());     //prints the name of the first name, which it shouldn't
        }
Example #13
0
        /* ShowDescendants prints the descendants of the GraphNode called name
         * @Param: name, the string of whose descendants are to be printed
         */
        private static void ShowDescendants(string name)
        {
            // find named node
            GraphNode beginning = rg.GetNode(name);

            if (beginning == null)
            {
                Console.WriteLine("{0} not found", name);
                return;
            }
            else
            {
                // find and print children, then print child's children
                List <GraphEdge> childEdges = beginning.GetEdges("hasChild");
                if (childEdges.Count == 0)
                {
                    Console.WriteLine("{0} has no descendants", name);
                }
                else
                {
                    Queue <GraphNode> descendants = new Queue <GraphNode>(childEdges.Count);
                    foreach (GraphEdge e in childEdges)
                    {
                        GraphNode initialChild = rg.GetNode(e.To());
                        initialChild.Label = 1.ToString();
                        descendants.Enqueue(initialChild);
                    }

                    while (descendants.Count != 0)
                    {
                        foreach (GraphNode descendant in descendants.ToList())
                        {
                            PrintDescendant(descendant);
                            // enqueue next edges and label them for printing
                            foreach (GraphEdge edge in descendant.GetEdges("hasChild"))
                            {
                                GraphNode nextChild = rg.GetNode(edge.To());
                                nextChild.Label = (int.Parse(rg.GetNode(edge.From()).Label) + 1).ToString();
                                descendants.Enqueue(rg.GetNode(edge.To()));
                            }
                            // reset label and dequeue
                            descendant.Label = "Unvisited";
                            descendants.Dequeue();
                        }
                    }
                    Console.WriteLine();
                }
            }
        }
Example #14
0
        /* ShowSiblings prints the siblings of the GraphNode called name
         * @Param: name, the string of whose siblings are to be printed
         */
        private static void ShowSiblings(string name)
        {
            // find named node
            GraphNode beginning = rg.GetNode(name);

            if (beginning == null)
            {
                Console.WriteLine("{0} not found", name);
                return;
            }
            else
            {
                // find named node's parents
                List <GraphEdge> parentEdges = beginning.GetEdges("hasParent");
                if (parentEdges.Count == 0)
                {
                    Console.WriteLine("No parents of {0} found", name);
                    return;
                }
                else
                {
                    List <GraphNode> parents = new List <GraphNode>(parentEdges.Count);
                    foreach (GraphEdge parentEdge in parentEdges)
                    {
                        parents.Add(rg.GetNode(parentEdge.To()));
                    }
                    // find named node's parent's other children
                    List <string> siblingStrings = new List <string>();
                    foreach (GraphNode parent in parents)
                    {
                        List <GraphEdge> siblingEdges = parent.GetEdges("hasChild");
                        foreach (GraphEdge siblingEdge in siblingEdges)
                        {
                            string possibleSibling = siblingEdge.To();
                            if ((!siblingStrings.Contains(possibleSibling)) && (possibleSibling != name))
                            {
                                siblingStrings.Add(possibleSibling);
                            }
                        }
                    }
                    // print the siblings
                    foreach (string sibling in siblingStrings)
                    {
                        Console.Write("{0} ", sibling);
                    }
                }
                Console.WriteLine();
            }
        }
Example #15
0
        /// <summary>
        ///shows all of a persons descendants as children, grandchildren, great grandchildren..
        /// </summary>
        /// <param name="name"></param> name of person of whom descendants will be found
        private static void Descendants(string name)
        {
            //start at name
            GraphNode n = rg.GetNode(name);

            //look for children edges
            List <GraphEdge> childEdges = n.GetEdges("child");

            foreach (GraphEdge e in childEdges)
            {
                //refference a name through an edge...
                Descendants(e.To());    //does this work?
            }

            Console.WriteLine(n.Name());     //does this work?
        }
Example #16
0
        private static void FindBingo(string name1, string name2)
        {
            GraphNode        beginning   = rg.GetNode(name1);
            GraphNode        end         = rg.GetNode(name2);
            List <GraphNode> connections = new List <GraphNode>();

            foreach (GraphEdge e in beginning.GetEdges())
            {
                if (e.To() == name2)
                {
                    // done
                    Console.WriteLine("Done!");
                }
                connections.Add(rg.GetNode(e.To()));
            }
        }
Example #17
0
        /**
         * @param: string, name; int, level
         * takes in the name of a particular descendant, and the number of "greats"
         * its descendants are. The method calls itself for each subsequend level of descendant
         * */
        public static void GreatGrandDescendants(string name, int level)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                List <GraphEdge> childEdges = n.GetEdges("hasChild");
                foreach (GraphEdge e in childEdges)
                {
                    for (int i = level; i > 0; i--) //prints the respective number of "greats"
                    {
                        Console.Write("great-");
                    }
                    Console.WriteLine("grandchild: {0} ", e.To());
                    GreatGrandDescendants(e.To(), level + 1); // for the desendant found, call the function on it's descendants
                }
            }
        }
Example #18
0
        // Show a person's friends
        private static void ShowFriends(string name)
        {
            GraphNode n = rg.GetNode(name);

            if (n != null)
            {
                Console.Write("{0}'s friends: ", name);
                List <GraphEdge> friendEdges = n.GetEdges("hasFriend");
                foreach (GraphEdge e in friendEdges)
                {
                    Console.Write("{0} ", e.To());
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
        }
Example #19
0
        private static void ShowFriends(string name)                 // Show a person's friends
        {
            GraphNode n = rg.GetNode(name);                          // create a graphnode variable for name

            if (n != null)                                           // check that the name is valid
            {                                                        // begin processing name
                Console.Write("{0}'s friends: ", name);              // write the initial statement to the console
                List <GraphEdge> friendEdges = n.GetEdges("friend"); // check all of name's relationships and create a list of name's friends
                foreach (GraphEdge e in friendEdges)                 // iterate through name's list of friends
                {
                    Console.Write("{0} ", e.To());                   // write the current friend's name to the console
                }
                Console.WriteLine();                                 // add a newline character after the last friend
            }                                                        // if the name the method is finished
            else                                                     // if the name wasn't valid
            {
                Console.WriteLine("{0} not found", name);            // write that the name wasn't valid to the console
            }
        }
Example #20
0
        private static void descendants(string name, int level)
        {
            GraphNode        n         = rg.GetNode(name);
            List <GraphEdge> childEdge = n.GetEdges("hasChild");

            if (level == 0)
            {
                Console.Write("The Descendants of ");
                Console.Write(name);
                Console.Write("\n\n");
                Console.WriteLine();
            }
            else if (level == 1)
            {
                Console.Write("Child --> ");
                Console.Write(name);
                // Console.Write("\n");
                Console.WriteLine();
            }
            else if (level == 2)
            {
                Console.Write("Grandchild --> ");
                Console.Write(name);
                // Console.Write("\n");
                Console.WriteLine();
            }
            else
            {
                for (int i = 2; i < level; i++)
                {
                    Console.Write("Great ");
                }
                Console.Write("Grandchild --> ");
                Console.Write(name);
                //  Console.Write("\n");
                Console.WriteLine();
            }
            foreach (GraphEdge e in childEdge)
            {
                descendants(e.To(), level + 1);
            }
        }
Example #21
0
        /**
         * Siblings function
         * @param: one person
         * author: Jonathan Winkle
         * returns: a list of all of the siblings of the parameter person
         */
        private static void Siblings(string person)
        {
            int totalSiblings             = 0;
            List <GraphNode> siblingsList = new List <GraphNode>();
            GraphNode        personNode   = rg.GetNode(person);
            List <GraphEdge> parentEdges  = personNode.GetEdges("hasParent");

            // if the person isn't an orphan
            if (parentEdges.Count != 0)
            {
                // for each parent (to account for step-siblings)
                foreach (GraphEdge edge in parentEdges)
                {
                    // get the parent node and find its children relationships
                    GraphNode        parentNode    = edge.To();
                    List <GraphEdge> siblingsEdges = parentNode.GetEdges("hasChild");

                    // for every child, add it to the sibling list
                    foreach (GraphEdge siblingEdge in siblingsEdges)
                    {
                        GraphNode sibNode = siblingEdge.To();
                        if (sibNode.Name != person && !siblingsList.Contains(sibNode))
                        {
                            siblingsList.Add(sibNode);
                            totalSiblings++;
                        }
                    }
                }
                Console.WriteLine("{0} has siblings: \n", person);
                foreach (GraphNode node in siblingsList)
                {
                    Console.WriteLine("{0}\n", node.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} has no siblings", person);
            }
            Console.WriteLine("{0} has {1} sibling(s)", person, totalSiblings);
        }
Example #22
0
        // Show all siblings of given person
        private static void ShowSiblings(string name)
        {
            List <GraphEdge> parentEdges = rg.GetNode(name).GetEdges("hasParent");

            if (parentEdges.Count == 0)
            {
                Console.WriteLine("No Siblings, No Parents, No body loves this person :(");
                return;
            }

            string           parentName            = parentEdges[0].To();
            GraphNode        parentNode            = rg.GetNode(parentName);
            List <GraphEdge> parentToChildrenEdges = parentNode.GetEdges("hasChild");

            foreach (GraphEdge parentToChildEdge in parentToChildrenEdges)
            {
                string childName = parentToChildEdge.To();
                if (childName != name)
                {
                    Console.WriteLine(childName);
                }
            }
        }
Example #23
0
        // List all the descendents of a person
        private static void ShowDescendents(string name)
        {
            //do a BFS starting at name to find children, then grandchildren, then great grandchildren...
            //keep counter to tell children, grandchildren... apart
            //GetNode(name) and add to Queue labeled as 0
            //  add every hasChild node to Queue and label as 1
            //  from those nodes
            //      add every hasChild node to Queue and label as 2
            //      from that list
            //          add every hasChild node to Queue and label as 3...
            //remove nodes from Queue
            //if node == 0: don't print
            //if node == 1: label as child
            //if node == 2: label as grandchild
            //if node >= 3: label as (node - 2)* great grandchild
            GraphNode      n          = rg.GetNode(name);
            Queue <String> descendent = new Queue <String>();

            //int counter = 0;
            descendent.Enqueue(name);
            if (n != null)
            {
                Console.Write("{0}'s descendents: ", name);
                //Queue<GraphEdge> childEdge = descendent.GetEdges("hasChild");
                List <GraphEdge> childEdges = n.GetEdges("hasChild");        //hasParent
                foreach (GraphEdge e in childEdges)
                {
                    Console.Write("{0} ", e.To());                          //e.From()
                    ShowDescendents(e.To());
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
        }
Example #24
0
        // Show a person's friends
        private static void ShowSiblings(string name)
        {
            GraphNode     n        = rg.GetNode(name);
            List <string> siblings = new List <string>();

            if (n != null)
            {
                Console.Write("{0}'s siblings: ", name);
                List <GraphEdge> parentEdges = n.GetEdges("hasParent");

                foreach (GraphEdge e in parentEdges)
                {
                    string           parentName    = e.To();
                    GraphNode        parentNode    = rg.GetNode(parentName);
                    List <GraphEdge> childrenEdges = parentNode.GetEdges("hasChild");

                    foreach (GraphEdge i in childrenEdges)
                    {
                        if (!siblings.Contains(i.To()) && i.To() != name)
                        {
                            siblings.Add(i.To());
                        }
                    }
                }

                foreach (string sibName in siblings)
                {
                    Console.Write("{0} ", sibName);
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
        }
Example #25
0
        // Find the shortest path of relationships between two people
        private static void Bingo(string start_person, string end_person)
        {
            //If the start and end person are the same person, inform the user
            if (start_person == end_person)
            {
                Console.Write(start_person + " and " + end_person + " are the same person.\n");
            }

            else
            {
                int depth_counter = 0;                                                        //Variable to keep track the minimum depth to get to end_person from start_person

                Dictionary <string, Boolean> dictionary = new Dictionary <string, Boolean>(); //new Dictionary to see if a node has been visited before
                foreach (GraphNode node_ in rg.nodes)
                {
                    dictionary.Add(node_.Name(), false);
                }

                Queue <string> q = new Queue <string>();    //Create a new queue

                dictionary[start_person] = true;            //Mark start_person as visited

                q.Enqueue(start_person);                    //Add the start_person to the queue

                //As long as the queue is not empty
                while (q.Count > 0)
                {
                    string           name_one      = q.Dequeue();           //Remove the next element in the queue
                    GraphNode        node_one      = rg.GetNode(name_one);
                    List <GraphEdge> AdjacentEdges = node_one.GetEdges();
                    depth_counter++;                                        //Increment the depth counter as we went one depth further

                    //Check each GraphEdge coming out of the current node
                    foreach (GraphEdge edge in AdjacentEdges)
                    {
                        if (name_one == end_person)                         //If the current node and the end_person are the same, break from the While Loop
                        {
                            break;
                        }

                        if (dictionary[edge.To()] == false)
                        {
                            dictionary[edge.To()] = true;                   //Update the visited dictionary
                            q.Enqueue(edge.To());                           //Add it to the queue
                        }
                    }
                }

                List <GraphNode> list_nodes = new List <GraphNode>();        //New lists for the nodes and the edges of the path to the end_person
                List <GraphEdge> list_edges = new List <GraphEdge>();

                Dictionary <string, Boolean> dictionary2 = new Dictionary <string, Boolean>();
                foreach (GraphNode node_ in rg.nodes)
                {
                    dictionary2.Add(node_.Name(), false);
                }

                int stack_depth = 0;                                     //Stack depth counter to keep track how far to go in the DFS
                Stack <GraphNode> stack_nodes = new Stack <GraphNode>(); //Create a new stack
                stack_nodes.Push(rg.GetNode(start_person));              //Push the start_person onto the stack

                while (true)
                {
                    while (stack_nodes.Count > 0)                               //As long as the stack is not empty
                    {
                        GraphNode node_one = stack_nodes.Pop();                 //Pop off the next element
                        list_nodes.Add(node_one);                               //Add it to the list

                        List <GraphEdge> edges_list = node_one.GetEdges();
                        list_edges.Add(edges_list[0]);

                        foreach (GraphEdge edge_two in edges_list)
                        {
                            stack_nodes.Push(rg.GetNode(edge_two.To()));       //Add each node coming off the edges to the Stack
                        }

                        stack_depth++;
                        if (stack_depth == depth_counter)                       //If the two depth counters are equal, break
                        {
                            break;
                        }
                    }

                    //Check to see if the first node in the list is the same as the start_person
                    //  and if the last node in the list is the same as the end_person
                    if (list_nodes[0] == rg.GetNode(start_person) && list_nodes[depth_counter - 1] == rg.GetNode(end_person))
                    {
                        break;
                    }
                }

                //Printing out the relationships between the start_person and the end_person
                int i = 0;
                foreach (GraphNode NODE in list_nodes)
                {
                    GraphEdge edge_label = list_edges[i];
                    Console.Write(NODE.Name() + edge_label.Label() + list_nodes[i + 1] + "\n");
                    i++;
                }
            }
        }
Example #26
0
        //List the descendants of an indvidual
        private static void ListDescendants(string name)
        {
            GraphNode node             = rg.GetNode(name);  //Get the node from the Relationship Graph of the name of the individual
            int       generation_count = 0;                 //Set the generation count to be zero

            //If the individual that the user is searching for does not exist in the current Relationship Graph
            if (node == null)
            {
                Console.Write("{0} not found\n", name);
            }

            else if (node.GetEdges("hasChild").Count == 0)       //If the node has no hasChild edge, print that there are no descendants
            {
                Console.Write("{0} has no descendants\n", name);
            }

            //Else, list out the descendants of the individual
            else
            {
                List <GraphEdge> descendantsList = node.GetEdges("hasChild");       //Get the edges hasChild of the individual

                List <GraphNode> nextDescendants    = new List <GraphNode>();       //Create two new lists of GraphNodes, one for the current generation of
                List <GraphNode> currentDescendants = new List <GraphNode>();       //descendants, and one for the following generation of descendants

                foreach (GraphEdge edge in descendantsList)                         //For every edge hasChild, add the node that the edge is directed towards
                {                                                                   //to the current descendants list of individuals
                    currentDescendants.Add(rg.GetNode(edge.To()));
                }

                while (currentDescendants.Count > 0)                                //As long as the currentDescendants List is not zero
                {
                    //The following if, if-else, and else statements determine the label of descendants to write to the console
                    if (generation_count == 0)
                    {
                        Console.Write(node.Name() + " Children:\n");
                    }

                    else if (generation_count == 1)
                    {
                        Console.Write(node.Name() + " Grandchildren:\n");
                    }

                    else
                    {
                        Console.Write(node.Name());
                        for (int i = 0; i < generation_count; i++)
                        {
                            Console.Write(" Great");
                        }
                        Console.Write(" GrandChildren:\n");
                    }

                    //For each node in the current descendants list
                    foreach (GraphNode descendant in currentDescendants)
                    {
                        Console.Write("{0}\n", descendant.Name());              //Write out the name of the descendant to the console
                        descendantsList = descendant.GetEdges("hasChild");      //Get a list of the edges hasChild from that descendant

                        foreach (GraphEdge edge1 in descendantsList)            //For each of those edges hasChild of the current descendant
                        {
                            nextDescendants.Add(rg.GetNode(edge1.To()));        //Add the node that the edge hasChild is directed towards
                        }
                    }

                    currentDescendants = nextDescendants;                       //Copy the nextDescendant List nodes to the currentDescandents List
                    nextDescendants    = new List <GraphNode>();                //Clear our the nextDescendant List
                    generation_count++;                                         //Increment the generation counter to be used for future labeling of descendants
                }
            }
        }
Example #27
0
        // Prints out the result from traversing the tree up Up times and down Down times. Used for Cousin finding cousins.
        //returns false if no cousins were found
        private static bool cousin(string name, int Up, int Down)
        {
            GraphNode node = rg.GetNode(name);

            if (node != null)
            {
                List <string> parents = new List <string>();
                parents.Add(name);
                List <string> lastparents = new List <string>(); //This is used to make sure you dont accidentally go down the path you took up.
                for (int i = 0; i < Up; i++)
                {
                    List <string> tempstring = new List <string>();
                    foreach (string p in parents)//Breadth First search
                    {
                        node = rg.GetNode(p);
                        List <GraphEdge> parentEdges = node.GetEdges("hasParent");
                        foreach (GraphEdge e in parentEdges)
                        {
                            tempstring.Add(e.To());//add the new parents to the temp string
                        }
                    }
                    lastparents = parents;       //last parents are kept track of so that when we start traversing down the tree we dont accidentally backtrack.
                    parents     = tempstring;    //new parents are updated after going up a generation
                    if (tempstring.Count() == 0) //if a parent cant be found then no cousins for this set will exist
                    {
                        return(false);
                    }
                }
                List <string> children = parents; //now we are going to go down Down times starting at parents.
                for (int i = 0; i < Down; i++)
                {
                    List <string> tempstring = new List <string>(); //this will be used to re update the children after going down a generation
                    foreach (string c in children)                  //breadth first search technique
                    {
                        node = rg.GetNode(c);
                        List <GraphEdge> childEdges = node.GetEdges("hasChild");
                        foreach (GraphEdge e in childEdges)
                        {
                            if (!lastparents.Contains(e.To()))//this is to make sure you dont go down where you came up from
                            {
                                tempstring.Add(e.To());
                            }
                        }
                    }
                    children = tempstring;       //update the new generation after going down once.
                    if (tempstring.Count() == 0) //if a child cant be found then no cousins for this set will exist
                    {
                        return(false);
                    }
                }

                foreach (string c in children)//print off all of the cousins
                {
                    Console.Write("{0}", c);
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("{0} not found", name);
            }
            return(true); //some cousins were found so return true
        }
Example #28
0
        // Prints the list of all descendants of one person

        /**
         * Algorithm:
         * make a queue to hold all descendants
         * make a hash table to hold the descendants and what level they are
         * enqueue ancestor
         * add the ancestor to the hash table with value 0
         * while descendants queue is not empty:
         *      dequeue the top descendant
         *      get a list of all their edges
         *      for each edge:
         *          if the label is "hasChild":
         *              enqueue the child node
         *              add the child node to the hash table with a value 1 greater than their parent's value in the hash table
         * keep a count
         * figure out how to print all descendants nicely //TODO
         */
        private static void Descendants(string ancestorString)
        {
            GraphNode ancestorNode = rg.GetNode(ancestorString);

            if (ancestorNode != null)
            {
                Queue <GraphNode> descendants    = new Queue <GraphNode>();
                Hashtable         allDescendants = new Hashtable();
                descendants.Enqueue(ancestorNode);
                allDescendants.Add(ancestorNode, 0);
                while (descendants.Count != 0)
                {
                    GraphNode        tempNode        = descendants.Dequeue();
                    List <GraphEdge> descentantEdges = tempNode.GetEdges("hasChild");
                    foreach (GraphEdge child in descentantEdges)
                    {
                        if (!allDescendants.Contains(child.To()))
                        {
                            descendants.Enqueue(child.To());
                            allDescendants.Add(child.To(), (int)allDescendants[tempNode] + 1);
                        }
                    }
                }
                //Printing nicely
                int         count          = 0;
                ICollection descendantKeys = allDescendants.Keys;
                Console.WriteLine("{0} has {1} descendants", ancestorString, allDescendants.Count - 1);
                ICollection Generations    = allDescendants.Values;
                int         numGenerations = 0;
                foreach (int i in Generations)
                {
                    if (i > numGenerations)
                    {
                        numGenerations = i;
                    }
                }
                while (true)
                {
                    if (count >= numGenerations)
                    {
                        break;
                    }
                    foreach (GraphNode descendant in descendantKeys)
                    {
                        if ((int)allDescendants[descendant] == count)
                        {
                            if (count == 0)
                            {
                                Console.WriteLine("{0}'s descendants are:", descendant.Name);
                            }
                            else if (count == 1)
                            {
                                Console.WriteLine("Child: {0}", descendant.Name);
                            }
                            else if (count == 2)
                            {
                                Console.WriteLine("Grandchild: {0}", descendant.Name);
                            }
                            else
                            {
                                string title = "Grandchild";
                                for (int i = 0; i < count; i++)
                                {
                                    title = "Great-" + title;
                                }
                                Console.WriteLine(title + ": {0}", descendant.Name);
                            }
                        }
                    }
                    count++;
                }
                //Console.WriteLine("{0} descendants found for {1}", numDescendants, ancestorString);
            }
            else
            {
                Console.WriteLine("{0} not found", ancestorString);
            }
        }
Example #29
0
        // Show the shortes chain of relationships between two people
        private static void Bingo(string person1, string person2)
        {
            Stack <string>        shortestpath = new Stack <string>();
            List <List <string> > explored     = new List <List <string> >();
            GraphNode             node1        = rg.GetNode(person1);
            GraphNode             node2        = rg.GetNode(person2);
            List <string>         temp         = new List <string>();

            temp.Add(person1);
            temp.Add(null);
            temp.Add(null);
            explored.Add(temp); //Add something to explored so the count is greater then 0.

            if ((node1 != null) && (node2 != null))
            {
                for (int i = 0; i < explored.Count(); i++)
                {
                    node1 = rg.GetNode(explored[i][0]);
                    if (node1 != null)
                    {
                        List <GraphEdge> edges = node1.GetEdges();
                        foreach (GraphEdge e in edges)
                        {
                            List <string> temp1    = new List <string>();
                            bool          contains = false;
                            temp1.Add(e.To());          //next node
                            temp1.Add(node1.Name);      //current node
                            temp1.Add(e.Label);         //current node is a e.Label to next node
                            temp1.Add(i.ToString());    //the list location from last. This will be used to retrace steps.
                            for (int j = 0; j < explored.Count; j++)
                            {
                                //Check to see if the node has already been explored
                                if (((explored[j][0] == node1.Name) && (explored[j][1] == e.To())) || ((explored[j][1] == node1.Name) && (explored[j][0] == e.To())))
                                {
                                    contains = true;
                                }
                            }
                            if (!contains)           //Continue if node is unexplored
                            {
                                explored.Add(temp1); //Add to list of explored nodes
                                if (e.To() == person2)
                                {
                                    int j = explored.Count - 1;
                                    while (j != 0)
                                    {
                                        shortestpath.Push(explored[j][1] + " " + explored[j][2] + " " + explored[j][0]);
                                        j = Convert.ToInt32(explored[j][3]); //Use "breadcrumbs" to trace your way back.
                                    }
                                    while (shortestpath.Count() != 0)
                                    {
                                        Console.Write(shortestpath.Pop());
                                        Console.WriteLine();
                                    }
                                    return;//break out of everything cause you're done!
                                }
                            }
                        }
                    }
                }
                Console.Write("No Connection"); //no connection has been made
            }
            else
            {
                Console.WriteLine("{0} or {1} not found", person1, person2);//one of the names is not present in graph.
            }
        }
Example #30
0
        //Bingo to show a relationship between two people
        private static void Bingo(string person_1, string person_2)
        {
            GraphNode person1 = rg.GetNode(person_1);

            person1.Label = "visited";                                                         //set as visited so cycles are not allowed
            GraphNode person2 = rg.GetNode(person_2);
            Dictionary <GraphNode, GraphNode> dict  = new Dictionary <GraphNode, GraphNode>(); //dict to store each nodes predecessor
            Dictionary <GraphNode, GraphEdge> dict2 = new Dictionary <GraphNode, GraphEdge>();
            Queue <GraphEdge> queue = new Queue <GraphEdge>();                                 //queue for BFS
            List <GraphEdge>  first = person1.GetEdges();

            //check for immediate bingo
            foreach (GraphEdge e in first)
            {
                if (rg.GetNode(e.To()) == person2)
                {
                    Console.WriteLine(e.ToString());
                    return;
                }
                queue.Enqueue(e);
                rg.GetNode(e.To()).Label = "visited";
                dict.Add(rg.GetNode(e.To()), person1);
                dict2.Add(rg.GetNode(e.To()), e);
            }

            bool broken = false;                                        //check if person2 is found and inner loop is broken

            while (queue.Count > 0)
            {
                GraphEdge next_edge   = queue.Dequeue();
                GraphNode next_person = rg.GetNode(next_edge.To());
                foreach (GraphEdge e in next_person.GetEdges())
                {
                    if (rg.GetNode(e.To()) == person2)
                    {
                        dict.Add(person2, next_person);
                        dict2.Add(person2, e);
                        broken = true;
                        break;
                    }
                    else
                    {
                        if (rg.GetNode(e.To()).Label != "visited")
                        {
                            queue.Enqueue(e);
                            dict.Add(rg.GetNode(e.To()), next_person);
                            dict2.Add(rg.GetNode(e.To()), e);
                            rg.GetNode(e.To()).Label = "visited";
                        }
                    }
                }
                if (broken)
                {
                    break;
                }
            }

            //traceback to person1
            GraphNode parent = dict[person2];
            GraphNode child  = person2;

            //get non-symmetric relationships
            if (child.GetRelationship(parent.Name) == null)
            {
                Console.WriteLine(parent.GetRelationship(child.Name));
            }
            else
            {
                Console.WriteLine(child.GetRelationship(parent.Name).ToString());
            }

            while (parent != person1)
            {
                child  = parent;
                parent = dict[parent];

                //get non-symmetric relationships
                if (child.GetRelationship(parent.Name) == null)
                {
                    Console.WriteLine(parent.GetRelationship(child.Name));
                }
                else
                {
                    Console.WriteLine(child.GetRelationship(parent.Name).ToString());
                }
            }

            reset_label();
        }