Theory of Computing Lecture 8 MAS 714 Hartmut Klauck.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Analysis of Algorithms CS 477/677
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
Heapsort Based off slides by: David Matuszek
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Prepared by- Jatinder Paul, Shraddha Rumade
Lecture 16. Shortest Path Algorithms
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
2018/12/27 chapter25.
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Chapter 24: Single-Source Shortest Paths
Graph Algorithms: Shortest Path
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Chapter 24: Single-Source Shortest-Path
Presentation transcript:

Theory of Computing Lecture 8 MAS 714 Hartmut Klauck

Data Structure: Priority Queue Store (up to) n elements and their keys (keys are numbers) Operations: – ExtractMin: Get (and remove) the element with minimum key – DecreaseKey(v,x): replace key(v) with a smaller value x – Initialize – Insert(v,key(v)) – Test for emptiness

Priority Queues We will show how to implement a priority queue with time O(log n) for all operations This leads to total time O((n+m) log n) for the Dijkstra algorithm Slightly suboptimal : we would like O(n log n + m) – Much more difficult to achieve

Heaps We will implement a priority queue with a heap Heaps can also be used for sorting! – Heapsort: Insert all elements, ExtractMin until empty If all operations take time log n we have sorting in time O(n log n)

Heaps A heap is an array of length n – can hold at most n elements The elements in the array are not sorted by keys, but their order has the heap-property Namely, they can be viewed as a tree, in which parents are smaller than their children – ExtractMin is easy (at the root) – Unfortunately we need to work to maintain the heap-property after removing the root

Heaps Keys in a heap are arranged as a full binary tree where the last level is filled from the left up to some point Example:

Heaps Heap property: – Element in cell i is smaller than elements in cells 2i and 2i+1 Example: Tree Array

Heaps Besides the array storing the keys we also keep a counter SIZE that tells us how many keys are in H – in cells 1…SIZE

Simple Procedures for Heaps Initialize: – Declare the array H of correct length n – Set SIZE to 0 Test for Empty: – Check SIZE FindMin: – Minimum is in H[1] All this in time O(1)

Simple Procedures for Heaps Parent(i) is b i/2 c LeftChild(i) is 2i Rightchild(i) is 2i+1

Procedures for Heaps Suppose we remove the root, and replace it with the rightmost element of the heap Now we still have a tree, but we (probably) violate the heap property at some vertex i, i.e., H[i]>H[2i] or H[i]>]H[2i+1] The procedure Heapify(i) will fix this Heapify(i) assumes that the subtrees below i are correct heaps, but there is a (possible) violation at i And no other violations in H (i.e., above i)

Procedures for Heaps Heapify(i) – l=LeftChild(i), r=Rightchild(i) – If l · SIZE and H[l]<H[i] Smallest=l else Smallest=i – If r · SIZE and H[r]<H[Smallest] Smallest=r – If Smallest  i Swap H[i] and H[Smallest] Heapify(Smallest)

Heapify Running Time: – A heap with SIZE=n has depth at most log n – Running time is dominated by the number of recursive calls – Each call leads to a subheap that is 1 level shallower – Time O( log n)

Procedures for Heaps ExtractMin(): – Return H[1] – H[1]=H[SIZE] – SIZE=SIZE-1 – Heapify(1) Time is O(log n)

Procedures for Heaps DecreaseKey(i,key) – If H[i]<key return error – H[i]=key \\Now parent(i) might violate heap property – While i>1 and H[parent(i)]>H[i] Swap H[parent(i)] and H[i], i=parent(i) \\Move the element towards the root Time is O(log n)

Procedures for Heaps Insert(key): – SIZE=SIZE+1 – H[SIZE]= 1 – DecreaseKey(SIZE,key) Time is O(log n)

Note for Dijkstra DecreaseKey(i,x) works on the vertex that is stored in position i in the heap But we want to decrease the key for vertex v! We need to remember the position of all v in the heap H Keep an array pos[1...n] – Whenever we move a vertex in H we need to change pos

The single-source shortest-path problem with negative edge weights Graph G, weight function W, start vertex s Output: a bit indicating if there is a negative cycle reachable from s AND (if not) the shortest paths from s to all v

Bellman-Ford Algorithm Initialize: d(s)=0,  (s)=s, d(v)= 1,  (v)=NIL for other v For i=1 to n-1: – Relax all edges (u,v) For all (u,v): if d(v)>d(u)+W(u,v) then output: „negative cycle!“ Remark: d(v) and  (v) contain distance from s and predecessor in a shortest path tree

Running time Running time is O(nm) – n-1 times relax all m edges

Correctness Assume that no cycle of negative length is reachable from s Theorem: After n-1 iterations of the for-loop we have d(v)=  (s,v) for all v. Lemma: Let v 0,…,v k be a shortest path from s=v 0 to v k. Relax edges (v 0,v 1 )….(v k-1,v k ) successively. Then d(v k )=  (s,v k ). This holds regardless of other relaxations performed.

Correctness Proof of the theorem: – Let v denote a reachable vertex – Let s, …,v be a shortest path with k edges – k · n-1 can be assumed (why?) – In every iteration all edges are relaxed – By the lemma d(v) is correct after k · n-1 iterations For all unreachable vertices we have d(v)= 1 at all times To show: the algorithm decides the existence of negative cycles correctly No neg. cycle present/reachable: for all edges (u,v): – d(v)=  (s,v) ·  (s,u)+W(u,v)=d(u)+W(u,v), pass test

Correctness If a negative cycle exists: – Let v 0,…,v k be a (reachable) path with negative length and v 0 =v k – Assume the algorithm does NOT stop with error message, then d(v i ) · d(v i-1 )+W(v i-1,v i ) for all i=1...k Hence

Correctness v 0 =v k, so d(v i )< 1 in the end for all reachable vertices, hence

The Lemma Lemma: Let v 0,…,v k be a shortest path from s=v 0 to v k. Relax edges (v 0,v 1 )….(v k-1,v k ) successively. Then d(v k )=  (s,v k ). This holds regardless of other relaxations performed. Proof: By induction. After relaxing (v i-1, v i ) the value d(v i ) is correct. Base: i=0, d(v 0 )=d(s)=0 is correct. Assume d(v i-1 ) correct. According to an earlier observation after relaxing (v i-1,v i ) also d(v i ) correct. Once d(v) is correct, the value stays correct. d(v) is always an upper bound

Application of Bellman Ford Graph is a distributed network – vertices are processors that can communicate via edges We look for distance/shortest path of vertices from s Computation can be performed in a distributed way, without – global control – global knowledge about the network Dijkstra needs global knowledge Running time: n-1 phases, vertices compute (in parallel)