All paths between two nodes in a graph

I need to make an unformatted search search program (Breadth-first-Search) that takes two nodes and returns all the paths between them.

public void BFS(Nod start, Nod end) {
            Queue<Nod> queue = new Queue<Nod>();
            queue.Enqueue(start);
            while (queue.Count != 0)
            {
                Nod u = queue.Dequeue();
                if (u == end)  break;
                else
                {
                    u.data = "Visited";
                    foreach (Edge edge in u.getChildren())
                    {
                        if (edge.getEnd().data == "")
                        {
                            edge.getEnd().data = "Visited";
                            if (edge.getEnd() != end)
                            {
                                edge.getEnd().setParent(u);
                            }
                            else
                            {
                                edge.getEnd().setParent(u);
                                cost = 0; 
                                PrintPath(edge.getEnd(), true);
                                edge.getEnd().data = "";
                                //return;
                            }
                        }
                        queue.Enqueue(edge.getEnd());
                    }
                }
            }
        }

My problem is that I only get two paths instead of all, and I don’t know what to edit in my code to get them all. The input of my problem is based on this map:map

+5
source share
4 answers

BFS , . , , , . , , . , , .

+3

- : , BFS node, , .

1----2
 \    \
  3--- 4----5

, 1 5. 1, 2 3, 4, 5. , 4-5.

DFS, BFS . - , node, , node. ,

+3

- , . , , : , F(u, v, intermediate_list, no_of_vertices), u - ( ), v is intermediate_list - , , , , , , no_of_vertices - , , 2, v, . , , u, destination - v, no_of_vertices. F(u, v, {}, 2), F(u, v, {}, 3), ..., F(u, v, {}, V), , F , . , , .

: BFS: - , . , BFS, . v : (v, {v}, {v}), : (current_vertex, list_of_vertices_already_visited, current_path). , , e current_vertex, x list_of_vertices_already_visited, (x, list_of_vertices_already_visited + {x}, current_path -> x) , . , , .

+2

Sounds like homework. But a funny sight.

The following is pseudo-code, at first - depth at first, not breathing (therefore, it should be transformed into a queue type algorithm and contain errors, but the general meaning should be clear.

class Node{
  Vector[Link] connections;
  String name;
}

class Link{
  Node destination;
  int distance;
}

Vector[Vector[Node]] paths(Node source, Node end_dest, Vector[Vector[Node]] routes){
  for each route in routes{
    bool has_next = false;
    for each connection in source.connections{
      if !connection.destination in route {
        has_next = true;
        route.push(destination);
        if (!connection.destination == end_dest){
          paths(destination, end_dest, routes);
        }
      }
    }
    if !has_next {
      routes.remove(route) //watch out here, might mess up the iteration
    }
  }
  return routes;
}

Edit: Is this actually the answer to the question you are looking for? Or do you really want to find the shortest path? If this is the last, use Dijkstra's algorithm: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

0
source

All Articles