Dynamic Programming
Sequence of decisions.
Problem state.
Principle of optimality.
Dynamic Programming Recurrence
Equations.
Solution of recurrence equations.
Sequence Of Decisions
As in the greedy method, the solution to a
problem is viewed as the resu
Tournament Trees
Winner trees.
Loser Trees.
Winner Trees
Complete binary tree with n external
nodes and n - 1 internal nodes.
External nodes represent tournament
players.
Each internal node represents a match
played between its two children;
the winner of
Balanced Binary Search Trees
height is O(log n), where n is the
number of elements in the tree
AVL (Adelson-Velsky and Landis)
trees
red-black trees
find, insert, and erase take O(log n)
time
Balanced Binary Search Trees
Indexed AVL trees
Indexed re
Union-Find Problem
Given a set cfw_1, 2, , n of n elements.
Initially each element is in a different set.
cfw_1, cfw_2, , cfw_n
An intermixed sequence of union and find
operations is performed.
A union operation combines two sets into one.
Each of t
Stacks
Linear list.
One end is called top.
Other end is called bottom.
Additions to and removals from the top end
only.
Stack Of Cups
top
top
F
E
D
D
C
C
B
bottom
E
B
A
bottom
Add a cup to the stack.
Remove a cup from new stack.
A stack is a LIFO list.
Queues
Linear list.
One end is called front.
Other end is called rear.
Additions are done at the rear only.
Removals are made from the front only.
Bus Stop Queue
Bus
Stop
front
rear
rear
rear
rear
rear
Bus Stop Queue
Bus
Stop
front
rear
rear
rear
Bus Stop
Data Compression
Reduce the size of data.
Reduces storage space and hence storage cost.
Compression ratio = original data size/compressed data size
Reduces time to retrieve and transmit data.
Lossless And Lossy Compression
compressedData = compress(o
Trees
Nature Lovers View Of A Tree
leaves
branches
root
Computer Scientists View
root
leaves
branches
nodes
Linear Lists And Trees
Linear lists are useful for serially ordered data.
(e0, e1, e2, , en-1)
Days of week.
Months in a year.
Students in thi
Dynamic Programming
Steps.
View the problem solution as the result of a sequence
of decisions.
Obtain a formulation for the problem state.
Verify that the principle of optimality holds.
Set up the dynamic programming recurrence
equations.
Solve these equ
Hard Problems
Some problems are hard to solve.
No polynomial time algorithm is known.
E.g., NP-hard problems such as machine scheduling,
bin packing, 0/1 knapsack.
Is this necessarily bad?
Data encryption relies on difficult to solve
problems.
Crypto
Single-Source All-Destinations
Shortest Paths With Negative Costs
Directed weighted graph.
Edges may have negative cost.
No cycle whose cost is < 0.
Find a shortest path from a given source vertex
s to each of the n vertices of the digraph.
Single-Source
Matrix Multiplication Chains
Determine the best way to compute the
matrix product M1x M2 x M3 x x Mq.
Let the dimensions of Mi be rix ri+1.
q-1 matrix multiplications are to be done.
Decide the matrices involved in each of
these multiplications.
Decis
Backtracking And Branch And Bound
Subset & Permutation Problems
Subset problem of size n.
Nonsystematic search of the space for the answer takes
O(p2n) time, where p is the time needed to evaluate
each member of the solution space.
Permutation problem
All-Pairs Shortest Paths
Given an n-vertex directed weighted graph,
find a shortest path from vertex i to vertex j
for each of the n2 vertex pairs (i,j).
1
7
2
5
1
9
3
7
6
9
5
4
1
8
4
2
5
4
16
1
2
4
7
Dijkstras Single Source Algorithm
Use Dijkstras algo
Rank
Rank of an element is its position in ascending key
order.
[2,6,7,8,10,15,18,20,25,30,35,40]
rank(2) = 0
rank(15) = 5
rank(20) = 7
Selection Problem
Given n unsorted elements, determine the
kth smallest element. That is, determine the
element whose
Divide And Conquer
Distinguish between small and large instances.
Small instances solved differently from large ones.
Small And Large Instance
Small instance.
Sort a list that has n <= 10 elements.
Find the minimum of n <= 2 elements.
Large instance
Divide-And-Conquer Sorting
Small instance.
n <= 1 elements.
n <= 10 elements.
Well use n <= 1 for now.
Large instance.
Divide into k >= 2 smaller instances.
k = 2, 3, 4, ?
What does each smaller instance look like?
Sort smaller instances recursively.
Minimum-Cost Spanning Tree
weighted connected undirected graph
spanning tree
cost of spanning tree is sum of edge costs
find spanning tree that has minimum cost
Example
8
1
7
2
2
3
4
9
4
10
5
12 6
6
14
7
3
8
Network has 10 edges.
Spanning tree has only
Graph Search Methods
A vertex u is reachable from vertex v iff there is a
path from v to u.
2
3
8
1
4
5
9
10
6
7
11
Graph Search Methods
A search method starts at a given vertex v and
visits/labels/marks every vertex that is reachable
from v.
2
3
8
1
4
Shortest Path Problems
Directed weighted graph.
Path length is sum of weights of edges on path.
The vertex at which the path begins is the
source vertex.
The vertex at which the path ends is the
destination vertex.
Example
1
6
8
2
16
3
7
6
3
5
4
10
1
Best-fit bin packing in
O(n log n) time
The search tree will contain one element for
each bin which is in use and has non-zero
available capacity
Notice! It is possible that for two or more
bins to have the same available capacity
Best-fit bin packing i
Priority Queues
Two kinds of priority queues:
Min priority queue.
Max priority queue.
Min Priority Queue
Collection of elements.
Each element has a priority or key.
Supports following operations:
empty
size
insert an element into the priority queue (
Binary Tree Traversal Methods
In a traversal of a binary tree, each element of
the binary tree is visited exactly once.
During the visit of an element, all action (make
a clone, display, evaluate the operator, etc.)
with respect to this element is taken
Binary Tree Properties & Representation
Minimum Number Of Nodes
Minimum number of nodes in a binary tree
whose height is h.
At least one node at each of first h levels.
minimum number of
nodes is h
Maximum Number Of Nodes
All possible nodes at first h