Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Graphs –Traversals Depth First Search Shortest Path algorithms

3 Graphs You implemented a graph as an adjacency matrix in lab yesterday. Let's look at my Graph class that implements it with an adjacency matrix.

4 Graphs Graph traversal –Depth first search (DFS)‏ Pick a vertex at which to start Visit one of it's adjacent vertices then visit one of that one's adjacent vertices, and so on until there is no unvisited adjacent vertex of the one we're working on. Then backtrack one level and visit another adjacent vertex from that one and repeat. Do this until we're at the start vertex and there's no more unvisited adjacent vertices Do not visit a vertex more than once –Only vertices that are reachable from the start vertex will be visited –Those vertices that are adjacent to a vertex can be visited in any order. –Example of DFS on the board.

5 Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –what real world examples might ever want to find the shortest path?

6 Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –the shortest path could be in terms of path length (number of edges between vertices e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –the shortest path could be in terms of minimum weight for weighted graphs (example on the board.)‏ e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem

7 Graphs the shortest path could be in terms of path length (number of edges between vertices) e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –Initialize all lengths to infinity –Can process the graph in a BFS starting at the given vertex –When visit a node, replace it's length with the current length. –The BFS uses a queue. –Let's implement this, using our BFS code.

8 Graphs the shortest path could be in terms of minimum weight for weighted graphs (note: weights are non-negative)‏ e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem –It attempts to minimize the weight at each step. Dijkstra's algorithm is a greedy algorithm. That is, its strategy is to locally minimize the weight, hoping that's the best way to get the minimum weight of the whole graph. –Sometimes the local minimum weight is not the correct choice for the overall problem. In that case, the algorithm will still work, but the initial guess was wrong. –Dijkstra's algorithm works in a similar way to BFS but instead of a queue, use a “minimum” priority queue. That is, a priority queue that returns an item whose priority is least among the items in the priority queue. –Let's see an example on the board and come up with pseudocode for this algorithm.

9 Graphs Example on the board and then pseudocode for this algorithm. 0-> 1(2), 3(1)‏ 1-> 3(3), 4(10)‏ 2-> 0(4), 5(5)‏ 3-> 2(2),4(2),5(8),6(4)‏ 4-> 6(6)‏ 5-> null 6-> 5(1)‏ Dijkstra's algorithm, given a starting vertex will find the minimum weight paths from that starting vertex to all other vertices.

10 We need code to handle a weighted, directed graph. We need a “minimum” Priority Queue, that is, one that returns the item with the lowest priority at any given remove(). We need a way to set all the minimum path lengths to Integer.MAX_VALUE (this is the initial value we want to use for the path lengths, because if we ever calculate a lesser weight path, then we store this lesser weight path.)‏

11 Dijkstra's algorithm pseudocode (given a startV)‏ set all vertices to unvisited and all to have pathLen MAX set pathLen from startV to startV to be 0 add (item=startV, priority=0) to PQ while (PQ !empty) { v = remove the lowest priority vertex from PQ (do this until we get an unvisited vertex out)‏ set v to visited for all unvisited adjacent vertices (adjV) to v { if ( current pathLen from startV to adjV ) > ( weight of the edge from v to adjV + pathLen from startV to v ) then { set adjV's pathLen from startV to adjV to be weight of the edge from v to adjV + pathLen from startV to v add (item=v, priority=pathLen just calculated) to PQ } // end if } // end for } // end while


Download ppt "CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google