cs 261 reachability problems. the classic reachability algorithm findreachable (graph g, vertex...

43
CS 261 Reachability Problems

Upload: neal-small

Post on 16-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

CS 261

Reachability Problems

Page 2: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

The classic Reachability algorithm

findReachable (graph g, vertex start) { create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c add start vertex to container c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v to the container c } } return r}

Page 3: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Different containers different algorithms

• What is interesting about this algorithm is that if you use different containers

• You get different algorithms

Page 4: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example Solving a Maze

Page 5: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Represented as a graph

Page 6: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Use a Stack: { 1 }

Page 7: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Use a Stack: { 2, 6 }

Page 8: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Use a Stack: { 7, 3, 6 }

Page 9: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Use a Stack: { 3, 6 } dead end

Page 10: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Try alternative: { 4, 8, 6 }

Page 11: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Keep plowing on: { 5, 9, 8, 6 }

Page 12: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Keep on: { 10, 9, 8, 6 }

Page 13: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Keep on: { 15, 9, 8, 6 }

Page 14: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Keep on: { 14, 20, 9, 8, 6 }

Page 15: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Note duplicate: { 9, 20, 9, 8, 6 }

Page 16: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Opps, 4 again: { 4, 20, 9, 8, 6 }

Page 17: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Pop, try alternative: { 20, 9, 8, 6 }

Page 18: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Keep going: { 19, 9, 8, 6 }

Page 19: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

And going: { 24, 9, 8, 6 }

Page 20: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Finished: { 25, 9, 8, 6 }

Page 21: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Depth first search

• Keep going in one direction as long as possible

• When you get stuck, back up to last choice, try alternative

• Did anybody do a Corn Maze last fall? Probably used this technique.

Page 22: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

What if we use a queue?

Page 23: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Initial queue: {1}

Page 24: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

First neighbors: {2, 6}

Page 25: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 2: {6, 3, 7}

Page 26: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 6: {3, 7, 11}

Page 27: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 3: {7, 11, 4, 8}

Page 28: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 7: {11, 4, 8}

Page 29: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 11: {4, 8, 12, 16}

Page 30: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Neighbors of 4: {8, 12, 16, 5, 9}

Page 31: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Breadth-first search

• Notice how this search jumps all over the place

• It’s - you go that way, I’ll go this way (but with a very large group of friends)

• Or think about spilling ink at the start, and watching it seep through the entire maze

Page 32: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Questions you can ask

• Which is faster?

• Which is guaranteed to find a solution?

• What if the graph is infinite, but there is a finite solution - which is guaranteed to find a solution?

Page 33: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

But there is more!

• If we have a weighted graph, and use a priority queue - then the same technique is called Dijkstra’s algorithm

• Idea: queue keeps shortest distance from some place you have been to someplace you might not have been

Page 34: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Essence of Dijkstra’s algorithm!

• Idea: queue keeps shortest distance from some place you have been to someplace you might not have been

• When you pull an item from the queue, if it is someplace you have seen, toss it out,

• If it is new, add to destinations, put neighbors into queue

Page 35: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

What about Weighted Graphs?

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Dijkstra’s algorithm.Use a priority queue instead of a stack. Return a map of city, distance pairs. Pqueue orders values on shortest distance

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 36: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

------------Pierre: 0

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 37: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Pierre: 0------------ Pendeleton: 2

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 38: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Pierre: 0, Pendleton: 2------------Phoenix: 6, Pueblo: 10

Notice how the distances have been added

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 39: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Pierre: 0, Pendleton: 2, Phoenix: 6------------Pueblo: 9, Peoria: 10, Pueblo: 10, Pittsburgh: 16

Notice how values are stored in the Pqueue in distance order

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 40: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: 9------------Peoria: 10, Pueblo: 10, Pierre: 13, Pittsburgh: 16

Pierre gets put in queue, although it is known to be reachable

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 41: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Example: What is the distance from Pierre

Pendleton

Pierre

Pensacola

Princeton

Pittsburgh

Peoria

Pueblo

Phoenix

Pierre: 0, Pendleton: 2, Phoenix: 6, Pueblo: 9, Peoria: 10------------Pueblo: 10, Pierre: 13, Pueblo: 13, Pittsburgh: 15, Pittsburgh: 16Duplicates only removed when pulled out of queue

2

4

3

3

4

3

5

410

8

5

2

Dijkstra (String startCity, Map[String, Map[String, double]] distances)Make empty map of distances into variable reachablePut (StartingCity, 0) into PqueueWhile Pqueue not empty pull new city from queue, if not ready in reachable, add to reachable add neighbors to queue, adding weight to distance from starting cityWhen done with loop, return reachable map

Page 42: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Depth first reachability, Breadth first Reachability,

Dijkstras Algorithm• All three are essentially the same

algorithm

• The only difference is the type of container they use

Page 43: CS 261 Reachability Problems. The classic Reachability algorithm findReachable (graph g, vertex start) { create a set of reachable vertices, initially

Bottom line

• So the key idea from this course is

• Selecting appropriate data structures is the key to programming

• You need to know what tools are available to make the best choices

• And with that, we end this course