  # Create a graph using a dictionary in Python In this article, we will see how to implement graph in python using dictionary data structure in python.
Keys of the used dictionary — these are the nodes of our graph, and the corresponding values ​​— lists with each node that are connected by an edge.
This simple graph has six nodes (af) and five arcs:

` a - & gt; cb - & gt; cb - & gt; ec - & gt; ac - & gt; bc - & gt; dc - & gt; ed - & gt; ce - & gt; ce - & gt; b `

It can be represented by the following Python data structure. It is a dictionary whose keys are the nodes of the graph. For each key, the corresponding value is a list containing the nodes that are connected by a direct arc from this node.

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

Graphic representation of the above example: defaultdict : Usually Python dictionary will throw KeyError if you try to get an element with a key that is not currently in the dictionary. defaultdict allows that if the key is not found in the dictionary, then a new entry is created instead of the generated KeyError. The type of this new entry is specified by the defaultdict argument.
Python function to generate a graph:

` # definition of function def generate_edges (graph): edges = [] # for each node in graph for node in graph: # for each neighbor node of a single node for neighbor in graph [node]: # if edge exists then append edges.append ((node, neighbor)) return edges `

 ` # Python program for ` ` # checking the graph `   ` # import of the graph dictionary ` ` from ` ` collections ` ` import ` ` defaultdict ` ` `  ` # function to add an edge to the graph ` ` graph ` ` = ` ` defaultdict (` ` list ` `) ` ` def ` ` addEdge (graph, u, v): ` ` ` ` graph [u] .append (v) `   ` # function definition ` ` def ` ` generate_edges (graph): ` ` edges ` ` = ` ` [] `   ` # for each node in the column ` ` for ` ` node ` ` in ` ` graph: `   ` ` ` # for each adjacent node of one node ` ` for ` ` neighbor ` ` in ` ` graph [node ]: `   ` # if an edge exists, add ` ` edges.append ((node, neighbor )) ` ` return ` ` edges `   ` # declaring the graph as a dictionary ` ` addEdge (graph, ` ` `a` ` `, ` `` c` ` `) ` ` addEdge (graph, ` ` `b` ` `, ` `` c` ` ` ) ` ` addEdge (graph, ` ` `b` ` `, ` ` `e` ` `) ` ` addEdge (graph, ` ` `c` ` `, ` `` d` ` `) ` ` addEdge (graph, ` ` `c` , `e` ) `` addEdge (graph, `c` , `a` ) addEdge (graph, ` c` , `b` ) addEdge (graph, `e` , ` b` ) addEdge (graph, ` d` , ` c` ) addEdge (graph, `e` , ` c` )   # Function call drivers # print the generated graph print (generate_edges (graph)) `

Output:

` [(`a`,` c`), (`c`,` d`), (`c`,` e`), (`c`,` a`), (`c `,` b`), (`b`,` c`), (`b`,` e`), (`e`,` b`), (`e`,` c`), (`d `,` c`)] `

As we took the example of an undirected graph, we printed the same reb po twice, say, like (& # 39; a & # 39;, & # 39; c & # 39;) and (& # 39; c & # 39;, & # 39; a & # 39;). We can overcome this with a directed graph.

Here are some more graphical programs in Python:

This article is updated Shivov Pradkhan (anuj_charm) and Rishabh Bansal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.