DAA Unit-II IT3
DAA Unit-II IT3
PF ALGORITHMS
UNIT-2
1
UNIT - II
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
5
Design and Analysis of Algorithms - Unit II
General Divide and Conquer recurrence:
Master Theorem
Algorithm:
Split array A[1..n] in two and make copies of each half
in arrays B[1.. n/2 ] and C[1.. n/2 ]
Sort arrays B and C
Merge sorted arrays B and C into array A as follows:
Repeat the following until no elements remain in one of the arrays:
compare the first elements in the remaining unprocessed portions of the
arrays
copy the smaller of the two into A, while incrementing the index indicating
the unprocessed portion of that array
Once all elements in one of the arrays are processed, copy the remaining
unprocessed elements from the other array into A.
7
Design and Analysis of Algorithms - Unit II
Mergesort Example
8 3 2 9 7 1 5 4
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 8
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)
9
Design and Analysis of Algorithms - Unit II
Pseudocode for Merge
10
Design and Analysis of Algorithms - Unit II
Recurrence Relation for Mergesort
11
Design and Analysis of Algorithms - Unit II
Efficiency of mergesort
12
Design and Analysis of Algorithms - Unit II
Quick-Sort
Quick-sort is a randomized sorting algorithm
based on the divide-and-conquer paradigm:
Divide: pick a random element x (called pivot)
and partition S into
x
L elements less than x
E elements equal x
G elements greater than x
Recur: sort L and G
Conquer: join L, E and G x
L E G
A[i]≤p A[i]>p
14
Design and Analysis of Algorithms - Unit II
The partition
algorithm
Improvements:
better pivot selection: median of three partitioning avoids worst case in sorted
files
switch to insertion sort on small subfiles
Considered the method of choice for internal sorting for large files (n ≥ 10000)
16
Design and Analysis of Algorithms - Unit II
Binary Search - an Iterative
Algorithm
Very efficient algorithm for searching in sorted array:
K vs A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search);
otherwise, continue searching by the same method
in A[0..m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
ALGORITHM BinarySearch(A[0..n-1], K)
l 0; r n-1
while l r do // l and r crosses over can’t find K
m (l+r)/2
if K = A[m] return m //the key is found
else if K < A[m] r m-1 //the key is on the left half of
the array
else l m+1 // the key is on the right half of
the array
return -1
18
Design and Analysis of Algorithms - Unit II
Binary Search – a Recursive Algorithm
ALGORITHM BinarySearchRecur(A[0..n-1], l, r, K)
if l > r
return –1
else
m (l + r) / 2
if K = A[m]
return m
else if K < A[m]
return BinarySearchRecur(A[0..n-1], l, m-1, K)
else
return BinarySearchRecur(A[0..n-1], m+1, r, K)
19
Design and Analysis of Algorithms - Unit II
Analysis of Binary Search
Worst-case (successful or fail) :
Cw (n) = 1 + Cw( n/2 ),
Cw (1) = 1
solution: Cw(n) = log2 n +1 = log2(n+1)
20
Design and Analysis of Algorithms - Unit II
Binary Tree Traversals
Definitions
A binary tree T is defined as a finite set of nodes that is
either empty or consists of a root and two disjoint binary
trees TL and TR called, respectively, the left and right subtree
of the root.
The height of a tree is defined as the length of the longest
path from the root to a leaf.
Problem: find the height of a binary tree.
TL TR
21
Design and Analysis of Algorithms - Unit II
Pseudocode - Height of a Binary
ALGORITHM Height(T) Tree
//Computes recursively the height of a binary tree
//Input: A binary tree T
//Output: The height of T
if T =
return –1
else
return max{Height(TL), Height(TR)} + 1
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 M3 + M 5
=
M2 + M 4 M1 + M 3 - M2 + M 6
28
Design and Analysis of Algorithms - Unit II
Submatrices:
Number of multiplications: 7
Number of additions: 18
30
Design and Analysis of Algorithms - Unit II
Time Analysis
N Multiplications Additions
32
Design and Analysis of Algorithms - Unit II
Greedy Technique
Greedy Technique
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
Optimal solutions:
change making for “normal” coin denominations
minimum spanning tree (MST)
single-source shortest paths
simple scheduling problems
Huffman codes
Approximations/heuristics:
traveling salesman problem (TSP)
knapsack problem
other combinatorial optimization problems
35
Design and Analysis of Algorithms - Unit II
Change-Making Problem
Given unlimited amounts of coins of denominations d1 > … > dm ,
give change for amount n with the least number of coins
Greedy solution is
optimal for any amount and “normal’’ set of denominations
<1,denominations
may not be optimal for arbitrary coin 2, 0, 3>
Example:
6 c 6 c c
a a a
4 1 4 1 1
2 2
d d d
b 3 b b 3 37
Design and Analysis of Algorithms - Unit II
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
On each iteration, construct Ti+1 from Ti by adding vertex not in Ti that is closest to those
already in Ti (this is a “greedy” step!)
38
Design and Analysis of Algorithms - Unit II
Pseudocode – Prim’s algorithm
ALGORITHM Prim(G)
// Prim’s algorithm for computing a MST
// Input:A weighted connected graph G = (V,E)
// Output: Et, the set of edges composing a MST of G
VT {v0 }
ET Ø
for I 1 to |v| - 1 do
find a minimum weight edge e*=(v*,u*) among all edges(v,u) such
that v is in VT and u is in V – VT
VT VT {u*}
ET ET {v*}
return ET
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
3
b 3
Design and Analysis of Algorithms - Unit II 40
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
“Grow” tree one edge at a time to produce MST through a series of expanding forests
F1, F2, …, Fn-1
On each iteration, add the next edge on the sorted list unless this would create a cycle.
(If it would, skip the edge.)
ALGORITHM Kruskal(G)
// Kruskal’s algorithm for constructing a minimum spanning tree
// Input: A weighted connected graph G = (V,E)
// Output: ET, The set of edges composing a MST of G
ET Ø; ecounter 0
k0
while ecounter < |V| - 1
k k +1
if ET {eik} is acyclic
ET ET {eik} ;
ecounter ecounter + 1
return ET
43
Design and Analysis of Algorithms - Unit II
Example
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 b 3
Design and Analysis of Algorithms - Unit II 44
Notes about Kruskal’s algorithm
Algorithm looks easier than Prim’s but is harder to implement (checking for cycles!)
Cycle checking: a cycle is created iff added edge connects vertices in the same
connected component
Runs in O(m log m) time, with m = |E|. The time is mostly spent on sorting.
45
Design and Analysis of Algorithms - Unit II
Disjoint Sets
Union of two sets A and B, denoted by A B, is the set {x | x A or x B}
The intersection of two sets A and B, denoted by A ∩ B, is the set {x| x A and x B}.
If S = {1,2,…,11} and there are 4 subsets {1,7,10,11} , {2,3,5,6}, {4,8} and {9}, these
subsets may be labeled as 1, 3, 8 and 9, in this order.
48
Design and Analysis of Algorithms - Unit II
OBJECTIVE
to design efficient algorithms for Union & Find operations.
Approach: to represent each set as a rooted tree with data elements stored in its
nodes.
Each element x other than the root has a pointer to its parent p(x) in the tree.
The root has a null pointer, and it serves as the name or set representative of the
set.
This results in a forest in which each tree corresponds to one set.
For any element x, let root(x) denote the root of the tree containing x.
FIND(x) returns root(x).
union(x, y) means UNION(root(x), root(y)).
FIND(x) follow the path from x until the root is reached, then return root(x).
Time complexity is O(n)
Find(x) = Find(y), when x and y are in the same set
51
Design and Analysis of Algorithms - Unit II
Forest Representation
1 6
5 3 4
2 7
Find(x)
Traverse from x up to the root
Union(x, y)
Merge the two trees containing x and y
53
Design and Analysis of Algorithms - Unit II
Forest Representation
Initial: 1 2 3 4
1 2 4
Union 1 3:
3
1 2
Union 2 4:
3 4
1 2
Find 4:
3 4
54
Design and Analysis of Algorithms - Unit II
Forest Representation
Union 1 4: 3 2
Find 4: 3 2
55
Design and Analysis of Algorithms - Unit II
Union by Rank & Path
Compression
Union by Rank: Each node is associated with a rank, which is the upper bound on
the height of the node (i.e., the height of subtree rooted at the node), then when
UNION, let the root with smaller rank point to the root with larger rank.
Path Compression: used in FIND-SET(x) operation, make each node in the path
from x to the root directly point to the root. Thus reduce the tree height.
57
Design and Analysis of Algorithms - Unit II
Shortest paths – Dijkstra’s algorithm
Single Source Shortest Paths Problem: Given a weighted
connected (directed) graph G, find shortest paths from source vertex s
to each of the other vertices
a
3
Example
7 2
d
5 4
6
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
b c
c(b,7) e(d,9) 3 6
2 5
a d e
7 4
e(d,9) Design and Analysis of Algorithms - Unit II 60
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).
Applicable to both undirected and directed graphs
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
61
Design and Analysis of Algorithms - Unit II