Learn practical skills, build real-world projects, and advance your career

Graph Algorithms (BFS, DFS, Shortest Paths) using Python

Part 5 of "Data Structures and Algorithms in Python"

Data Structures and Algorithms in Python is a beginner-friendly introduction to common data structures (linked lists, stacks, queues, graphs) and algorithms (search, sorting, recursion, dynamic programming) in Python, designed to help you prepare for coding interviews and assessments.

Ask questions, get help & participate in discussions on the course community forum. Earn a verified certificate of accomplishment for this course by signing up here: http://pythondsa.com.

Graphs in the Real World

Railway network

alt

Flight routes

alt

Hyperlinks

alt

Graph Data Strucutre

alt

num_nodes = 5
edges = [(0,1), (0,4), (1,2), (2,3), (1,3), (1,4), (3,4)]
num_nodes, len(edges)
(5, 7)

Adjacency Lists

alt

Question: Create a class to represent a graph as an adjacency list in Python