Graph Algorithms Visualization

Dijkstra's Shortest Path Algorithm

Algorithm Overview

Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in a weighted graph with non-negative edge weights. It uses a greedy approach, always selecting the vertex with the smallest known distance.

Pseudocode

Algorithm Dijkstra(G, source):
    Initialize all distances to infinity
    Set distance[source] = 0
    Create priority queue Q with all vertices

    while Q is not empty:
        u = vertex in Q with minimum distance
        remove u from Q
        mark u as visited

        for each neighbor v of u:
            if distance[u] + weight(u,v) < distance[v]:
                distance[v] = distance[u] + weight(u,v)

    return distance array
Select a starting node to begin Dijkstra's algorithm

Limitations: Dijkstra with Negative Edges

⚠️ Why Dijkstra Fails with Negative Edges

Dijkstra's algorithm assumes that once a node is visited (has the minimum distance), that distance is final. With negative edges, a shorter path might be found later, but Dijkstra won't reconsider already-visited nodes.

Demonstration

Click "Run Dijkstra" to see how it fails with negative edges
Graph Details:
• Path A→B: cost = 5
• Path A→C→B: cost = 7 + (-4) = 3

Correct shortest path: A→C→B = 3
Dijkstra finds: A→B = 5 ❌

Worse Case: Negative Cycles

🔁 Negative Cycles Make Shortest Path Undefined

A negative cycle is a cycle where the sum of edge weights is negative. With a negative cycle, you can keep going through the cycle repeatedly to make the path cost arbitrarily negative (approaching -∞). The shortest path problem becomes undefined!

Negative Cycle Demo

Click "Show Cycle Iterations" to see infinite reduction
Cycle Analysis:
• Cycle: A→B→C→A
• Edge weights: 2, 3, -6
Cycle sum: 2 + 3 + (-6) = -1

Each iteration through the cycle reduces distance by 1:
• 0 cycles: distance = 0
• 1 cycle: distance = -1
• 2 cycles: distance = -2
• 3 cycles: distance = -3
• ... → -∞

Minimum Spanning Tree Algorithms

MST Overview

A Minimum Spanning Tree (MST) connects all vertices in a weighted graph with the minimum total edge weight, forming a tree (no cycles) with exactly n-1 edges for n vertices.

Prim's Algorithm

Grows the MST from a starting vertex by repeatedly adding the minimum-weight edge connecting the tree to a new vertex.

Algorithm Prim(G):
    MST = empty set
    Start with arbitrary vertex

    while MST has fewer than n-1 edges:
        find minimum-weight edge connecting
            tree to a new vertex
        add edge to MST

    return MST

Kruskal's Algorithm

Sorts all edges by weight and adds them one by one, skipping edges that would create a cycle. Uses Union-Find data structure.

Algorithm Kruskal(G):
    MST = empty set
    Sort all edges by weight
    Initialize Union-Find structure

    for each edge (u,v) in sorted order:
        if u and v are in different components:
            add (u,v) to MST
            union(u, v)

    return MST
A B C D E F 2 2 3 4 5 5 6 7
Select an algorithm and click Start to begin