Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 214 – Computer Science II Graph Walking

Similar presentations


Presentation on theme: "CSE 214 – Computer Science II Graph Walking"— Presentation transcript:

1 CSE 214 – Computer Science II Graph Walking
Source:

2 Get from point A to B

3 Let’s use a Greedy Algorithm

4 What’s a Greedy Algorithm?
To find path in weighted tree, at each step, if destination not available, choose best choice for short-term Ex: Find path from B to A Greedy Solution? B-E-F-A Total Trip: 13 A 6 7 2 D G 8 C 5 8 4 3 B F 3 E 2

5 Greedy: You might get a poor result

6 What’s good about using a Greedy algorithm?
It works, you get a path It’s fairly easy to implement relatively speaking It’s not optimal, but often it produces a decent result this is called heuristics What would an optimal result be? the best possible path

7 Want to learn more? CSE 373 – Analysis of Algorithms
heuristics, optimal algorithms, graph path-finding algorithms & problems, etc. CSE 380 – Computer Game Programming AI path-finding algorithms (like A*), etc. AMS 301 – Combinatorial Logic graph path-finding algorithms, etc.

8 So how do we implement a Greedy Algorithm?
One solution: use three lists (or arrays): path being built neighbors of node being examined nodes already visited A C D B E F 6 5 3 2 7 8

9 Greedy Steps Using Iteration
Make start node the “current node” Add current node node to path list Add current node to visited list Fill neighbor list with all neighbors of current node that are not in visited list Is the neighbor list empty? If yes, we’ve reached a dead end, to go back: i. remove the current node from the path list if the path list is empty, we’re done, no path possible else make the last remaining node in the path the current node and go back to step 4 If no, continue Are any neighbors the destination? if yes, add to path and return, we’re done if no, continue 7. Get closest neighbor and make it the current node, then go back to step 2

10 Airport public class Airport implements Comparable { public String id; public int latitudeDegrees; public int latitudeMinutes; public int longitudeDegrees; public int longitudeMinutes; public static float calculateDistance( Airport a1, Airport a2) … }

11 public class AirportGraph { // THESE ARE THE NODES IN THE GRAPH, SORTED BY ID Vector<Airport> vertices = new Vector<Airport>(); // THESE ARE THE CONNECTIONS BETWEEN THE NODES Vector<Edge> edges = new Vector<Edge>(); public void addAirport(Airport airportToAdd){…} public void addConnection( String airportID1, String airportID2) {…} public Airport getAirport(String code) {…} … public class Edge implements Comparable String source = null; String destination = null; AirportGraph

12 Building the Graph public void initAirportGraph() { graph.addAirport(new Airport("AUS",30,18,97,42)); graph.addAirport(new Airport("BOS",42,22,71,2)); graph.addAirport(new Airport("DFW",32,51,96,51)); graph.addAirport(new Airport("DEN",39,45,104,52)); graph.addAirport(new Airport("ELP",31,48,106,24)); … graph.addConnection("LGA", "BOS"); graph.addConnection("BOS", "SEA"); graph.addConnection("AUS", "DFW"); graph.addConnection("DFW", "HOU"); graph.addConnection("SFO", "SEA"); }

13 How would we generate a path?
public Vector<String> generatePath( String sourceID, String destinationID) { … }

14 Let’s use our Greedy Algorithm
Make start node the “current node” Add current node node to path list Add current node to visited list Fill neighbor list with all neighbors of current node that are not in visited list Is the neighbor list empty? If yes, we’ve reached a dead end, to go back: i. remove the current node from the path list if the path list is empty, we’re done, no path possible else make the last remaining node in the path the current node and go back to step 4 If no, continue Are any neighbors the destination? if yes, add to path and return, we’re done if no, continue 7. Get closest neighbor and make it the current node, then go back to step 2

15 Greedy is similar to a depth-first solution
Another option: a breadth first solution How would that work? From the start location, build paths out, remembering the shortest path to each node as you go until you find your destination node


Download ppt "CSE 214 – Computer Science II Graph Walking"

Similar presentations


Ads by Google