math - Why are graphs represented using dictionaries in Python? -


python not have direct support graphs lot of sources saying can represented using dictionaries eg.

graph = { "a" : ["c"],           "b" : ["c", "e"],           "c" : ["a", "b", "d", "e"],           "d" : ["c"],           "e" : ["c", "b"],           "f" : []         } 

since undirected graph , dictionaries directional mapping, seems inconcise. better graph = {'x':['y'], 'y':['x']} rather graph = {{'x', 'y'}} ?

storing them connections makes easy walk:

vertex = 'x' connected_to = graph[vertex] second_degree_connections = {p subset in graph[p] p in connected_to} 

try doing efficiently set of two-tuples. not easy, right?


Comments