Download presentation
Presentation is loading. Please wait.
1
CPSC 311 Section 502 Analysis of Algorithm
Fall 2002 Department of Computer Science Texas A&M University
2
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.
3
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 return Randomized-Select(A, start, pivot-1, i) 6. else return Randomized-Select(A, pivot+1, end, i - (pivot - start + 1))
4
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.
5
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))
6
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.
7
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 cn/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)
8
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}
9
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
10
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
11
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
12
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
13
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}
14
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
15
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Initially Empty Stack
16
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (10) StackTop 10
17
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (2) StackTop 2 10
18
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (5) StackTop 5 2 10
19
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()5 5 StackTop 2 10
20
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (7) StackTop 7 2 10
21
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (12) StackTop 12 7 2 10
22
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Push (3) StackTop 3 12 7 2 10
23
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()3 3 StackTop 12 7 2 10
24
Example Stack Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Stack Operations Pop ()12 12 StackTop 7 2 10
25
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] }
26
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
27
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
28
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
29
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
30
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
31
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
32
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(10) 10 Queue Head and Tail
33
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(2) 10 2 Head Tail
34
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(5) 10 2 5 Tail Head
35
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()10 2 5 Head Tail
36
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(7) 2 5 7 Tail Head
37
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Enqueue(12) 2 5 7 12 Tail Head
38
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()2 5 7 12 3 Head Tail
39
Example Queue Operations
CPSC 311 O. Burchan Bayazit – Fall 02 Example Queue Operations Dequeue ()5 7 12 3 Head Tail
40
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 }
41
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
42
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
43
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
44
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
45
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
46
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
47
CPSC 311 O. Burchan Bayazit – Fall 02
Linked list head 12 3 1 NULL
48
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
49
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
50
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
51
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
52
Rooted Trees Regular Tree Any number of children
CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Regular Tree Any number of children
53
Rooted Trees Binary Tree At most 2 children
CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Binary Tree At most 2 children
54
Rooted Trees Left-Child Right Sibling
CPSC 311 O. Burchan Bayazit – Fall 02 Rooted Trees Left-Child Right Sibling
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.