Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
-
Upload
pamela-mason -
Category
Documents
-
view
228 -
download
0
description
Transcript of Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
![Page 1: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/1.jpg)
Breadth-first and depth-first traversal
Prof. Noah SnavelyCS1114http://cs1114.cs.cornell.edu
![Page 2: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/2.jpg)
Administrivia Assignment 2, Part 2, due Friday
– Please sign up for a Friday slot Assignment 3 will be out Friday
Survey:– Should we move lecture closer to the lab?
Prelim 1! Next Thursday, 2/26, in class– There will be a review session TBA
2
![Page 3: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/3.jpg)
Final notes on Big-O Notation If algorithm A is O(n2) and algorithm B is
O(n), we know that:
– For large n, A will eventually run much slower than B
– For small n, we know very little:• A could be slower• B could be slower• They could have similar runtimes• Or difference could be very large
3
![Page 4: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/4.jpg)
4
Final notes on Big-O Notation
A
B
B
A
![Page 5: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/5.jpg)
5
Finding blobs1 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 0 0 0 0 0 0 0
![Page 6: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/6.jpg)
6
Finding blobs1 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 1 1 1 0 0 0 00 0 0 0 0 0 0 0 0 0
Blobs are connected components!
![Page 7: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/7.jpg)
7
Finding components
1. Pick a 1 to start with, where you don’t know which component it is in– When there aren’t any, you’re done
2. Give it a new component color3. Assign the same component color to each
pixel that is part of the same component– Basic strategy: color any neighboring 1’s, have
them color their neighbors, and so on
![Page 8: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/8.jpg)
8
For each vertex we visit, we color its neighbors and remember that we need to visit them at some point– Need to keep track of the vertices we still need
to visit in a todo list– After we visit a vertex, we’ll pick one of the
vertices in the todo list to visit next
This is also called graph traversal
Finding components
![Page 9: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/9.jpg)
Stacks and queues Two ways of representing a “todo list”
Stack: Last In First Out (LIFO)– (Think cafeteria trays)– The newest task is the one you’ll do next
Queue: First In First Out (FIFO)– (Think a line of people at the cafeteria)– The oldest task is the one you’ll do next
9
![Page 10: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/10.jpg)
Stacks Two operations:
Push: add something to the top of the stack
Pop: remove the thing on top of the stack
10
![Page 11: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/11.jpg)
Queue
Two operations: Enqueue: add something to the end of the
queue Dequeue: remove something from the
front of the queue
11
![Page 12: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/12.jpg)
Graph traversal
Suppose you’re in a maze
What strategy can you use to find the exit?
12
![Page 13: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/13.jpg)
Graph traversal
13
Paris
Berlin
London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Oslo Stockholm
![Page 14: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/14.jpg)
14
Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Current node: LondonTodo list: [ ]
Graph traversal (stack)
Oslo Stockholm
![Page 15: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/15.jpg)
15
Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (stack)
Current node: LondonTodo list: [ Paris ]
Oslo Stockholm
![Page 16: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/16.jpg)
16
2Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (stack)
Current node: ParisTodo list: [ ]
Oslo Stockholm
![Page 17: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/17.jpg)
17
2Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (stack)
Current node: ParisTodo list: [ Frankfurt, Berlin, Rome ]
Oslo Stockholm
![Page 18: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/18.jpg)
18
2Paris
Berlin
1London
3Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (stack)
Current node: RomeTodo list: [ Frankfurt, Berlin ]
Oslo Stockholm
![Page 19: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/19.jpg)
19
2Paris
Berlin
1London
3Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (stack)
Current node: RomeTodo list: [ Frankfurt, Berlin, Naples ]
Oslo Stockholm
![Page 20: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/20.jpg)
20
2Paris
Berlin
1London
3Rome
Frankfurt
Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: NaplesTodo list: [ Frankfurt, Berlin ]
Oslo Stockholm
![Page 21: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/21.jpg)
21
2Paris
5Berlin
1London
3Rome
Frankfurt
Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: BerlinTodo list: [ Frankfurt ]
Oslo Stockholm
![Page 22: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/22.jpg)
22
2Paris
5Berlin
1London
3Rome
Frankfurt
Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: BerlinTodo list: [ Frankfurt, Hamburg, Vienna ]
Oslo Stockholm
![Page 23: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/23.jpg)
23
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: ViennaTodo list: [ Frankfurt, Hamburg ]
Oslo Stockholm
![Page 24: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/24.jpg)
24
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: ViennaTodo list: [ Frankfurt, Hamburg, Prague, Warsaw ]
Oslo Stockholm
![Page 25: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/25.jpg)
25
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna Prague
4Naples
Warsaw
Hamburg
Graph traversal (stack)
Current node: ViennaTodo list: [ Frankfurt, Hamburg, Prague, Warsaw ]
Oslo Stockholm
![Page 26: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/26.jpg)
26
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna Prague
4Naples 7
Warsaw
Hamburg
Graph traversal (stack)
Current node: Warsaw Todo list: [ Frankfurt, Hamburg, Prague ]
Oslo Stockholm
![Page 27: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/27.jpg)
27
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna
8Prague
4Naples 7
Warsaw
Hamburg
Graph traversal (stack)
Current node: PragueTodo list: [ Frankfurt, Hamburg ]
Oslo Stockholm
![Page 28: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/28.jpg)
28
2Paris
5Berlin
1London
3Rome
Frankfurt
6Vienna
8Prague
4Naples 7
Warsaw
9 Hamburg
Graph traversal (stack)
Current node: HamburgTodo list: [ Frankfurt ]
Oslo Stockholm
![Page 29: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/29.jpg)
29
2Paris
5Berlin
1London
3Rome
10Frankfurt
6Vienna
8Prague
4Naples 7
Warsaw
9 Hamburg
Graph traversal (stack)
Current node: FrankfurtTodo list: [ ]
Oslo Stockholm
![Page 30: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/30.jpg)
Depth-first search (DFS)
Call the starting node the root We traverse paths all the way until we get
to a dead-end, then backtrack (until we find an unexplored path)
30
2
5
1
3
10
6 8
47
9
![Page 31: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/31.jpg)
Another strategy
31
1. Explore all the cities that are one hop away from the root
2. Explore all cities that are two hops away from the root
3. Explore all cities that are three hops away from the root
…
This corresponds to using a queue
![Page 32: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/32.jpg)
32
Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Current node: LondonTodo list: [ ]
Graph traversal (queue)
Oslo Stockholm
![Page 33: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/33.jpg)
33
Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: LondonTodo list: [ Paris ]
Oslo Stockholm
![Page 34: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/34.jpg)
34
2Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: ParisTodo list: [ ]
Oslo Stockholm
![Page 35: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/35.jpg)
35
2Paris
Berlin
1London
Rome
Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: ParisTodo list: [ Frankfurt, Berlin, Rome ]
Oslo Stockholm
![Page 36: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/36.jpg)
36
2Paris
Berlin
1London
Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: FrankfurtTodo list: [ Berlin, Rome ]
Oslo Stockholm
![Page 37: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/37.jpg)
37
2Paris
Berlin
1London
Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: FrankfurtTodo list: [ Berlin, Rome, Hamburg ]
Oslo Stockholm
![Page 38: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/38.jpg)
38
2Paris
4Berlin
1London
Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: BerlinTodo list: [ Rome, Hamburg ]
Oslo Stockholm
![Page 39: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/39.jpg)
39
2Paris
4Berlin
1London
Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: BerlinTodo list: [ Rome, Hamburg, Vienna ]
Oslo Stockholm
![Page 40: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/40.jpg)
40
2Paris
4Berlin
1London
5Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: RomeTodo list: [ Hamburg, Vienna ]
Oslo Stockholm
![Page 41: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/41.jpg)
41
2Paris
4Berlin
1London
5Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
Hamburg
Graph traversal (queue)
Current node: RomeTodo list: [ Hamburg, Vienna, Naples ]
Oslo Stockholm
![Page 42: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/42.jpg)
42
2Paris
4Berlin
1London
5Rome
3Frankfurt
Vienna Prague
NaplesWarsaw
6 Hamburg
Graph traversal (queue)
Current node: HamburgTodo list: [ Vienna, Naples ]
Oslo Stockholm
![Page 43: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/43.jpg)
43
2Paris
4Berlin
1London
5Rome
3Frankfurt
7Vienna Prague
NaplesWarsaw
6 Hamburg
Graph traversal (queue)
Current node: ViennaTodo list: [ Naples ]
Oslo Stockholm
![Page 44: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/44.jpg)
44
2Paris
4Berlin
1London
5Rome
3Frankfurt
7Vienna Prague
NaplesWarsaw
6 Hamburg
Graph traversal (queue)
Current node: ViennaTodo list: [ Naples, Prague, Warsaw ]
Oslo Stockholm
![Page 45: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/45.jpg)
45
2Paris
4Berlin
1London
5Rome
3Frankfurt
7Vienna Prague
8Naples
Warsaw
6 Hamburg
Graph traversal (queue)
Current node: NaplesTodo list: [ Prague, Warsaw ]
Oslo Stockholm
![Page 46: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/46.jpg)
46
2Paris
4Berlin
1London
5Rome
3Frankfurt
7Vienna
9Prague
8Naples
Warsaw
6 Hamburg
Graph traversal (queue)
Current node: PragueTodo list: [ Warsaw ]
Oslo Stockholm
![Page 47: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/47.jpg)
47
2Paris
4Berlin
1London
5Rome
3Frankfurt
7Vienna
9Prague
8Naples 10
Warsaw
6 Hamburg
Graph traversal (queue)
Current node: WarsawTodo list: [ ]
Oslo Stockholm
![Page 48: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/48.jpg)
Breadth-first search (BFS)
We visit all the vertices at the same level (same distance to the root) before moving on to the next level
48
2
4
1
5
3
7 9
810
6
![Page 49: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/49.jpg)
BFS vs. DFS
49
2
5
1
3
10
6 8
47
92
4
1
5
3
7 9
810
6
Breadth-first (queue) Depth-first (stack)
![Page 50: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/50.jpg)
BFS vs. DFS
50
(tree = graph with no cycles)
![Page 51: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/51.jpg)
Basic algorithms
BREADTH-FIRST SEARCH (Graph G) While there is an uncolored node r
– Choose a new color– Create an empty queue Q– Let r be the root node, color it, and add it to Q– While Q is not empty
• Dequeue a node v from Q• For each of v’s neighbors u
If u is not colored, color it and add it to Q
51
![Page 52: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/52.jpg)
Basic algorithms
DEPTH-FIRST SEARCH (Graph G) While there is an uncolored node r
– Choose a new color– Create an empty stack S– Let r be the root node, color it, and push it on S– While S is not empty
• Pop a node v from S• For each of v’s neighbors u
If u is not colored, color it and push it onto S
52
![Page 53: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/53.jpg)
Queues and Stacks Examples of Abstract Data Types (ADTs) ADTs fulfill a contract:
– The contract tells you what the ADT can do, and what the behavior is
– For instance, with a stack:• We can push and pop• If we push X onto S and then pop S, we get back
X, and S is as before
Doesn’t tell you how it fulfills the contract
53
![Page 54: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/54.jpg)
Implementing DFS
How can we implement a stack?– Needs to support several operations:– Push (add an element to the top)– Pop (remove the element from the top)– IsEmpty
54
256
42
17
![Page 55: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/55.jpg)
Implementing a stack IsEmpty
function e = IsEmpty(S) e = (length(S) == 0);
Push (add an element to the top) function S = push(S, x) S = [ S x ]
Pop (remove an element from the top)function [S, x] = pop(S) n = length(S); x = S(n); S = S(1:n-1); % but what happens if n = 0?
55
![Page 56: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/56.jpg)
Implementing BFS
How can we implement a queue?– Needs to support several operations:– Enqueue (add an element to back)– Dequeue (remove an element from front)– IsEmpty
Not quite as easy as a stack…
56
256
42
17
![Page 57: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/57.jpg)
Efficiency
Ideally, all of the operations (push, pop, enqueue, dequeue, IsEmpty) run in constant (O(1)) time
To figure out running time, we need a model of how the computer’s memory works
57
![Page 58: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/58.jpg)
58
Computers and arrays
Computer memory is a large array – We will call it M
In constant time, a computer can:– Read any element of M (random access)– Change any element of M to another element– Perform any simple arithmetic operation
This is more or less what the hardware manual for an x86 describes
![Page 59: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/59.jpg)
Computers and arrays Arrays in Matlab are consecutive
subsequences of M
59
M …A = zeros(8)
![Page 60: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/60.jpg)
Memory manipulation How long does it take to:
– Read A(8)?
– Set A(7) = A(8)?
– Copy all the elements of an array (of size n) A to a new part of M?
– Shift all the elements of A one cell to the left?
60
![Page 61: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/61.jpg)
Implementing a queue: Take 1 First approach: use an array Add (enqueue) new elements to the end of
the array When removing an element (dequeue),
shift the entire array left one unit
61
Q = [];
![Page 62: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/62.jpg)
Implementing a queue: Take 1 IsEmpty
function e = IsEmpty(Q) e = (length(S) == 0);
Enqueue (add an element)function Q = enqueue(Q,x) Q = [ Q x ];
Dequeue (remove an element)function [Q, x] = dequeue(Q) n = length(Q); x = Q(1); for i = 1:n-1 Q(i) = Q(i+1); % everyone steps forward one step
62
![Page 63: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/63.jpg)
What is the running time? IsEmpty
Enqueue (add an element)
Dequeue (remove an element)
63
![Page 64: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/64.jpg)
Implementing a queue: Take 2 Second approach: use an array AND Keep two pointers for the front and back of the queue
Add new elements to the back of the array Take old elements off the front of the array
64
Q = zeros(1000000);front = 1; back = 1;
front back
![Page 65: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114](https://reader036.fdocuments.net/reader036/viewer/2022062317/5a4d1b467f8b9ab0599a3743/html5/thumbnails/65.jpg)
Implementing a queue: Take 2 IsEmpty
Enqueue (add an element)
Dequeue (remove an element)
65