Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

20 GraphAlgorithms

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

L20: Graph Algorithms CSE332, Summer 2021

Graph Algorthms
CSE 332 Summer 2021

Instructor: Kristofer Wong

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

v Midterm reflection Q1 resubmissions

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

What is the Data Structure?


v Is a Graph an ADT? Maybe!
§ “Develop an algorithm over the graph, then use whatever data
structure is efficient”

v The “best” data structure can depend on:


§ Properties of the graph (e.g., dense versus sparse)
§ Common queries
• e.g., “is (u,v) an edge?” vs “what are the neighbors of node u?”

v There are two standard graph representations:


§ Adjacency Matrix and Adjacency List
§ Different trade-offs, particularly time vs space

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

Adjacency Matrix: Representation


v Assign each node a number from 0 to |V|-1
v Graph is a |V|x|V| matrix (ie, 2-D array) of booleans

§ M[u][v] == true means there is an edge from u to v

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

Adjacency Matrix: Properties (1 of 3)


v Running time to: A B C D
§ Get a vertex’s out-edges:
• O(|V|)
A F T F F
§ Get a vertex’s in-edges: B T F F F
• O(|V|)
§ Decide if some edge exists:
C F T F T
• O(1) D F F F F
§ Insert an edge:
• O(1)
D
§ Delete an edge:
• O(1) A
C
v Space requirements:
§ |V|2 bits B
v Best for sparse or dense graphs?

§ Best for dense graphs 7


L20: Graph Algorithms CSE332, Summer 2021

Adjacency Matrix: Properties (2 of 3)


v How does the adjacency matrix vary for an undirected graph?
§ Undirected graphs are symmetric about diagonal axis
§ Languages with array-of-array matrix representations can save ½ the
space by omitting the symmetric half
• Languages with “proper” 2D matrix representations (eg, C/C++) can’t do this

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

Adjacency Matrix: Properties (3 of 3)


v How can we adapt the representation for weighted graphs?
§ Store the weight in each cell
§ Need some value to represent “not an edge”
• In some situations, 0 or -1 works
A B C D
D
6 A 0 7 0 0
A
7 C B 3 0 0 0
2
3 B C 0 2 0 6
D 0 0 0 0

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

Adjacency List: Representation


v Assign each node a number from 0 to |V|-1
v Graph is an array of length |V|; each entry stores a list of all

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

Adjacency List: Properties (1 of 3)


v Running time to:
§ Get a vertex’s out-edges:
• O(d) where d is out-degree of vertex
§ Get a vertex’s in-edges: A B
• O(|V| + |E|)
• (but could keep a second adjacency list for this!) B A
§ Decide if some edge exists: C B D
• O(d) where d is out-degree of source vertex
§ Insert an edge: D /
• O(1)
• (unless you need to check if it’s there; then O(d)) D
§ Delete an edge:
• O(d) where d is out-degree of source vertex A
C
v Space requirements:
§ O(|V|+|E|)
B
v Best for sparse or dense graphs?
§ Best for sparse graphs, so usually just stick with linked lists for the buckets 12
L20: Graph Algorithms CSE332, Summer 2021

Adjacency List: Properties (2 of 3)


v How does the adjacency list vary for an undirected graph?
§ Optionally, can double the entries to increase edge lookup speed
A B
D B C
A C D
C
D /
B
… or …

A B
B A C
C B D
D C 13
L20: Graph Algorithms CSE332, Summer 2021

Adjacency List: Properties (3 of 3)


v How can we adapt the representation for weighted graphs?
§ Store the weight alongside the destination vertex
§ No need for a special value to represent “not an edge”!

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

Summary: Which is Better?


v Graphs are often sparse:
§ Road networks are often grids
• Every corner isn’t connected to every other corner
§ Airlines rarely fly to all possible cities
• Or if they do it is to/from a hub

v Adjacency lists should generally be your default choice


§ Slower performance compensated by greater space savings
§ Many graph algorithms rely heavily on getAllEdgesFrom(v)
getAllEdgesFrom(v) hasEdge(v, w) getAllEdges()
Adjacency
Θ(V) Θ(1) Θ(V2)
Matrix
Adjacency List Θ(degree(v)) Θ(degree(v)) Θ(E + V)
15
L20: Graph Algorithms CSE332, Summer 2021

Quick Detour: Overview of Graph Problems

16
L20: Graph Algorithms CSE332, Summer 2021

Graph Problems

v Lots of interesting questions we can ask about a graph:


§ What is the shortest route from S to T? What is the longest route
without cycles?
§ Are there cycles in this graph?
§ Is there a cycle that uses each vertex exactly once?
§ Is there a cycle that uses each edge exactly once?

T S
L20: Graph Algorithms CSE332, Summer 2021

Graph Problems More Theoretically


v Some well known graph problems and their common names:
§ s-t Path. Is there a path between vertices s and t?
§ Connectivity. Is the graph connected?
§ Biconnectivity. Is there a vertex whose removal disconnects the
graph?
§ Shortest s-t Path. What is the shortest path between vertices s and t?
§ Cycle Detection. Does the graph contain any cycles?
§ Euler Tour. Is there a cycle that uses every edge exactly once?
§ Hamilton Tour. Is there a cycle that uses every vertex exactly once?
§ Planarity. Can you draw the graph on paper with no crossing edges?
§ Isomorphism. Are two graphs the same graph (in disguise)?

v Often can’t tell how difficult a graph problem is without very


deep consideration.
L20: Graph Algorithms CSE332, Summer 2021

Graph Problem Difficulty

v Some well known graph problems:


§ Euler Tour: Is there a cycle that uses every edge exactly once?
§ Hamilton Tour: Is there a cycle that uses every vertex exactly once?

v Difficulty can be deceiving


§ An efficient Euler tour algorithm O(# edges) was found as early as
1873 [Link].
§ Despite decades of intense study, no efficient algorithm for a
Hamilton tour exists. Best algorithms are exponential time.

v Graph problems are among the most mathematically rich areas


of CS theory
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

Disclaimer: Do not use for official advising purposes!


Topological Sort Falsely implies CSE 332 is a prereq for CSE 312, etc.

v Given a DAG, output all the vertices in an order such that no


vertex appears before any other vertex that has a path to it

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

v List 3 valid Topological sorts: 2


1 4
3
0

v Why do we perform topological sorts only on DAGs?


§ A cycle means there is no correct answer
v Does a DAG always have a unique answer?
§ No; there can be 1 or more answers, depending on the graph
v What DAGs have exactly 1 answer?
§ A list
v Terminology: A DAG represents a partial order, and a topological sort
produces a total order that is consistent with it

22
L20: Graph Algorithms CSE332, Summer 2021

Topological Sort: Applications


v Figuring out how to finish your degree

v Determining the order for recomputing spreadsheet cells

v Computing the order to compile files using a Makefile

v Scheduling jobs in a big data pipeline

v Often: finding an order of execution for a dependency graph

23
L20: Graph Algorithms CSE332, Summer 2021
2
1 4
TopoSort: A Naïve Algorithm 3
0

1. Label (“mark”) each vertex with its in-degree


§ Could write directly into a vertex’s field or a parallel data structure
(e.g., array)
2. While there are vertices not yet output:
§ Choose a vertex v with labeled with in-degree of 0
§ Output v and conceptually remove it from the graph
§ Foreach vertex w adjacent to v: In-
Degree
Adj
List

• Decrement the in-degree of w


0 3
1 2 3
2 4

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!

v If >1 vertex with in-degree=0, can break ties arbitrarily


§ Potentially many different correct orders!

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

TopoSort’s Runtime: Doing Better


v Avoid searching for a zero-degree node every time!
§ Keep the “pending” 0-degree nodes in a list, stack, queue, table, etc
§ The irder we process them affects output, but not correctness or
efficiency (as long as add/remove are both O(1))

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

v You’ve seen a graph traversal before in 143. List all three.

30
L20: Graph Algorithms CSE332, Summer 2021

Tree and Graph Reachability


v Find all nodes reachable from a starting node v
§ ie, there exists a path
§ Might “do something” at each visited node (an iterator!)
• “Do something” is called visiting or processing a node
– eg, print to output, set some field, etc.
• Traversing a node or iterating over a node is different!
– Just fetch adjacent/child nodes

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

Tree and Graph Traversals


v Can answer reachability with a tree traversal or graph
traversal
§ Iterates over every node in a graph in some defined ordering
§ “Processes” or “visits” its contents

v There are several types of tree traversals


§ Level Order Traversal aka Breadth-First Traversal
§ Depth-First Traversal
• Pre-order Traversal
• In-order Traversal
• Post-order Traversal

32
L20: Graph Algorithms CSE332, Summer 2021

Tree/Graph Traversal: High-level Algorithm


v High-level Algorithm:
traverseGraph(Node start) {
§ Initialization:
pending = emptyFringe()
• Create an empty data structure
pending.add(start)
(often called a “fringe”) to track
“remaining work” mark start as visited
• Mark start as visited
while (!pending.empty()) {
§ While we still have work,
next = pending.remove()
follow the nodes:
process(next) //marks visited
• Get a node
foreach u adjacent to next
• Visit/process that node
if (!u.marked)
• Update its neighbors (eg, add
mark u
to “remaining work” if it’s not
already there) pending.add(u)
}
v Memorize this 5-step
pattern!
33
L20: Graph Algorithms CSE332, Summer 2021

Tree/Graph Traversal: Running Time


v Assuming add() and remove() are O(1), traversal is O(|E|)
§ Remember: we default to using an adjacency list

34
L20: Graph Algorithms CSE332, Summer 2021

Tree/Graph Traversal: Order


v The order we process() depends entirely on how pending.add()
and pending.remove() are implemented
§ Queue:
• Tree: Level-order traversal
• Graph: Breadth-first graph search (BFS)
§ Stack:
• Tree: Depth-first search (3 flavors!)
• Graph: Depth-first graph search (DFS)

v DFS and BFS are “big ideas” in computer science


§ Depth: explore one part before exploring other unexplored parts
§ Breadth: explore parts closer to the start before exploring farther
parts

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

Graphs: Breadth-First Search


v The fringe here is a Queue!

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”

v Resembles how we converted our binary heap (ie, a complete


tree) to its array representation

38
L20: Graph Algorithms CSE332, Summer 2021

Breadth-First Search on a Graph


Queue:

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

Breadth-First Search on a Graph


Queue:
A
A
Marked:
A
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)
}
40
L20: Graph Algorithms CSE332, Summer 2021

Breadth-First Search on a Graph


Queue:
B, C, D
A
Marked:
A, B, C, D
B
Order Processed: C D
A

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

Breadth-First Search on a Graph


Queue:
C, D, E, F
A
Marked:
A, B, C, D, E, F
B
Order Processed: C D
A, B

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

Breadth-First Search on a Graph


Queue:
D, E, F, G
A
Marked:
A, B, C, D, E, F, G
B
Order Processed: C D
A, B, C

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

Breadth-First Search on a Graph


Queue:
E, F, G, H
A
Marked:
A, B, C, D, E, F, G, H
B
Order Processed: C D
A, B, 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)
}
44
L20: Graph Algorithms CSE332, Summer 2021

Breadth-First Search on a Graph


Queue:
F, G, H, J
A
Marked:
A, B, C, D, E, F, G, H, J
B
Order Processed: C D
A, B, C, D, E

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

Breadth-First Search on a Graph


Queue:
G, H, J
A
Marked:
A, B, C, D, E, F, G, H, J
B
Order Processed: C D
A, B, C, D, E, F

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

Breadth-First Search on a Graph


Queue:
H, J
A
Marked:
A, B, C, D, E, F, G, H, J
B
Order Processed: C D
A, B, C, D, E, F, G

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

Breadth-First Search on a Graph


Queue:
J, I
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

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

Breadth-First Search on a Graph


Queue:
I
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

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

Breadth-First Search on a Graph


Queue:

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

Graphs: Depth-First Search


v The fringe here is a Stack!
v Note: many algorithms that use a stack have an Iterative and a
Recursive solution…

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

Depth-First Search on a Graph


Stack:

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

Depth-First Search on a Graph


Stack:
A
A
Marked:
A
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)
}
54
L20: Graph Algorithms CSE332, Summer 2021

Depth-First Search on a Graph


Stack:
B, C, D
A
Marked:
A, B, C, D
B
Order Processed: C D
A

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

Depth-First Search on a Graph


Stack:
B, C, G, H
A
Marked:
A, B, C, D, G, H
B
Order Processed: C D
A, 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)
}
56
L20: Graph Algorithms CSE332, Summer 2021

Depth-First Search on a Graph


Stack:
B, C, G, I
A
Marked:
A, B, C, D, G, H, I
B
Order Processed: C D
A, D, H

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

Depth-First Search on a Graph


Stack:
B, C, G, J
A
Marked:
A, B, C, D, G, H, I, J
B
Order Processed: C D
A, D, H, I

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

Depth-First Search on a Graph


Stack:
B, C, G
A
Marked:
A, B, C, D, G, H, I, J
B
Order Processed: C D
A, D, H, I, J

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

Depth-First Search on a Graph


Stack:
B, C,
A
Marked:
A, B, C, D, G, H, I, J
B
Order Processed: C D
A, D, H, I, J, G

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

Depth-First Search on a Graph


Stack:
B,
A
Marked:
A, B, C, D, G, H, I, J
B
Order Processed: C D
A, D, H, I, J, G, C

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

Depth-First Search on a Graph


Stack:
E, F
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

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

Depth-First Search on a Graph


Stack:
E
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)
}
63
L20: Graph Algorithms CSE332, Summer 2021

Depth-First Search on a Graph


Stack:

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

v Were the Pre/In/Post-Order Traversals from 143 examples of BFS or


DFS?

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

Saving the Path


v These graph traversals can answer the “reachability question”:
§ “Is there a path from node x to node y?”

v But what if we want to output the actual path or its length?


§ Eg, getting driving directions vs knowing it’s possible to get there

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

Saving the Path: Example using BFS (1 of 2)


v Find the shortest path from Seattle to Austin
§ Remember marked nodes are not re-enqueued
§ Shortest paths may not be unique
Chicago
Seattle

Salt Lake City

San Francisco Austin


Dallas

68
L20: Graph Algorithms CSE332, Summer 2021

Saving the Path: Example using BFS (2 of 2)


v Find the shortest path from Seattle to Austin
§ Remember marked nodes are not re-enqueued
§ Shortest paths may not be unique
0
Chicago
Seattle 1
1
Salt Lake City
0
Chicago
1 3 Seattle 1
2 1
San Francisco Austin Salt Lake City
Dallas
1 3
2
San Francisco Austin
Dallas
69
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

It Doesn’t Have to be Either/Or


v A third approach: Iterative deepening (IDDFS):
§ Try DFS, but don’t allow recursion more than K levels deep
§ If fails to find a solution, increment K and start the entire search over

v Like BFS, finds shortest paths. Like DFS, less space

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

v TopoSort finds a total ordering in a DAG representing a partial


ordering
§ Runtime for TopoSort was dependent on graph representation and a
helper data structure!

v We can traverse both trees and graphs


§ Depth-first-style tree traversals have 3 flavors (named by when the
processing happens)
§ Breadth-first-style tree traversals are called “level-order”
§ Graphs can have “pre-” and “post-” style traversals, but ordering is
less important than in trees
72

You might also like