Professional Documents
Culture Documents
O Analysis of Methods and Data Structures Reasonable vs. Unreasonable Algorithms Using O Analysis in Design
O Analysis of Methods and Data Structures Reasonable vs. Unreasonable Algorithms Using O Analysis in Design
http://www.coursesurvey.gatech.edu
O() Analysis of Methods
and Data Structures
The Scenario
MAX is 30
ArrayType definesa array[1..MAX] of Num
• Simple/Linear Search:
– If there is no ordering in the data structure
– If the ordering is not applicable
• Binary Search:
– If the data is ordered or sorted
– Requires non-linear access to the elements
Simple Search
?
Binary Tree N N
BST N N
F&B BST N
Balanced Binary Search Trees
14
11 42
7 58
Binary Search Trees
If a binary search tree is not full or balanced, then in
the worst case it takes on the structure and
characteristics of a sorted linked list.
7
11
14
42
58
Example: Linked List
Head
5 19 35 42 \\
83 Not Found!
Simple/Linear Search Algorithm
cur <- head
loop
exitif(cur = NIL) OR (cur^.data = target)
cur <- cur^.next
endloop
94
If I have to watch
3 cur 36 one more of these
I think I’m going
to die.
LB
22 14 67 9
Big-O of Simple Search
34
25 45
21 29 41 52
Binary Search Example
7 12 42 59 71 86 104 212
Looking for 89
Binary Search Example
7 12 42 59 71 86 104 212
Looking for 89
Binary Search Example
7 12 42 59 71 86 104 212
Looking for 89
Binary Search Example
7 12 42 59 71 86 104 212
3 = Log(8)
Binary Search Big-O
What?
N Searches
2 1
4 2
8 3
16 4
32 5
64 6
128 7
256 8
512 9
LB
What?
log2(N) Searches
log2(2) = 1
log2(4) = 2
log2(8) = 3
log2(16) = 4
log2(32) = 5
log2(64) = 6
log2(128) = 7
log2(256) = 8
log2(512) = 9
LB
CS Notation
lg(N) Searches
lg(2) = 1
lg(4) = 2
lg(8) = 3
lg(16) = 4
lg(32) = 5
lg(64) = 6
lg(128) = 7
lg(256) = 8
lg(512) = 9
LB
Recall
log2 N = k • log10 N
k = 0.30103...
5 12 35 77 101
Insert 29
Big-O of Shuffle
5 12 35 77 101
3 41
2 7 35 98
Full and Balanced BST Insert
12
3 41
2 7 35 98
4
Big-O of Full & Balanced BST Insert
Sequential, so add:
Total work is O(Log N + 1) = O(Log N)
LB
Comparing Data Structures and
Methods
Data Structure Traverse Search Insert
Unsorted L List N N 1
Sorted L List N N N
Unsorted Array N N 1
Sorted Array N Log N N
Binary Tree N N 1
BST N N
F&B BST N Log N Log N
LB
• Bubblesort
– Brute-force method of sorting
– Loop inside of a loop
• Mergesort
– Divide and conquer approach
– Recursively call, splitting in half
– Merge sorted halves together
Bubblesort Review
1 2 3 4 5 6
77 42 35 12 101 5
Bubblesort Review
1 2 3 4 5 6
42 35 12 77 5 101
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
N-1 to_do
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Analysis of Bubblesort
Bubblesort Complexity
Graphically
2N-1
N-1
N-1 2N-1
O(N2) Runtime Example
98 23 45 14 6 67 33 42
Log N
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
Log N
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Analysis of
Phase I Mergesort
– Divide the list of N numbers into two lists of N/2
numbers
– Divide those lists in half until each list is size 1
Log N steps for this stage.
Phase II
– Build sorted lists from the decomposed lists
– Merge pairs of lists, doubling the size of the
sorted lists each time
Log N steps for this stage.
Analysis of the Merging
98 23 45 14 6 67 33 42
Merge Merge Merge Merge
23 98 14 45 6 67 33 42
Merge Merge
14 23 45 98 6 33 42 67
Merge
6 14 23 33 42 45 67 98
Mergesort
Complexity
Each of the N numerical values is compared or
copied during each pass
– The total work for each pass is O(N).
– There are a total of Log N passes
2 seconds
Summary
• You now have the O() for basic methods on
varied data structures.
• You can combine these in more complex
situations.
For N = 256
N5 = 2565 = 1,100,000,000,000
• It would take
– 585 years to grow enough grain
just for the 64th day
– Over a thousand years to fulfill
the peasant’s request!
LB
A B C
Move 2 Move 3
Move 4 Move 5
Move 6 Move 7
Towers of Hanoi -
Complexity
For 3 rings we have 7 operations.
For N = 64
2N = 264 = 18,450,000,000,000,000,000
Matching tiles
Tiling a 5x5 Area
25 available
tiles remaining
Tiling a 5x5 Area
24 available
tiles remaining
Tiling a 5x5 Area
23 available
tiles remaining
Tiling a 5x5 Area
22 available
tiles remaining
Tiling a 5x5 Area
2 available
tiles remaining
Analysis of the Bounded Tiling
Problem
Tile a 5 by 5 area (N = 25 tiles)
1st location: 25 choices
2nd location: 24 choices
And so on…
For N = 25
25! = 15,500,000,000,000,000,000,000,000
Sub-linear O(Log N)
Polynomial
Linear O(N)
Nearly linear O(N Log N)
Quadratic O(N2)
Exponential O(2N)
O(N!)
O(NN)
Reasonable vs. Unreasonable
Reasonable algorithms have polynomial factors
– O (Log N)
– O (N)
– O (NK) where K is a constant
Reasonable algorithms
• May be usable depending upon the input size
Unreasonable algorithms
• Are impractical and useful to theorists
• Demonstrate need for approximate solutions
1030 NN
1025 2N
1020
Runtime
1015
trillion N5
billion
Reasonable
million
1000 N
100
Don’t Care!
10
2 4 8 16 32 64 128 256 512 1024
Size of Input (N)
Summary
Conflict Alert