Open In App

Graphs in Python

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( ) and a set of edges( ). The graph is denoted by G(V, E).

Graph Basics

Graph Algorithms

Graph algorithms are methods used to manipulate and analyze graphs, solving various range of problems like finding the shortest path, cycles detection.

BFS and DFS

1. Breadth First Search:

Breadth First Search (BFS) is a fundamental graph traversal algorithm. It begins with a node, then first traverses all its adjacent nodes. Once all adjacent are visited, then their adjacent are traversed.

  • BFS is different from DFS in a way that closest vertices are visited before others. We mainly traverse vertices level by level.
  • Popular graph algorithms like Dijkstra’s shortest path, Kahn’s Algorithm, and Prim’s algorithm are based on BFS.
  • BFS itself can be used to detect cycle in a directed and undirected graph, find shortest path in an unweighted graph and many more problems.

Explore in detail about Breadth First Search in Python

2. Depth First Search:

In Depth First Search (or DFS) for a graph, we traverse all adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all vertices reachable through that adjacent vertex. This is similar to a tree, where we first completely traverse the left subtree and then move to the right subtree. The key difference is that, unlike trees, graphs may contain cycles (a node may be visited more than once). To avoid processing a node multiple times, we use a boolean visited array.

Explore in detail about Depth First Search in Python

Shortest Path

1. Dijkstra’s shortest path algorithm:

Dijkstra’s algorithm finds the shortest path between nodes in a graph. It starts from a chosen node and explores the shortest known distance to other nodes, updating as it finds better paths. It keeps track of visited nodes to avoid unnecessary checks. The process continues until the shortest path to all nodes is found. It works well for graphs with non-negative weights, like road maps and network routing.

Explore in detail about Dijkstra’s shortest path algorithm in Python

2. Bellman–Ford Algorithm:

The Bellman-Ford algorithm finds the shortest path from a starting node to all other nodes in a graph. It works by repeatedly updating distances for all edges, allowing it to handle graphs with negative weights. Unlike Dijkstra’s algorithm, it can detect negative weight cycles. It is useful for routing and network analysis but slower than Dijkstra’s for large graphs.

Explore in detail about Bellman–Ford Algorithm in Python

3. Floyd Warshall Algorithm:

The Floyd Warshall Algorithm is an all-pair shortest path algorithm that uses Dynamic Programming to find the shortest distances between every pair of vertices in a graph, unlike Dijkstra and Bellman-Ford which are single source shortest path algorithms. This algorithm works for both the directed and undirected weighted graphs and can handle graphs with both positive and negative weight edges.

Explore in detail about Floyd Warshall Algorithm in Python

Minimum Spanning Tree

1. Prim's Algorithm:

Prim’s algorithm is a Greedy algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.

  • The algorithm starts with an empty spanning tree.
  • The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included.
  • At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.

Explore in detail about Prim's Algorithm in Python

2. Kruskal's Algorithm:

Kruskal’s algorithm finds the Minimum Spanning Tree (MST) of a graph, which connects all nodes with the least total edge weight. It sorts all edges by weight and adds the smallest edge to the MST, ensuring no cycles form. This continues until all nodes are connected. It is useful for network design, like laying cables or roads with minimal cost.

Explore in detail about Kruskal’s Algorithm in Python

Topological Sorting

1. Topological Sorting:

Topological sorting is used to arrange directed acyclic graph (DAG) nodes in a sequence where each node appears before the nodes it points to. It is mainly used in task scheduling, dependency resolution, and course prerequisites.

Explore in detail about Topological Sorting in Python

2. Kahn’s algorithm:

Kahn’s Algorithm for Topological Sorting is a method used to order the vertices of a directed graph in a linear order such that for every directed edge from vertex A to vertex B, A comes before B in the order. The algorithm works by repeatedly finding vertices with no incoming edges, removing them from the graph, and updating the incoming edges of the remaining vertices. This process continues until all vertices have been ordered.

Explore in detail about Kahn's Algorithm in Python

Maximum Flow in Graph

1. Maximum Flow Problem:

The maximum flow problem is a classic optimization problem in network theory, where the goal is to determine the maximum possible flow from a source node to a sink node in a flow network. A flow network is represented as a directed graph with each edge having a capacity, which is the maximum amount of flow that can pass through it. The problem finds applications in various fields such as computer networks, logistics, and transportation systems.

Explore in detail about Maximum Flow Problem in Python

2. Ford-Fulkerson Algorithm:

The Ford-Fulkerson algorithm is a widely used algorithm to solve the maximum flow problem in a flow network. The maximum flow problem involves determining the maximum amount of flow that can be sent from a source vertex to a sink vertex in a directed weighted graph, subject to capacity constraints on the edges.

The algorithm works by iteratively finding an augmenting path, which is a path from the source to the sink in the residual graph, i.e., the graph obtained by subtracting the current flow from the capacity of each edge. The algorithm then increases the flow along this path by the maximum possible amount, which is the minimum capacity of the edges along the path.

Explore in detail about Ford-Fulkerson Algorithm in Python


Next Article
Practice Tags :

Similar Reads