Design and Analysis of Algorithms

Similar documents
6 March Array Implementation of Binary Trees

Cpt S 223. School of EECS, WSU

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Algorithms and Data Structures

Binary Heaps. CSE 373 Data Structures

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Exam study sheet for CS2711. List of topics

CS711008Z Algorithm Design and Analysis

Introduction to Data Structures and Algorithms

Converting a Number from Decimal to Binary

Analysis of Algorithms I: Binary Search Trees

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structure [Question Bank]

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

1/1 7/4 2/2 12/7 10/30 12/25

Cpt S 223. School of EECS, WSU

Algorithms Chapter 12 Binary Search Trees

Algorithms and Data Structures

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

International Journal of Software and Web Sciences (IJSWS)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Binary Heap Algorithms

Data Structures and Algorithms Written Examination

Any two nodes which are connected by an edge in a graph are called adjacent node.

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

Analysis of Algorithms, I

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Search Trees CMPSC 122

Ordered Lists and Binary Trees

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Sample Questions Csci 1112 A. Bellaachia

Data Structures and Data Manipulation

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

From Last Time: Remove (Delete) Operation

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithms Stacks and Queues

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Rotation Operation for Binary Search Trees Idea:

PES Institute of Technology-BSC QUESTION BANK

Data Structures. Level 6 C Module Descriptor

Binary Search Trees 3/20/14

Heaps & Priority Queues in the C++ STL 2-3 Trees

Symbol Tables. Introduction

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Lecture 4: Balanced Binary Search Trees

Data Structures and Algorithms

Scheduling Shop Scheduling. Tim Nieberg

The number of marks is given in brackets [ ] at the end of each question or part question. The total number of marks for this paper is 72.

Social Media Mining. Graph Essentials

CSE 326: Data Structures B-Trees and B+ Trees

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Mining on Streams

Analyzing the Facebook graph?

ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN. Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Linked Lists Linked Lists, Queues, and Stacks

The Goldberg Rao Algorithm for the Maximum Flow Problem

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Common Data Structures

Data Structures. Chapter 8

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Approximation Algorithms

CompSci-61B, Data Structures Final Exam

How To Create A Tree From A Tree In Runtime (For A Tree)

Big Data and Scripting. Part 4: Memory Hierarchies

DATA STRUCTURES USING C

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

Shortest Path Algorithms

GRAPH THEORY LECTURE 4: TREES

Data storage Tree indexes

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/

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