1

I'm trying to find the shortest path from a vertex to another of a connected, unweighted graph.

In this problem,the distance from a vertex to its adjacent vertex will be equal to 1.ie., if a graph with edges (a,b),(a,c) is considered, the distance from a to b and c will be 1 and the distance from b to c will be 2. Also, an adjacency list is maintained to store all the adjacent vertices of each vertex.

So, is there any algorithms to find all the shortest paths for the given problem??

Gayathri BS
  • 37
  • 1
  • 5
  • 1
    possible duplicate of [the best shortest path algorithm](http://stackoverflow.com/questions/1846836/the-best-shortest-path-algorithm) – dting May 02 '15 at 08:51

4 Answers4

2

You could use dijkstra's algorithm to find the distance.

Here's one way to do using networkx

In [28]: import networkx as nx

Create a grpah with nodes a, b, c where links are a, b and 'a, c'

In [29]: g = nx.Graph()

In [30]: g.add_edge('a', 'b')

In [31]: g.add_edge('a', 'c')

Then using nx.dijkstra_path_length() find the distance between b and c

In [32]: nx.dijkstra_path_length(g, 'b', 'c')
Out[32]: 2

Also, you can find the path trail using dijkstra_path()

In [33]: nx.dijkstra_path(g, 'b', 'c')
Out[33]: ['b', 'a', 'c']

You could also use shortest_path() for path between b and c

In [34]: nx.shortest_path(g, source='b',target='c')
Out[34]: ['b', 'a', 'c']
Zero
  • 74,117
  • 18
  • 147
  • 154
  • I tried it out, but the package networkx is not there in the pyscipter that i am using. The version of python installed is 2.7 and the version of pyscripter is 2.53.. Does it support networkx? – Gayathri BS May 04 '15 at 08:15
  • pyscripter is an IDE only. Use `pip install networkx` for the module. – Zero May 04 '15 at 08:34
1

You can find all the paths with a function then choose the path with minimum length.

But note that this problem is more based on your search algorithm, for example with a BFS algorithm :

You can use the following function that return a generator of paths :

def all_paths(graph, start, goal):
    queue = [(start, [start])]
    while queue:
        (v, path) = queue.pop(0)
        for next in graph[v] - set(path):
            if next == goal:
                yield path + [next]
            else:
                queue.append((next, path + [next])) 

And find the minimum path with min functions with len as its key :

min_path = min(all_paths(graph, start, goal),key=len)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

Dijkstra algorithm solve "the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized".

http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

So, i think that you can solve this with Dijkstra where the distance from a vertex to its adjacent vertex is equal for every path between two vertex.

Anyway, you can use BFS http://en.wikipedia.org/wiki/Breadth-first_search

eduardogr
  • 391
  • 5
  • 16
0

You can use BFS from that point: http://en.wikipedia.org/wiki/Breadth-first_search

You can also use Floyd–Warshall algorithm which runs O(n^3) if time is not an issue and you want simplicity: http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

Neithrik
  • 2,002
  • 2
  • 20
  • 33