CS 535: LECTURE II
Adrian Dumitrescu
ALGORITHM DESIGN AND ANALYSIS
PROBLEM EXAMPLES
c Adrian Dumitrescu, 2012
1
Finding the median
Median of n numbers: say n = 5; if x1 x2 x3 x4 x5 , the
median of these 5 numbers is x3 .
Say n = 4; if x1 x2 x3 x4 , the me
CS 535: LECTURE XIX
Adrian Dumitrescu
HASHING
c Adrian Dumitrescu, 2012
1
Hashing
Many applications of dynamic sets only require the operations
search, insert, delete; for instance, we dont need to list the
keys in order, etc. These 3 operations above are
CS 535: LECTURE XV
Adrian Dumitrescu
MINIMUM SPANNING TREES
c Adrian Dumitrescu, 2012
1
Minimum Spanning Tree Problem
Given an undirected graph G = (V, E), with a non-negative cost
(weight) function c : E R0 , nd a minimum cost subset of edges
X E that co
CS 535: LECTURE XII
Adrian Dumitrescu
RED-BLACK TREES
c Adrian Dumitrescu, 2012
1
Red-black trees
A red-black tree (RB-tree) is a b.s.t. T augmented with NIL nodes
that satises the following red-black properties:
1.
2.
3.
4.
Every node of T is either red
CS 535: LECTURE XVII
Adrian Dumitrescu
DYNAMIC PROGRAMMING
c Adrian Dumitrescu, 2012
1
Dynamic Programming (DP)
DP is a method (approach) used in algorithm design. It is similar
to Divide & Conquer in that an instance of the problem is divided
into smalle
CS 535: LECTURE XVI
Adrian Dumitrescu
SINGLE SOURCE SHORTEST PATHS
c Adrian Dumitrescu, 2012
1
Shortest path problem
S: (single source) Given a directed graph G = (V, E), with a
non-negative length function : E R0 , nd a shortest path from
a given source
CS 535: LECTURE XIV
Adrian Dumitrescu
GRAPH ALGORITHMS
c Adrian Dumitrescu, 2012
1
DFS: discovery and nishing times
We will use DFS to compute the SCCs of a directed graph. For
every v V , consider the interval Iv = [d(v), f (v)] (recall that
d(v) < f (v)
CS 535: LECTURE XI
Adrian Dumitrescu
MORE ON BINARY SEARCH TREES
c Adrian Dumitrescu, 2012
1
AVL trees
The idea of using rotations to maintain balance of BSTs is due to
Adelson-Velski & Landis (1962).
Assume that we add NIL nodes, so that every internal n
CS 535: LECTURE VIII
Adrian Dumitrescu
SORTING ALGORITHMS
c Adrian Dumitrescu, 2012
1
insertion-sort(a,n) / input: a[1.n]; output in place /
for j=2 to n do
cfw_key:=a[j];
/ insert a[j] into the sorted seq. a[1.j-1] /
i:=j-1;
while i>0 and a[i]>key do cfw
CS 535: LECTURE IX
Adrian Dumitrescu
BINARY SEARCH TREES
c Adrian Dumitrescu, 2012
1
Binary search trees (BSTs)
BSTs: data structures that support dynamic operations:
search, min, max, pred, succ
insert, delete
5
4
7
2
0
6
8
3
Can be used as: dictionari
CS 535: LECTURE VII
Adrian Dumitrescu
SORTING AND RELATED PROBLEMS:
ALGORITHMS AND LOWER BOUNDS
c Adrian Dumitrescu, 2012
1
Heap-sort
heap-sort(a,n)
/ input: a[1.n] ; output: in-place /
build-heap(a,n) / builds a max-heap; sets size(a) to n /
for i=n down
CS 535: LECTURE X
Adrian Dumitrescu
BINARY SEARCH TREES
c Adrian Dumitrescu, 2012
1
Insert in a BST
Example. Insert the weekdays
cfw_M on, T ue, W ed, T hu, F ri, Sat, Sun. BST order: alphabetical;
Insertion order: chronological.
insert(Mon)
Mon
Mon
inser
CS 535: LECTURE I
Adrian Dumitrescu
ASYMPTOTIC NOTATION AND
ALGORITHM ANALYSIS
c Adrian Dumitrescu, 2012
1
Asymptotic notation and algorithm analysis
Algorithm analysis: predict (estimate) the resources (time and
space) the algorithm requires.
Computing m
CS 535: LECTURE VI
Adrian Dumitrescu
BINARY HEAPS
c Adrian Dumitrescu, 2012
1
Binary heaps
Used as priority queues. Also, a data structure used by heapsort.
Binary heap: an almost complete binary tree (lled except for the
last level).
There are two types
CS 535: LECTURE V
Adrian Dumitrescu
APPLICATIONS OF
LISTS AND STACKS
c Adrian Dumitrescu, 2012
1
Data structure for disjoint sets
Maintain a collection of disjoint sets.
Scenario: Consider a collection cfw_S1 , . . . , Sk of disjoint dynamic
sets. Each s
CS 535: LECTURE IV
Adrian Dumitrescu
DATA STRUCTURES FOR
DYNAMIC SETS
c Adrian Dumitrescu, 2012
1
Dynamic sets
Examples: lists, stacks, queues, heaps, binary search trees, hash
tables, etc.
Operations for modifying or querying the data structure.
insert(
CS 535: LECTURE III
Adrian Dumitrescu
RECURRENCES AND
RECURSIVE ALGORITHMS
c Adrian Dumitrescu, 2012
1
Recurrences (examples with solutions)
Solve exactly for n = 2k :
T (n) = 2T
n
+1
2
T (1) = 0
Plug in for small values of n and guess solution. Then prov
CS 535: LECTURE XVIII
Adrian Dumitrescu
TRANSITIVE CLOSURE
OF DIRECTED GRAPHS
c
Adrian Dumitrescu, 2012
1
Transitive closure
Given a directed graph G = (V, E), where V = cfw_1, 2, . . . , n nd
whether there exists a path in G from i to j for all pairs (i