Python
Graph Theory
Programming
Algorithms
Coding

Solving a graph issue with Python

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Graphs are abstract data structures that consist of nodes (or vertices) connected by edges. They are an essential concept in computer science and mathematics, used to model relationships and solve complex problems across various domains, including social networks, transportation systems, and resource planning. This article explores a common graph-related issue and demonstrates how Python can be leveraged to solve it.

Understanding the Graph Problem

One frequently encountered problem in graph theory is finding the shortest path between nodes. This problem is crucial in scenarios such as GPS navigation systems and network routing protocols. Let's examine the shortest path problem and how we can solve it using Python.

Graph Representation

Graphs can be represented in multiple ways. The most common approaches are through adjacency lists and adjacency matrices.

  • Adjacency List: Represents a graph as an array of lists. Each list corresponds to a vertex and contains a list of all neighbouring vertices.
  • Adjacency Matrix: Represents a graph as a matrix. The matrix is square, with rows and columns representing vertices. The value in the cell at the intersection of row ii and column jj indicates the presence (and possibly the weight) of an edge between individuals ii and jj.

Solving the Shortest Path Problem

Let's use Dijkstra's algorithm to find the shortest path in a weighted graph. The algorithm works efficiently with graphs that have non-negative weights.

Dijkstra's Algorithm Overview

  1. Initialization: Start with a node (source) and set the distance to all nodes to infinity, except the source node, which is set to zero. Mark all nodes as unvisited.
  2. Path Calculation:
    • Select the unvisited node with the smallest distance value.
    • For the current node, consider all unvisited neighbors and calculate their tentative distances through the current node.
    • If the calculated distance is less than the known distance, update the neighbor's distance.
  3. Mark Visited: Once a node has been visited (i.e., processed), it cannot be revisited.
  4. Repeat: Continue with the next closest unvisited node until all nodes have been visited.

Python Implementation


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.