Graph Algorithms

Shortest-path algorithms differ by graph type: Dijkstra is fastest for non-negative weights, Bellman-Ford tolerates negatives and detects negative cycles, and Floyd-Warshall computes all-pairs distances in O(V³).

Key formulas & points

Skim these first — then read the full notes below.

  • Dijkstra fails with negative edge weights
  • Topological sort exists only for a DAG
  • BFS equals Dijkstra on an unweighted graph

Topic details

Introduction

This topic covers weighted-graph algorithms. You apply Dijkstra with a priority queue, use Bellman-Ford when edges can be negative, compute all-pairs distances with Floyd-Warshall, and relate these to spanning trees and topological ordering.

Key relations & formulas

Formulas (Indian textbook notation)

  • Dijkstra:O((V+E)logV)withabinaryheapDijkstra: O((V + E) log V) with a binary heap

Formulas (Indian textbook notation)

  • BellmanFord:O(VE),handlesnegativeedgesBellman-Ford: O(VE), handles negative edges

Formulas (Indian textbook notation)

  • FloydWarshall:O(V3)allpairsshortestpathsFloyd-Warshall: O(V^{3}) all-pairs shortest paths

Notation and sign conventions

Relation 1 —
Dijkstra:ODijkstra: O

Formulas (Indian textbook notation)

  • Dijkstra:O((V+E)logV)withabinaryheapDijkstra: O((V + E) log V) with a binary heap
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
BellmanFord:OBellman-Ford: O

Formulas (Indian textbook notation)

  • BellmanFord:O(VE),handlesnegativeedgesBellman-Ford: O(VE), handles negative edges
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
FloydWarshall:OFloyd-Warshall: O

Formulas (Indian textbook notation)

  • FloydWarshall:O(V3)allpairsshortestpathsFloyd-Warshall: O(V^{3}) all-pairs shortest paths
Write this relation with symbols exactly as in Clrs Algorithms — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Choosing a shortest-path algorithm depends on the edges. Dijkstra greedily finalises the closest unvisited vertex, which only works when no negative edge can later offer a cheaper route — hence its non-negativity requirement. Bellman-Ford instead relaxes every edge V−1 times, so it handles negative weights and, with one more pass, flags negative cycles that make shortest paths undefined. Floyd-Warshall uses dynamic programming over intermediate vertices to fill a full distance matrix, worthwhile when you need all pairs. On unweighted graphs, every edge costs one, so plain BFS already yields shortest paths, matching what Dijkstra would compute.

Assumptions and validity limits

State assumptions explicitly before using any relation for graph algorithms — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Algorithms viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Algorithms papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to graph algorithms.
4. Use equation 1:
Dijkstra:ODijkstra: O
.
5. Use equation 2:
BellmanFord:OBellman-Ford: O
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Graph Algorithms appears in competitive programming and backend systems. In Indian it software curricula this topic is tested because it connects theory to design and analysis of algorithms.
GATE and semester exams often combine graph algorithms with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use graph algorithms?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students run Dijkstra on graphs with negative edges (producing wrong answers), forget that topological sort needs a DAG, and misjudge complexity by ignoring the heap factor in Dijkstra. Confusing single-source (Dijkstra/Bellman-Ford) with all-pairs (Floyd-Warshall) is common.

Quick revision checklist

Before attempting graph algorithms problems, confirm you can:
1. Dijkstra fails with negative edge weights
2. Topological sort exists only for a DAG
3. BFS equals Dijkstra on an unweighted graph
Revise the solved examples in Clrs Algorithms — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Dijkstra relaxation

Problem

The current shortest distance to v is 15. Through u (distance 7) an edge w(u,v) = 5 exists. Should v be updated?

Solution

Candidate = dist(u) + w = 7 + 5 = 12. Since 12 < 15, relax: set dist(v) = 12 and predecessor(v) = u. Relaxation only updates when a shorter path is found.

Conceptual check — Graph Algorithms

Problem

In a Algorithms semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of graph algorithms." What should a complete answer include?

📖 Standard books (India)

  • Clrs AlgorithmsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus