![Page 1: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/1.jpg)
Theory of Algorithms IGraph Algorithms, Revisited
Guoqiang Li
School of Software, Shanghai Jiao Tong University
![Page 2: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/2.jpg)
Instructor and Teaching Assistants
• Guoqiang LI
• Homepage: https://basics.sjtu.edu.cn/˜liguoqiang• Course page:
https://basics.sjtu.edu.cn/˜liguoqiang/teaching/SE222/• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167
• TA:
• Yuan HANG: amberbabatt (AT) gmail (DOT)com
• Office hour: Tue. 14:00-17:00 @ Software Building 3203• Office hour: Wed. 13:30-15:00 @ Software Building 1212 (need
reserve!)
![Page 3: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/3.jpg)
Instructor and Teaching Assistants
• Guoqiang LI• Homepage: https://basics.sjtu.edu.cn/˜liguoqiang• Course page:
https://basics.sjtu.edu.cn/˜liguoqiang/teaching/SE222/• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167
• TA:
• Yuan HANG: amberbabatt (AT) gmail (DOT)com
• Office hour: Tue. 14:00-17:00 @ Software Building 3203• Office hour: Wed. 13:30-15:00 @ Software Building 1212 (need
reserve!)
![Page 4: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/4.jpg)
Instructor and Teaching Assistants
• Guoqiang LI• Homepage: https://basics.sjtu.edu.cn/˜liguoqiang• Course page:
https://basics.sjtu.edu.cn/˜liguoqiang/teaching/SE222/• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167
• TA:• Yuan HANG: amberbabatt (AT) gmail (DOT)com
• Office hour: Tue. 14:00-17:00 @ Software Building 3203• Office hour: Wed. 13:30-15:00 @ Software Building 1212 (need
reserve!)
![Page 5: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/5.jpg)
Instructor and Teaching Assistants
• Guoqiang LI• Homepage: https://basics.sjtu.edu.cn/˜liguoqiang• Course page:
https://basics.sjtu.edu.cn/˜liguoqiang/teaching/SE222/• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167
• TA:• Yuan HANG: amberbabatt (AT) gmail (DOT)com
• Office hour: Tue. 14:00-17:00 @ Software Building 3203
• Office hour: Wed. 13:30-15:00 @ Software Building 1212 (needreserve!)
![Page 6: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/6.jpg)
Instructor and Teaching Assistants
• Guoqiang LI• Homepage: https://basics.sjtu.edu.cn/˜liguoqiang• Course page:
https://basics.sjtu.edu.cn/˜liguoqiang/teaching/SE222/• Email: [email protected]• Office: Rm. 1212, Building of Software• Phone: 3420-4167
• TA:• Yuan HANG: amberbabatt (AT) gmail (DOT)com
• Office hour: Tue. 14:00-17:00 @ Software Building 3203• Office hour: Wed. 13:30-15:00 @ Software Building 1212 (need
reserve!)
![Page 7: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/7.jpg)
Text Book
• Algorithms: Design Techniques and Analysis• M. H. Alsuwalyel• World Scientific Publishing, 1999.
![Page 8: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/8.jpg)
Text Book
• Algorithms• Sanjoy Dasgupta
University of California• San Diego Christos Papadimitriou
University of California at Berkeley• Umesh Vazirani
University of California at Berkeley• McGraw-Hill, 2007.
• Available at:http://www.cs.berkeley.edu/˜vazirani/algorithms.html
![Page 9: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/9.jpg)
Which one comes first, computer or algorithms?
![Page 10: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/10.jpg)
Al Khwarizmi
Al Khwarizmi (780 - 850)
![Page 11: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/11.jpg)
Al Khwarizmi
Al Khwarizmi (780 - 850)
In the 12th century, Latin translationsof his work on the Indian numerals,introduced the decimal system to theWestern world. (Source: Wikipedia)
![Page 12: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/12.jpg)
Algorithms
Al Khwarizmi laid out the basic methods for• adding,• multiplying,• dividing numbers,• extracting square roots,• calculating digits of π.
These procedures were precise, unambiguous, mechanical, efficient,correct.
They were algorithms, a term coined to honor the wise man after thedecimal system was finally adopted in Europe, many centuries later.
![Page 13: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/13.jpg)
Algorithms
Al Khwarizmi laid out the basic methods for• adding,• multiplying,• dividing numbers,• extracting square roots,• calculating digits of π.
These procedures were precise, unambiguous, mechanical, efficient,correct.
They were algorithms, a term coined to honor the wise man after thedecimal system was finally adopted in Europe, many centuries later.
![Page 14: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/14.jpg)
Algorithms
Al Khwarizmi laid out the basic methods for• adding,• multiplying,• dividing numbers,• extracting square roots,• calculating digits of π.
These procedures were precise, unambiguous, mechanical, efficient,correct.
They were algorithms, a term coined to honor the wise man after thedecimal system was finally adopted in Europe, many centuries later.
![Page 15: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/15.jpg)
DFS in Graphs, Revisited
![Page 16: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/16.jpg)
Exploring Graphs
EXPLORE(G, v)input : G = (V ,E) is a graph; v ∈ Voutput: visited(u) to true for all nodes u reachable from v
visited(v) = true;PREVISIT(v);for each edge (v, u) ∈ E do
if not visited(u) then EXPLORE(G, u);endPOSTVISIT(v);
![Page 17: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/17.jpg)
Types of Edges in Undirected Graphs
Those edges in G that are traversed by EXPLORE are tree edges.
The rest are back edges.
A
C
B
F
D
H I J
K
E
G
L I
E
J
C
F
B
A
D
G
H
![Page 18: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/18.jpg)
Types of Edges in Undirected Graphs
Those edges in G that are traversed by EXPLORE are tree edges.
The rest are back edges.
A
C
B
F
D
H I J
K
E
G
L I
E
J
C
F
B
A
D
G
H
![Page 19: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/19.jpg)
Depth-First Search
DFS(G)
for all v ∈ V dovisited(v) = false;
endfor all v ∈ V do
if not visited(v) then Explore(G, v);end
![Page 20: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/20.jpg)
Previsit and Postvisit Orderings
For each node, we will note down the times of two important events:
• the moment of first discovery (corresponding to PREVISIT);• and the moment of final departure (POSTVISIT).
PREVISIT(v)
pre[v] = clock;clock ++;
POSTVISIT(v)
post[v] = clock;clock ++;
LemmaFor any nodes u and v, the two intervals [pre(u), post(u)] and[pre(u), post(u)] are either disjoint or one is contained within the other.
![Page 21: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/21.jpg)
Previsit and Postvisit Orderings
For each node, we will note down the times of two important events:• the moment of first discovery (corresponding to PREVISIT);• and the moment of final departure (POSTVISIT).
PREVISIT(v)
pre[v] = clock;clock ++;
POSTVISIT(v)
post[v] = clock;clock ++;
LemmaFor any nodes u and v, the two intervals [pre(u), post(u)] and[pre(u), post(u)] are either disjoint or one is contained within the other.
![Page 22: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/22.jpg)
Previsit and Postvisit Orderings
For each node, we will note down the times of two important events:• the moment of first discovery (corresponding to PREVISIT);• and the moment of final departure (POSTVISIT).
PREVISIT(v)
pre[v] = clock;clock ++;
POSTVISIT(v)
post[v] = clock;clock ++;
LemmaFor any nodes u and v, the two intervals [pre(u), post(u)] and[pre(u), post(u)] are either disjoint or one is contained within the other.
![Page 23: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/23.jpg)
Previsit and Postvisit Orderings
For each node, we will note down the times of two important events:• the moment of first discovery (corresponding to PREVISIT);• and the moment of final departure (POSTVISIT).
PREVISIT(v)
pre[v] = clock;clock ++;
POSTVISIT(v)
post[v] = clock;clock ++;
LemmaFor any nodes u and v, the two intervals [pre(u), post(u)] and[pre(u), post(u)] are either disjoint or one is contained within the other.
![Page 24: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/24.jpg)
Previsit and Postvisit Orderings
(a)
A B C D
E F G H
I J K L
(b) A
B E
I
J G
K
FC
D
H
L
1,10
2,3
4,9
5,8
6,7
11,22 23,24
12,21
13,20
14,17
15,16
18,19
![Page 25: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/25.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.
• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 26: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/26.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.
• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 27: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/27.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.
• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 28: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/28.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 29: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/29.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.
• Forward edges lead from a node to a nonchild descendant in theDFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 30: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/30.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.
• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 31: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/31.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.
• Cross edges lead to neither descendant nor ancestor.
![Page 32: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/32.jpg)
Types of Edges in Directed Graphs
DFS yields a search tree/forests.• root.• descendant and ancestor.• parent and child.
• Tree edges are actually part of the DFS forest.• Forward edges lead from a node to a nonchild descendant in the
DFS tree.• Back edges lead to an ancestor in the DFS tree.• Cross edges lead to neither descendant nor ancestor.
![Page 33: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/33.jpg)
Directed Graphs
AB C
F DE
G H
A
H
B C
E D
F
G
12,15
13,14
1,16
2,11
4,7
5,6
8,9
3,10
![Page 34: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/34.jpg)
Types of Edges
pre/post ordering for (u, v) Edge type
[u [v ]v ]u Tree/forward[v [u ]u ]v Back[v ]v [u ]u Cross
Q: Is that all?
![Page 35: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/35.jpg)
Types of Edges
pre/post ordering for (u, v) Edge type
[u [v ]v ]u Tree/forward[v [u ]u ]v Back[v ]v [u ]u Cross
Q: Is that all?
![Page 36: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/36.jpg)
Directed Acyclic Graphs (DAG)
Definition:A cycle in a directed graph is a circular path
v0 → v1 → v2 → . . . vk → v0
Lemma:A directed graph has a cycle if and only if its depth-first search revealsa back edge.
![Page 37: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/37.jpg)
Directed Acyclic Graphs (DAG)
Definition:A cycle in a directed graph is a circular path
v0 → v1 → v2 → . . . vk → v0
Lemma:A directed graph has a cycle if and only if its depth-first search revealsa back edge.
![Page 38: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/38.jpg)
Directed Acyclic Graphs (DAG)
A
B
C
D
E
F
![Page 39: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/39.jpg)
Directed Acyclic Graphs (DAG)
Linearization/Topologically Sort: Order the vertices such that everyedge goes from a earlier vertex to a later one.
Q: What types of dags can be linearized?
A: All of them.
DFS tells us exactly how to do it: perform tasks in decreasing order oftheir post numbers.
The only edges (u, v) in a graph for which post(u) < post(v) are backedges, and we have seen that a DAG cannot have back edges.
![Page 40: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/40.jpg)
Directed Acyclic Graphs (DAG)
Linearization/Topologically Sort: Order the vertices such that everyedge goes from a earlier vertex to a later one.
Q: What types of dags can be linearized?
A: All of them.
DFS tells us exactly how to do it: perform tasks in decreasing order oftheir post numbers.
The only edges (u, v) in a graph for which post(u) < post(v) are backedges, and we have seen that a DAG cannot have back edges.
![Page 41: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/41.jpg)
Directed Acyclic Graphs (DAG)
Linearization/Topologically Sort: Order the vertices such that everyedge goes from a earlier vertex to a later one.
Q: What types of dags can be linearized?
A: All of them.
DFS tells us exactly how to do it: perform tasks in decreasing order oftheir post numbers.
The only edges (u, v) in a graph for which post(u) < post(v) are backedges, and we have seen that a DAG cannot have back edges.
![Page 42: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/42.jpg)
Directed Acyclic Graphs (DAG)
Linearization/Topologically Sort: Order the vertices such that everyedge goes from a earlier vertex to a later one.
Q: What types of dags can be linearized?
A: All of them.
DFS tells us exactly how to do it: perform tasks in decreasing order oftheir post numbers.
The only edges (u, v) in a graph for which post(u) < post(v) are backedges, and we have seen that a DAG cannot have back edges.
![Page 43: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/43.jpg)
Directed Acyclic Graphs (DAG)
Linearization/Topologically Sort: Order the vertices such that everyedge goes from a earlier vertex to a later one.
Q: What types of dags can be linearized?
A: All of them.
DFS tells us exactly how to do it: perform tasks in decreasing order oftheir post numbers.
The only edges (u, v) in a graph for which post(u) < post(v) are backedges, and we have seen that a DAG cannot have back edges.
![Page 44: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/44.jpg)
Directed Acyclic Graphs (DAG)
Lemma:In a DAG, every edge leads to a vertex with a lower post number.
![Page 45: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/45.jpg)
Directed Acyclic Graphs (DAG)
There is a linear-time algorithm for ordering the nodes of a DAG.
Acyclicity, linearizability, and the absence of back edges during adepth-first search - are the same thing.
The vertex with the smallest post number comes last in thislinearization, and it must be a sink - no outgoing edges.
Symmetrically, the one with the highest post is a source, a node withno incoming edges.
![Page 46: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/46.jpg)
Directed Acyclic Graphs (DAG)
There is a linear-time algorithm for ordering the nodes of a DAG.
Acyclicity, linearizability, and the absence of back edges during adepth-first search - are the same thing.
The vertex with the smallest post number comes last in thislinearization, and it must be a sink - no outgoing edges.
Symmetrically, the one with the highest post is a source, a node withno incoming edges.
![Page 47: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/47.jpg)
Directed Acyclic Graphs (DAG)
There is a linear-time algorithm for ordering the nodes of a DAG.
Acyclicity, linearizability, and the absence of back edges during adepth-first search - are the same thing.
The vertex with the smallest post number comes last in thislinearization, and it must be a sink - no outgoing edges.
Symmetrically, the one with the highest post is a source, a node withno incoming edges.
![Page 48: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/48.jpg)
Directed Acyclic Graphs (DAG)
There is a linear-time algorithm for ordering the nodes of a DAG.
Acyclicity, linearizability, and the absence of back edges during adepth-first search - are the same thing.
The vertex with the smallest post number comes last in thislinearization, and it must be a sink - no outgoing edges.
Symmetrically, the one with the highest post is a source, a node withno incoming edges.
![Page 49: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/49.jpg)
Directed Acyclic Graphs (DAG)
Lemma:Every DAG has at least one source and at least one sink.
The guaranteed existence of a source suggests an alternative approachto linearization:
1 Find a source, output it, and delete it from the graph.
2 Repeat until the graph is empty.
![Page 50: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/50.jpg)
Directed Acyclic Graphs (DAG)
Lemma:Every DAG has at least one source and at least one sink.
The guaranteed existence of a source suggests an alternative approachto linearization:
1 Find a source, output it, and delete it from the graph.
2 Repeat until the graph is empty.
![Page 51: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/51.jpg)
Directed Acyclic Graphs (DAG)
Lemma:Every DAG has at least one source and at least one sink.
The guaranteed existence of a source suggests an alternative approachto linearization:
1 Find a source, output it, and delete it from the graph.
2 Repeat until the graph is empty.
![Page 52: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/52.jpg)
Directed Acyclic Graphs (DAG)
Lemma:Every DAG has at least one source and at least one sink.
The guaranteed existence of a source suggests an alternative approachto linearization:
1 Find a source, output it, and delete it from the graph.
2 Repeat until the graph is empty.
![Page 53: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/53.jpg)
Strongly Connected Components (SCC)
![Page 54: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/54.jpg)
Defining Connectivity for DirectedGraphs
Definition:Two nodes u and v of a directed graph are connected if there is a pathfrom u to v and a path from v to u.
This relation partitions V into disjoint sets that we call stronglyconnected components (SCC).
Lemma:Every directed graph is a DAG of its SCC.
![Page 55: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/55.jpg)
Defining Connectivity for DirectedGraphs
Definition:Two nodes u and v of a directed graph are connected if there is a pathfrom u to v and a path from v to u.
This relation partitions V into disjoint sets that we call stronglyconnected components (SCC).
Lemma:Every directed graph is a DAG of its SCC.
![Page 56: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/56.jpg)
Defining Connectivity for DirectedGraphs
Definition:Two nodes u and v of a directed graph are connected if there is a pathfrom u to v and a path from v to u.
This relation partitions V into disjoint sets that we call stronglyconnected components (SCC).
Lemma:Every directed graph is a DAG of its SCC.
![Page 57: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/57.jpg)
Strongly Connected Components
(a)
A
D E
C
F
B
HG
K
L
JI
(b)
A B,E C,F
DJ,K,L
G,H,I
![Page 58: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/58.jpg)
An Efficient Algorithm
Lemma:If the EXPLORE subroutine at node u, then it will terminate preciselywhen all nodes reachable from u have been visited.
If we call explore on a node that lies somewhere in a sink SCC, thenwe will retrieve exactly that component.
We have two problems:
1 How do we find a node that we know for sure lies in a sink SCC?
2 How do we continue once this first component has beendiscovered?
![Page 59: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/59.jpg)
An Efficient Algorithm
Lemma:If the EXPLORE subroutine at node u, then it will terminate preciselywhen all nodes reachable from u have been visited.
If we call explore on a node that lies somewhere in a sink SCC, thenwe will retrieve exactly that component.
We have two problems:
1 How do we find a node that we know for sure lies in a sink SCC?
2 How do we continue once this first component has beendiscovered?
![Page 60: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/60.jpg)
An Efficient Algorithm
Lemma:If the EXPLORE subroutine at node u, then it will terminate preciselywhen all nodes reachable from u have been visited.
If we call explore on a node that lies somewhere in a sink SCC, thenwe will retrieve exactly that component.
We have two problems:
1 How do we find a node that we know for sure lies in a sink SCC?
2 How do we continue once this first component has beendiscovered?
![Page 61: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/61.jpg)
An Efficient Algorithm
Lemma:If the EXPLORE subroutine at node u, then it will terminate preciselywhen all nodes reachable from u have been visited.
If we call explore on a node that lies somewhere in a sink SCC, thenwe will retrieve exactly that component.
We have two problems:
1 How do we find a node that we know for sure lies in a sink SCC?
2 How do we continue once this first component has beendiscovered?
![Page 62: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/62.jpg)
An Efficient Algorithm
Lemma:The node that receives the highest post number in a depth-first searchmust lie in a source SCC.
Lemma:If C and C′ are SCC, and there is an edge from a node in C to a nodein C′, then the highest post number in C is bigger than the highest postnumber in C′.
Hence the SCCs can be linearized by arranging them in decreasingorder of their highest post numbers.
![Page 63: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/63.jpg)
An Efficient Algorithm
Lemma:The node that receives the highest post number in a depth-first searchmust lie in a source SCC.
Lemma:If C and C′ are SCC, and there is an edge from a node in C to a nodein C′, then the highest post number in C is bigger than the highest postnumber in C′.
Hence the SCCs can be linearized by arranging them in decreasingorder of their highest post numbers.
![Page 64: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/64.jpg)
An Efficient Algorithm
Lemma:The node that receives the highest post number in a depth-first searchmust lie in a source SCC.
Lemma:If C and C′ are SCC, and there is an edge from a node in C to a nodein C′, then the highest post number in C is bigger than the highest postnumber in C′.
Hence the SCCs can be linearized by arranging them in decreasingorder of their highest post numbers.
![Page 65: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/65.jpg)
Solving Problem A
Consider the reverse graph GR, the same as G but with all edgesreversed.
GR has exactly the same SCCs as G.
If we do a depth-first search of GR, the node with the highest postnumber will come from a source SCC in GR.It is a sink SCC in G.
![Page 66: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/66.jpg)
Solving Problem A
Consider the reverse graph GR, the same as G but with all edgesreversed.
GR has exactly the same SCCs as G.
If we do a depth-first search of GR, the node with the highest postnumber will come from a source SCC in GR.It is a sink SCC in G.
![Page 67: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/67.jpg)
Solving Problem A
Consider the reverse graph GR, the same as G but with all edgesreversed.
GR has exactly the same SCCs as G.
If we do a depth-first search of GR, the node with the highest postnumber will come from a source SCC in GR.It is a sink SCC in G.
![Page 68: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/68.jpg)
Strongly Connected Components
A
D E
C
F
B
HG
K
L
JI
A
D E
C
F
B
HG
K
L
JI
![Page 69: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/69.jpg)
Solving Problem B
Once we have found the first SCC and deleted it from the graph, thenode with the highest post number among those remaining will belongto a sink SCC of whatever remains of G.
Therefore we can keep using the post numbering from our initialdepth-first search on GR to successively output the second stronglyconnected component, the third SCC, and so on.
![Page 70: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/70.jpg)
Solving Problem B
Once we have found the first SCC and deleted it from the graph, thenode with the highest post number among those remaining will belongto a sink SCC of whatever remains of G.
Therefore we can keep using the post numbering from our initialdepth-first search on GR to successively output the second stronglyconnected component, the third SCC, and so on.
![Page 71: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/71.jpg)
The Linear-Time Algorithm
1 Run depth-first search on GR.
2 Run the EXPLORE algorithm on G, and during the depth-firstsearch, process the vertices in decreasing order of their postnumbers from step 1.
![Page 72: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/72.jpg)
The Linear-Time Algorithm
1 Run depth-first search on GR.
2 Run the EXPLORE algorithm on G, and during the depth-firstsearch, process the vertices in decreasing order of their postnumbers from step 1.
![Page 73: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/73.jpg)
Strongly Connected Components
A
D E
C
F
B
HG
K
L
JI
A
D E
C
F
B
HG
K
L
JI
![Page 74: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/74.jpg)
Thinking About
How the SCC algorithm works when the graph is very, very huge?
An Easter egg: A report or a solution will earn extra scores!
![Page 75: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/75.jpg)
Thinking About
How the SCC algorithm works when the graph is very, very huge?
An Easter egg: A report or a solution will earn extra scores!
![Page 76: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/76.jpg)
A Question to Think About
How about edges instead of paths?
![Page 77: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/77.jpg)
Exercises
![Page 78: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/78.jpg)
Exercises 1
3.16. Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G
has a node for each course, and an edge from course v to course w if and only if v is a prerequisite
for w. Find an algorithm that works directly with this graph representation, and computes the
minimum number of semesters necessary to complete the curriculum (assume that a student
can take any number of courses in one semester). The running time of your algorithm should be
linear.
![Page 79: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/79.jpg)
Exercises 2
3.22. Give an efficient algorithm which takes as input a directed graph G = (V, E), and determineswhether or not there is a vertex s ∈ V from which all other vertices are reachable.
![Page 80: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/80.jpg)
BFS in Graphs„ Revisited
![Page 81: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/81.jpg)
Breadth-First SearchBFS(G, v)input : Graph G = (V ,E), directed or undirected; Vertex v ∈ Voutput: For all vertices u reachable from v, dist(u) is the set to the
distance from v to u
for all u ∈ V dodist(u) =∞;
enddist[v] = 0;Q = [v] queue containing just v;while Q is not empty do
u=Eject(Q);for all edge (u, s) ∈ E do
if dist(s) =∞ thenInject(Q,s);dist[s] = dist[u] + 1;
endend
end
![Page 82: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/82.jpg)
Dijkstra’s Shortest-Path AlgorithmDIJKSTRA(G, l, s)input : Graph G = (V ,E), directed or undirected; positive edge length
{le | e ∈ E}; Vertex s ∈ Voutput: For all vertices u reachable from s, dist(u) is the set to the distance from s
to u
for all u ∈ V dodist(u) = ∞;prev(u) = nil;
enddist(s) = 0;H =makequeue(V)\\ using dist-values as keys;while H is not empty do
u=deletemin(H);for all edge (u, v) ∈ E do
if dist(v) > dist(u) + l(u, v) thendist(v) = dist(u) + l(u, v); prev(v) = u;decreasekey (H ,v);
endend
end
![Page 83: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/83.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 84: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/84.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 85: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/85.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 86: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/86.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 87: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/87.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 88: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/88.jpg)
An Example
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D:∞
B: 4 E:∞
C: 2
B
C
D
E
A
4
1 3
2
4
1
3
5
2
A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
4
2
4
1
3
5
2
1 3A: 0 D: 6B: 3 E: 7C: 2
B
C
D
E
A
4
1 3
2
1
5
2
3
4 A: 0 D: 5B: 3 E: 6C: 2
B
C
D
E
A
2
1 3
2
![Page 89: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/89.jpg)
Shortest Paths in the Presence of Negative Edges
![Page 90: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/90.jpg)
Negative EdgesDijkstra’s algorithm works in part because the shortest path from thestarting point s to any node v must pass exclusively through nodes thatare closer than v.
This no longer holds when edge lengths can be negative.
Q: What needs to be changed in order to accommodate this newcomplication?
A crucial invariant of Dijkstra’s algorithm is that the dist values itmaintains are always either overestimates or exactly correct.
They start off at∞, and the only way they ever change is by updatingalong an edge:
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
![Page 91: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/91.jpg)
Negative EdgesDijkstra’s algorithm works in part because the shortest path from thestarting point s to any node v must pass exclusively through nodes thatare closer than v.
This no longer holds when edge lengths can be negative.
Q: What needs to be changed in order to accommodate this newcomplication?
A crucial invariant of Dijkstra’s algorithm is that the dist values itmaintains are always either overestimates or exactly correct.
They start off at∞, and the only way they ever change is by updatingalong an edge:
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
![Page 92: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/92.jpg)
Negative EdgesDijkstra’s algorithm works in part because the shortest path from thestarting point s to any node v must pass exclusively through nodes thatare closer than v.
This no longer holds when edge lengths can be negative.
Q: What needs to be changed in order to accommodate this newcomplication?
A crucial invariant of Dijkstra’s algorithm is that the dist values itmaintains are always either overestimates or exactly correct.
They start off at∞, and the only way they ever change is by updatingalong an edge:
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
![Page 93: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/93.jpg)
Negative EdgesDijkstra’s algorithm works in part because the shortest path from thestarting point s to any node v must pass exclusively through nodes thatare closer than v.
This no longer holds when edge lengths can be negative.
Q: What needs to be changed in order to accommodate this newcomplication?
A crucial invariant of Dijkstra’s algorithm is that the dist values itmaintains are always either overestimates or exactly correct.
They start off at∞, and the only way they ever change is by updatingalong an edge:
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
![Page 94: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/94.jpg)
Negative EdgesDijkstra’s algorithm works in part because the shortest path from thestarting point s to any node v must pass exclusively through nodes thatare closer than v.
This no longer holds when edge lengths can be negative.
Q: What needs to be changed in order to accommodate this newcomplication?
A crucial invariant of Dijkstra’s algorithm is that the dist values itmaintains are always either overestimates or exactly correct.
They start off at∞, and the only way they ever change is by updatingalong an edge:
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
![Page 95: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/95.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
This UPDATE operation is simply an expression of the fact that thedistance to v cannot possibly be more than the distance to u, plusl(u, v). It has the following properties,
1 It gives the correct distance to v in the particular case where u isthe second-last node in the shortest path to v, and dist(u) iscorrectly set.
2 It will never make dist(v) too small, and in this sense it is safe.For instance, a slew of extraneous update’s can’t hurt.
![Page 96: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/96.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
This UPDATE operation is simply an expression of the fact that thedistance to v cannot possibly be more than the distance to u, plusl(u, v). It has the following properties,
1 It gives the correct distance to v in the particular case where u isthe second-last node in the shortest path to v, and dist(u) iscorrectly set.
2 It will never make dist(v) too small, and in this sense it is safe.For instance, a slew of extraneous update’s can’t hurt.
![Page 97: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/97.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
This UPDATE operation is simply an expression of the fact that thedistance to v cannot possibly be more than the distance to u, plusl(u, v). It has the following properties,
1 It gives the correct distance to v in the particular case where u isthe second-last node in the shortest path to v, and dist(u) iscorrectly set.
2 It will never make dist(v) too small, and in this sense it is safe.For instance, a slew of extraneous update’s can’t hurt.
![Page 98: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/98.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
This UPDATE operation is simply an expression of the fact that thedistance to v cannot possibly be more than the distance to u, plusl(u, v). It has the following properties,
1 It gives the correct distance to v in the particular case where u isthe second-last node in the shortest path to v, and dist(u) iscorrectly set.
2 It will never make dist(v) too small, and in this sense it is safe.For instance, a slew of extraneous update’s can’t hurt.
![Page 99: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/99.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
Lets→ u1 → u2 → u3 → . . .→ uk → k
be a shortest path from s to t.
This path can have at most |V | − 1 edges (why?).
![Page 100: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/100.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
Lets→ u1 → u2 → u3 → . . .→ uk → k
be a shortest path from s to t.
This path can have at most |V | − 1 edges (why?).
![Page 101: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/101.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
Lets→ u1 → u2 → u3 → . . .→ uk → k
be a shortest path from s to t.
This path can have at most |V | − 1 edges (why?).
![Page 102: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/102.jpg)
Update
UPDATE ((u, v) ∈ E)
dist(v) = min{dist(v), dist(u) + l(u, v)};
Lets→ u1 → u2 → u3 → . . .→ uk → k
be a shortest path from s to t.
This path can have at most |V | − 1 edges (why?).
![Page 103: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/103.jpg)
Bellman-Ford Algorithm
If we don’t know all the shortest paths beforehand, how can we besure to update the right edges in the right order?
We simply update all the edges, |V | − 1 times!
![Page 104: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/104.jpg)
Bellman-Ford Algorithm
If we don’t know all the shortest paths beforehand, how can we besure to update the right edges in the right order?
We simply update all the edges, |V | − 1 times!
![Page 105: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/105.jpg)
Bellman-Ford Algorithm
SHORTEST-PATHS(G, l, s)input : Graph G = (V ,E), edge length{le | e ∈ E}; Vertex s ∈ Voutput: For all vertices u reachable from s, dist(u) is the set to the
distance from s to u
for all u ∈ V dodist(u) =∞;prev(u) = nil;
enddist[s] = 0;repeat |V | − 1 times: for e ∈ E do
UPDATE(e);end
Running time:O(|V | · |E|)
![Page 106: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/106.jpg)
Bellman-Ford Algorithm
SHORTEST-PATHS(G, l, s)input : Graph G = (V ,E), edge length{le | e ∈ E}; Vertex s ∈ Voutput: For all vertices u reachable from s, dist(u) is the set to the
distance from s to u
for all u ∈ V dodist(u) =∞;prev(u) = nil;
enddist[s] = 0;repeat |V | − 1 times: for e ∈ E do
UPDATE(e);end
Running time:O(|V | · |E|)
![Page 107: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/107.jpg)
Bellman-Ford Algorithm
E
B
A
G
F
D
S
C
3
1
1
−2
2
10
−1
−1
−4
1
8 Iteration
Node 0 1 2 3 4 5 6 7
S 0 0 0 0 0 0 0 0A ∞ 10 10 5 5 5 5 5B ∞ ∞ ∞ 10 6 5 5 5C ∞ ∞ ∞ ∞ 11 7 6 6D ∞ ∞ ∞ ∞ ∞ 14 10 9E ∞ ∞ 12 8 7 7 7 7F ∞ ∞ 9 9 9 9 9 9G ∞ 8 8 8 8 8 8 8
![Page 108: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/108.jpg)
Negative Cycles
If the graph has a negative cycle, then it doesn’t make sense to evenask about shortest path.
Q: How to detect the existence of negative cycles:
Instead of stopping after |V | − 1, iterations, perform one extra round.
There is a negative cycle if and only if some dist value is reducedduring this final round.
![Page 109: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/109.jpg)
Negative Cycles
If the graph has a negative cycle, then it doesn’t make sense to evenask about shortest path.
Q: How to detect the existence of negative cycles:
Instead of stopping after |V | − 1, iterations, perform one extra round.
There is a negative cycle if and only if some dist value is reducedduring this final round.
![Page 110: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/110.jpg)
Negative Cycles
If the graph has a negative cycle, then it doesn’t make sense to evenask about shortest path.
Q: How to detect the existence of negative cycles:
Instead of stopping after |V | − 1, iterations, perform one extra round.
There is a negative cycle if and only if some dist value is reducedduring this final round.
![Page 111: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/111.jpg)
Negative Cycles
If the graph has a negative cycle, then it doesn’t make sense to evenask about shortest path.
Q: How to detect the existence of negative cycles:
Instead of stopping after |V | − 1, iterations, perform one extra round.
There is a negative cycle if and only if some dist value is reducedduring this final round.
![Page 112: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/112.jpg)
Shortest Paths in Dags
![Page 113: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/113.jpg)
Graphs without Negative EdgesThere are two subclasses of graphs that automatically exclude thepossibility of negative cycles:
• graphs without negative edges,• and graphs without cycles.
We already know how to efficiently handle the former.
We will now see how the single-source shortest-path problem can besolved in just linear time on directed acyclic graphs.
As before, we need to perform a sequence of updates that includesevery shortest path as a subsequence.
In any path of a DAG, the vertices appear in increasing linearizedorder.
![Page 114: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/114.jpg)
Graphs without Negative EdgesThere are two subclasses of graphs that automatically exclude thepossibility of negative cycles:• graphs without negative edges,• and graphs without cycles.
We already know how to efficiently handle the former.
We will now see how the single-source shortest-path problem can besolved in just linear time on directed acyclic graphs.
As before, we need to perform a sequence of updates that includesevery shortest path as a subsequence.
In any path of a DAG, the vertices appear in increasing linearizedorder.
![Page 115: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/115.jpg)
Graphs without Negative EdgesThere are two subclasses of graphs that automatically exclude thepossibility of negative cycles:• graphs without negative edges,• and graphs without cycles.
We already know how to efficiently handle the former.
We will now see how the single-source shortest-path problem can besolved in just linear time on directed acyclic graphs.
As before, we need to perform a sequence of updates that includesevery shortest path as a subsequence.
In any path of a DAG, the vertices appear in increasing linearizedorder.
![Page 116: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/116.jpg)
Graphs without Negative EdgesThere are two subclasses of graphs that automatically exclude thepossibility of negative cycles:• graphs without negative edges,• and graphs without cycles.
We already know how to efficiently handle the former.
We will now see how the single-source shortest-path problem can besolved in just linear time on directed acyclic graphs.
As before, we need to perform a sequence of updates that includesevery shortest path as a subsequence.
In any path of a DAG, the vertices appear in increasing linearizedorder.
![Page 117: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/117.jpg)
Graphs without Negative EdgesThere are two subclasses of graphs that automatically exclude thepossibility of negative cycles:• graphs without negative edges,• and graphs without cycles.
We already know how to efficiently handle the former.
We will now see how the single-source shortest-path problem can besolved in just linear time on directed acyclic graphs.
As before, we need to perform a sequence of updates that includesevery shortest path as a subsequence.
In any path of a DAG, the vertices appear in increasing linearizedorder.
![Page 118: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/118.jpg)
A Shortest-Path Algorithm for DAG
DAG-SHORTEST-PATHS(G, l, s)input : Graph G = (V ,E), edge length{le | e ∈ E}; Vertex s ∈ Voutput: For all vertices u reachable from s, dist(u) is the set to the
distance from s to u
for all u ∈ V dodist(u) =∞;prev(u) = nil;
enddists = 0;linearize G;for each u ∈ V in linearized order do
for all e ∈ E doUPDATE(e);
endend
![Page 119: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/119.jpg)
A Shortest-Path Algorithm for DAG
Note that the scheme doesn’t require edges to be positive.
In particular, we can find longest paths in a DAG by the samealgorithm: just negate all edge lengths.
![Page 120: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/120.jpg)
A Shortest-Path Algorithm for DAG
Note that the scheme doesn’t require edges to be positive.
In particular, we can find longest paths in a DAG by the samealgorithm: just negate all edge lengths.
![Page 121: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/121.jpg)
Exercises
![Page 122: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/122.jpg)
Exercises 3
Professor Fake suggests the following algorithm for finding theshortest path from node s to node t in a directed graph with somenegative edges: add a large constant to each edge weight so that all theweights become positive, then run Dijkstra’s algorithm starting atnode s, and return the shortest path found to node t.
![Page 123: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/123.jpg)
Exercises 4
4.14. You are given a strongly connected directed graph G = (V, E) with positive edge weights alongwith a particular node v0 ∈ V . Give an efficient algorithm for finding shortest paths between all
pairs of nodes, with the one restriction that these paths must all pass through v0.
![Page 124: Theory of Algorithms I - SJTU · Theory of Algorithms I Graph Algorithms, Revisited Guoqiang Li School of Software, Shanghai Jiao Tong University](https://reader033.vdocuments.us/reader033/viewer/2022050515/5f9ef57018825b2cd92f2f36/html5/thumbnails/124.jpg)
Homework
[DPV07]. 3.7, 3.11, 3.28, 4.11, 4.12, 4.16
[Als99]. 9.18, 9.33,