CS 206 Introduction to Computer Science II 04 / 13 / 2018 Instructor: Michael Eckmann
Today s Topics Questions? Comments? Graphs Depth First Search (DFS) Shortest path Shortest weight path (Dijkstra's algorithm) Michael Eckmann - Skidmore College - CS 206 - Spring 2018
Graph traversal Depth first search (DFS) Pick a vertex at which to start Visit one of its 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.
Recall that the BFS used a Queue. DFS Any thoughts on how DFS could be implemented? What data structure allows us to backtrack?
DFS set all vertices to UNVISITED push start vertex visit start vertex - set start vertex to visited while (stack is not empty) peek to get vertex at top of stack try to get an unvisited adjacent vertex to the peeked one if there isn't one, pop else push it visit it - set it to visited
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?
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
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 Process the graph in a BFS order starting at the given vertex but when visit a node, also replace its length with the current length. Example on the board This is just BFS while also keeping track of path lengths.
Example on the board and then pseudocode for Dijkstra's 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) > (pathlen from startv to v + weight of the edge from v to adjv) 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=adjv, 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.)