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
O Analysis of Methods and Data Structures Reasonable vs. Unreasonable Algorithms Using O Analysis in Design
The Scenario
Weve talked about data structures and methods
to act on these structures
Linked lists, arrays, trees
Inserting, Deleting, Searching, Traversal,
Sorting, etc.
Now that we know about O() notation, lets
discuss how each of these methods perform on
these data structures!
Traversals
Traversals involve visiting every
element in a collection.
Because we must visit every
node, a traversal must be O(N)
for any data structure.
If we visit less than N
elements, then it is not a
traversal.
LB
Traverse
N
N
N
N
N
Simple Search
Worst case: the element to be found is the Nth
element examined.
Simple search must be used for:
Sorted or unsorted linked lists
Unsorted array
Binary tree
Binary Search Tree if it is not full and balanced
LB
Traverse
N
N
N
N
N
N
Search
N
N
N
N
42
58
Head
5
19
83 Not Found!
35
42
\\
Left ???
Right ???
Find 9
94
22
36
cur
14
67
If I have to watch
one more of these
I think Im going
to die.
LB
Binary Search
We may perform binary search on
Sorted arrays
Full and balanced binary search trees
Tosses out the elements at each
comparison.
25
21
45
29
41
52
12
42
59
71
Looking for 89
86
104
212
12
42
59
71
Looking for 89
86
104
212
12
42
59
71
Looking for 89
86
104
212
12
42
59
71
86
104
212
LB
What?
N
2
4
8
16
32
64
128
256
512
Searches
1
2
3
4
5
6
7
8
9
LB
What?
log2(N)
log2(2)
log2(4)
log2(8)
log2(16)
log2(32)
log2(64)
log2(128)
log2(256)
log2(512)
=
=
=
=
=
=
=
=
=
Searches
1
2
3
4
5
6
7
8
9
LB
CS Notation
lg(N)
lg(2)
lg(4)
lg(8)
lg(16)
lg(32)
lg(64)
lg(128)
lg(256)
lg(512)
=
=
=
=
=
=
=
=
=
Searches
1
2
3
4
5
6
7
8
9
LB
Recall
log2 N = k log10 N
k = 0.30103...
So: O(lg N) = O(log N)
LB
Traverse
N
N
N
N
N
N
Log N
Search
N
N
N
Log N
N
Insertion
Inserting an element requires two steps:
Find the right location
Perform the instructions to insert
If the data structure in question is unsorted,
then it is O(1)
Simply insert to the front
Simply insert to end in the case of an array
There is no work to find the right spot and
only constant work to actually insert.
LB
Traverse
N
N
N
N
N
N
Log N
Search
N
N
N
Log N
N
Insert
1
1
1
Shuffling Elements
12
35
Insert 29
77
101
Big-O of Shuffle
12
35
77
101
LB
Traverse
N
N
N
N
N
N
Log N
Search
N
N
N
Log N
N
Insert
1
N
1
N
1
35
98
35
98
LB
Traverse
N
N
N
N
N
N
Log N
Search
N
N
N
Log N
N
Log N
Insert
1
N
1
N
1
LB
LB
Traverse
N
N
N
N
N
N
Log N
Search
N
N
N
Log N
N
N
Log N
Insert
1
N
1
N
1
Bubblesort Review
Bubblesort works by comparing and swapping
values in a list
77
42
35
12
101
Bubblesort Review
Bubblesort works by comparing and swapping
values in a list
42
35
12
77
101
N-1
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])
endif
to_do
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Analysis of Bubblesort
How many comparisons in the inner loop?
to_do goes from N-1 down to 1, thus
(N-1) + (N-2) + (N-3) + ... + 2 + 1
Average: N/2 for each pass of the
outer loop.
How many passes of the outer loop?
N1
LB
Bubblesort Complexity
Look at the relationship between the two loops:
Inner is nested inside outer
Inner will be executed for each iteration of
outer
Therefore the complexity is:
LB
Graphically
2N-1
N-1
N-1
2N-1
Mergesort
98 23 45 14
98 23 45 14
Log N
98 23
98
23
23 98
Log N
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6 33 42 67
6 14 23 33 42 45 67 98
Phase I
Analysis of
Mergesort
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.
23
45
14
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
Therefore the complexity is:
Merging
Summary
You now have the O() for basic methods on
varied data structures.
You can combine these in more complex
situations.
Break algorithm down into known pieces
Identify relationships between pieces
Sequential is additive
Nested (loop/recursion) is multiplicative
Drop constants
Keep only dominant factor for each variable
Questions?
An O(N5) Example
For N = 256
N5 = 2565 = 1,100,000,000,000
If we had a computer that could execute a million
instructions per second
1,100,000 seconds = 12.7 days to complete
But it could get worse
Pieces of Grain
2
4
8
16
2N
9,223,000,000,000,000,000
18,450,000,000,000,000,000
LB
Original State
Move 1
Move 2
Move 3
Move 4
Move 5
Move 6
Move 7
1 = O(2N)
Matching tiles
25! = 15,500,000,000,000,000,000,000,000
If we could place a million tiles per second
It would take 470 billion years to complete
Why not a faster computer?
A Faster Computer
If we had a computer that could execute a trillion
instructions per second (a million times faster
than our MIPS computer)
5x5 tiling problem would take 470,000 years
64-ring Tower of Hanoi problem would take 213
days
Why not an even faster computer!
Polynomial
O(Log N)
O(N)
O(N Log N)
O(N2)
Exponential
O(N!)
O(NN)
O(2N)
Unreasonable
Runtime
35
NN
2N
N5
Reasonable
N
Dont Care!
2 4 8 16 32 64 128 256 512 1024
Size of Input (N)
Summary
Reasonable algorithms feature
polynomial factors in their O() and may
be usable depending upon input size.
Unreasonable algorithms feature
exponential factors in their O() and
have no practical utility.
Questions?
Conflict Alert
Coast, add, delete
Problem Statement
What data structure should be used to store the
aircraft records for this system?
Normal operations conducted are:
Data Entry: adding new aircraft entering the
area
Radar Update: input from the antenna
Coast: global traversal to verify that all aircraft
have been updated [coast for 5 cycles, then
drop]
Query: controller requesting data about a
specific aircraft by location
Conflict Analysis: make sure no two aircraft
are too close together
Algorithm
Freq
Insert
N*Search
Traverse
Search
Traverse*Search
15
12
60
1
12
#1
#2
#3
#4
#5
LLU
1
N^2
N
N
N^2
LLS
N
N^2
N
N
N^2
AU
1
N^2
N
N
N^2
AS
N
NlogN
N
LogN
NlogN
BT
1
N^2
N
N
N^2
F/B BST
LogN
NlogN
N
LogN
NlogN
Questions?