Ejemplo n.º 1
0
        /// <summary>
        ///   Add an edge if its equivalent does not already exist.
        /// </summary>
        public void AddIncidentEdge(GraphEdge e)
        {
            // We silently ignore duplicate edges.
            if (Edges.Any(edge => edge.ToString() == e.ToString()))
                return;

            edges.Add(e);
        }
Ejemplo n.º 2
0
 // Add an edge (but don't add duplicate edges)
 public void AddIncidentEdge(GraphEdge e)
 {
     foreach (GraphEdge edge in incidentEdges)
     {
         if (edge.ToString() == e.ToString())
         {
             return;
         }
     }
     incidentEdges.Add(e);
 }
Ejemplo n.º 3
0
        // AddEdge adds the edge, creating endpoint nodes if necessary.
        // Edge is added to adjacency list of from edges.
        public void AddEdge(string name1, string name2, string relationship)
        {
            AddNode(name1);                     // create the node if it doesn't already exist
            GraphNode n1 = nodeDict[name1];     // now fetch a reference to the node

            AddNode(name2);
            GraphNode n2 = nodeDict[name2];
            GraphEdge e  = new GraphEdge(n1, n2, relationship);

            n1.AddIncidentEdge(e);
        }
Ejemplo n.º 4
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++;
                }
            }
        }
Ejemplo n.º 5
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();
        }