MCS-208 · Unit 8

Graphs

Section 4 of 8

Block 3 · Unit 8Graphs10 min · Section 4 of 8

8.4Negative Edge Weights and Acyclic Graphs

Covers IGNOU MCS-208 §8.3.2, 8.3.3

Some graphs have negative edge weights, for example a route that earns a refund, or a currency exchange that gains money. Dijkstra’s algorithm fails on such graphs. This page explains what can go wrong, gives the Bellman–Ford algorithm that handles negative edges, and shows a faster method for graphs without cycles.

Negative edges and negative cycles

Two situations can occur when a graph has negative edges:

  1. Negative edges, but every cycle has non-negative total weight: shortest paths exist, but Dijkstra may give wrong answers.
  2. A cycle with negative total weight reachable from the source: going round the cycle again and again makes paths cheaper without limit, so shortest paths are not defined for the vertices reachable from the cycle.

In Figure 8.6 (S→A 5, A→B 5, B→A −3) the cycle A→B→A weighs 5−3=25 - 3 = 2: case 1. In Figure 8.7 (B→A −8) it weighs 5−8=−35 - 8 = -3: case 2, since each extra trip round the cycle saves 3.

The Bellman–Ford algorithm

  1. Set d[v] = ∞ for all vv, d[s] = 0.
  2. Repeat n−1n - 1 times: relax every edge (u,v)(u, v).
  3. For every edge (u,v)(u, v): if d[u]+w(u,v)<d[v]d[u] + w(u, v) < d[v], report “negative-weight cycle”.

Running time: O(n⋅e)O(n \cdot e), slower than Dijkstra but correct with negative edges.

Shortest paths in a directed acyclic graph

A DAG (directed acyclic graph) has no cycles at all, so negative edges cannot create negative cycles. Its vertices can be put in topological order: an order in which every edge goes from an earlier vertex to a later one. Relaxing the edges of each vertex in topological order gives correct shortest paths in one pass, in O(n+e)O(n + e) time, even with negative weights.

A topological order can be found with a queue (Kahn’s method): repeatedly output a vertex with in-degree 0 and remove its outgoing edges (or by listing vertices in decreasing DFS finishing time).

DAGs model tasks with dependencies (a project plan, a course prerequisite chart); the longest path in such a DAG gives the critical path of a project.

Key takeaways

  • A reachable negative-weight cycle makes shortest paths undefined.
  • Bellman–Ford: relax all edges n−1n - 1 times, then one check round for negative cycles: O(ne)O(ne).
  • A DAG has a topological order; relaxing in that order solves shortest paths in O(n+e)O(n + e).
  • Dijkstra needs non-negative weights.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

A cycle has edges of weights 4, −2 and −3. What can be said about shortest paths through it?

Show answer

Answer: B. They are undefined, because the cycle’s total weight is −1.

4−2−3=−14 - 2 - 3 = -1: each round trip lowers the cost by 1, so there is no minimum.

Question 2 · multiple choice

How many rounds of relaxing every edge does Bellman–Ford perform (before the check round) on a graph with 8 vertices?

Show answer

Answer: B. 7

n−1=7n - 1 = 7, because a shortest path has at most 7 edges. The extra 8th round is only the negative-cycle check.

Question 3 · multiple choice

What is the running time of Bellman–Ford?

Show answer

Answer: D. O(n⋅e)O(n \cdot e)

n−1n - 1 rounds, each relaxing all ee edges. O(n+e)O(n + e) is the DAG method.

Question 4 · multiple choice

Which is a valid topological order of the DAG with edges a→b, a→c, b→d, c→d?

Show answer

Answer: C. a c b d

Every edge must go forward: a before b and c, and both before d. “a c b d” satisfies all four edges.

Question 5 · multiple choice

What does this program print?

C · q8-bf.c
#include <stdio.h>

int main(void)
{
    int u[3] = {0, 0, 1}, v[3] = {1, 2, 2}, w[3] = {4, 3, -2};
    int d[3] = {0, 100, 100};
    for (int round = 0; round < 2; round++)
        for (int k = 0; k < 3; k++)
            if (d[u[k]] + w[k] < d[v[k]]) d[v[k]] = d[u[k]] + w[k];
    printf("%d %d\n", d[1], d[2]);
    return 0;
}
Show answer

Answer: D. 4 2

Path 0→1→2 costs 4−2=24 - 2 = 2, cheaper than the direct edge 0→2 (3). Dijkstra would settle vertex 2 at 3 before using the negative edge.

Question 6 · short answer

Explain why Dijkstra’s algorithm can fail with negative edges, using the graph s→a 2, s→b 3, b→a −2.

Show worked answer

Dijkstra settles s (d[a] = 2, d[b] = 3), then settles a because 2 < 3, fixing d[a] = 2. Then it settles b (3) and relaxes b→a: 3 − 2 = 1 < 2, but a is already settled, so its distance is not corrected (or, if it is, vertices settled using the old value are not updated). The true shortest distance to a is 1 via s→b→a. Dijkstra’s greedy rule assumes that a path cannot become cheaper by adding edges, which is false with negative weights. Bellman–Ford, which relaxes all edges repeatedly, finds d[a] = 1.

↑ ↓ move↵ openTab switch type