Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs: Shortest path and mst

Similar presentations


Presentation on theme: "Graphs: Shortest path and mst"— Presentation transcript:

1

2 Graphs: Shortest path and mst

3 Shortest Path: Single-source shortest path:
Finding the shortest path from a particular vertex to all other vertices on a graph Think of taking a plane from Philadelphia International Airport to any other airport – what is the shortest path? Note that this can be easily modified to find the shortest path from a single source to any other single source

4 Other uses for shortest path:
Maps: finding the shortest route, finding the fastest route Vertices: intersections, Edges: roads (cost: distance, speed, or something else) Networks: for routing packets (data) across a network or the internet Vertices: routers Edges: connections (cost: time for travel) Epidemiology: modeling the spread of infectious diseases Vertices: individuals with disease Edges: contacts (cost: maybe likelihood of infection?)

5 Assumptions: Costs are positive numbers or zero
Otherwise could find that the path with the lowest cost is infinite Not all vertices may be reachable from the single source Weights are not necessarily distance – could be time, cost, likelihood, etc. Shortest paths may not be unique

6 Dijkstra "Computer Science is no more about computers than astronomy is about telescopes."

7 Dijkstra's algorithm Dijkstra's algorithm – solves single-source shortest path problem Works on both directed and undirected graphs. all edges must have nonnegative weights. Directed graph: Undirected Graph:

8 Dijkstra Approach: Greedy (means make a locally optimal choice in the hope that the ultimate result will be optimal) Input: Weighted graph G={E,V} <- Graph is a set of edges and vertices source vertex v∈V, all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices

9 Idea: We’ve got a graph with edges and distances between connected edges (may be represented as a 2-dimensional array) Initiate all distances to infinity (or a really big number) except the distance from our starting vertex (0) All vertexes go into a priority queue Vertex with shortest distance from initial v is at top of queue the unvisited vertex with the shortest distance, gets added to a set of visited nodes. Remove it from the queue. Recalculate all distances from initial vertex to all other vertices not visited yet take the minimum of the distance from the initial vertex v0 to vx or the distance from ther original vertex to the current vertex + the distance from the current vertex to vx Continue until all vertices have been visited (or no more vertices can be visited)

10 Dijkstra Animated Example
Initiate all distances to infinity (or a really big number) except the distance from our starting vertex (0) V A B C D E D[v] Inf Pred[v] null Visited False

11 Dijkstra Example Update distances with the distances to vertices that can be reached from A V A B C D E D[v] 10 3 Inf Pred[v] null Visited True False

12 Dijkstra Example V A B C D E D[v] 10 3 Inf Pred[v] null Visited True
10 3 Inf Pred[v] null Visited True False V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False

13 Dijkstra Example Recalculate all distances from initial vertex to all other vertices not visited yet Take the minimum of the distance from the initial vertex v0 to vx or the distance from ther original vertex to the current vertex + the distance from the current vertex to vx V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False

14 Dijkstra Example Take the next smallest path and add it to the visited set V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False

15 Dijkstra Example Recalculate distances based on smallest of either old distance or distance to added vertex plus distance from added vertex to unvisited node (in this case, no change) V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False

16 Dijkstra Example Take the next smallest path and add it to the visited set V A B C D E D[v] 7 3 11 5 Pred[v] null Visited True False

17 Dijkstra Example Recalculate distances based on smallest of either old distance or distance to added vertex plus distance from added vertex to unvisited node (D changes and prev node of D changes) V A B C D E D[v] 7 3 9 5 Pred[v] null Visited True False

18 Dijkstra Example V A B C D E D[v] 7 3 9 5 Pred[v] null Visited True

19 Find Shortest path from A to any node:
Work Backwards: A-> D? D’s prev was B, B’s prev was C, C’s prev was A, so A->C->B->D A->E? E’s prev was C, s’s prev was A, so A->C->E V A B C D E D[v] 7 3 9 5 Pred[v] null Visited True

20 Represent a graph as a matrix
B C D E 10 3 Inf inf 1 2 4 8 7 9

21 Pseudocode: function Dijkstra(Graph, source): for each vertex v in Graph{ // Initializations dist[v] = infinity ; // Unknown distance function from source to v previous[v] = undefined ; // Previous node in optimal path (maybe -1 to start) from source }//for dist[source] = 0 ; // Distance from source to source Q = the set of all nodes in Graph ; // All nodes in the graph are put in a priority queue (many have a //cost of infinity right now (or largest int) while (Q is not empty) { // The main loop u = vertex in Q with smallest distance in dist[] ; // Start node in first case remove u from Q ; if (dist[u] == infinity) { break; // all remaining vertices are inaccessible from source } //if for each neighbor v of u in Q { // where v has not yet been removed from Q. alt = dist[u] + dist_between(u, v) ; if (alt < dist[v]) { dist[v] = alt ; // new shortest path to v previous[v] = u ; // shortest path goes through u decrease-key v in Q; // Reorder v in the priority queue } // if } //for } // while return dist;

22


Download ppt "Graphs: Shortest path and mst"

Similar presentations


Ads by Google