Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest path algorithm using dictionaries [Python]

This is my first question and actually my first time trying this but I read the rules of the questions and I hope my question comply with all of them.

I have a project for my algorithm subject, and it is to design a gui for dijkstra shortest path algorthim. I chose to use python because it is a language that I would like to master. I have been trying for more than a week actually and I am facing troubles all the way. But anyways this is good fun :)!

I chose to represent my directed graph as a dictionary in this way :

 g= {'A': {"B": 20, 'D': 80, 'G' :90}, # A can direct to B, D and G
'B': {'F' : 10},
'F':{'C':10,'D':40},
'C':{'D':10,'H':20,'F':50},
'D':{'G':20},
'G':{'A':20},
'E':{'G':30,'B':50},
'H':None}  # H is not directed to anything, but can accessed through C

so the key is the vertice and the value is the linked vetrices and the weights. This is an example of a graph but I was planning to ask the user to input their own graph details and examine the shortest path between each two nodes [start -> end] The problem is however that I don't even know how to access the inner dictionary so I can work on the inner paramteters, and I tried many ways like those two:

for i in g:
    counter = 0
    print g[i[counter]]     # One
    print g.get(i[counter]) # Two

but the both give me the same output which is: (Note that I can't really access and play with the inner paramters)

{"B": 20, 'D': 80, 'G' :90}
{'F' : 10}
{'C':10,'D':40}
{'D':10,'H':20,'F':50}
{'G':20}
{'A':20}
{'G':30,'B':50}
None

So my question is, could you please help me with how to access the inner dictionaries so I can start working on the algorithm itself. Thanks a lot in advance and thanks for reading.

like image 454
Ahmed Al-haddad Avatar asked May 01 '15 18:05

Ahmed Al-haddad


People also ask

How do you find the shortest path in Python?

To find the shortest path or distance between two nodes, we can use get_shortest_paths() . If we're only interested in counting the unweighted distance, then we can do the following: import igraph as ig import matplotlib. pyplot as plt # Find the shortest path on an unweighted graph g = ig.

How do I use Dijkstra in Python?

1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty. 2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE.


Video Answer


2 Answers

This is actually not so hard, and should make complete sense once you see it. Let's take your g. We want to get the weight of the 'B' connection from the 'A' node:

>>> d = g['A']
>>> d
{"B": 20, 'D': 80, 'G' :90}
>>> d['B']
20
>>> g['A']['B']
20

Using g['A'] gets us the value of the key in dictionary g. We can act directly on this value by referring to the 'B' key.

like image 94
paidhima Avatar answered Oct 01 '22 11:10

paidhima


Using a for loop will iterate over the keys of a dictionary, and by using the key, you can fetch the value that is associated to the key. If the value itself is a dictionary, you can use another loop.

for fromNode in g:
    neighbors = g[fromNode]
    for toNode in neighbors:
        distance = neighbors[toNode]
        print("%s -> %s (%d)" % (fromNode, toNode, distance))

Note that for this to work, you should use an empty dictionary {} instead of None when there are no neighbors.

like image 44
Aasmund Eldhuset Avatar answered Oct 01 '22 11:10

Aasmund Eldhuset