Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ideal algorithm for finding all paths in a graph [duplicate]

Let's take this graph for example:

graph

Now let's say I'm starting at vertex 3 and want to find vertex 7. Depth first search (depending on the implementation) will look at the children first. Now, in our example, for the sake of the argument, I start at vertex 2, then go to vertex 4 and vertex 2, returns to vertex and goes to vertex 7, problem solved.

What I'd like: I'd like to get all the possible paths that would get me from x to y (e.g. 3 to 7: 3,1,4,7 - 3,5,7 - 3,4,7 - 3,5,6,9,7). That I don't get from Depth first search.

What is the algorithm you'd suggest?

Thank you!

like image 679
None None Avatar asked Sep 23 '13 07:09

None None


People also ask

What is the fastest path finding algorithm?

Dijkstra's algorithm is used for our fastest path algorithm because it can find the shortest path between vertices in the graph. The coordinates on the arena are considered as the vertices in the graph.

How do you find all possible paths in a directed graph?

Finding all the possible paths in any graph in Exponential. It can be solved by using Backtracking. For DAG's we can do it using Depth first search(DFS). In DFS code, Start at any node, Go to the extreme dead end path and note down all the nodes visited in that path using some array or list.

Which one is the fastest algorithm to find all shortest path in a graph?

The Shortest Path Faster Algorithm (SPFA) is an improvement of the Bellman–Ford algorithm which computes single-source shortest paths in a weighted directed graph. The algorithm is believed to work well on random sparse graphs and is particularly suitable for graphs that contain negative-weight edges.

Which algorithm is used in graph?

Some Common Graph AlgorithmsBreadth First Search (BFS) Depth First Search (DFS) Dijkstra. Floyd-Warshall Algorithm.


1 Answers

You should use modified BFS algorithm (http://en.wikipedia.org/wiki/Breadth-first_search). On each vertex you should save list of neighbors, which are leading to this vertex(predecessors) instead of having just 1 neighbor leading to this vertex.

When you want to find all paths from this you just have to start on your end node and branch your path at every vertex, that have more than 1 predecessor and go with the way created by predecessors in each vertex until you get to start node with all branches you have.

EDIT: You can use DSF algorithm instead of BFS and modify it in a same way.

like image 112
Maciej Oziębły Avatar answered Oct 22 '22 10:10

Maciej Oziębły