csc2001f 2024 11 Graphs Paths

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Graphs: Bellman-Ford and DAGs

CSC2001F Data Structures


Jan Buys <jbuys@cs.uct.ac.za>

Department of Computer Science


School of IT
University of Cape Town

2024
(Based on slides from Hussein Suleman)
Dijkstra: Problem 1
0,null
 A negative weight path
might never be processed
A
5
Dijkstra’s algorithm:
D
1
-10
A (d=0)
E (d=1)
E F
F (d=2)
1
D (d=5)

department of computer science


Dijkstra: Problem 2
0,null
 A negative-weight cycle
4 B means it is always possible
A
1
1 to lower the cost
2
 Algorithm never terminates
D G
5
8
3
2
F->D->E: cycle with weight -1
-8
E F
4

department of computer science


Bellman-Ford Algorithm
 Find the shortest path from a given node to all other
nodes
 Assume the edges are weighted
 Positive or negative weights
 Basic idea:
 Do BFS multiple times (|V| is the upper bound)
 Check for negative cycles – will cause algorithm to not
terminate

department of computer science


Bellman-Ford: Algorithm
 Add start node to queue with 0 distance
 While queue is not empty:
 Get node v from queue
 Check node has not been processed more than |V| times – if
so, there is a negative cycle!
 For each edge from v to w:
 If distance to w > distance to v + cost of edge
 Distance to w = distance to v + cost of edge
 Path to w goes through v
 Add w to queue with its new distance value, if w is not on the
queue already
department of computer science
Bellman-Ford: Example 1/11
0,null Queue: A
4 B
A
1
1 2 Distance to A set to 0
D G
A has no previous node
5
8 All other distances set to
3
2 infinity
-3
E F
All nodes marked as
4
processed 0 times

department of computer science


Bellman-Ford: Example 2/11
0,null Dequeue A;
4 4,A
B
A Mark A as processed 1 time
1
1 2 Update distances for B=4,
D=1, E=5; previous node in
D 1,A G
5
each case is A; enqueue
8
3 B/D/E;
2
-3
E F
5,A
4 Queue: B, D, E
Times: 1(A)

department of computer science


Bellman-Ford: Example 3/11
0,null Dequeue B;
4 4,A
B
A Mark B as processed 1 time
1
1 2 Update distances for G=5;
5,B
previous node in each case is
D 1,A G
5
B; enqueue G;
8
3
2
-3
E
Queue: D, E, G
F
5,A
4
Times: 1(A,B)

department of computer science


Bellman-Ford: Example 4/11
0,null Dequeue D;
4 3,D
B
A Mark D as processed 1 time
1
1 2 Update distances for B=3,
5,B
E=4, F=9; previous node in
D 1,A G
5
each case is D; enqueue B, F
8
3 (skip E);
2
-3
E F 9,D
4,D
4 Queue: E, G, B, F
Times: 1(A,B,D)

department of computer science


Bellman-Ford: Example 5/11
0,null Dequeue E;
4 3,D
B
A Mark E as processed 1 time
1
1 2 Update distances for F=8;
5,B
previous node in each case is
D 1,A G
5
E; (skip F);
8
3
2
-3
E
Queue: G, B, F
F 8,E
4,D
4
Times: 1(A,B,D,E)

department of computer science


Bellman-Ford: Example 6/11
0,null Dequeue G;
4 3,D
B
A Mark G as processed 1 time
1
1 2 Update distances for F=7;
5,B
previous node in each case is
D 1,A G
5
G; (skip F);
8
3
2
-3
E
Queue: B, F
F 7,G
4,D
4
Times: 1(A,B,D,E,G)

department of computer science


Bellman-Ford: Example 7/11
0,null Dequeue B;
4 3,D
B
A Mark B as processed 2 times
1
1 2 Update distances for G=4;
4,B
previous node in each case is
D 1,A G
5
B; enqueue G;
8
3
2
-3
E
Queue: F, G
F 7,G
4,D
4
Times: 1(A,D,E,G)2(B)

department of computer science


Bellman-Ford: Example 8/11
0,null Dequeue F;
4 3,D
B
A Mark F as processed 1 time
1
1 2 No updates
4,B
D 1,A G
5
8 Queue: G
3
2 Times: 1(A,D,E,F,G)2(B)
-3
E F 7,G
4
4,D

department of computer science


Bellman-Ford: Example 9/11
0,null Dequeue G;
4 3,D
B
A Mark G as processed 2 times
1
1 2 Update distances for F=6;
4,B
previous node in each case is
D 1,A G
5
G; enqueue F;
8
3
2
-3
E
Queue: F
F 6,G
4,D
4
Times: 1(A,D,E,F)2(B,G)

department of computer science


Bellman-Ford: Example 10/11
0,null Dequeue F;
4 3,D
B
A Mark F as processed 2 times
1
1 2 Update distances for E=3;
4,B
previous node in each case is
D 1,A G
5
F; enqueue E;
8
3
2
-3
E
Queue: E
F 6,G
3,F
4
Times: 1(A,D,E)2(B,F,G)

department of computer science


Bellman-Ford: Example 11/11
0,null Dequeue E;
4 3,D
B
A Mark E as processed 2 times
1
1 2 No updates
4,B
D 1,A G
5
8 Queue:
3
2 Times: 1(A,D)2(B,E,F,G)
-3
E F 6,G
4
3,F
Algorithm terminates.
Therefore no negative cycles.
department of computer science
Bellman-Ford: Code 1/2

From Weiss

department of computer science


Bellman-Ford: Code 2/2

From Weiss

department of computer science


Bellman-Ford: Complexity
 O(|V||E|)
 Because we need at most |V| iterations to propagate
values through graph without cycles
 Each time we process up to |E| edges for the entire
graph
 An alternative formulation (see Cormen, Leiserson &
Rivest) simply iterates |V|-1 times and checks each edge
for a lower cost each time
 Simpler than Weiss’s implementation and easier to
analyse but does not naturally follow graph edges
department of computer science
Topological Sort
 In a directed acyclic graph (DAG), order the nodes of a graph
such that every node u appears before every node v for every
edge (u,v)
 Creates a total order for nodes from a partial order
 Define in-degree(v) = number of incoming edges (u,v)
 Topological sort algorithm:
 Add any node v with in-degree(v)=0 to the queue
 While the queue is not empty:
 Dequeue u; For each edge (u, v):
 Remove edge and decrease the in-degree of v
 If in-degree(v)=0, add v to the queue
department of computer science
Topological Sort: Example 1/7
0 Calculate all in-degrees
4 2
B
A
1
1 2 A has in-degree=0
1
D G
Add A to queue
5 1
8 Queue: A
3
2

E F 3
4
2

department of computer science


Topological Sort: Example 2/7
0 Dequeue A
4 1
B
A Decrease in-degree for B/D/E
1
1 2 Enqueue D
1
D 0 G
5
8 Queue: D
3
2 Sort: A
E F 3
4
1

department of computer science


Topological Sort: Example 3/7
0 Dequeue D
4 0
B
A Decrease in-degree for B/E/F
1
1 2 Enqueue B/E
1
D 0 G
5
8 Queue: B,E
3
2 Sort: A,D
E F 2
4
0

department of computer science


Topological Sort: Example 4/7
0 Dequeue B
4 0
B
A Decrease in-degree for G
1
1 2 Enqueue G
0
D 0 G
5
8 Queue: E,G
3
2 Sort: A,D,B
E F 2
4
0

department of computer science


Topological Sort: Example 5/7
0 Dequeue E
4 0
B
A Decrease in-degree for F
1
1 2 0
D G
Queue: G
5 0
8 Sort: A,D,B,E
3
2

E F 1
4
0

department of computer science


Topological Sort: Example 6/7
0 Dequeue G
4 0
B
A Decrease in-degree for F
1
1 2 Enqueue F
0
D 0 G
5
8 Queue: F
3
2 Sort: A,D,B,E,G
E F 0
4
0

department of computer science


Topological Sort: Example 7/7
0 Dequeue F
4 0
B
A
1
1 2 Queue:
0
D G
Sort: A,D,B,E,G,F
5 0
8
3
2 Algorithm terminates.
E F 0
4
0

department of computer science


Topological Sort: Flattening Graph

A D B E G F
1 2
1 2 4

department of computer science


DAG Shortest Path
 A shortest path in a DAG can be computed by processing nodes
in the topological sort order.
 Basic Idea:
 Update shortest distances as we find each node during a
topological sort.
 If we have not processed all nodes and run out of in-
degree=0 nodes, then some nodes are stuck in a cycle and it
is not a DAG.

department of computer science


DAG Shortest Path: Code 1/2

From Weiss

department of computer science


DAG Shortest Path: Code 2/2

From Weiss

department of computer science


DAG Shortest Path: Complexity
 O(|E|)
 Because a topological sort only goes through all edges
once.
 We get better performance because the graph is
constrained to be a DAG.

department of computer science

You might also like