Download presentation

Presentation is loading. Please wait.

Published byNeal Small Modified over 2 years ago

1
CS 261 Reachability Problems

2
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 }

3
Different containers different algorithms What is interesting about this algorithm is that if you use different containers You get different algorithms

4
Example Solving a Maze

5
Represented as a graph

6
Use a Stack: { 1 }

7
Use a Stack: { 2, 6 }

8
Use a Stack: { 7, 3, 6 }

9
Use a Stack: { 3, 6 } dead end

10
Try alternative: { 4, 8, 6 }

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

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

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

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

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

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

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

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

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

20
Finished: { 25, 9, 8, 6 }

21
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.

22
What if we use a queue?

23
Initial queue: {1}

24
First neighbors: {2, 6}

25
Neighbors of 2: {6, 3, 7}

26
Neighbors of 6: {3, 7, 11}

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

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

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

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

31
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

32
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?

33
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

34
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

35
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 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

36
Example: What is the distance from Pierre Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix ------------ Pierre: 0 2 4 3 3 4 3 5 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

37
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 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

38
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 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

39
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 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

40
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 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

41
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: 16 Duplicates only removed when pulled out of queue 2 4 3 3 4 3 5 4 10 8 5 2 Dijkstra (String startCity, Map[String, Map[String, double]] distances) Make empty map of distances into variable reachable Put (StartingCity, 0) into Pqueue While 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 city When done with loop, return reachable map

42
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

43
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

Similar presentations

OK

Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.

Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on data collection methods statistics Ppt on cloud computing free download Ppt on unknown facts about india Ppt on power sharing in democracy sovereignty Ppt on as 14 amalgamation define Ppt on ganga action plan download Run ppt on iphone Ppt on my school life Ppt on inhabiting other planets that could support Ppt on queen victoria