5 Best Ways to Find the Minimum Cost Path in a Weighted Graph Using Python

Rate this post

πŸ’‘ Problem Formulation: Determining the minimum cost path in a weighted graph is a classic algorithmic problem commonly found in fields like networking, operations research, and computer science. Given a weighted graph with nodes and edges, the objective is to find a path between two nodes such that the total cost (sum of the edge weights) is minimized. For example, for input graph with weights and a pair of nodes ‘A’ and ‘B’, the desired output is the minimum cost to move from ‘A’ to ‘B’.

Method 1: Dijkstra’s Algorithm

Dijkstra’s Algorithm is a time-proven method used to find the shortest paths from a starting vertex to all other vertices in a weighted graph, effectively finding the minimum cost path. The algorithm works by iteratively selecting the vertex with the minimum distance that hasn’t been processed and calculates the distances to its adjacent vertices. It’s worth noting that Dijkstra’s algorithm does not work for graphs with negative weight edges.

Here’s an example:

import heapq

def dijkstra(graph, start):
    distances = {vertex: float('infinity') for vertex in graph}
    distances[start] = 0
    priority_queue = [(0, start)]

    while priority_queue:
        current_distance, current_vertex = heapq.heappop(priority_queue)

        for neighbor, weight in graph[current_vertex].items():
            distance = current_distance + weight
            if distance < distances[neighbor]:
                distances[neighbor] = distance
                heapq.heappush(priority_queue, (distance, neighbor))
    return distances

# A sample graph
graph = {
    'A': {'B': 1, 'C': 4},
    'B': {'A': 1, 'C': 2, 'D': 5},
    'C': {'A': 4, 'B': 2, 'D': 1},
    'D': {'B': 5, 'C': 1}

print(dijkstra(graph, 'A'))

The output of this code snippet showing the minimum distances from node ‘A’ would look like:

{'A': 0, 'B': 1, 'C': 3, 'D': 4}

In the above example, we use Dijkstra’s algorithm to determine the shortest paths from the start node ‘A’ to all other nodes in the graph. The heapq module is used to maintain a priority queue, ensuring that we always process the vertex with the smallest distance next.

Method 2: Bellman-Ford Algorithm

The Bellman-Ford algorithm is another classical algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra’s Algorithm but it can handle graphs with negative weight edges, provided that there are no negative weight cycles.

Here’s an example:

def bellman_ford(graph, start):
    distance = {vertex: float('infinity') for vertex in graph}
    distance[start] = 0

    for _ in range(len(graph) - 1):
        for vertex in graph:
            for neighbor in graph[vertex]:
                new_distance = distance[vertex] + graph[vertex][neighbor]
                if new_distance < distance[neighbor]:
                    distance[neighbor] = new_distance
    return distance

# Example graph
graph = {
    'A': {'B': 6, 'D': 1},
    'B': {'C': 5, 'D': 2, 'E': 2},
    'C': {'E': 5},
    'D': {'B': 2, 'E': 1},
    'E': {'C': 5}

print(bellman_ford(graph, 'A'))

The output of this code snippet showing the minimum distances from node ‘A’ would look like:

{'A': 0, 'B': 3, 'C': 8, 'D': 1, 'E': 2}

This example applies the Bellman-Ford algorithm to find the shortest paths from the starting node ‘A’ to every other node. Despite being slower than Dijkstra’s, Bellman-Ford’s ability to accommodate negative weights can be crucial in certain applications.

Method 3: Floyd-Warshall Algorithm

The Floyd-Warshall Algorithm is an all-pairs shortest path algorithm, which calculates the shortest paths between every pair of vertices in a given edge-weighed graph. This algorithm is a good choice when dealing with dense graphs where we need to find shortest paths between all pairs of nodes.

Here’s an example:

def floyd_warshall(weights, num_vertices):
    distance_matrix = [[float('infinity') for _ in range(num_vertices)] for _ in range(num_vertices)]

    for vertex in range(num_vertices):
        distance_matrix[vertex][vertex] = 0

    for start, end, weight in weights:
        distance_matrix[start][end] = weight

    for k in range(num_vertices):
        for i in range(num_vertices):
            for j in range(num_vertices):
                distance_matrix[i][j] = min(
                    distance_matrix[i][j], distance_matrix[i][k] + distance_matrix[k][j])

    return distance_matrix

# Example weights data (from_node, to_node, weight)
weights = [(0, 1, 5), (0, 3, 10), (1, 2, 3), (2, 3, 1)]
num_vertices = 4

print(floyd_warshall(weights, num_vertices))

The output of this code snippet showing the minimum distance matrix would look like:

[[0, 5, 8, 9], [infinity, 0, 3, 4], [infinity, infinity, 0, 1], [infinity, infinity, infinity, 0]]

In the provided example, the Floyd-Warshall algorithm creates a distance matrix that computes the shortest paths between all pairs of vertices in the graph. While it’s efficient for dense graphs or those with small numbers of vertices, it can be computationally expensive for larger graphs with a higher number of vertices.

Method 4: Tree-Based Algorithm (Prim’s or Kruskal’s)

Tree-based algorithms like Prim’s or Kruskal’s are typically used to find the minimum spanning tree (MST) of a graph. While MST is not a shortest path per se, it is closely related and could be used to derive the minimum cost path in an undirected graph, especially when you need to visit each node exactly once.

Here’s an example:

import heapq

def prims(graph, start_vertex):
    mst = []
    visited = set()
    edges = [(0, start_vertex, None)]

    while edges:
        weight, vertex, prev = heapq.heappop(edges)
        if vertex not in visited:
            mst.append((prev, vertex, weight))

            for next_vertex, next_weight in graph[vertex].items():
                if next_vertex not in visited:
                    heapq.heappush(edges, (next_weight, next_vertex, vertex))

    return mst

# Example graph
graph = {
    'A': {'B': 2, 'C': 3},
    'B': {'A': 2, 'C': 1, 'D': 1, 'E': 4},
    'C': {'A': 3, 'B': 1, 'F': 5},
    'D': {'B': 1, 'E': 1},
    'E': {'B': 4, 'D': 1, 'F': 1},
    'F': {'C': 5, 'E': 1},

print(prims(graph, 'A'))

The output of this code snippet, representing the edges included in the MST, would look like:

[(None, 'A', 0), ('A', 'B', 2), ('B', 'C', 1), ('B', 'D', 1), ('D', 'E', 1), ('E', 'F', 1)]

In the provided example, we use Prim’s algorithm to find the minimum spanning tree of a graph. This is not directly a minimum cost path algorithm, but with some modifications or additional constraints, it can be adapted to find a path that visits each vertex with minimum total weight.

Bonus One-Liner Method 5: Using NetworkX

NetworkX is a Python library designed for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It includes built-in functions to calculate the shortest path in weighted graphs, making it a convenient one-liner solution for those already working with NetworkX.

Here’s an example:

import networkx as nx

graph = nx.Graph()
graph.add_weighted_edges_from([('A', 'B', 2), ('A', 'C', 5), ('B', 'C', 1), ('C', 'D', 7)])

path = nx.shortest_path(graph, source='A', target='D', weight='weight')

The output of this code snippet, representing the shortest path from node ‘A’ to ‘D’, will be:

['A', 'B', 'C', 'D']

Using the NetworkX library, we can find the shortest path by simply defining our graph with weights and calling the shortest_path function. This method is perfect for convenience and quick prototyping.


  • Method 1: Dijkstra’s Algorithm. Highly efficient for non-negative edge weights. Does not handle negative cycles.
  • Method 2: Bellman-Ford Algorithm. Accommodates negative edge weights. Slower performance than Dijkstra’s, especially for large graphs with many edges.
  • Method 3: Floyd-Warshall Algorithm. Finds shortest paths between all pairs of nodes. Computationally intensive for larger graphs due to its cubic time complexity.
  • Method 4: Tree-Based Algorithm. Prim’s and Kruskal’s algorithms are good for finding MSTs, which can sometimes be used for certain types of minimum path problems. Not directly a shortest path solution.
  • Bonus Method 5: NetworkX. Offers a highly convenient and quick solution for users already utilizing the NetworkX library. Handles complex graphs with ease.