Definition of Big Oh
CSE 2011
Prof. J. Elder
-6-
Last Updated: 1/7/10 10:14 AM
Arithmetic Progression
The running time of
prefixAverages1 is
O(1 + 2 + + n)
7
6
5
The sum of the first n
integers is n(n + 1) / 2
4
3
There is a simple visual
proof of this fa
A List-Based Dictionary
A log file or audit trail is a dictionary implemented by means of an
unsorted sequence
We store the items of the dictionary in a sequence (based on a doublylinked list or array), in arbitrary order
Performance:
insert takes O(1
Lecture 4.
The Java Collections Framework
Chapters 6.3-6.4
CSE 2011
Prof. J. Elder
-1-
Last Updated: 18/01/12 9:35 AM
Outline
Introduction to the Java Collections Framework
Iterators
Interfaces
Abstract Classes
Classes
CSE 2011
Prof. J. Elder
-2-
Las
Performance of Hashing
In the worst case, searches, insertions and removals on a hash table
take O(n) time
The worst case occurs when all the keys inserted into the map collide
The load factor = n/N affects the performance of a hash table
For separate
Correctness of Dijkstras Algorithm: Upper Bound Property
Upper Bound Property:
Proof:
A valid path from s to v!
CSE 2011
Prof. J. Elder
- 206 -
Last Updated: 4/1/10 10:16 AM
Correctness of Dijkstras Algorithm
Optimal substructure
property!
Handled
CSE 201
Remove with Linear Probing
Suppose we receive a remove(44) Example:
message.
h(x) = x mod 13
What problem arises if we simply
Insert keys 18, 41, 22, 44,
remove the key = 44 entry?
59, 32, 31, 73, in this order
k h(k)
i
18
5
5
41
2
2
22
9
9
44
5
6
59
Example
CSE 2011
Prof. J. Elder
- 201 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 202 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 203 -
Last Updated: 4/1/10 10:16 AM
Djikstras Algorithm Cannot Handle Negative Ed
Map Methods with Separate Chaining
Delegate operations to a list-based map at each cell:
Algorithm get(k):
Output: The value associated with the key k in the map, or null if there is no
entry with key equal to k in the map
return A[h(k)].get(k)
CSE 2011
Dijkstras algorithm
Dijkstras algorithm can be viewed as greedy, since it always
chooses the lightest vertex in V S to add to S.
CSE 2011
Prof. J. Elder
- 196 -
Last Updated: 4/1/10 10:16 AM
Dijkstras algorithm: Analysis
Analysis:
Using minheap, queue ope
Hash Codes
Memory address:
We reinterpret the memory address of the key object as an integer
(default hash code of all Java objects)
Does not work well when copies of the same object may be stored at
different locations.
Integer cast:
We reinterpret
Correctness: Path relaxation property
CSE 2011
Prof. J. Elder
- 191 -
Last Updated: 4/1/10 10:16 AM
Correctness of DAG Shortest Path Algorithm
Because we process vertices in topologically sorted
order, edges of any path are relaxed in order of
appearance
Hash Tables
A hash table is a data structure that can be used to
make map operations faster.
While worst-case is still O(n), average case is typically
O(1).
CSE 2011
Prof. J. Elder
- 11 -
Last Updated: 06/02/12 10:23 PM
Applications of Hash Tables
data
Trees
Chapter 7
CSE 2011
Prof. J. Elder
-1-
Last Updated: 12-01-24 11:27 AM
Graph
a
b Node ~ city or computer
Edge ~ road or data cable
c
Undirected or Directed
A surprisingly large number of computational
problems can be expressed as graph problems.
CSE
Ordered Search Tables
Performance:
find takes O(log n) time, using binary search
insert takes O(n) time since in the worst case we have to shift n
items to make room for the new item
remove takes O(n) time since in the worst case we have to shift n
it
Midterm Review
CSE 2011
Prof. J. Elder
-1-
Last Updated: 1/7/10 10:14 AM
Asymptotic Analysis
CSE 2011
Prof. J. Elder
-2-
Last Updated: 1/7/10 10:14 AM
Seven Important Functions
Seven functions that often
appear in algorithm analysis:
Constant
1
Logarithmi
Selected column is placed
in the middle
CSE 2011
Prof. J. Elder
- 76 -
Last Updated: 06/02/12 10:23 PM
I will rearrange the cards
CSE 2011
Prof. J. Elder
- 77 -
Last Updated: 06/02/12 10:23 PM
Which
column?
left
CSE 2011
Prof. J. Elder
- 78 -
Last Updated
Selected column is placed
in the middle
CSE 2011
Prof. J. Elder
- 71 -
Last Updated: 06/02/12 10:23 PM
I will rearrange the cards
CSE 2011
Prof. J. Elder
- 72 -
Last Updated: 06/02/12 10:23 PM
Relax Loop Invariant:
I will remember the same
about each colu
Card Trick
A volunteer, please.
CSE 2011
Prof. J. Elder
- 66 -
Last Updated: 06/02/12 10:23 PM
Pick a Card
Done
CSE 2011
Prof. J. Elder
- 67 -
Last Updated: 06/02/12 10:23 PM
Loop Invariant:
The selected card is one
of these.
CSE 2011
Prof. J. Elder
- 68
Boundary Conditions
No progress
toward goal:
Loops Forever!
Another bug!
mid
key 25
3
5
6
13
18
CSE 2011
Prof. J. Elder
21
21
25
36
43
49
51
53
p +q
Select mid =
2
60
72
74
83
88
91
95
If key mid, If key > mid,
then key is in then key is in
right half.
Array-Based Representation of Binary Trees
nodes are stored in an array, using
a level-numbering scheme.
1
A
2
let rank(node) be defined as follows:
4
rank(root) = 1
B
D
56
3
7
if node is the left child of parent(node),
E
F
C
J
rank(node) = 2*rank(parent
Running Time
Binary search can interact poorly with the memory
hierarchy (i.e. caching), because of its random-access
nature.
It is common to abandon binary searching for linear
searching as soon as the size of the remaining span falls
below a small val
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Want a fast meal?
No
Yes
How about coffee?
Yes
Second Cup
CSE 2011
Prof. J. Elder
On expense a
Define Step
It is faster not to check if the middle element is the key.
Simply continue.
key 43
3
5
6
13
18
21
21
25
36
43
49
If key mid,
then key is in
left half.
CSE 2011
Prof. J. Elder
51
53
60
72
74
83
88
91
95
If key > mid,
then key is in
right hal
Position ADT
The Position ADT models the notion of place
within a data structure where a single object is
stored
It gives a unified view of diverse ways of storing
data, such as
a cell of an array
a node of a linked list
a node of a tree
Just one metho
Example
CSE 2011
Prof. J. Elder
- 186 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 187 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 188 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 189 -
Last
A Simple List-Based Map
We could implement a map using an
unsorted list
We store the entries of the map in a doubly-linked
list S, in arbitrary order
S supports the node list ADT (Section 6.2)
nodes/positions
header
9c
5a
6b
trailer
8d
entries
CSE 2011
P
BFS
s d=0
Found
Not Handled
Queue
d=1
b
a
d=3
i
l
k d=4
e
d
g
c
f
j
i
d=2
h
m
k
CSE 2011
Prof. J. Elder
d=4
l
- 146 -
d=3
Last Updated: 4/1/10 10:16 AM
BFS
s d=0
Found
Not Handled
Queue
d=1
b
a
d=3
e
d
l
k d=4
g
c
f
j
i
d=2
h
m
k
CSE 2011
Prof. J. Elder
d
Example array-based stack in Java
public class ArrayStack
implements Stack cfw_
public Object pop()
throws EmptyStackException cfw_
if isEmpty()
throw new EmptyStackException
(Empty stack: cannot pop);
Object temp = S[top];
/ facilitates garbage collectio