0

I am trying to print all possible paths using Floyd-Warshall algorithm and networkx (Python). The thing is, I don't know how to do it properly (readable, as a list). I have these paths from my graph:

X = nx.floyd_warshall(gra)
Y = {a: dict(b) for a, b in X.items()}
print(Y)

and it returns this:

{(0, 0): {(0, 0): 0, (1, 0): 1, (0, 1): 1, (1, 1): 2, (0, 2): 2, (1, 2): 3}, (1, 0): {(1, 0): 0, (0, 0): 1, (1, 1): 1, (0, 1): 2, (0, 2): 3, (1, 2): 2}, (0, 1): {(0, 1): 0, (0, 0): 1, (1, 1): 1, (0, 2): 1, (1, 0): 2, (1, 2): 2}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 1): 1, (1, 2): 1, (0, 0): 2, (0, 2): 2}, (0, 2): {(0, 2): 0, (0, 1): 1, (1, 2): 1, (0, 0): 2, (1, 0): 3, (1, 1): 2}, (1, 2): {(1, 2): 0, (1, 1): 1, (0, 2): 1, (0, 0): 3, (1, 0): 2, (0, 1): 2}}

How can I convert it in a more readable format? For example, is it possible to print all possible paths one by one? I would like an output like this:

[(0, 0), (1, 0)]
[(1, 0), (1, 1)]
[(0, 0), (1, 0), (1, 1)]
...
[(0, 0), (1, 0), (1, 1), (0, 1), (0, 2), (1, 2)]

Alternatively, is it possible to print them as a JSON (or like this):

{(0, 0): 
   {(0, 0): 0, 
    (1, 0): 1, 
    (0, 1): 1, 
    (1, 1): 2, 
    (0, 2): 2, 
    (1, 2): 3},  
(1, 0): 
   {(1, 0): 0, 
    (0, 0): 1, 
    (1, 1): 1, 
    (0, 1): 2, 
    (0, 2): 3, 
    (1, 2): 2}, 

[......]

    (0, 1): 2}}

Thanks...

hellomynameisA
  • 546
  • 1
  • 7
  • 28
  • Result of function looks like a table, is that Adjacency matrix? – jupiterbjy Jun 17 '20 at 13:55
  • Not really... I have this dictionary with all possible paths between 2, 3, 4..., n nodes and I would like to print every possible path (like, between two adiacent nodes, then three adiacent ones, four, ..., till the most distant ones) – hellomynameisA Jun 17 '20 at 13:57
  • Seems like you need to apply algorithm [again](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Path_reconstruction) to reconstruct route from your minimum distance [matrix](https://pastebin.com/pWVK42uT). – jupiterbjy Jun 17 '20 at 14:37
  • Yes... I just thought about another option: is it possible to print the whole dictionary as a json? – hellomynameisA Jun 17 '20 at 14:40

1 Answers1

1

For printing Json:

import json


a = {(0, 0): {(0, 0): 0, (1, 0): 1, (0, 1): 1, (1, 1): 2, (0, 2): 2, (1, 2): 3}, (1, 0): {(1, 0): 0, (0, 0): 1, (1, 1): 1, (0, 1): 2, (0, 2): 3, (1, 2): 2}, (0, 1): {(0, 1): 0, (0, 0): 1, (1, 1): 1, (0, 2): 1, (1, 0): 2, (1, 2): 2}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 1): 1, (1, 2): 1, (0, 0): 2, (0, 2): 2}, (0, 2): {(0, 2): 0, (0, 1): 1, (1, 2): 1, (0, 0): 2, (1, 0): 3, (1, 1): 2}, (1, 2): {(1, 2): 0, (1, 1): 1, (0, 2): 1, (0, 0): 3, (1, 0): 2, (0, 1): 2}}


def dict_key_convert(dic):
    converted = {}
    for key, item in dic.items():
        if isinstance(item, dict):
            sub_dict = dict_key_convert(item)
            converted[str(key)] = sub_dict
        else:
            converted[str(key)] = item

    return converted


# print(json.dumps(dict_key_convert(a), indent=2))
with open('temp.json', 'w') as file:
    json.dump(dict_key_convert(a), file, indent=2)

Results:

{
  "(0, 0)": {
    "(0, 0)": 0,
    "(1, 0)": 1,
    "(0, 1)": 1,
    "(1, 1)": 2,
    "(0, 2)": 2,
    "(1, 2)": 3
  },
  "(1, 0)": {
    "(1, 0)": 0,
    "(0, 0)": 1,
    "(1, 1)": 1,
    "(0, 1)": 2,
    "(0, 2)": 3,
    "(1, 2)": 2
  },
  "(0, 1)": {
    "(0, 1)": 0,
    "(0, 0)": 1,
    "(1, 1)": 1,
    "(0, 2)": 1,
    "(1, 0)": 2,
    "(1, 2)": 2
  },
  "(1, 1)": {
    "(1, 1)": 0,
    "(1, 0)": 1,
    "(0, 1)": 1,
    "(1, 2)": 1,
    "(0, 0)": 2,
    "(0, 2)": 2
  },
  "(0, 2)": {
    "(0, 2)": 0,
    "(0, 1)": 1,
    "(1, 2)": 1,
    "(0, 0)": 2,
    "(1, 0)": 3,
    "(1, 1)": 2
  },
  "(1, 2)": {
    "(1, 2)": 0,
    "(1, 1)": 1,
    "(0, 2)": 1,
    "(0, 0)": 3,
    "(1, 0)": 2,
    "(0, 1)": 2
  }
}
jupiterbjy
  • 2,882
  • 1
  • 10
  • 28