cs10:# problem#solving#viaobjectoriented# programming#mcasey/cs10/slides/day16.pdfcs10:#...
TRANSCRIPT
![Page 1: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/1.jpg)
CS 10: Problem solving via Object Oriented
Programming Winter 2017
Tim Pierson 260 (255) Sudikoff
Day 16 – Graph Traversals
![Page 2: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/2.jpg)
2
Agenda
1. Depth first search
2. Breadth first search
![Page 3: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/3.jpg)
3
Graph traversals are useful to answer quesRons about vertex relaRonships Some Graph traversals uses
• Uses are typically around reachability • CompuRng path from vertex u to vertex v • Given start vertex s of Graph G, compute a path with the
minimum number of edges between s and all other verRces (or report no such path exists)
• TesRng whether G is fully connected (e.g., all verRces reachable) • IdenRfying cycles in G (or reporRng no cycle exists)
• Today’s examples have no cycles (next class will consider them)
![Page 4: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/4.jpg)
4
Depth First Search (DFS) uses a stack to explore as if in a maze
A
B
C
D
F
Start
DFS basic idea • Keep going unRl you
can’t go any further, then back track
• Relies on a stack (implicit or explicit) to keep track of where you’ve been
Goal
E
H
G I
Graph structure from h_p://stackoverflow.com/quesRons/687731/breadth-‐first-‐vs-‐depth-‐first
![Page 5: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/5.jpg)
5
Some of you did Depth First Search on Problem Set 1 RegionFinder Loop over all the pixels
If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
![Page 6: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/6.jpg)
Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
6
Some of you did Depth First Search on Problem Set 1 RegionFinder
If you added to end of list…
![Page 7: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/7.jpg)
Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
7
Some of you did Depth First Search on Problem Set 1 RegionFinder
And if you get pixel from end of list, you implemented a stack
If you added to end of list…
![Page 8: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/8.jpg)
8
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
![Page 9: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/9.jpg)
9
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
A
Push(A)
![Page 10: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/10.jpg)
10
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
Pop(A), mark visited
1
![Page 11: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/11.jpg)
11
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C B
Push unvisited adjacent
1
![Page 12: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/12.jpg)
12
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Pop(B), mark visited
2
1
![Page 13: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/13.jpg)
13
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Push unvisited adjacent (F, but not A)
2
F
1
![Page 14: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/14.jpg)
14
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Pop(F), mark visited
2
3
1
![Page 15: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/15.jpg)
15
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Push unvisited adjacent (H, but not B)
2
3
1
H
![Page 16: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/16.jpg)
16
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Pop(H), mark visited
2
3
1
4
![Page 17: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/17.jpg)
17
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D C
Nothing to push, back up by popping C
2
3
1
4
![Page 18: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/18.jpg)
18
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D
Pop(C), mark visited
2
3
1
4
5
![Page 19: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/19.jpg)
19
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E D
Nothing to push, back up by popping D
2
3
1
4
5
![Page 20: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/20.jpg)
20
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Pop(D), mark visited
2
3
1
4
5
6
![Page 21: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/21.jpg)
21
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Push unvisited adjacent (G, but not A)
2
3
1
4
5
6
G
![Page 22: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/22.jpg)
22
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Pop(G), mark visited
2
3
1
4
5
6 7
![Page 23: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/23.jpg)
23
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Push unvisited adjacent (I, but not D)
2
3
1
4
5
6 7
I
![Page 24: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/24.jpg)
24
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Pop(I), mark visited
2
3
1
4
5
6 7 8
![Page 25: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/25.jpg)
25
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
E
Nothing to push, back up by popping E
2
3
1
4
5
6 7 8
![Page 26: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/26.jpg)
26
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
Pop(E), mark visited
2
3
1
4
5
6 7 8
9
![Page 27: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/27.jpg)
27
Depth First Search (DFS) is like exploring a maze
DFS algorithm stack.push(s) //start node repeat until find goal vertex or stack empty:
u = stack.pop() if !u.visited u.visited = true (maybe do something while here) for v ∈ u.adjacent
if !v.visited stack.push(v)
A
B
C
D
F
Start
Goal
E
H
G I
Stack
Found goal
2
3
1
4
5
6 7 8
9
![Page 28: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/28.jpg)
28
Node discovery tells us something about the graph Discovery edges • Edges that lead to unvisited nodes • Discovery edges form a tree on the graph • Can traverse from start to goal on tree (if goal reachable) • Can tell us which nodes are not reachable (not on path formed by discovery nodes)
• Not guaranteed to be shortest path! Back edges • Edges that lead to previously discovered nodes • Lead to ancestor nodes in tree • Indicate presence of a cycle in the graph
![Page 29: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/29.jpg)
29
Run Rme is O(n+m) Run >me • Assume graph with n nodes and m edges • Visit each node at most one Rme (due to visited indicator)
• Visit each edge at most one Rme
• Run Rme is O(n+m)
![Page 30: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/30.jpg)
30
Agenda
1. Depth first search
2. Breadth first search
![Page 31: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/31.jpg)
31
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
BFS basic idea • Explore outward in
“ripples”
• Look at all nodes 1 step away, then all nodes 2 steps away…
• Relies on a queue (implicit or explicit) implementaRon
• Path found from s to any other vertex is shortest Goal
E
H
G I
![Page 32: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/32.jpg)
32
Some of you did Breadth First Search on Problem Set 1 RegionFinder Loop over all the pixels
If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
![Page 33: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/33.jpg)
Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
33
Some of you did Breadth First Search on Problem Set 1 RegionFinder
If you added to end of list…
![Page 34: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/34.jpg)
Loop over all the pixels If a pixel is unvisited and of the correct color Start a new region Keep track of pixels need to be visited, initially just one As long as there's some pixel that needs to be visited Get one to visit Add it to the region Mark it as visited Loop over all its neighbors If the neighbor is of the correct color Add it to the list of pixels to be visited If the region is big enough to be worth keeping, do so
34
Some of you did Breadth First Search on Problem Set 1 RegionFinder
And if you get pixel from front of list, you implemented a queue
If you added to end of list…
![Page 35: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/35.jpg)
35
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
![Page 36: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/36.jpg)
36
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
enqueue(A)
![Page 37: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/37.jpg)
37
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
enqueue(A)
A
1
![Page 38: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/38.jpg)
38
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(A), unvisited enqueue adjacent
B C D E
1
![Page 39: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/39.jpg)
39
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(B), enqueue unvisited adjacent F
C D E F
2
1
![Page 40: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/40.jpg)
40
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(C), enqueue unvisited adjacent (none)
D E F
2
1
3
![Page 41: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/41.jpg)
41
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(D), enqueue unvisited adjacent G
E F G
2
1
3
4
![Page 42: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/42.jpg)
42
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(E), enqueue unvisited adjacent
F G
2
1
3
4
5
![Page 43: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/43.jpg)
43
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
Found goal!
F G
2
1
3
4
5
![Page 44: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/44.jpg)
44
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
Keep going for fun
F G
2
1
3
4
5
![Page 45: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/45.jpg)
45
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(F), enqueue unvisited adjacent H
G H
2
1
3
4
5
6
![Page 46: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/46.jpg)
46
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(G), enqueue unvisited adjacent I
H I
2
1
3
4
5
6
7
![Page 47: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/47.jpg)
47
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(H), enqueue unvisited adjacent (none)
I
2
1
3
4
5
6
7
8
![Page 48: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/48.jpg)
48
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
dequeue(I), enqueue unvisited adjacent (none)
2
1
3
4
5
6
7
8
9
![Page 49: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/49.jpg)
49
Breadth First Search (BFS) can find the shortest path between nodes
A
B
C
D
F
Start
Goal
E
H
G I
BFS algorithm enqueue(s) //start node s.visited = true repeat until find goal vertex or queue empty:
u = dequeque() for v ∈ u.adjacent if !v.visited v.visited = true enqueue(v)
Queue
All nodes explored
2
1
3
4
5
6
7
8
9
![Page 50: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/50.jpg)
50
Node discovery tells us something about the graph Discovery edges • Lead to unvisited nodes • Form a tree on the graph • Can traverse from start to goal (or any node) • Can tell us which nodes are not reachable (not on path formed by discovery nodes)
• Path guaranteed to have smallest number of edges Can track how we got to node to find shortest path • Build vertex tree • Parent of each vertex is vertex that discovered it • Parent is unique because we don’t visit verRces twice
![Page 51: CS10:# Problem#solving#viaObjectOriented# Programming#mcasey/cs10/slides/Day16.pdfCS10:# Problem#solving#viaObjectOriented# Programming# Winter#2017# # TimPierson 260(255) Sudikoff#](https://reader035.vdocuments.net/reader035/viewer/2022070220/6132e905dfd10f4dd73ac005/html5/thumbnails/51.jpg)
51
Run Rme is O(n+m) Run >me • Assume graph with n nodes and m edges • Visit each node at most one Rme (due to visited indicator
• Visit each edge at most one Rme
• Run Rme O(n+m)
• Useful for the Kevin Bacon game!