Ejemplo n.º 1
0
 public void AddNeighbor(DirectedGraphNode <T> neighborToAdd)
 {
     if (!m_Neighbors.ContainsKey(neighborToAdd.Data))
     {
         m_Neighbors.Add(neighborToAdd.Data, neighborToAdd);
     }
 }
Ejemplo n.º 2
0
 public DirectedGraphNode(DirectedGraphNode <T> initialNeighbor, T data) : this(data)
 {
     m_Neighbors.Add(data, initialNeighbor);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Search path - DFS algorithm to determine if there is a path from starting city to itself.
        /// </summary>
        /// <param name="nodes">Graph to search in</param>
        /// <param name="node">Node object to start from</param>
        /// <param name="startCity">Node name to start from</param>
        public static void SearchPath(Dictionary <string, DirectedGraphNode <string> > nodes, DirectedGraphNode <string> node, string startCity)
        {
            //Get the list of neighbors of the starting node.
            //And for each neighbor starting from the first one perform the following instructions
            //GetNeighbors returns a dictionary <string, DirectedGraphNode<string>>
            foreach (var neighbor in node.GetNeighbors())
            {
                //Check if the first neighbor is visited
                if (!neighbor.Value.IsVisited)
                {
                    //Check if path contains the data of the current neighbor (the value of the node)
                    if (!Path.Contains(neighbor.Value.Data))
                    {
                        //If it doesn't add the data to the path of visited nodes.
                        Path.Push(neighbor.Value.Data);
                    }

                    //Set the neighbor as visited.
                    neighbor.Value.IsVisited = true;

                    //Call SearchPath recursively for this neighbor.
                    SearchPath(nodes, neighbor.Value, startCity);
                }

                //If node is visited perform the following checks.

                //Check if the current node is the starting city and check if a path hasn't already been found.
                if (node.Data == startCity && !PathFound)
                {
                    //If both conditions are true then a path has been found and can be written to console.
                    Console.WriteLine(string.Join(" ", Path.Reverse()) + " " + startCity);
                    //Because we are in recursion this value must be set to false in order to avoid printing more info to the console.
                    PathFound = true;
                    return;
                }

                //Independent from the node being visited this row is reached when the node in question has no more unvisited neighbors
                //Or has no neighbors. This is where we remove from the Path variable the nodes that are not important to the final answer.

                /*
                 * For example we are at node NYC it has 2 kids PAR and TOK. We go down TOK and the path becomes: NYC -> TOK ->LAX->SFO & SFO has no kids.
                 * What we want to do is go back to NYC to check the path through PAR but we dont want TOK->LAX->SFO in the path of visited nodes because
                 * its not a correct path. That is why we pop from the stack.
                 * So we get to SFO and reach this line, we Pop it from the stack and  go back to LAX, we pop it as well and so on until we get to NYC
                 * we dont pop that. And then we go down the route via PAR.
                 */
                if (Path.Count > 0 && Path.Peek() != node.Data && !PathFound)
                {
                    Path.Pop();
                }
            }
        }