DOC PREVIEW
MSU CSE 830 - Lecture16

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Shortest PathsSingle Source DefinitionAll Pairs DefinitionCommentsNegative weight edgesRelaxation techniqueBellman-Ford AlgorithmRunning TimeProof of CorrectnessNegative weight cycleDAG shortest path algorithmRunning time ImprovementSlide 13Dijkstra’s AlgorithmRunning Time AnalysisRunning Time Analysis cont’dCompare Dijkstra’s algorithm to Prim’s algorithm for MSTSlide 18Slide 19Computing paths (not just distance)All pairs algorithms using single source algorithmsTwo adjacency matrix based algorithmsConceptual picturesRunning TimesJohnson’s AlgorithmReweightingComputing vertex weights h(v)Algorithm implementationShortest Paths•Definitions•Single Source Algorithms –Bellman Ford–DAG shortest path algorithm–Dijkstra•All Pairs Algorithms–Using Single Source Algorithms–Matrix multiplication–Floyd-Warshall•Both of above use adjacency matrix representation and dynamic programming–Johnson’s algorithm•Uses adjacency list representationSingle Source Definition•Input–Weighted, connected directed graph G=(V,E)•Weight (length) function w on each edge e in E–Source node s in V•Task–Compute a shortest path from s to all nodes in VAll Pairs Definition•Input–Weighted, connected directed graph G=(V,E)•Weight (length) function w on each edge e in E•We will typically assume w is represented as a matrix•Task–Compute a shortest path from all nodes in V to all nodes in VComments•If edges are not weighted, then BFS works for single source problem•Optimal substructure–A shortest path between s and t contains other shortest paths within it•No known algorithm is better at finding a shortest path from s to a specific destination node t in G than finding the shortest path from s to all nodes in VNegative weight edges•Negative weight edges can be allowed as long as there are no negative weight cycles•If there are negative weight cycles, then there cannot be a shortest path from s to any node t (why?)•If we disallow negative weight cycles, then there always is a shortest path that contains no cyclesRelaxation technique•For each vertex v, we maintain an upper bound d[v] on the length of shortest path from s to v•d[v] initialized to infinity•Relaxing an edge (u,v)–Can we shorten the path to v by going through u?–If d[v] > d[u] + w(u,v), d[v] = d[u] + w(u,v)–This can be done in O(1) timeBellman-Ford Algorithm•Bellman-Ford (G, w, s)–Initialize-Single-Source(G,s)–for (i=1 to V-1)•for each edge (u,v) in E–relax(u,v);–for each edge (u,v) in E•if d[v] > d[u] + w(u,v)–return NEGATIVE WEIGHT CYCLE357-5321s35-1-5321sG1G2Running Time•for (i=1 to V-1)–for each edge (u,v) in E•relax(u,v);•The above takes (V-1)O(E) = O(VE) time•for each edge (u,v) in E–if d[v] > d[u] + w(u,v)•return NEGATIVE WEIGHT CYCLE•The above takes O(E) timeProof of Correctness•Theorem: If there is a shortest path from s to any node v, then d[v] will have this weight at end of Bellman-Ford algorithm•Theorem restated: –Define links[v] to be the minimum number of edges on a shortest path from s to v–After i iterations of the Bellman-Ford for loop, nodes v with links[v] ≤ i will have their d[v] value set correctly•Prove this by induction on links[v]•Base case: links[v] = 0 which means v is s–d[s] is initialized to 0 which is correct unless there is a negative weight cycle from s to s in which case there is no shortest path from s to s.•Induction hypothesis: After k iterations for k ≥ 0, d[v] must be correct if links[v] ≤ k •Inductive step: Show after k+1 iterations, d[v] must be correct if links[v] ≤ k+1–If links[v] ≤ k, then by IH, d[v] is correctly set after kth iteration–If links[v] = k+1, let p = (e1, e2, …, ek+1) = (v0, v1, v2, …, vk+1) be a shortest path from s to v•s = v0, v = vk+1, ei = (vi-1, vi)–In order for links[v] = k+1, then links[vk] = k.–By the inductive hypothesis, d[vk] will be correctly set after the kth iteration. –During the k+1st iteration, we relax all edges including edge ek+1 = (vk,v)–Thus, at end of the k+1st iteration, d[v] will be correctNegative weight cycle•for each edge (u,v) in E–if d[v] > d[u] + w(u,v)•return NEGATIVE WEIGHT CYCLE•If no neg weight cycle, d[v] ≤ d[u] + w(u,v) for all (u,v)•If there is a negative weight cycle C, for some edge (u,v) on C, it must be the case that d[v] > d[u] + w(u,v).–Suppose this is not true for some neg. weight cycle C–sum these (d[v] ≤ d[u] + w(u,v)) all the way around C–We end up with Σv in C d[v] ≤ (Σu in C d[u]) + weight(C) •This is impossible unless weight(C) = 0•But weight(C) is negative, so this cannot happen–Thus for some (u,v) on C, d[v] > d[u] + w(u,v)DAG shortest path algorithm•DAG-SP (G, w, s)–Initialize-Single-Source(G,s)–Topologically sort vertices in G–for each vertex u, taken in sorted order•for each edge (u,v) in E–relax(u,v);35-4381s54Running time Improvement•O(V+E) for the topological sorting •We only do 1 relaxation for each edge: O(E) time–for each vertex u, taken in sorted order•for each edge (u,v) in E–relax(u,v);•Overall running time: O(V+E)Proof of Correctness•If there is a shortest path from s to any node v, then d[v] will have this weight at end•Let p = (e1, e2, …, ek) = (v1, v2, v3, …, vk+1) be a shortest path from s to v–s = v1, v = vk+1, ei = (vi, vi+1)•Since we sort edges in topological order, we will process node vi (and edge ei) before processing later edges in the path.Dijkstra’s Algorithm•Dijkstra (G, w, s)•/* Assumption: all edge weights non-negative */–Initialize-Single-Source(G,s)•Completed = {};•ToBeCompleted = V;–While ToBeCompleted is not empty•u =EXTRACT-MIN(ToBeCompleted);•Completed += {u};•for each edge (u,v) relax(u,v);15574321sRunning Time Analysis•While ToBeCompleted is not empty–u =EXTRACT-MIN(ToBeCompleted);–Completed += {u};–for each edge (u,v) relax(u,v);•Each edge relaxed at most once: O(E)–Need to decrease-key potentially once per edge•Need to extract-min once per node–The node’s d[v] is then completeRunning Time Analysis cont’d•Priority Queue operations–O(E) decrease key operations–O(V) extract-min operations•Three implementations of priority queues–Array: O(V2) time•decrease-key is O(1) and extract-min is O(V)–Binary heap: O(E log V) time assuming E ≥ V•decrease-key and extract-min are O(log V)–Fibonacci heap: O(V log V + E) time •decrease-key is


View Full Document

MSU CSE 830 - Lecture16

Download Lecture16
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture16 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture16 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?