breadth-first and depth-first traversal prof. noah snavely cs1114

65
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114 http://cs1114.cs.cornell.edu

Upload: pamela-mason

Post on 18-Jan-2018

227 views

Category:

Documents


0 download

DESCRIPTION

Final notes on Big-O Notation  If algorithm A is O(n 2 ) 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

TRANSCRIPT

Page 1: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114

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

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

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

4

Final notes on Big-O Notation

A

B

B

A

Page 5: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

BFS vs. DFS

50

(tree = graph with no cycles)

Page 51: Breadth-first and depth-first traversal Prof. Noah Snavely CS1114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Implementing a queue: Take 2 IsEmpty

Enqueue (add an element)

Dequeue (remove an element)

65