CPSC 311 Section 502 Analysis of Algorithm

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Medians and Order Statistics
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Spring 2015 Lecture 5: QuickSort & Selection
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Tirgul 10 Solving parts of the first two theoretical exercises: –T1 Q.1(a,b) –T1 Q.4(a,e) –T2 Q.2(a) –T2 Q.3(b) –T2 Q.4(a,b)
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Information and Computer Sciences University of Hawaii, Manoa
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Randomized Quicksort (8.4.2/7.4.2) Randomized Quicksort –i = Random(p, r) –swap A[p]  A[i] –partition A(p, r) Average analysis = Expected runtime –solving.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Analysis of Algorithms CS 477/677
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Data Structures and Algorithms I Day 9, 9/22/11 Heap Sort
Order Statistics.
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Chapter 15 Lists Objectives
CPSC 311 Section 502 Analysis of Algorithm
Randomized Algorithms
Week 15 – Monday CS221.
Chapter 7 Sorting Spring 14
CS 583 Analysis of Algorithms
Elementary Data Structures
CSC 413/513: Intro to Algorithms
Order Statistics(Selection Problem)
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Lesson Objectives Aims
Randomized Algorithms
ITEC 2620M Introduction to Data Structures
Data Structures Review Session
Medians and Order Statistics
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
CS6045: Advanced Algorithms
CS 3343: Analysis of Algorithms
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
CS 332: Algorithms Quicksort David Luebke /9/2019.
Chapter 9: Medians and Order Statistics
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Data Structures & Algorithms
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
Richard Anderson Lecture 14 Divide and Conquer
CSE 332: Sorting II Spring 2016.
CS200: Algorithm Analysis
Analysis of Algorithms CS 477/677
Medians and Order Statistics
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University

Announcements There will be a quiz on next Tuesday (Sep 30) CPSC 311 O. Burchan Bayazit – Fall 02 Announcements There will be a quiz on next Tuesday (Sep 30) A new homework will be put to the web site today ( due date next Thursday (October 3)) Midterm Tuesday October 8th.

Randomize-Select Randomized-Select(A, start, end, i) CPSC 311 O. Burchan Bayazit – Fall 02 Randomize-Select Randomized-Select(A, start, end, i) 1. if start = end then return A[p] 2. pivot = Randomized-Partition(A, start, end) 3. if pivot = i then return A[i] 4. else if i is in [start, pivot-1] then 5. return Randomized-Select(A, start, pivot-1, i) 6. else return Randomized-Select(A, pivot+1, end, i - (pivot - start + 1))

Running Time of Randomized-Select CPSC 311 O. Burchan Bayazit – Fall 02 Running Time of Randomized-Select Worst-case : unlucky with bad 0 : n - 1 partitions. T(n) = T(n - 1) + (n) = (n2) (same as for worst-case of QuickSort) Best-case : really lucky T(n) = (n) Average-case : like Quick-Sort, will be asymptotically close to best-case.

Selection in Linear Worst-Case Time CPSC 311 O. Burchan Bayazit – Fall 02 Selection in Linear Worst-Case Time Select(A, start, end, i) /* i is the ith order statistic. */ divide input array A into n/5 groups of size 5 (and one leftover group if n % 5 != 0) find the median of each group of size 5 by insertion sorting the groups of 5 and then picking the middle element. call Select recursively to find x=A[k], the median of the n/5 medians. partition array around x, splitting it into two arrays A[start, pivot-1] and A[pivot+1, end] if (i = k) return x else if (i < k) then /* like Randomized-Select */ call Select(A[start, pivot-1], i) else call Select(A[pivot+1, end], i - (pivot - start + 1))

Selection in Linear Worst-Case Time CPSC 311 O. Burchan Bayazit – Fall 02 Selection in Linear Worst-Case Time Main idea: this guarantees that x causes a "good" split (at least a constant fraction of the n elements <= x and a constant fraction > x). 3(1/2 n/5 - 2)  (3n/10) - 6 elements are > x (or < x) "At least 1/2 of the medians found in step 2 are greater than the median of medians x. So at least half of the n/5 groups contribute 3 elements that are > x, except for the one group with < 5 elements and the group with x itself." (also holds that (3n/10) - 6 are < x) So worst-case split has (7n/10) + 6 elements in "big" section of the problem.

Selection in Linear Worst-Case Time CPSC 311 O. Burchan Bayazit – Fall 02 Selection in Linear Worst-Case Time Running Time (each step): 1. O(n) (break into groups of 5) 2. O(n) (sorting 5 numbers and finding median is O(1) time) 3. T(n/5) (recursive call to find median of medians) 4. O(n) (partition is linear time) 5. T(7n/10 + 6) (maximum size of subproblem) T(n) = T(n/5) + T(7n/10 + 6) + O(n) Solve this recurrence using the substitution method. Guess T(n)  cn  cn/5 + c(7n/10 + 6) + O(n)  c((n/5) + 1) + 7cn/10 + 6c + O(n) = cn - (cn/10 - 7c) + O(n)  cn This step holds since n >= 80 implies (cn/10 -7c) is positive Choosing big enough c makes O(n) + (cn/10 -7c) positive, so last line holds. (Try c = 200)

Example Partition this array using Select CPSC 311 O. Burchan Bayazit – Fall 02 Example Partition this array using Select A={12,34,0,3,22,4,17,32,3,28,43,82,25,27,34,2,19,12,5,18,20,33,16,33,21,30,3,47}

Example First make groups of 5 12 34 3 22 4 17 32 3 28 43 82 25 27 34 CPSC 311 O. Burchan Bayazit – Fall 02 Example First make groups of 5 12 34 3 22 4 17 32 3 28 43 82 25 27 34 2 19 12 5 18 20 33 16 21 30 3 47

Example Then find medians in each group 3 12 34 22 4 3 17 32 28 25 27 CPSC 311 O. Burchan Bayazit – Fall 02 Example Then find medians in each group 3 12 34 22 4 3 17 32 28 25 27 34 43 82 2 5 12 19 18 20 16 21 33 3 30 47

Example Then find median of medians 3 12 34 22 4 3 17 32 28 25 27 34 CPSC 311 O. Burchan Bayazit – Fall 02 Example Then find median of medians 3 12 34 22 4 3 17 32 28 25 27 34 43 82 2 5 12 19 18 20 16 21 33 3 30 47 12,12,17,21,34,30

Example Use 17 as the pivot value and partition original array 3 12 34 CPSC 311 O. Burchan Bayazit – Fall 02 Example Use 17 as the pivot value and partition original array 3 12 34 22 4 3 17 32 28 25 27 34 43 82 2 5 12 19 18 20 16 21 33 3 30 47 12,12,17,21,34,30

Example After partitioning {12,0,3,4,3,2,12,5,16,3} 17 CPSC 311 O. Burchan Bayazit – Fall 02 Example After partitioning {12,0,3,4,3,2,12,5,16,3} 17 {34,22,32,28,43,82,25,27,34,19,18,20,33,33,21,30,47}

Stacks and Queries Stack operations: Push: Add an element to the stack CPSC 311 O. Burchan Bayazit – Fall 02 Stacks and Queries Both are dynamic sets Stacks: Last-In-First-Out Queries: First-In-First-Out Stack operations: Push: Add an element to the stack Pop: Remove the last element from the stack

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Initially Empty Stack

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (10) StackTop 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (2) StackTop 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (5) StackTop 5 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()5 5 StackTop 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (7) StackTop 7 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (12) StackTop 12 7 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (3) StackTop 3 12 7 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()3 3 StackTop 12 7 2 10

Example Stack Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()12 12 StackTop 7 2 10

Implementation Push(S,x) { top[S]=top[S]+1 S[top[S]]=x } CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Push(S,x) { top[S]=top[S]+1 S[top[S]]=x } Stack-Empty(S) { if (top[S]=0) { return TRUE }else { return FALSE } Pop(S,x) { if(Stack-Empty(S)) { return error “underflow” }else{ top[S]=top[S]-1 return S[top[S]+1] }

Implementation Initial Stack S= Top[S]=3 10 2 7 1 2 3 4 5 6 7 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Initial Stack 1 2 3 4 5 6 7 S= 10 2 7 Top[S]=3

Implementation Push(12) S= Top[S]=4 10 2 7 12 1 2 3 4 5 6 7 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Push(12) 1 2 3 4 5 6 7 S= 10 2 7 12 Top[S]=4

Implementation Push(3) S= Top[S]=5 10 2 7 12 3 1 2 3 4 5 6 7 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Push(3) 1 2 3 4 5 6 7 S= 10 2 7 12 3 Top[S]=5

Implementation Pop ()3 S= Top[S]=4 10 2 7 12 1 2 3 4 5 6 7 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Pop ()3 1 2 3 4 5 6 7 S= 10 2 7 12 Top[S]=4

Implementation Pop ()12 S= Top[S]=3 10 2 7 1 2 3 4 5 6 7 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Pop ()12 1 2 3 4 5 6 7 S= 10 2 7 Top[S]=3

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Initially Empty Queue Queue operations: Enqueue: Add an element to the queue Dequeue: Remove the last element from the queue

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(10) 10 Queue Head and Tail

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(2) 10 2 Head Tail

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(5) 10 2 5 Tail Head

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()10 2 5 Head Tail

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(7) 2 5 7 Tail Head

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(12) 2 5 7 12 Tail Head

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()2 5 7 12 3 Head Tail

Example Queue Operations CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()5 7 12 3 Head Tail

Implementation Enqueue(Q,x) { Q[tail[Q]]=x if (tail[Q]=length[Q]) { CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Enqueue(Q,x) { Q[tail[Q]]=x if (tail[Q]=length[Q]) { tail[Q]=1 }else { tail[Q]=tail[Q]+1 } Dequeue(Q) { x=Q[head[Q]]=x if (tail[Q]=length[Q]) { tail[Q]=1 }else { tail[Q]=tail[Q]+1 }

Implementation Initially Queue Q= 10 2 7 1 2 3 4 5 6 7 Tail[Q]=6 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Initially Queue 1 2 3 4 5 6 7 Q= 10 2 7 Tail[Q]=6 Head[Q]=4

Implementation Dequeue ()10 Q= 10 2 7 1 2 3 4 5 6 7 Head[Q]=5 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Dequeue ()10 1 2 3 4 5 6 7 Q= 10 2 7 Head[Q]=5 Tail[Q]=6

Implementation Enqueue (12) Q= 10 2 7 12 1 2 3 4 5 6 7 Head[Q]=5 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Enqueue (12) 1 2 3 4 5 6 7 Q= 10 2 7 12 Head[Q]=5 Tail[Q]=7

Implementation Enqueue (3) Q= 3 10 2 7 12 1 2 3 4 5 6 7 Tail[Q]=1 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Enqueue (3) 1 2 3 4 5 6 7 Q= 3 10 2 7 12 Tail[Q]=1 Head[Q]=5

Implementation Dequeue ()2 Q= 3 10 2 7 12 1 2 3 4 5 6 7 Tail[Q]=1 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Dequeue ()2 1 2 3 4 5 6 7 Q= 3 10 2 7 12 Tail[Q]=1 Head[Q]=6

Implementation Dequeue ()2 Q= 3 10 2 7 12 1 2 3 4 5 6 7 Tail[Q]=1 CPSC 311 O. Burchan Bayazit – Fall 02 Implementation Dequeue ()2 1 2 3 4 5 6 7 Q= 3 10 2 7 12 Tail[Q]=1 Head[Q]=7

CPSC 311 O. Burchan Bayazit – Fall 02 Linked list head 12 3 1 NULL

Double-Linked list head 12 3 1 prev next key CPSC 311 O. Burchan Bayazit – Fall 02 Double-Linked list prev next key head NULL 12 3 1 NULL

Double-Linked list Insert 20 head 20 12 3 1 CPSC 311 O. Burchan Bayazit – Fall 02 Double-Linked list Insert 20 head NULL 20 12 3 1 NULL

Double-Linked list Remove 3 head 20 12 1 CPSC 311 O. Burchan Bayazit – Fall 02 Double-Linked list Remove 3 head NULL 20 12 1 NULL

Implementation List-Search(L,k) { x=head[L] CPSC 311 O. Burchan Bayazit – Fall 02 Implementation List-Search(L,k) { x=head[L] while (x  NULL and key[x]  k) { x=next[x] } return x List-Delete(L,x) { if (prev[x]  NULL) { next[prev[x]]=next[x] else head[L]=next[x] } if (next[x] ]  NULL) { prev[next[x]]=prev[x] List-Insert(L,k) { next[x]=head[L] If (head[L]  NULL ) { prev[head[L]]=x } head[L]=x prev[x]=NULL

Rooted Trees Regular Tree Any number of children CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Regular Tree Any number of children

Rooted Trees Binary Tree At most 2 children CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Binary Tree At most 2 children

Rooted Trees Left-Child Right Sibling CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Left-Child Right Sibling