Design and Analysis of Algorithms

Similar documents
CS350: Data Structures Heaps and Priority Queues

Sorting and Searching

Sorting and Searching

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Partha Sarathi Manal

Algorithms and Data Structures

Chapter 6 Heapsort 1

Basic Data Structures and Heaps

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Module 2: Priority Queues

Module 2: Priority Queues

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Course Review for Finals. Cpt S 223 Fall 2008

ME/CS 132: Advanced Robotics: Navigation and Vision

CSci 231 Final Review

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Binary Heaps. CSE 373 Data Structures Lecture 11

CSE 214 Computer Science II Heaps and Priority Queues

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CS165: Priority Queues, Heaps

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

COMP 251 Winter 2017 Online quizzes with answers

CS521 \ Notes for the Final Exam

CS 241 Analysis of Algorithms

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

CSCI2100B Data Structures Heaps

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Total Points: 60. Duration: 1hr

Course Review. Cpt S 223 Fall 2009

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Properties of a heap (represented by an array A)

Priority Queues. Binary Heaps

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Heapsort. Heap data structure

Questions from the material presented in this lecture

BM267 - Introduction to Data Structures

( ) n 3. n 2 ( ) D. Ο

Binary Heaps in Dynamic Arrays

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Data Structures and Algorithms

CE 221 Data Structures and Algorithms

3. Fundamental Data Structures

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

A data structure and associated algorithms, NOT GARBAGE COLLECTION

CS301 - Data Structures Glossary By

Shortest path problems

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Heaps, Heapsort, Priority Queues

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Topic: Heaps and priority queues

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Data Structures Lesson 9

Collection of priority-job pairs; priorities are comparable.

Algorithms and Data Structures

9. Heap : Priority Queue

Algorithm Design (8) Graph Algorithms 1/2

( D. Θ n. ( ) f n ( ) D. Ο%

CS711008Z Algorithm Design and Analysis

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

CS711008Z Algorithm Design and Analysis

Course Review. Cpt S 223 Fall 2010

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Figure 1: A complete binary tree.

Elementary Data Structures. CS 114/116 and CS 505 material. Chapters , of GT

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

The Heap Data Structure

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

CH 8. HEAPS AND PRIORITY QUEUES

) $ f ( n) " %( g( n)

CH. 8 PRIORITY QUEUES AND HEAPS

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Basic Data Structures (Version 7) Name:

Priority Queues and Binary Heaps

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Heaps, Heap Sort, and Priority Queues.

Data Structures Question Bank Multiple Choice

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Priority Queues. Chapter 9

Priority Queues and Heaps

Binary Trees. Recursive definition. Is this a binary tree?

Transcription:

CSE 1, Winter 201 Design and Analysis of Algorithms Lecture 7: Bellman-Ford, SPs in DAGs, PQs Class URL: http://vlsicad.ucsd.edu/courses/cse1-w1/

Lec. Added after class Figure.: Single-Edge Extensions of SPs Find shortest path to v k+1 by extending by a single edge the shortest path to one of v 0, v 1,, v k

Lec. Proof of Dijkstra Correctness Added after class

Dijkstra key points Lec.

Negative Edges Dijkstra s algorithm assumes that the shortest path from s to v must pass through vertices that are closer to s than v. This fails when there are negative edges in G (Figure.)

Bellman-Ford Algorithm Idea: Successive Approximation / Relaxation Find SP using 1 edges Find SP using 2 edges Find SP using n-1 edges have true shortest paths Let l j (k) denote shortest v 0 v j pathlength using k edges

Bellman-Ford Algorithm Idea: Successive Approximation / Relaxation Find SP using 1 edges Find SP using 2 edges Find SP using n-1 edges have true shortest paths Let l (k) j denote shortest v 0 v j pathlength using k edges Then, l (1) i = d 0j j = 1,, n-1 // d ij = if no i-j edge In general, l j (k+1) = min { l j (k), min i (l i (k) + d ij ) } l(k) j : don t need k+1 arcs min i (l (k) i + d ij ) : view as length-k SP plus a single edge

Bellman-Ford vs. Dijkstra A B S 3 1 C 2 2 D PASS: 1 2 3 LABEL A min(, 3+, +1, 2+ ) = 7 min(7, 3+, +1, 2+ ) = min(, 3+, +1, 2+ )= B 3 min(3, +, +, 2+ ) = 3 min(3, 7+, +, 2+ ) = 3 min(3, +, +, 2+ ) = 3 C min(, +1, 3+, 2+2) = min(, 7+1, 3+, 2+2) = min(, +1, 3+, 2+2) = D 2 min(2, +, 3+, +2) = 2 min(2, 7+, 3+, +2) = 2 min(2, +, 3+, +2) = 2 EXAMPLE: SP(S,A) = S D C A S D C is found at Pass 2, allowing S D C A to be found at Pass 3

Bellman-Ford (avoiding unneeded work) A B S 3 1 C 2 2 D PASS: 1 2 3 LABEL A min(, 3+, +1) = 7 etc. B 3 min(3, +) = 3 etc. C min(, +1, 2+2) = etc. D 2 min(2, +2) = 2 etc. EXAMPLE: SP(S,A) = S D C A S D C is found at Pass 2, allowing S D C A to be found at Pass 3

Bellman-Ford vs. Dijkstra A B S 3 1 C 2 2 D PASS: 1 2 3 Label A min([], 2+ ) = min([], 3+) = 7 min([7], +1) = * B 3 min([3], 2+ ) = 3* C min([ ], 2+2) = min(, 3+ ) = * D 2*

Special Case: Longest/Shortest Paths in DAGs (Single-Source) Longest-Path Problem: well-defined only when there are no cycles DAG: can topologically sort the vertices labels v 1,, v n s.t. all edges directed from v i to v j, i < j Let l j denote longest v 0 v j pathlength l 0 = 0 l 1 = d 01 // d ij = - if no i-j edge l 2 = max(d 01 + d, d 02 ) In general, l k = max j<k (l j + d jk ) l(x) l(y) x y d xz d yz l(z) z l(z) = max (l(x) + d xz, l(y) + d yz )

Special Case: Longest/Shortest Paths in DAGs (Single-Source) Longest-Path Problem: well-defined only when there are no cycles DAG: can topologically sort the vertices labels v 1,, v n s.t. all edges directed from v i to v j, i < j Let l j denote longest v 0 v j pathlength l 0 = 0 l 1 = d 01 // d ij = - if no i-j edge l 2 = max(d 01 + d, d 02 ) In general, l k = max j<k (l j + d jk ) Shortest pathlength in DAG replace max by min // use d ij = + if no i-j edge l(x) l(y) x y d xz d yz l(z) z l(z) = max (l(x) + d xz, l(y) + d yz )

DAG Longest/Shortest Paths Complexity Generic Bellman-Ford = O(VE) In a DAG: Topological sort = O(V+E) (DFS) Edges out of vertex v aren t processed (traversed) until after all edges in to v have been processed Runtime O(V+E) Exercise: Understand why runtime is O(V+E) for both longest-path and shortest-path in a DAG Application: PERT (program evaluation and review technique) critical path is the longest path in the DAG

Dynamic Sets Dynamic sets (data structures): change a dictionary, e.g., add/remove words reuse of structured information fast updating for on-line algorithms Elements: key is element ID dynamic set of key values satellite information associated with key Operations query: return information about the set modify: change the set x record key Satellite data

Search(S,k) Dynamic Set Operations Given set S and key value k, return pointer x to an element of S such that key[x] = k, or NIL if no such element query Insert(S,x) Augment set S with element pointed to by x modify Delete(S,x) Given pointer x to an element in set S, remove x from S modify Minimum(S) / Maximum(S) Given totally ordered set S, return pointer to element of S with smallest / largest key query

Dynamic Set Operations Predecessor / Successor(S, x) Given element x whose key is from a totally ordered set S, return a pointer to next smaller / larger element in S, or NIL if x is minimum / maximum element query Union(S, S ) Given two sets S, S, return a new set S = S S modify

Elementary Data Structures Different data structures support/optimize different operations Stack has top, LIFO policy insert = push x: top(s) = top(s)+1); S[top(S)] = x O(1) delete = pop O(1) 1 2 3 7 1 2 top[s] = 1 2 3 7 1 2 17 After push(s,17) top[s] = Queue has head, tail, FIFO policy insert = enqueue: add element to the tail O(1) delete = dequeue: remove element from the head O(1) 1 2 3 7 1 2 1 2 3 7 1 2 head = 2 tail = head = 2 After enqueue(q,) tail = 7

Priority Queue (PQ) Abstract Data Structure Operations: Insert(S,x) : add element x Minimum(S) / Maximum(S): return element with min/max key DeleteMin(S) / DeleteMax(S): return min/max key, remove element Applications Simulation systems: key event time OS scheduler: key job priority Numerical methods key inherent error in operation Dijkstra s shortest-path algorithm Prim s minimum spanning tree algorithm Question: How do we use a PQ to sort?

What Are Naïve PQ Implementations? [[Answer: Insert elements one by one; perform DeleteMin n times]] Unordered list Insert O(1) DeleteMin O(n) Ordered list Insert O(n) DeleteMin O(1) Observation: If Insert, DeleteMin could each be accomplished in O(log n) time, then we would have an O(n log n) sorting algorithm (= heapsort)

Heaps A heap is a binary tree of depth d such that (1) all nodes not at depth d-1 or d are internal nodes each level is filled before the next level is started (2) at depth d-1 the internal nodes are to the left of the leaves and have degree 2, except perhaps for the rightmost, which has a left child each level is filled left to right A max heap (min heap) is a heap with node labels from an ordered set, such that the label at any internal node is ( ) the label of any of its children All root-leaf paths are monotone

Heaps, and Sorting With Heaps Fact: Every node in a heap is the root of a heap (!) How do we store a heap? Implicit data structure: // maxheap example Array index: 1 2 3 7 Value: 20 3 2 3 1 2 A[i] is parent of A[2i] and A[2i+1] How do we sort using a heap? Insert: Put new value at A[n]; fix violation of heap condition ( re-heapify ) DeleteM*: Remove root; replace by A[n]; re-heapify If maxheap, DeleteMax (return largest element first) If minheap, DeleteMin (return smallest element first)

Heaps 2 1 2 3 7 1 2 3 7 2 Pointers: Parent parent of A[i] has index = i div 2 Left, Right (children) children of A[i] have indices 2i, 2i+1 Parent Child this is a minheap example

Heap Operations 2 2 2 3 7 3 7 7 3 Insert(S,x): O(height) O(log n) Keep swapping with parent until heap condition satisfied 2 Extract-min(S): return head, replace head key with the last key, float down O(log n) Float down : If heap condition violated, swap with smaller child

O(n log n) Heapsort Build heap easy time bound: n x O(log n) time for i = 1..n do insert (A[1..i],A[i]) Extract elements in sorted order: n x O(log n) time for i = n..2 do Swap (A[1] A[i]) Heapsize = Heapsize-1 Float down A[1] 7 7 7 7 7 7 7 7

Actual Time To Build Heap: O(n) Heapify (i,j) makes range [i,j] satisfy heap property: Heapify (i,j) // minheap if i not a leaf and child of i is < i let k = smaller child of i interchange a[i], a[k] Heapify (k,j) BuildHeap: for i = n to 1 do Heapify (i,n) 2 1 2 3 7 2 1 2 3 7

Actual Time To Build Heap: O(n) Heapify (i,j) makes range [i,j] satisfy heap property: Heapify (i,j) // minheap if i not a leaf and child of i is < i let k = smaller child of i interchange a[i], a[k] Heapify (k,j) BuildHeap: for i = n to 1 do Heapify (i,n) We will show that BuildHeap actually takes O(n) time (!) Observation: If vertices i+1,..., n are roots of heaps, then after Heapify(i,n) vertices i,...,n will be roots of heaps

Actual Time To Build Heap: O(n) BuildHeap: for i=n to 1 do Heapify (i,n) Observation: If vertices i+1,..., n are roots of heaps, then after Heapify(i,n) vertices i,...,n will be roots of heaps Let T(h) time for Heapify on v at height h T(h) O(h) 2 1 2 3 7 2 1 2 3 7

Actual Time To Build Heap: O(n) BuildHeap: for i=n to 1 do Heapify (i,n) Observation: If vertices i+1,..., n are roots of heaps, then after Heapify(i,n) vertices i,...,n will be roots of heaps Let T(h) time for Heapify on v at height h T(h) O(h) Heapify called once for each v total BuildHeap time is O( v h(v)) Vertex at height i is root of heap with 2 i+1 nodes n/2 i+1 vertices at height i i n/2 i is upper bound on BuildHeap time Fact: i/2 i = 2 O(n) bound X = 1/2 + 2/ + 3/ + /1 + = [1/2 + 1/ + 1/ ] + [1/ + 2/ + 3/1 + ] = 1 + X/2 = 2