Heaps
Create a Hash Function
will not need to design a hash function on exam
Proofs
Prove that an n-element heap has height h = log2 n . You
Searching Hash Table
need to prove this for any given n. Showing h = 3 for n = 8 is
not a proof. (5 points)
By the
LI
UJOI
V1AA
I Ve
tag e
c vi -,5
i
o
clr,
Pt( K
f9t
\Pe ( L
hi 1101,1
e r)9.<
%ivri ire.)
ter f-C)
7
V ;AI
411
\ qv c
tt ,
0\ a
t i' 'ir
not rr
k
V'-, r ), ( -.r 5
fa
145
~
l41
qt.\
1
61, rte
\'r
11?
a
Put v.), I
" LiS1
11, e
I
I
57-s
.a
otdf - n
G dj
c
2/27/20l1
CSE 331 Homework 4
1. Prove that an n-element heap has height h = log2 n . You need to
prove this for any given n. Showing h = 3 for n = 8 is not a proof. (5
points)
Theorem: an n-element heap has height h=log2 n
h is the height of an n-element
CSE331 final exam review
20-25%: all the topics before the first exam (computational time complexity, sorting,
search tree);
20-25%: all the topics between the first and the second exam (heap, hashing, graph
algorithms);
50-60%: everything else (MST, Huff
Fibonacci numbers
Dynamic Programming
F(1) = F(0) = 1
F(n) = F(n-1) + F(n-2)
Algorithm design technique 2
A problem with some (trivial) algorithms:
Computing the same thing over and over
again.
Fib( n:integer)
cfw_
if ( n=0 or n=1 )
return 1
else
ret
Motivation
Hashing
So far we have seen structures that
can support the following options
Hash Functions
Collision resolution
Insert
Delete
Search
All of these take times bounded
around O(N) in the worst case
What if all we care about is insert and
Heaps
Suppose
Uses for heaps/priority queues
Youve been asked to write a notification
system for the web
Users enter events
System sends email at certain date/times
Millions of messages a day
Millions of new messages in a day
Heapsort
What data st
Amortized Analysis
Splay Trees
Amortization
Put money aside, for graduate payment of a debt
(original meaning)
Average the running time of operations in a sequence
over the entire sequence
Amortized Analysis
Splaying the tree
life time total complex
-Sort
QUICK
MERGE&
External
INSER
T&
Bubbl
e
Wors n2
Nlog n n2
t
Best N log
n
Ave. n
n2
BST
Ave.
Worst
Search log n
n
Insert log n
n
Delete log n
n
H=log(n)
AVL
Ave.
+1
TREE
Worst
#node of
Search log n
Full
BST=2
-1
Insert log n
Delete log n
(k+1)
Def. fo
-Sort
QUICK
MERGE&
External
INSER
T&
Bubbl
e
Wors n2
Nlog n n2
t
Best N log
n
Ave. n
n2
BST
Ave.
Worst
Search log n
n
Insert log n
n
Delete log n
n
H=log(n)
AVL
Ave.
+1
TREE
Worst
#node of
Search log n
Full
BST=2
-1
Insert log n
Delete log n
(k+1)
Def. fo
Divide and Conquer
Divide and Conquer Examples
Divide: Divide the problem into smaller
subproblems
Solve the subproblems recursively.
If subproblem is small enough, solve it in a
straightforward manner (base case)
Conquer: Combine the solution of the
Introduction
CSE 331 Algorithms and Data
Structures
Introduction to the class.
What is an algorithm?
What is a data structure?
Li Xiao (Instructor)
1128 EB, lxiao@cse.msu.edu
Office Hours: TuTh 11:40am-12:40pm
TA (general assignments boss)
James D
Heaps
Suppose
Uses for heaps/priority queues
Youve been asked to write a notification
system for the web
Users enter events
System sends email at certain date/times
Millions of messages a day
Millions of new messages in a day
Heapsort
What data st
AVL Trees
Good Trees and Bad Trees
Balance
Checking/Tracking balance
Adelson-Velskii and Landis (AVL) tree
Insert
Delete
d = (log n)
d = (n)
1
Balancing Search Tree
2
How to Avoid Bad Trees?
AVL Tree
for every node in the tree, the height of the le
Sorting II
QuickSort
QuickSort
General lower bound for sorting
External sorting
The fastest known sorting algorithm
Divide-and-conquer recursive algorithm
Basic algorithm to sort an array S:
1. If |S|=0 or 1, then return
2. Pick any v in S, which is ca
Greedy Algorithms
Greedy Algorithms
Used to solve optimization problems
Multiple solutions exist, need to find the
best one
Make the choice that looks best at the
moment
A locally optimal choice at each step in
expectation of achieving global optimum
AVL Trees
Good Trees and Bad Trees
Balance
Checking/Tracking balance
Adelson-Velskii and Landis (AVL) tree
Insert
Delete
d = (log n)
d = (n)
1
Balancing Search Tree
2
How to Avoid Bad Trees?
AVL Tree
for every node in the tree, the height of the le
Trees
(Directed) Tree
2
Trees
Binary tree and Binary tree traversals
Expression trees
Infix, Postfix, Prefix notation
Binary Search Trees
Operations supported
Complexity
9
3
6
2
6
7 8
8
4
7
1
3
4
2
1
6
3
0
9
5
8
1
2
Definition of Trees
Notation about Tree
Very basic problem: Maps
Graphs and Graph Algorithms
How does Mapquest work?
What is a graph
Uses of graphs
Topological search
Depth first search
Breath first search
1
2
Graphs
Extensions
Graph G=(V,E)
Do edges have a direction?
V a set of vertices
Dir
BubbleSort
Sorting 1 and Analysis
procedure BubbleSort( var A : InputArray; N : int);
var
j, P : integer;
begin
for P := N to 2 by -1 do begin
for j := 1 to P - 1 do begin
if A[ j ] > A[ j + 1 ] then
Swap( A[ j ], A[ j + 1 ] );
end; cfw_for
end; cfw_for
e
Linked Lists, Queues, Stacks
The linked list
Youve probably seen most of this, so lets
breath for a while
Chapter 3
This is an data structure
Dynamically allocated nodes
Each node has:
Data (payload)
Pointer to next node
1
Example
2
What do we keep
B+ Trees
External Storage
External Storage and Indices
Welcome to the world of database!
Another balanced tree approach
What are we storing?
Balance comes from allowing an n-ary
structure rather than binary
Data records
clustered index
<indexing_f
BubbleSort
Sorting 1 and Analysis
procedure BubbleSort( var A : InputArray; N : int);
var
j, P : integer;
begin
for P := N to 2 by -1 do begin
for j := 1 to P - 1 do begin
if A[ j ] > A[ j + 1 ] then
Swap( A[ j ], A[ j + 1 ] );
end; cfw_for
end; cfw_for
e
4/20/2011 CSE 331 HW 8
CSE331 homework 8 (Greedy algorithm and dynamic programming)
Due by the class on 4/28/2011
1. (10 pts) We used dynamic programming to solve the longest common
subsequence (LCS) problem in class. This algorithm needs to use a 2 dimen
A 7M
1POPOMmlailar.
March 2, 2011
CSE 331
Homework 5
1. Do textbook problem 5.1 on page 208. h(x)=x mod 10. Read 5.4.2 about quadratic probing.
a)
1
3
4
9
4371
1323,6173
4344
4199, 9679, 1989
b)
7
c) h(k, i) = (h(k)+i+i^2) Net" 0-0kgy t CZ)
1989
"
Ak,c
1A
Dynamic Programming
An algorithm design technique
Decompose problems in different ways Different divisions lead to different solutions One is optimal Systematically records answers to subproblems so they are not recomputed.
1
Fibonacci numbers