5

I am currently writing a program to model Dijkstra's algorithm, however I am having some trouble the graph in its current form below:

G = [['a', 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h', 'i', 'j'],
     [({'a', 'b'}, 4), ({'a', 'c'}, 6), ({'a', 'd'}, 8), ({'b', 'e'}, 1) ,
      ({'b', 'f'}, 9), ({'c', 'f'}, 2), ({'d', 'g'}, 7), ({'d', 'h'}, 1) ,
      ({'e', 'i'}, 2), ({'e', 'j'}, 7), ({'g', 'h'}, 2), ({'i', 'j'}, 4)]]

I want to get the graph in the form such as the one below

{ 'a': {'b': 4, 'c': 6, 'd': 8},
    'b': {'a': 4, 'e': 1, 'f': 9}, etc

Would this be possible?

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40

2 Answers2

5

You can use collections.defaultdict for this.

Code:

from collections import defaultdict

G = [['a', 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h', 'i', 'j'],
     [({'a', 'b'}, 4), ({'a', 'c'}, 6), ({'a', 'd'}, 8), ({'b', 'e'}, 1) ,
      ({'b', 'f'}, 9), ({'c', 'f'}, 2), ({'d', 'g'}, 7), ({'d', 'h'}, 1) ,
      ({'e', 'i'}, 2), ({'e', 'j'}, 7), ({'g', 'h'}, 2), ({'i', 'j'}, 4)]]

result = defaultdict(dict)
for edge in G[1]:
    v1, v2 = edge[0]
    result[v1][v2] = edge[1]
    result[v2][v1] = edge[1]

print(result)

Output:

defaultdict(<class 'dict'>,
            {'a': {'b': 4, 'c': 6, 'd': 8},
             'b': {'a': 4, 'e': 1, 'f': 9},
             'c': {'a': 6, 'f': 2},
             'd': {'a': 8, 'g': 7, 'h': 1},
             'e': {'b': 1, 'i': 2, 'j': 7},
             'f': {'b': 9, 'c': 2},
             'g': {'d': 7, 'h': 2},
             'h': {'d': 1, 'g': 2},
             'i': {'e': 2, 'j': 4},
             'j': {'e': 7, 'i': 4}})
Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
3

Here is a compact way using list comprehension:

d = {k: dict((list(v[0] ^ {k})[0], v[1]) for v in G[1] if k in v[0]) for k in G[0]}
print(d)
#{'a': {'b': 4, 'c': 6, 'd': 8},
# 'b': {'a': 4, 'e': 1, 'f': 9},
# 'c': {'a': 6, 'f': 2},
# 'd': {'a': 8, 'g': 7, 'h': 1},
# 'e': {'b': 1, 'i': 2, 'j': 7},
# 'f': {'b': 9, 'c': 2},
# 'g': {'d': 7, 'h': 2},
# 'h': {'d': 1, 'g': 2},
# 'i': {'e': 2, 'j': 4},
# 'j': {'e': 7, 'i': 4}}

Breaking this down:

  • {k: ... for k in G[0]} is iterating over the first element of G to get the keys of the output dictionary.
  • {k: (... for v in G[1] if k in v[0]) ... } is a generator expression over the second element in G, which will yield a value if the key k is contained in the set.
  • v[0] ^ {k} is the symmetric difference of the set with the key- this removes the key from the set.
  • list(v[0] ^ {k})[0] converts the set to a list and gets the first element. This assumes there will always be only 2 elements in the set, one of which is the key.
  • (list(v[0] ^ {k})[0], v[1]) makes a tuple where the second value is the number.
  • dict([(list(v[0] ^ {k})[0], v[1]) ... calls the dict constructor on this list of tuples.

Perhaps a slightly friendlier version, using more sensible variable names instead of tuple indexing and set.pop() instead of list indexing:

d = {edge: dict(((pair ^ {edge}).pop(), value) for pair, value in G[1] if edge in pair) 
     for edge in G[0]}
pault
  • 41,343
  • 15
  • 107
  • 149