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
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
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
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
-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
ICAT 2
InsertSort vs MergeSort:
InsertSorts run time analysis is : (n2) and MergeSorts run time is (nlogn)
Question: Insert Sort is faster for substantially small n than Merge Sort.
Explain why?
ANSWER : In d2lGo to Lectures module, select Lecture 2 selec
ICAT 1
Iterating Insert Sort in Class.
Given an array 8,2,4,9,3,6
For Answer: Look into Lecture 2
This question was also asked as one of the hw questions. Given an array and the pseudocode for
InsertSort, sort the given array.
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
Welcome to CSE 331
Algorithms and Data Structures
Introduction
2
Course Description
Fundamental data structures and many associated algorithms.
Emphasis will be placed on matching the appropriate data structures
and algorithms to application problems.
Ana
HOMEWORK 1
100 points
DUE DATE: January 26th 9:00pm.
Homework 1 Deliverables:
Please use a text editor and type your answers using only one text file, convert that text file to
PDF upon submission. Do not submit multiple files (Example: Hw1.pdf should con
ICAT 6 for class
Using the quicksort implementation(the
algorithm) that we covered in this class,
determine the running time (Big O will do )of
quicksort for:
A.sorted input
B.reverse-ordered input
C.random input
Hint: Remember what approach we used to pi
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
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
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
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, [email protected]
Office Hours: TuTh 11:40am-12:40pm
TA (general assignments boss)
James D
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
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
Question for the class: ICAT5
0
1
2
3
4
5
6
7
8
9
8
1
4
9
6
3
5
2
7
0
Find the Pivot using
median3 function,
re arrange the array as the
algorithm suggests
and write down your final
array after this call
executes.
Answer: ICAT 5
Pivot is = 6 in this examp