CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
Transcript of CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
![Page 1: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/1.jpg)
CSE 326: Data StructuresLecture #4
Alon Halevy
Spring Quarter 2001
![Page 2: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/2.jpg)
Agenda
• Today:– Finish complexity issues.
– Linked links (Read Ch 3; skip “radix sort”)
![Page 3: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/3.jpg)
Direct Proof of Recursive Fibonacci
• Recursive Fibonacci:int Fib(n)
if (n == 0 or n == 1) return 1
else return Fib(n - 1) + Fib(n - 2)
• Lower bound analysis• T(0), T(1) >= b
T(n) >= T(n - 1) + T(n - 2) + c if n > 1
• Analysislet be (1 + 5)/2 which satisfies 2 = + 1show by induction on n that T(n) >= bn - 1
![Page 4: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/4.jpg)
Direct Proof Continued
• Basis: T(0) b > b-1 and T(1) b = b0
• Inductive step: Assume T(m) bm - 1 for all m < nT(n) T(n - 1) + T(n - 2) + c bn-2 + bn-3 + c bn-3( + 1) + c = bn-32 + c bn-1
![Page 5: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/5.jpg)
Fibonacci Call Tree
5
3
1 2
0 1
4
2
0 1
3
1 2
0 1
![Page 6: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/6.jpg)
Learning from Analysis
• To avoid recursive calls– store all basis values in a table
– each time you calculate an answer, store it in the table
– before performing any calculation for a value n • check if a valid answer for n is in the table
• if so, return it
• Memoization– a form of dynamic programming
• How much time does memoized version take?
![Page 7: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/7.jpg)
Kinds of Analysis• So far we have considered worst case analysis• We may want to know how an algorithm performs
“on average”• Several distinct senses of “on average”
– amortized• average time per operation over a sequence of operations
– average case• average time over a random distribution of inputs
– expected case• average time for a randomized algorithm over different random
seeds for any input
![Page 8: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/8.jpg)
Amortized Analysis
• Consider any sequence of operations applied to a data structure– your worst enemy could choose the sequence!
• Some operations may be fast, others slow• Goal: show that the average time per operation is
still good
n
operationsn for timetotal
![Page 9: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/9.jpg)
Stack ADT
• Stack operations– push
– pop
– is_empty
• Stack property: if x is on the stack before y is pushed, then x will be popped after y is popped
What is biggest problem with an array implementation?
A
BCDEF
E D C B A
F
![Page 10: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/10.jpg)
Stretchy Stack Implementation
int data[];int maxsize;int top;
Push(e){if (top == maxsize){
temp = new int[2*maxsize];copy data into temp;deallocate data;data = temp; }
else { data[++top] = e; }
Best case Push = O( )
Worst case Push = O( )
![Page 11: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/11.jpg)
Stretchy Stack Amortized Analysis
• Consider sequence of n operationspush(3); push(19); push(2); …
• What is the max number of stretches?• What is the total time?
– let’s say a regular push takes time a, and stretching an array containing k elements takes time kb, for some constants a and b.
• Amortized = (an+b(2n-1))/n = a+2b-(1/n)= O(1)
log n
)12()12(
2)...8421(
log1
log
nbanban
bannban
n
n
oi
i
![Page 12: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/12.jpg)
Average Case Analysis
• Attempt to capture the notion of “typical” performance
• Imagine inputs are drawn from some random distribution– Ideally this distribution is a mathematical model of the
real world
– In practice usually is much more simple – e.g., a uniform random distribution
![Page 13: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/13.jpg)
Example: Find a Red Card
• Input: a deck of n cards, half red and half black• Algorithm: turn over cards (from top of deck) one
at a time until a red card is found. How many cards will be turned over?– Best case =
– Worst case =
– Average case: over all possible inputs (ways of shuffling deck)
![Page 14: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/14.jpg)
Summary
• Asymptotic Analysis – scaling with size of input• Upper bound O, Lower bound • O(1) or O(log n) great• O(2n) almost never okay• Worst case most important – strong guarantee• Other kinds of analysis sometimes useful:
– amortized
– average case
![Page 15: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/15.jpg)
List ADT• List properties
– Ai precedes Ai+1 for 1 i < n– Ai succeeds Ai-1 for 1 < i n– Size 0 list is defined to be the empty list
• Key operations– Find(item) = position– Find_Kth(integer) = item– Insert(item, position)– Delete(position)– Next(position) = position
• What are some possible data structures?
( A1 A2 … An-1 An )length = n
![Page 16: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/16.jpg)
Implementations of Linked Lists
a b c
(a b c)(optionalheader)
L
W 1 I S E S
1 7 92 3 4 5 6 8 10
Array:
Linked list:
H A Y
Can we apply binary search to an array representation?
![Page 17: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/17.jpg)
Linked List vs. Array
Find(item) = position
Find_Kth(integer)=item
Find_Kth(1)=item
Insert(item, position)
Insert(item)
Delete(position)
Next(position) = position
linked list array sorted array
![Page 18: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/18.jpg)
Tradeoffs• For what kinds of applications is a linked list best?
• Examples for an unsorted array?
• Examples for a sorted array?
![Page 19: CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.](https://reader036.fdocuments.in/reader036/viewer/2022080902/56649cfa5503460f949cbb75/html5/thumbnails/19.jpg)
Implementing in C++
Create separate classes for– Node
– List (contains a pointer to the first node)
– List Iterator (specifies a position in a list; basically, just a pointer to a node)
Pro: syntactically distinguishes uses of node pointers
Con: a lot of verbage! Also, is a position in a list really distinct from a list?
a b c
(a b c)(optionalheader)
L