20 GraphAlgorithms
20 GraphAlgorithms
20 GraphAlgorithms
Graph Algorthms
CSE 332 Summer 2021
Teaching Assistants:
Alena Dickmann Arya GJ Finn Johnson
Joon Chong Kimi Locke Peyton Rapo
Rahul Misal Winston Jodjana
L20: Graph Algorithms CSE332, Summer 2021
Announcements
v Exercises 13, 14 out!
§ Correct due dates listed on Ed
2
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
3
L20: Graph Algorithms CSE332, Summer 2021
4
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
5
L20: Graph Algorithms CSE332, Summer 2021
A B C D
A F T F F
D
A
B T F F F
C
C F T F T
B
D F F F F
6
L20: Graph Algorithms CSE332, Summer 2021
A B C D
D A F T F F
A
C B T F T F
B
C F T F T
D F F T F
8
L20: Graph Algorithms CSE332, Summer 2021
9
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
10
L20: Graph Algorithms CSE332, Summer 2021
adjacent vertices
§ E.g. linked list
D
A B
A B A
C
C B D
B
D /
11
L20: Graph Algorithms CSE332, Summer 2021
A B
B A C
C B D
D C 13
L20: Graph Algorithms CSE332, Summer 2021
D A B, 7
6
A B A, 3
7 C
C B, 2 D, 6
2
3 B D /
14
L20: Graph Algorithms CSE332, Summer 2021
16
L20: Graph Algorithms CSE332, Summer 2021
Graph Problems
T S
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
20
L20: Graph Algorithms CSE332, Summer 2021
v Example input:
CSE 311 CSE 312 CSE 440
MATH 126 CSE 331 CSE 332
CSE 143
CSE 341 CSE 333
CSE 142
CSE 351 CSE 352
v Example output:
§ 126, 142, 143, 311, 331, 332, 312, 341, 351, 333, 352, 440
21
L20: Graph Algorithms CSE332, Summer 2021
gradescope.com/courses/275833
22
L20: Graph Algorithms CSE332, Summer 2021
23
L20: Graph Algorithms CSE332, Summer 2021
2
1 4
TopoSort: A Naïve Algorithm 3
0
3 4
4 /
24
L20: Graph Algorithms CSE332, Summer 2021
TopoSort: Notes
v Needed a vertex with in-degree of 0 to start
§ Remember: graph must be acyclic!
25
L20: Graph Algorithms CSE332, Summer 2021
2
1 4
Naïve TopoSort: Running Time? 3
0
labelEachVertexWithItsInDegree();
for (i=0; i < numVertices; i++){
v = findNewVertexOfDegreeZero();
put v next in output
for each w adjacent to v
w.indegree--;
}
In- Adj
Degree List
0 3
1 2 3
2 4
3 4
4
26
L20: Graph Algorithms CSE332, Summer 2021
v Using a queue:
§ Label each vertex with its in-degree, enqueuing 0-degree nodes
§ While “pending” queue is not empty:
• v = dequeue()
• Output v and remove it from the graph
• For each vertex w adjacent to v (i.e. w such that (v,w) in E):
– decrement the in-degree of w
– if new degree is 0, enqueue it
27
L20: Graph Algorithms CSE332, Summer 2021
2
1 4
Better TopoSort: Running Time? 3
0
labelAllAndEnqueueZeros();
for (i=0; i < numVertices; i++){
v = dequeue();
put v next in output
for each w adjacent to v
w.indegree--;
if (w.indegree == 0)
enqueue(w);
}
In- Adj
Degree List
0 3
1 2 3
2 4
3 4
4
28
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
29
L20: Graph Algorithms CSE332, Summer 2021
gradescope.com/courses/275833
30
L20: Graph Algorithms CSE332, Summer 2021
v Related Questions:
§ Is an undirected graph connected?
§ Is a directed graph weakly / strongly connected?
• For strongly, need a cycle back to starting node
31
L20: Graph Algorithms CSE332, Summer 2021
32
L20: Graph Algorithms CSE332, Summer 2021
34
L20: Graph Algorithms CSE332, Summer 2021
35
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
36
L20: Graph Algorithms CSE332, Summer 2021
BFS(Node start) {
q.enqueue(start)
mark start as visited
while (!q.empty())
next = q.dequeue()
process(next)
foreach u adjacent to next
if (!u.marked)
mark u
q.enqueue(u)
}
37
L20: Graph Algorithms CSE332, Summer 2021
Trees: Level-Order
D
v Process top-to-bottom, left-to-right
B F
§ Like reading in English
A C E G
§ Goes “broad” instead of “deep”
38
L20: Graph Algorithms CSE332, Summer 2021
A
Marked:
B
Order Processed: C D
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
39
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
40
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
41
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
42
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
43
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
44
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
45
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
46
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
47
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
48
L20: Graph Algorithms CSE332, Summer 2021
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
49
L20: Graph Algorithms CSE332, Summer 2021
A
Marked:
A, B, C, D, E, F, G, H, J, I
B
Order Processed: C D
A, B, C, D, E, F, G, H, J, I
E H
BFS(Node start) {
q.enqueue(start)
F
mark start as visited
G
while (!q.empty()) I
next = q.dequeue()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.enqueue(u)
}
50
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
51
L20: Graph Algorithms CSE332, Summer 2021
DFSIterative(Node start) {
s.push(start)
mark start as visited
while (!s.empty())
next = s.pop()
process(next)
foreach u adjacent to next
if (!u.marked)
mark u
q.push(u)
}
52
L20: Graph Algorithms CSE332, Summer 2021
A
Marked:
B
Order Processed: C D
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
53
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
54
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
55
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
56
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
57
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
58
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
59
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
60
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
61
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
62
L20: Graph Algorithms CSE332, Summer 2021
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
63
L20: Graph Algorithms CSE332, Summer 2021
A
Marked:
A, B, C, D, G, H, I, J, E, F
B
Order Processed: C D
A, D, H, I, J, G, C, B, F
E H
DFSIterative(Node start) {
s.push(start)
F
mark start as visited
G
while (!s.empty())
I
next = s.pop()
process(next)
foreach u adjacent to next J
if (!u.marked)
mark u
q.push(u)
}
64
L20: Graph Algorithms CSE332, Summer 2021
gradescope.com/courses/275833
65
L20: Graph Algorithms CSE332, Summer 2021
Lecture Outline
v Graph Representations
§ Adjacency Matrix
§ Adjacency List
v Topological Sort
v Traversals
§ Breadth-first
§ Depth-first
§ Conclusion
66
L20: Graph Algorithms CSE332, Summer 2021
v Modifications:
§ Instead of just “marking” a node, store the path’s previous node
• ie: when processing u, if we add v to the “remaining work” set v.prev to u
§ When you reach the goal, follow prev fields backwards to start
• (don’t forget to reverse the answer)
§ Path length:
• Same idea, but also store integer distance at each node
67
L20: Graph Algorithms CSE332, Summer 2021
68
L20: Graph Algorithms CSE332, Summer 2021
DFS/BFS Comparison
v Breadth-first search:
§ Always finds shortest paths, i.e., finds “optimal solutions”
• Better for “what is the shortest path from x to y?”
§ But queue may hold up to O(|V|) nodes
• Eg, at the bottom level of perfect binary tree, queue contains |V|/2 nodes
v Depth-first search:
§ Can use less space when finding a path
• If longest path in the graph is p and highest out-degree is d then stack never
has more than d*p elements
70
L20: Graph Algorithms CSE332, Summer 2021
71
L20: Graph Algorithms CSE332, Summer 2021
Summary
v Two very different “standard” graph representations
§ Must understand tradeoffs to choose between adj list and adj matrix