5

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.

Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41
  • 3
    Dictionnaries can be access with dictionnary[ 'value' ]; because it is a key-value pair. Welcome to Stack Overflow and I highly respect those who reads the rules. Good luck! – richerlariviere May 01 '15 at 18:27

3 Answers3

6

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.

paidhima
  • 2,312
  • 16
  • 13
2

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
1

I guess these give you some ideas:

for dict in g:
     print dict.get("B","")

for dict in g:
     print dict.keys() #or dict.values()

for dict in g:
     print dict["B"]
pjsofts
  • 170
  • 4
  • 18