# CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.

## Presentation on theme: "CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Graphs –Shortest path (for unwieghted graphs)‏ –Dijkstra's algorithm (solve minimum weight path w/ weighted graph – all edge weights are non-negative)‏

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?

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

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.

Graphs the shortest path could be in terms of minimum weight for weighted graphs (note well: 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 when we dequeue an item, we get the 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.

Graphs Example on the board and then pseudocode for this algorithm. vi means vertex i, and (j) means weight j v0-> v1(2), v3(1)‏ v1-> v3(3), v4(10)‏ v2-> v0(4), v5(5)‏ v3-> v2(2), v4(2), v5(8), v6(4)‏ v4-> v6(6)‏ v5-> null v6-> v5(1)‏ Dijkstra's algorithm, given a starting vertex will find the minimum weight paths from that starting vertex to all other vertices.

We can reuse the code we wrote to set vertices as visited or unvisited. We can reuse our code to handle a directed graph, but we must add the ability for it to be a weighted graph. We need a “minimum” Priority Queue, that is, one that returns the item with the lowest priority at any given dequeue(). We need a way to store all the path lengths. Also, we need to initially 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.)‏

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

What if we wanted to not only display the minimum weight path to each vertex, but also the actual path (with the intervening vertices)? e.g. Minimum weight from v0 to v5 is 8, and the path is: v0 to v3 to v2 to v5

What if we wanted to not only display the minimum weight path to each vertex, but also the actual path (with the intervening vertices)? Notice that the minimum path from v0 to v3 is: v0 to v3 with a weight of 1 Notice that the minimum path from v0 to v2 is: v0 to v3 to v2 with a weight of 3 Notice that the minimum path from v0 to v5 is: v0 to v3 to v2 to v5 with a weight of 8 See how all these just extend each other by one more edge. For example if we end up at v5 from v2, we get to v2 the same way we would have gotten to v2 (w/ the minimum weight.)‏

Download ppt "CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann."

Similar presentations