Breadth-first search

77
Breadth-first search CSC263 Tutorial 8

description

Breadth-first search. CSC263 Tutorial 8. BFS algorithm. BFS(G, start) Create new queue Q Q.push (start) dist [1..n] = {∞, …, ∞} dist [start] = 0 while Q is not empty u = Q.pop () for each node v adjacent to u if dist [v] = ∞ then - PowerPoint PPT Presentation

Transcript of Breadth-first search

Page 1: Breadth-first search

Breadth-first search

CSC263 Tutorial 8

Page 2: Breadth-first search

BFS algorithm

BFS(G, start) Create new queue Q Q.push(start) dist[1..n] = {∞, …, ∞} dist[start] = 0

while Q is not empty u = Q.pop() for each node v adjacent to u if dist[v] = ∞ then dist[v] = dist[u] + 1 Q.push(v)

Suppose we have a graphG = (V,E) containing|V| = n nodes and

|E| = m edges.

BFS takes O(n+m) time and space.

Page 3: Breadth-first search

Example: BFS starting at node a

• All weights initially set to infinity.

a

b

d

c

g

e

f

i

h

a

b

d

c

g

e

f

i

h

0

1

1

2

3

4

4

4

5

Page 4: Breadth-first search

Application 1: checking connectedness

• Run BFS on any node• If no node has distance infinity, it’s connected!

a

b

d

c

g

e

f

i

h

a

b

d

c

g

e

f

i

h

0

1

1

2

3

4

4

4

5

Page 5: Breadth-first search

Application 1: checking connectedness

• Run BFS on any node• If a node has distance infinity, disconnected!

a

b

d

c

g

e

f

i

h

a

b

d

c

g

e

f

h

0

1

1

2

3

4

4

5

Distance is infinity!

Page 6: Breadth-first search

Application 2:finding connected components

Algorithm:initially label each node 0c := 1for each node u = 1..n do if u is still labeled 0 then do a BFS starting at u give the label c to each node reached by the BFS c := c+1 end ifend for

Page 7: Breadth-first search

Application 2:finding connected components

• Initially, each node has label 0

a

b

d

c

g

e

f

i

h

a

b

d

c

g

e

f

i

h

1

1

1

2

2

3

2

4

4

Page 8: Breadth-first search

BFS algorithm with coloursBFS(G, start) Create new queue Q Q.push(start) dist[1..n] = {∞, …, ∞} dist[start] = 0 colour[1..n] = {white, …, white}

while Q is not empty u = Q.pop() colour[u] = black for each node v adjacent to u if colour[v] = white then Q.push(v) dist[v] = dist[u] + 1 colour[v] = gray

Suppose we have a graphG = (V,E) containing|V| = n nodes and

|E| = m edges.

BFS takes O(n+m) time and space.

Page 9: Breadth-first search

Application 3: finding cycles

• Repeatedly do BFS from any unvisited node, until all nodes are visited.

• In any of these BFSs, if we see an edge that points to a gray node, then there is a cycle!

Page 10: Breadth-first search

Application 3: finding cycles

a

b

d

c

g

e

f

i

h

a

b

0

1

1

Can also use DFS(next week)

Enqueue node b

Enqueue node d

Try to enqueue a;black, so do nothing

Dequeue node a

Dequeue node b

Enqueue node a

a d

b

Try to enqueue d;gray node, so cycle!

Page 11: Breadth-first search

Application 4: computing distance

• Suppose we have an m x n grid of farms.• Initially, one of the farms is on fire!• At each hour, the fire spreads from each

burning farm to each farm (going up, down, left and right).

• How long before all farms are on fire?

Page 12: Breadth-first search

Application 4: computing distance

• How can we represent this problem as a graph problem?– Nodes are farms.– There is an edge between two farms if the farms

are adjacent (next to one another).

Page 13: Breadth-first search

Application 4: computing distance

• How should we store this graph in memory?• One possibility (for a 3x3 grid of farms):

Wastes lots of memory!

Page 14: Breadth-first search

Application 4: computing distance

• How should we store this graph in memory?• A more memory efficient way:– Store any data associated with the farms in a 3x3

array (one element for each farm)

– Two farms are adjacent if their array elements are adjacent in the 3x3 array.

Page 15: Breadth-first search

Application 4: computing distance(for a 15 x 10 grid of farms)

Solution: run BFS starting from the fire to compute the “distance” (actually time) to each farm.

Page 16: Breadth-first search

Application 4: computing distance

11 1

1

Page 17: Breadth-first search

Application 4: computing distance

22 1 2

2 1 1 22 1 2

2

Page 18: Breadth-first search

Application 4: computing distance

3 2 33 2 1 2 3

3 2 1 1 2 33 2 1 2 3

3 2 33

Page 19: Breadth-first search

Application 4: computing distance

4 3 2 3 44 3 2 1 2 3 43 2 1 1 2 3 44 3 2 1 2 3 4

4 3 2 3 44 3 4

4

Page 20: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 54 3 2 1 2 3 4 53 2 1 1 2 3 4 54 3 2 1 2 3 4 55 4 3 2 3 4 5

5 4 3 4 55 4 5

5

Page 21: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 64 3 2 1 2 3 4 5 63 2 1 1 2 3 4 5 64 3 2 1 2 3 4 5 65 4 3 2 3 4 5 66 5 4 3 4 5 6

6 5 4 5 66 5 6

6

Page 22: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 74 3 2 1 2 3 4 5 6 73 2 1 1 2 3 4 5 6 74 3 2 1 2 3 4 5 6 75 4 3 2 3 4 5 6 76 5 4 3 4 5 6 77 6 5 4 5 6 7

7 6 5 6 77 6 7

7

Page 23: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 84 3 2 1 2 3 4 5 6 7 83 2 1 1 2 3 4 5 6 7 84 3 2 1 2 3 4 5 6 7 85 4 3 2 3 4 5 6 7 86 5 4 3 4 5 6 7 87 6 5 4 5 6 7 88 7 6 5 6 7 8

8 7 6 7 88 7 8

Page 24: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 94 3 2 1 2 3 4 5 6 7 8 93 2 1 1 2 3 4 5 6 7 8 94 3 2 1 2 3 4 5 6 7 8 95 4 3 2 3 4 5 6 7 8 96 5 4 3 4 5 6 7 8 97 6 5 4 5 6 7 8 98 7 6 5 6 7 8 99 8 7 6 7 8 9

9 8 7 8 9

Page 25: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10

4 3 2 1 2 3 4 5 6 7 8 9 10

3 2 1 1 2 3 4 5 6 7 8 9 10

4 3 2 1 2 3 4 5 6 7 8 9 10

5 4 3 2 3 4 5 6 7 8 9 10

6 5 4 3 4 5 6 7 8 9 10

7 6 5 4 5 6 7 8 9 10

8 7 6 5 6 7 8 9 10

9 8 7 6 7 8 9 10

10 9 8 7 8 9 10

Page 26: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11

5 4 3 2 3 4 5 6 7 8 9 10 11

6 5 4 3 4 5 6 7 8 9 10 11

7 6 5 4 5 6 7 8 9 10 11

8 7 6 5 6 7 8 9 10 11

9 8 7 6 7 8 9 10 11

10 9 8 7 8 9 10 11

Page 27: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12

6 5 4 3 4 5 6 7 8 9 10 11 12

7 6 5 4 5 6 7 8 9 10 11 12

8 7 6 5 6 7 8 9 10 11 12

9 8 7 6 7 8 9 10 11 12

10 9 8 7 8 9 10 11 12

Page 28: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13

7 6 5 4 5 6 7 8 9 10 11 12 13

8 7 6 5 6 7 8 9 10 11 12 13

9 8 7 6 7 8 9 10 11 12 13

10 9 8 7 8 9 10 11 12 13

Page 29: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13 14

7 6 5 4 5 6 7 8 9 10 11 12 13 14

8 7 6 5 6 7 8 9 10 11 12 13 14

9 8 7 6 7 8 9 10 11 12 13 14

10 9 8 7 8 9 10 11 12 13 14

Page 30: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13 14

7 6 5 4 5 6 7 8 9 10 11 12 13 14 15

8 7 6 5 6 7 8 9 10 11 12 13 14 15

9 8 7 6 7 8 9 10 11 12 13 14 15

10 9 8 7 8 9 10 11 12 13 14 15

Page 31: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13 14

7 6 5 4 5 6 7 8 9 10 11 12 13 14 15

8 7 6 5 6 7 8 9 10 11 12 13 14 15 16

9 8 7 6 7 8 9 10 11 12 13 14 15 16

10 9 8 7 8 9 10 11 12 13 14 15 16

Page 32: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13 14

7 6 5 4 5 6 7 8 9 10 11 12 13 14 15

8 7 6 5 6 7 8 9 10 11 12 13 14 15 16

9 8 7 6 7 8 9 10 11 12 13 14 15 16 17

10 9 8 7 8 9 10 11 12 13 14 15 16 17

Page 33: Breadth-first search

Application 4: computing distance

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

3 2 1 1 2 3 4 5 6 7 8 9 10 11

4 3 2 1 2 3 4 5 6 7 8 9 10 11 12

5 4 3 2 3 4 5 6 7 8 9 10 11 12 13

6 5 4 3 4 5 6 7 8 9 10 11 12 13 14

7 6 5 4 5 6 7 8 9 10 11 12 13 14 15

8 7 6 5 6 7 8 9 10 11 12 13 14 15 16

9 8 7 6 7 8 9 10 11 12 13 14 15 16 17

10 9 8 7 8 9 10 11 12 13 14 15 16 17 18

18 hours until all farms are on fire!

Page 34: Breadth-first search

Application 4: modification 1

• What if there are lakes in the grid, where fire cannot pass? Just don’t let BFS visit the lakes!

Page 35: Breadth-first search

Application 4: modification 1

11 1

1

• What if there are lakes in the grid, where fire cannot pass?

Page 36: Breadth-first search

Application 4: modification 1

22 1 21 12 1 2

2

• What if there are lakes in the grid, where fire cannot pass?

Page 37: Breadth-first search

Application 4: modification 1

3 2 32 1 21 1

3 2 1 23 2 3

• What if there are lakes in the grid, where fire cannot pass?

Page 38: Breadth-first search

Application 4: modification 1

3 2 3 42 1 21 1

3 2 1 24 3 2 3 4

• What if there are lakes in the grid, where fire cannot pass?

Page 39: Breadth-first search

Application 4: modification 1

3 2 3 4 52 1 21 1

3 2 1 25 4 3 2 3 4 5

5

• What if there are lakes in the grid, where fire cannot pass?

Page 40: Breadth-first search

Application 4: modification 1

3 2 3 4 5 62 1 2 61 1

3 2 1 2 65 4 3 2 3 4 5 66 5 6

6

• What if there are lakes in the grid, where fire cannot pass?

Page 41: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 72 1 2 6 71 1

3 2 1 2 6 75 4 3 2 3 4 5 6 76 5 6 77 6 7

7

• What if there are lakes in the grid, where fire cannot pass?

Page 42: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 82 1 2 6 7 81 1 8

3 2 1 2 6 7 85 4 3 2 3 4 5 6 7 86 5 6 7 87 6 7 88 7 8 8

8

• What if there are lakes in the grid, where fire cannot pass?

Page 43: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 92 1 2 6 7 8 91 1 8 9

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 99 8 9 9

9

• What if there are lakes in the grid, where fire cannot pass?

Page 44: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10

2 1 2 6 7 8 9 10

1 1 8 9 10

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 99 8 9 10 10 9 10

10 9 10 10

• What if there are lakes in the grid, where fire cannot pass?

Page 45: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11

2 1 2 6 7 8 9 10 11

1 1 8 9 10

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 99 8 9 10 11 10 9 10 11

10 9 10 11 11 10 11

• What if there are lakes in the grid, where fire cannot pass?

Page 46: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12

1 1 8 9 10

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 99 8 9 10 11 10 9 10 11 12

10 9 10 11 12 11 10 11 12

• What if there are lakes in the grid, where fire cannot pass?

Page 47: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13

1 1 8 9 10

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 99 8 9 10 11 10 9 10 11 12 13

10 9 10 11 12 11 10 11 12 13

• What if there are lakes in the grid, where fire cannot pass?

Page 48: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14

3 2 1 2 6 7 8 95 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 9 14

9 8 9 10 11 10 9 10 11 12 13 14

10 9 10 11 12 11 10 11 12 13 14

• What if there are lakes in the grid, where fire cannot pass?

Page 49: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14 15

3 2 1 2 6 7 8 9 15

5 4 3 2 3 4 5 6 7 86 5 6 7 8 97 6 7 8 98 7 8 9 8 9 14 15

9 8 9 10 11 10 9 10 11 12 13 14 15

10 9 10 11 12 11 10 11 12 13 14 15

• What if there are lakes in the grid, where fire cannot pass?

Page 50: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14 15

3 2 1 2 6 7 8 9 16 15 16

5 4 3 2 3 4 5 6 7 8 16

6 5 6 7 8 97 6 7 8 9 16

8 7 8 9 8 9 14 15 16

9 8 9 10 11 10 9 10 11 12 13 14 15

10 9 10 11 12 11 10 11 12 13 14 15

• What if there are lakes in the grid, where fire cannot pass?

Page 51: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14 15

3 2 1 2 6 7 8 9 16 15 16

5 4 3 2 3 4 5 6 7 8 17 16 17

6 5 6 7 8 9 17

7 6 7 8 9 16 17

8 7 8 9 8 9 14 15 16 17

9 8 9 10 11 10 9 10 11 12 13 14 15

10 9 10 11 12 11 10 11 12 13 14 15

• What if there are lakes in the grid, where fire cannot pass?

Page 52: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14 15

3 2 1 2 6 7 8 9 16 15 16

5 4 3 2 3 4 5 6 7 8 17 16 17

6 5 6 7 8 9 18 17 18

7 6 7 8 9 16 17 18

8 7 8 9 8 9 14 15 16 17 18

9 8 9 10 11 10 9 10 11 12 13 14 15

10 9 10 11 12 11 10 11 12 13 14 15

• What if there are lakes in the grid, where fire cannot pass?

Page 53: Breadth-first search

Application 4: modification 1

3 2 3 4 5 6 7 8 9 10 11 12

2 1 2 6 7 8 9 10 11 12 13 14

1 1 8 9 10 14 15

3 2 1 2 6 7 8 9 16 15 16

5 4 3 2 3 4 5 6 7 8 17 16 17

6 5 6 7 8 9 18 17 18

7 6 7 8 9 16 17 18 19

8 7 8 9 8 9 14 15 16 17 18

9 8 9 10 11 10 9 10 11 12 13 14 15

10 9 10 11 12 11 10 11 12 13 14 15

• What if there are lakes in the grid, where fire cannot pass?

19 hours until all farms are on fire!

Page 54: Breadth-first search

Application 4: modification 1

• Will the fire always burn everything?

Page 55: Breadth-first search

Application 4: modification 1

11 1

1

• Will the fire always burn everything?

Page 56: Breadth-first search

Application 4: modification 1

2 1 21 12 1 2

2

• Will the fire always burn everything?

Page 57: Breadth-first search

Application 4: modification 1

2 1 21 1

3 2 1 23 2 3

• Will the fire always burn everything?

Page 58: Breadth-first search

Application 4: modification 1

2 1 21 1

3 2 1 24 3 2 3 4

• Will the fire always burn everything?

Page 59: Breadth-first search

Application 4: modification 1

2 1 21 1

3 2 1 25 4 3 2 3 4

5

• Will the fire always burn everything?

Page 60: Breadth-first search

Application 4: modification 1

2 1 21 1

3 2 1 25 4 3 2 3 46 5

6

• Will the fire always burn everything?

Page 61: Breadth-first search

Application 4: modification 1

2 1 21 1

3 2 1 25 4 3 2 3 46 57 6

• Will the fire always burn everything?

Page 62: Breadth-first search

Application 4: modification 1

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞2 1 2 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞1 1 ∞ ∞ ∞ ∞ ∞

3 2 1 2 ∞ ∞ ∞ ∞ ∞ ∞ ∞5 4 3 2 3 4 ∞ ∞ ∞ ∞ ∞ ∞6 5 ∞ ∞ ∞ ∞ ∞ ∞ ∞7 6 ∞ ∞ ∞ ∞ ∞ ∞ ∞

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

• Will the fire always burn everything?

All of these farms are safe!

Page 63: Breadth-first search

Application 4: modification 2

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 64: Breadth-first search

Application 4: modification 2

11 1 1

1 1 11

11 1

1

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 65: Breadth-first search

Application 4: modification 2

2 1 22 1 2 2 1 1 21 1 2 1 22 1 2 2

2

1 22 1 1 2

2 1 2

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 66: Breadth-first search

Application 4: modification 2

3 2 1 2 32 1 2 2 1 1 2 31 1 2 1 2

3 2 1 2 3 2 33 2 3 3

31 2 3

3 2 1 1 2 33 2 1 2 3

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 67: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 42 1 2 2 1 1 2 3 41 1 2 1 2

3 2 1 2 4 3 2 34 3 2 3 4 4 3 4

4 43 4

4 1 2 34 3 2 1 1 2 3 4

4 3 2 1 2 3 4

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 68: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 52 1 2 2 1 1 2 3 4 51 1 2 1 2

3 2 1 2 4 3 2 35 4 3 2 3 4 4 3 4

5 4 5 4 53 4 5

4 1 2 35 4 3 2 1 1 2 3 4 5

5 4 3 2 1 2 3 4 5

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 69: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6

3 2 1 2 4 3 2 35 4 3 2 3 4 4 3 46 5 4 5 4 5

6 3 4 54 1 2 3 6

5 4 3 2 1 1 2 3 4 5 66 5 4 3 2 1 2 3 4 5 6

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 70: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6 7

3 2 1 2 4 3 2 3 75 4 3 2 3 4 4 3 46 5 4 5 4 57 6 3 4 5

4 1 2 3 6 75 4 3 2 1 1 2 3 4 5 6 76 5 4 3 2 1 2 3 4 5 6 7

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 71: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6 7

3 2 1 2 4 3 2 3 8 7 85 4 3 2 3 4 4 3 4 86 5 4 5 4 57 6 3 4 5 8

4 1 2 3 6 7 85 4 3 2 1 1 2 3 4 5 6 76 5 4 3 2 1 2 3 4 5 6 7

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 72: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6 7

3 2 1 2 4 3 2 3 8 7 85 4 3 2 3 4 4 3 4 9 8 96 5 4 5 4 5 97 6 3 4 5 8 9

4 1 2 3 6 7 8 95 4 3 2 1 1 2 3 4 5 6 76 5 4 3 2 1 2 3 4 5 6 7

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 73: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6 7

3 2 1 2 4 3 2 3 8 7 85 4 3 2 3 4 4 3 4 9 8 96 5 4 5 4 5 10 9 10

7 6 3 4 5 8 9 10

4 1 2 3 6 7 8 9 10

5 4 3 2 1 1 2 3 4 5 6 76 5 4 3 2 1 2 3 4 5 6 7

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

Page 74: Breadth-first search

Application 4: modification 2

4 3 2 1 2 3 4 5 62 1 2 2 1 1 2 3 4 5 61 1 2 1 2 6 7

3 2 1 2 4 3 2 3 8 7 85 4 3 2 3 4 4 3 4 9 8 96 5 4 5 4 5 10 9 10

7 6 3 4 5 8 9 10 11

4 1 2 3 6 7 8 9 10

5 4 3 2 1 1 2 3 4 5 6 76 5 4 3 2 1 2 3 4 5 6 7

• What if multiple fires start at the same time?• Just place all fires in the initial BFS queue!

11 hours until all farms are on fire!

Page 75: Breadth-first search

Application 4:other modifications to think about

• What if fires start at different times?• What if fires spread at different speeds?

Page 76: Breadth-first search

A neat problem to think about

19 16 14 12 16 13 20 21 19

27 24 20 20 10 9 18 20 17 16 17 18

23 18 16 9 12 14 14 20

19 20 16 13 7 7 9 11 16 24 20

6 7 12 14 20 5 8 12 9 25 22 15

5 6 14 9 14 10 25 23 21

1 1 15 16 11 19 26 19 25

2 12 18 24 24 13 27 30

7 8 13 17 17 13 19 22 20 21 23 20 12

10 12 15 18 16 15 20 18 16 19 18 15

• It’s the year 2241. You’re in a cave and it’s collapsing! A device tells you when each section of ceiling will cave in. Can you escape?

Page 77: Breadth-first search

A neat problem to think about

19 16 14 12 16 13 20 21 19

27 24 20 20 10 7 18 20 17 16 17 18

23 18 16 9 12 14 14 20

19 20 16 13 7 7 9 11 16 24 20

6 7 12 14 20 5 8 12 9 25 22 15

5 6 14 9 14 10 25 23 21

1 1 15 16 11 19 26 19 25

2 12 18 24 24 13 27 30

7 8 13 17 17 13 19 22 20 21 23 20 12

10 12 15 18 16 15 20 18 16 19 18 15

• It’s the year 2241. You’re in a cave and it’s collapsing! A device tells you when each section of ceiling will cave in. Can you escape?Here’s a sample solution. How

would you solve it in general?