Professional Documents
Culture Documents
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
Tree traversals
Binary search
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Design and Analysis of Algorithms - Unit II 7
Pseudocode for Mergesort
ALGORITHM Mergesort(A[0..n-1])
//Sorts array A[0..n-1] by recursive mergesort
// Input: An array A[0..n-1] of orderable elements
// Output: Array A[0..n-1] sorted in non-increasing order
If n>1
copy A[0..[n/2]-1] to B[0..[n/2]-1]
copy A[[n/2]..n-1] to C[0..[n/2]-1]
Mergesort(B[0..[n/2]-1])
Mergesort(C[0..[n/2]-1])
Merge(B,C,A)
A[i]≤p A[i]>p
Design and Analysis of Algorithms - Unit II 13
The partition algorithm
Improvements:
• better pivot selection: median of three partitioning avoids worst
case in sorted files
• switch to insertion sort on small subfiles
TL TR
Algorithm Preorder(T)
//Implement the preorder traversal of a binary tree
//Input: Binary tree T (with labeled vertices)
//Output: Node labels listed in preorder
if T ‡
write label of T’s root
Preorder(TL)
Preorder(TR)
A = 12345678901357986429 B = 87654321284820912836
M 1 + M4 - M 5 + M 7 M 3 + M5
=
M2 + M 4 M 1 + M 3 - M2 + M 6
Number of multiplications: 7
Number of additions: 18
N Multiplications Additions
Approximations/heuristics:
• traveling salesman problem (TSP)
• knapsack problem
• other combinatorial optimization problems
Design and Analysis of Algorithms - Unit II 34
Change-Making Problem
Given unlimited amounts of coins of denominations d1 > … > dm ,
give change for amount n with the least number of coins
Q: What are the objective function and constraints?
Example: d1 = 25c, d2 =10c, d3 = 5c, d4 = 1c and n = 48c
Greedy solution is
optimal for any amount and “normal’’ set of denominations
may not be optimal for arbitrary coin denominations
For example, d1 = 25c, d2 = 10c, d3 = 1c, and n = 30c
Design and Analysis of Algorithms - Unit II 35
Minimum Spanning Tree (MST)
Spanning tree of a connected graph G: a connected acyclic
subgraph of G that includes all of G’s vertices
Example:
6 c 6 c c
a a a
4 1 4 1 1
2 2
d d d
b 3 b
Design and Analysis of Algorithms - Unit II b 3 36
Prim’s MST algorithm
Start with tree T1 consisting of one (any) vertex and “grow”
tree one vertex at a time to produce MST through a series of
expanding subtrees T1, T2, …, Tn
VT VT {u*}
ET ET {v*}
return ET
Design and Analysis of Algorithms - Unit II 38
Example
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d b d d
b 3 3 b 3
4 c
4 c
a a
1
1 6
6
2
2
d
b d b 3
3 Design and Analysis of Algorithms - Unit II 39
Notes about Prim’s algorithm
Needs priority queue for locating closest fringe vertex.
Efficiency
• O(n2) for weight matrix representation of graph and array
implementation of priority queue
• O(m log n) for adjacency lists representation of graph with
n vertices and m edges and min-heap implementation of the
priority queue
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d d d
b 3 b 3 b 3
4 c c c
a a a
1 1 1
6 6
2 2 2
d d d
b 3 b 3
Design and Analysis of Algorithms - Unit II b 3 43
Notes about Kruskal’s algorithm
Algorithm looks easier than Prim’s but is harder to
implement (checking for cycles!)
1 6
5 3 4
2 7
Initial: 1 2 3 4
1 2 4
Union 1 3:
3
1 2
Union 2 4:
3 4
1 2
Find 4:
3 and Analysis4 of Algorithms - Unit II
Design 53
Forest Representation
Union 1 4: 3 2
Find 4: 3 2
f f
e c d e
d
c
Example
3 6
7 2 5 4
a d
d e
4
b c
b(a,3) c(b,3+4) d(b,3+2) e(-,∞) 3 6
2 5
a d e
7 4
4
d(b,5) c(b,7) e(d,5+4) 3
b c
6
2 5
a d e
7 4
4
c(b,7) e(d,9) 3
b c
6
2 5
a d e
7 4
e(d,9) Design and Analysis of Algorithms - Unit II 59
Notes on Dijkstra’s algorithm
Doesn’t work for graphs with negative weights (whereas
Floyd’s algorithm does, as long as there is no negative
cycle).
Efficiency
• O(|V|2) for graphs represented by weight matrix and
array implementation of priority queue
• O(|E|log|V|) for graphs represented by adj. lists and
min-heap implementation of priority queue