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.

How to Run the Code

The best way to learn the material is to execute the code and experiment with it yourself. This tutorial is an executable Jupyter notebook. You can run this tutorial and experiment with the code examples in a couple of ways: using free online resources (recommended) or on your computer.

Option 1: Running using free online resources (1-click, recommended)

The easiest way to start executing the code is to click the Run button at the top of this page and select Run on Binder. You can also select "Run on Colab" or "Run on Kaggle", but you'll need to create an account on Google Colab or Kaggle to use these platforms.

Option 2: Running on your computer locally

To run the code on your computer locally, you'll need to set up Python, download the notebook and install the required libraries. We recommend using the Conda distribution of Python. Click the Run button at the top of this page, select the Run Locally option, and follow the instructions.

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), (1,3), (1,4), (2,3), (3,4)]
num_nodes, len(edges)
(5, 7)