**Prerequisites:**

In this article, we will be looking at how to build an undirected graph and then find the shortest path between two nodes/vertex of that graph easily using dictionaries in Python Language.

### Building a Graph using Dictonaries

**Approach:** The idea is to store the adjacency list into the dictionaries, which helps to store the graph in any format not only in the form of the integers. Here we have used characters as a reference on those places any custom objects can also be used.

Below is the implementation of the above approach:

## Python3

`# Python3 implementation to build a ` `# graph using Dictonaries ` ` ` `from` `collections ` `import` `defaultdict ` ` ` `# Function to build the graph ` `def` `build_graph(): ` ` ` `edges ` `=` `[ ` ` ` `[` `"A"` `, ` `"B"` `], [` `"A"` `, ` `"E"` `], ` ` ` `[` `"A"` `, ` `"C"` `], [` `"B"` `, ` `"D"` `], ` ` ` `[` `"B"` `, ` `"E"` `], [` `"C"` `, ` `"F"` `], ` ` ` `[` `"C"` `, ` `"G"` `], [` `"D"` `, ` `"E"` `] ` ` ` `] ` ` ` `graph ` `=` `defaultdict(` `list` `) ` ` ` ` ` `# Loop to iterate over every ` ` ` `# edge of the graph ` ` ` `for` `edge ` `in` `edges: ` ` ` `a, b ` `=` `edge[` `0` `], edge[` `1` `] ` ` ` ` ` `# Creating the graph ` ` ` `# as adjacency list ` ` ` `graph[a].append(b) ` ` ` `graph[b].append(a) ` ` ` `return` `graph ` ` ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `graph ` `=` `build_graph() ` ` ` ` ` `print` `(graph) ` |

*chevron_right*

*filter_none*

**Output:**

{ 'G': ['C'], 'F': ['C'], 'E': ['A', 'B', 'D'], 'A': ['B', 'E', 'C'], 'B': ['A', 'D', 'E'], 'D': ['B', 'E'], 'C': ['A', 'F', 'G'] }

### Shortest Path between two nodes of graph

**Approach:** The idea is to use queue and visit every adjacent node of the starting nodes that is traverse the graph in Breadth-First Search manner to find the shortest path between two nodes of the graph.

Below is the implementation of the above approach:

## Python3

`# Python implementation to find the ` `# shortest path in the graph using ` `# dictionaries ` ` ` `# Function to find the shortest ` `# path between two nodes of a graph ` `def` `BFS_SP(graph, start, goal): ` ` ` `explored ` `=` `[] ` ` ` ` ` `# Queue for traversing the ` ` ` `# graph in the BFS ` ` ` `queue ` `=` `[[start]] ` ` ` ` ` `# If the desired node is ` ` ` `# reached ` ` ` `if` `start ` `=` `=` `goal: ` ` ` `print` `(` `"Same Node"` `) ` ` ` `return` ` ` ` ` `# Loop to traverse the graph ` ` ` `# with the help of the queue ` ` ` `while` `queue: ` ` ` `path ` `=` `queue.pop(` `0` `) ` ` ` `node ` `=` `path[` `-` `1` `] ` ` ` ` ` `# Codition to check if the ` ` ` `# current node is not visited ` ` ` `if` `node ` `not` `in` `explored: ` ` ` `neighbours ` `=` `graph[node] ` ` ` ` ` `# Loop to iterate over the ` ` ` `# neighbours of the node ` ` ` `for` `neighbour ` `in` `neighbours: ` ` ` `new_path ` `=` `list` `(path) ` ` ` `new_path.append(neighbour) ` ` ` `queue.append(new_path) ` ` ` ` ` `# Condition to check if the ` ` ` `# neighbour node is the goal ` ` ` `if` `neighbour ` `=` `=` `goal: ` ` ` `print` `(` `"Shortest path = "` `, ` `*` `new_path) ` ` ` `return` ` ` `explored.append(node) ` ` ` ` ` `# Condition when the nodes ` ` ` `# are not connected ` ` ` `print` `(` `"So sorry, but a connecting"` `\ ` ` ` `"path doesn't exist :("` `) ` ` ` `return` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Graph using dictionaries ` ` ` `graph ` `=` `{` `'A'` `: [` `'B'` `, ` `'E'` `, ` `'C'` `], ` ` ` `'B'` `: [` `'A'` `, ` `'D'` `, ` `'E'` `], ` ` ` `'C'` `: [` `'A'` `, ` `'F'` `, ` `'G'` `], ` ` ` `'D'` `: [` `'B'` `, ` `'E'` `], ` ` ` `'E'` `: [` `'A'` `, ` `'B'` `, ` `'D'` `], ` ` ` `'F'` `: [` `'C'` `], ` ` ` `'G'` `: [` `'C'` `]} ` ` ` ` ` `# Function Call ` ` ` `BFS_SP(graph, ` `'A'` `, ` `'D'` `) ` |

*chevron_right*

*filter_none*

**Output:**

Shortest path = A B D

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Shortest cycle in an undirected unweighted graph
- Print all shortest paths between given source and destination in an undirected graph
- Eulerian Path in undirected graph
- Eulerian path and circuit for undirected graph
- Find if there is a path between two vertices in an undirected graph
- Finding shortest path between any two nodes using Floyd Warshall Algorithm
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Multistage Graph (Shortest Path)
- Shortest path in a complement graph
- Shortest path in an unweighted graph
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest Path in Directed Acyclic Graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path in a directed graph by Dijkstra’s algorithm
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Multi Source Shortest Path in Unweighted Graph
- Check if given path between two nodes of a graph represents a shortest paths
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.