2

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??

1

4 Answers 4

3

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']
Sign up to request clarification or add additional context in comments.

2 Comments

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?
pyscripter is an IDE only. Use pip install networkx for the module.
2

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

2 Comments

Could you please add here some parts of the solution and not only a link?
Your idea is excellent. Yes, we can set the edge weight to 1 and then use the Dijkstra.
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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.