Big-Oh Notation
2014 IOI Camp 1
Robert Spencer
December 11, 2013
Robert Spencer
Big-Oh Notation
1/13
Introduction
How long will the following code take to run?
Robert Spencer
Big-Oh Notation
2/13
Introduction
How long will the following code take to run?
CS 332: Algorithms
NP Completeness Continued:
Reductions
David Luebke
1
Review: P and NP
What do we mean when we say a problem
is in P?
What do we mean when we say a problem
is in NP?
What is the relation between P and NP?
David Luebke
2
Review: P and
CS 332: Algorithms
NP Completeness Continued:
Reductions
David Luebke
1
Homework 7
Check the web later today
Change in grading policy: drop lowest hw
David Luebke
2
Review: P And NP Summary
P = set of problems that can be solved in
polynomial time
NP
CS 332: Algorithms
NP Completeness Continued
David Luebke
1
Homework 5
Extension: due midnight Monday 22 April
David Luebke
2
Review: Tractibility
Some problems are undecidable: no computer
can solve them
E.g., Turings Halting Problem
We dont care abo
CS 332: Algorithms
Greedy Algorithms
David Luebke
1
Review: Dynamic Programming
Dynamic programming is another strategy for
designing algorithms
Use when problem breaks down into recurring
small subproblems
David Luebke
2
Review: Optimal Substructure of
CS 332 - Algorithms
Dynamic programming
0-1 Knapsack problem
09/26/12
1
Review: Dynamic programming
s
s
s
s
s
DP is a method for solving certain kind of
problems
DP can be applied when the solution of a
problem includes solutions to subproblems
We need to
CS 332 - Algorithms
Dynamic programming
Longest Common Subsequence
09/26/12
1
Dynamic programming
s
It is used, when the solution can be
recursively described in terms of solutions
to subproblems (optimal substructure)
s
Algorithm finds solutions to subpr
CS 332: Algorithms
Dynamic Programming
David Luebke
1
Review: Amortized Analysis
To illustrate amortized analysis we examined
dynamic tables
1. Init table size m = 1
2. Insert elements until number n > m
3. Generate new table of size 2m
4. Reinsert old e
CS 332: Algorithms
Amortized Analysis
David Luebke
1
Review: Running Time of
Kruskals Algorithm
Expensive operations:
Sort edges: O(E lg E)
O(V) MakeSet()s
O(E) FindSet()s
O(V) Union()s
Upshot:
Comes down to efficiency of disjoint-set
operations, p
CS 332: Algorithms
Review of MST Algorithms
Disjoint-Set Union
Amortized Analysis
David Luebke
1
Review: MST Algorithms
In a connected, weighted, undirected graph,
will the edge with the lowest weight be in the
MST? Why or why not?
Yes:
If T is MST of
CS 332: Algorithms
Dijkstras Algorithm
Disjoint-Set Union
David Luebke
1
Homework 5
Check web page this afternoon
David Luebke
2
Review: Bellman-Ford Algorithm
BellmanFord()
for each v V
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v) E
Relax(u,v
CS 332: Algorithms
S-S Shortest Path: Dijkstras Algorithm
Disjoint-Set Union
Amortized Analysis
David Luebke
1
Review:
Single-Source Shortest Path
Problem: given a weighted directed graph G,
find the minimum-weight path from a given
source vertex s to an
CS 332: Algorithms
Single-Source Shortest Path
David Luebke
1
Review: Prims Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] =
CS 332: Algorithms
Topological Sort
Minimum Spanning Trees
David Luebke
1
Review: Breadth-First Search
BFS(G, s) cfw_
initialize vertices;
Q = cfw_s;
/ Q is a queue (duh); initialize to s
while (Q not empty) cfw_
u = RemoveTop(Q);
for each v u->adj cfw_
i
CS 332: Algorithms
Graph Algorithms
David Luebke
1
Review: Depth-First Search
Depth-first search is another strategy for
exploring a graph
Explore deeper in the graph whenever possible
Edges are explored out of the most recently
discovered vertex v tha
CS 332: Algorithms
Graph Algorithms
David Luebke
1
Administrative
Test postponed to Friday
Homework:
Turned in last night by midnight: full credit
Turned in tonight by midnight: 1 day late, 10% off
Turned in tomorrow night: 2 days late, 30% off
Extr
CS 332: Algorithms
Review for Final
David Luebke
1
Final Exam
Coverage: the whole semester
Goal: doable in 2 hours
Cheat sheet: you are allowed two 811 sheets,
both sides
David Luebke
2
Final Exam: Study Tips
Study tips:
Study each lecture
Study the
CS 332: Algorithms
Introduction
Proof By Induction
Asymptotic notation
David Luebke
1
The Course
Purpose: a rigorous introduction to the design
and analysis of algorithms
Not a lab or programming course
Not a math course, either
Textbook: Introduction t
CS 332: Algorithms
Skip Lists
David Luebke
1
Administration
Hand back homework 3
Hand back exam 1
Go over exam
David Luebke
2
Review: Red-Black Trees
Red-black trees:
Binary search trees augmented with node color
Operations designed to guarantee tha
CS 332: Algorithms
Red-Black Trees
David Luebke
1
Review: Red-Black Trees
Red-black trees:
Binary search trees augmented with node color
Operations designed to guarantee that the height
h = O(lg n)
We described the properties of red-black trees
We pr
CS 332: Algorithms
Red-Black Trees
David Luebke
1
Review: Binary Search Trees
Binary Search Trees (BSTs) are an important
data structure for dynamic sets
In addition to satellite data, eleements have:
key: an identifying field inducing a total ordering
CS 332: Algorithms
Go over exam
Binary Search Trees
David Luebke
1
Exam
Hand back, go over exam
David Luebke
2
Review: Dynamic Sets
Next few lectures will focus on data structures
rather than straight algorithms
In particular, structures for dynamic se
CS 332: Algorithms
Review for Exam 1
David Luebke
1
Administrative
Reminder: homework 3 due today
Reminder: Exam 1 Wednesday, Feb 13
1 8.5x11 crib sheet allowed
Both sides, mechanical reproduction okay
You will turn it in with the exam
David Luebke
2
CS 332: Algorithms
Medians and Order Statistics
Structures for Dynamic Sets
David Luebke
1
Homework 3
On the web shortly
Due Wednesday at the beginning of class (test)
David Luebke
2
Review: Radix Sort
Radix sort:
Assumption: input has d digits rangin
CS 332: Algorithms
Linear-Time Sorting Continued
Medians and Order Statistics
David Luebke
1
Review: Comparison Sorts
Comparison sorts: O(n lg n) at best
Model sort with decision tree
Path down tree = execution trace of algorithm
Leaves of tree = poss
CS 332: Algorithms
Linear-Time Sorting Algorithms
David Luebke
1
Sorting So Far
q
Insertion sort:
s
s
s
s
s
s
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
O(n2) worst case
O(n2) average (equally-likely inputs) ca
CS 332: Algorithms
Quicksort
David Luebke
1
Review: Analyzing Quicksort
q
What will be the worst case for the algorithm?
s
q
What will be the best case for the algorithm?
s
q
Partition is balanced
Which is more likely?
s
q
Partition is always unbalanced
T
CS 332: Algorithms
Quicksort
David Luebke
1
Homework 2
Assigned today, due next Wednesday
q Will be on web page shortly after class
q Go over now
q
David Luebke
2
Review: Quicksort
Sorts in place
q Sorts O(n lg n) in the average case
q Sorts O(n2) in the
CS 332: Algorithms
Heapsort
Priority Queues
Quicksort
David Luebke
1
Review: Heaps
q
A heap is a complete binary tree, usually
represented as an array:
16
4
10
14
2
7
8
3
1
A = 16 14 10 8
David Luebke
9
7
9
3
2
4
2
1
Review: Heaps
q
To represent a heap as
CS 332: Algorithms
Introduction to heapsort
David Luebke
1
Review: The Master Theorem
q
Given: a divide and conquer algorithm
s
s
q
An algorithm that divides the problem of size n
into a subproblems, each of size n/b
Let the cost of each stage (i.e., the