Daa Unit-Ii
Daa Unit-Ii
UNIT - 2
DIVIDE & CONQUER AND GREEDY
1.1 Divide and Conquer
Problem
of size n
Problem Problem
of size n of size n
Solution to
original
problem
NOTE:
19
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Therefore, the order of growth of T(n) depends on the values of the constants a & b and
the order of growth of the function f(n).
Master theorem
Theorem: If f(n) Є Θ (nd) with d ≥ 0 in recurrence equation
T(n) = aT(n/b) + f(n),
then
Θ (nd) if a < bd
T(n) = Θ (ndlog n) if a = bd
Θ (nlogba ) if a > bd
Example:
20
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
d=0
Therefore:
a > bd i.e., 2 > 20
Case 3 of master theorem holds good. Therefore:
T(n) Є Θ (nlogba )
Є Θ (nlog22 )
Є Θ (n)
1.3Binary Search
Description:
Binary tree is a dichotomic divide and conquer search algorithm. Ti inspects the middle
element of the sorted list. If equal to the sought value, then the position has been found.
Otherwise, if the key is less than the middle element, do a binary search on the first half,
else on the second half.
Algorithm:
Algorithm can be implemented as recursive or non-recursive algorithm.
l 0
r n-1
while l ≤ r do
m ( l + r) / 2
if key = = A[m]
return m
else
if key < A[m]
r m-1
else
l m+1
return -1
Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Depend on
Best – key matched with mid element
Worst – key not found or key sometimes in the list
• Let C(n) denotes the number of times basic operation is executed. Then
Cworst(n) = Worst case efficiency. Since after each comparison the algorithm
divides the problem into half the size, we have
Cworst(n) = Cworst(n/2) + 1 for n > 1
21
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
C(1) = 1
• Solving the recurrence equation using master theorem, to give the number of
times the search key is compared with an element in the array, we have:
C(n) = C(n/2) + 1
a=1
b=2
f(n) = n0 ; d = 0
case 2 holds:
C(n) = Θ (ndlog n)
= Θ (n0log n)
= Θ ( log n)
Applications of binary search:
• Number guessing game
• Word lists/search dictionary etc
Advantages:
• Efficient on very big list
• Can be implemented iteratively/recursively
Limitations:
• Interacts poorly with the memory hierarchy
• Requires given list to be sorted
• Due to random access of list element, needs arrays instead of linked list.
1.4Merge Sort
Definition:
Merge sort is a sort algorithm that splits the items to be sorted into two groups,
recursively sorts each group, and merges them into a final sorted sequence.
Features:
• Is a comparison based algorithm
• Is a stable algorithm
• Is a perfect example of divide & conquer algorithm design strategy
• It was invented by John Von Neumann
Algorithm:
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 )
22
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
I →0
j→0
k→0
while i < p and j < q do
if B[i] ≤ C[j]
A[k] →B[i]
i→i + 1
else
A[k] →C[j]
j→j + 1
k→k + 1
if i == p
copy C [ j… q-1 ] to A [ k… (p+q-1) ]
else
copy B [ i… p-1 ] to A [ k… (p+q-1) ]
Example:
Apply merge sort for the following list of elements: 6, 3, 7, 8, 2, 4, 5, 1
6 3 7 8 2 4 5 1
6 3 7 8 2 4 5 1
6 3 7 8 2 4 5 1
6 3 7 8 2 4 5 1
3 6 7 8 2 4 1 5
3 6 7 8 1 2 4 5
1 2 3 4 5 6 7 8
23
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Worst case: During key comparison, neither of the two arrays becomes empty
before the other one contains just one element.
• Let C(n) denotes the number of times basic operation is executed. Then
C(n) = 2C(n/2) + Cmerge(n) for n > 1
C(1) = 0
where, Cmerge(n) is the number of key comparison made during the merging stage.
In the worst case:
Cmerge(n) = 2 Cmerge(n/2) + n-1 for n > 1
Cmerge(1) = 0
• Solving the recurrence equation using master theorem:
C(n) = 2C(n/2) + n-1 for n > 1
C(1) = 0
Here a=2
b=2
f(n) = n; d = 1
Therefore 2 = 21, case 2 holds
C(n) = Θ (ndlog n)
= Θ (n1log n)
= Θ (n log n)
Advantages:
• Number of comparisons performed is nearly optimal.
• Mergesort will never degrade to O(n2)
• It can be applied to files of any size
Limitations:
• Uses O(n) additional memory.
1.6 Quick Sort and its performance
Definition:
Quick sort is a well –known sorting algorithm, based on divide & conquer approach. The
steps are:
1. Pick an element called pivot from the list
2. Reorder the list so that all elements which are less than the pivot come before the
pivot and all elements greater than pivot come after it. After this partitioning, the
pivot is in its final position. This is called the partition operation
3. Recursively sort the sub-list of lesser elements and sub-list of greater elements.
Features:
• Developed by C.A.R. Hoare
• Efficient algorithm
• NOT stable sort
• Significantly faster in practice, than other algorithms
24
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
25
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Best case: when partition happens in the middle of the array each time.
Worst case: When input is already sorted. During key comparison, one half is
empty, while remaining n-1 elements are on the other partition.
• Let C(n) denotes the number of times basic operation is executed in worst case:
Then
C(n) = C(n-1) + (n+1) for n > 1 (2 sub-problems of size 0 and n-1 respectively)
C(1) = 1
Best case:
C(n) = 2C(n/2) + Θ(n) (2 sub-problems of size n/2 each)
NOTE:
The quick sort efficiency in average case is Θ( n log n) on random input.
26
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
UNIT - 3
THE GREEDY METHOD
3.1 The General Method
3.2 Knapsack Problem
3.3 Job Sequencing with Deadlines
3.4 Minimum-Cost Spanning Trees
3.5 Prim’sAlgorithm
3.6 Kruskal’s Algorithm
3.7 Single Source Shortest Paths.
The method:
• Applicable to optimization problems ONLY
• Constructs a solution through a sequence of steps
• Each step expands a partially constructed solution so far, until a complete solution
to the problem is reached.
On each step, the choice made must be
• Feasible: it has to satisfy the problem‘s constraints
• Locally optimal: it has to be the best local choice among all feasible choices
available on that step
• Irrevocable: Once made, it cannot be changed on subsequent steps of the
algorithm
NOTE:
• Greedy method works best when applied to problems with the greedy-choice
property
• A globally-optimal solution can always be found by a series of local
improvements from a starting configuration.
27
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
• Optimal solutions:
Change making
Minimum Spanning Tree (MST)
Single-source shortest paths
Huffman codes
• Approximations:
Traveling Salesman Problem (TSP)
Fractional Knapsack problem
28
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
• Unable to fill the knapsack to capacity, and the empty space lowers the effective value per
pound of the packing
• We must compare the solution to the sub-problem in which the item is included with the
solution to the sub-problem in which the item is excluded before we can make the choice
29
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
• Consider the jobs in the non increasing order of profits subject to the constraint that the resulting
job sequence J is a feasible solution.
• In the example considered before, the non-increasing profit vector is
(100 27 15 10) (2 1 2 1)
p1 p 4 p3 p2 d1 d d3 d2
J = { 1} is a feasible one
J = { 1, 4} is a feasible one with processing sequence ( 4,1)
J = { 1, 3, 4} is not feasible
J = { 1, 2, 4} is not feasible
J = { 1, 4} is optimal
30
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
• So, we have only to prove that if J is a feasible one, then Σ represents a possible order in which
the jobs may be processed.
• Suppose J is a feasible solution. Then there exists Σ1 = (r1,r2,…,rk) such that
drj ≥ j, 1 ≤ j <k
i.e. dr1 ≥1, dr2 ≥ 2, …, drk ≥ k.
each job requiring an unit time.
31
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
32
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
5 2
d
c 3
33
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Definition:
MST of a weighted, connected graph G is defined as: A spanning tree of G with
minimum total weight.
Example: Consider the example of spanning tree:
For the given graph there are three possible spanning trees. Among them the spanning
tree with the minimum weight 6 is the MST for the given graph
Algorithm:
ALGORITHM Prim (G)
//Prim‘s algorithm for constructing a MST
//Input: A weighted connected graph G = { V, E }
//Output: ET the set of edges composing a MST of G
// the set of tree vertices can be initialized with any vertex
VT → { v0}
ET → Ø
for i→ 1 to |V| - 1 do
Find a minimum-weight edge e* = (v*, u*) among all the edges (v, u) such
that v is in VT and u is in V - VT
VT → VT U { u*}
34
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
ET → ET U { e*}
return ET
STEP 1: Start with a tree, T0, consisting of one vertex
STEP 2: ―G row‖ tree one vertex/edge at a time
• Construct a series of expanding sub-trees T1, T2, … Tn-1.
• At each stage construct Ti + 1 from Ti by adding the minimum weight edge
connecting a vertex in tree (Ti) to one vertex not yet in tree, choose from
“fringe” edges (this is the “greedy” step!)
Algorithm stops when all vertices are included
Example:
Apply Prim‘s algorithm for the following graph to find MST.
1
b c
3 4 4 6
a 5 f 5
d
2
6 8
e
Solution:
1
c(b,1) b c
3
d(-,∞)
b ( a, 3 )
e(a,6) a
f(b,4)
1 c
b
d(c,6) 3
c ( b, 1 ) e(a,6) 4
f(b,4)
a f
35
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
1
b c
3 4
d(f,5) a f
f ( b, 4)
e(f,2)
2
1
b c
3 4
e ( f, 2) d(f,5) a f 5
d
2
Efficiency:
Efficiency of Prim‘s algorithm is based on data structure used to store priority queue.
• Unordered array: Efficiency: Θ(n2)
• Binary heap: Efficiency: Θ(m log n)
• Min-heap: For graph with n nodes and m edges: Efficiency: (n + m) log n
Conclusion:
• Prim‘s algorithm is a ―vertex based algorithm‖
• Prim‘s algorithm ― Needs priority queue for locating the nearest vertex.‖ The
choice of priority queue matters in Prim implementation.
o Array - optimal for dense graphs
o Binary heap - better for sparse graphs
o Fibonacci heap - best in theory, but not in practice
36
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Algorithm:
The method:
STEP 1: Sort the edges by increasing weight
STEP 2: Start with a forest having |V| number of trees.
STEP 3: Number of trees are reduced by ONE at every inclusion of an edge
At each stage:
• Among the edges which are not yet included, select the one with minimum
weight AND which does not form a cycle.
• the edge will reduce the number of trees by one by combining two trees of
the forest
Algorithm stops when |V| -1 edges are included in the MST i.e : when the number of
trees in the forest is reduced to ONE.
37
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Example:
Apply Kruskal‘s algorithm for the following graph to find MST.
1
b c
3 4 4 6
a 5 f 5
d
2
6 8
e
Solution:
The list of edges is:
Edge ab af ae bc bf cf cd df de ef
Weight 3 5 6 1 4 4 6 5 8 2
1
bc
Edge b c
1
Weight a
f
d
Insertion
YES
status
Insertion e
1
order
ef 1
Edge b c
2
Weight a f d
Insertion
YES
status 2
Insertion e
2
order
38
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
ab 1
Edge 3 b c
3
Weight a f d
Insertion
YES
status 2
Insertion e
3
order
bf 1
Edge 3 b c
4
Weight a 4 f d
Insertion
YES
status 2
Insertion e
4
order
Edge cf
Weight 4
Insertion
NO
status
Insertion
-
order
Edge af
Weight 5
Insertion
NO
status
Insertion
-
order
df 1
Edge
3 b c
5
Weight
4
Insertion a f d
YES
status 5
2
Insertion
5 e
order
Algorithm stops as |V| -1 edges are included in the MST
39
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Efficiency:
Efficiency of Kruskal‘s algorithm is based on the time needed for sorting the edge
weights of a given graph.
• With an efficient sorting algorithm: Efficiency: Θ(|E| log |E| )
Conclusion:
• Kruskal‘s algorithm is an ―e
dge based algorithm‖
• Prim‘s algorithm with a heap is faster than Kruskal‘s algorithm.
3.7 Single Source Shortest Paths.
VT→0
40
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Example:
Apply Dijkstra‘s algorithm to find Single source shortest paths with vertex a as the
source.
1
b c
3 4 4 6
a 5 f 5
d
2
6 8
e
Solution:
Length Dv of shortest path from source (s) to other vertices v and Penultimate vertex Pv
for every vertex v in V:
Da = 0 , Pa = null
Db = ∞ , Pb = null
Dc = ∞ , Pc = null
Dd = ∞ , Pd = null
De = ∞ , Pe = null
Df = ∞ , Pf = null
41
Downloaded by Shubham Sanodiya ([email protected])
lOMoARcPSD|45122995
Da = 0 Pa = a 1
c ( b , 3+1Db)= 3 Pb = [ a, b ] b c
3
d ( - , ∞Dc) = 4 Pc = [a,b,c]
b ( a, 3 )
e ( a , 6Dd) = ∞ Pd = null a
f ( a , 5De
) = 6 Pe = [ a, e ]
Df = 5 Pf = [ a, f ]
Da = 0 Pa = a
Db = 3 Pb = [ a, b ]
d ( c , 4+6 ) 5
Dc = 4 Pc = [a,b,c]
c ( b, 4 ) e ( a , 6)Dd=10 a f
Pd = [a,b,c,d]
f(a,5)
De = 6 Pe = [ a, e ]
Df = 5 Pf = [ a, f ]
Da = 0 Pa = a
Db = 3 Pb = [ a, b ]
Dc = 4 Pc = [a,b,c] a
d ( c , 10D)d =10 Pd = [a,b,c,d]
f ( a, 5) 6
e ( a , 6De
) = 6 Pe = [ a, e ]
e
Df = 5 Pf = [ a, f ]
Da = 0 Pa = a 1
Db = 3 Pb = [ a, b ] b c
Dc = 4 Pc = [a,b,c]
e ( a, 6) d ( c, 10Dd=10
) Pd = [a,b,c,d] 3 6
De = 6 Pe = [ a, e ] d
Df = 5 Pf = [ a, f ] a
Conclusion:
• Doesn‘t work with negative weights
• Applicable to both undirected and directed graphs
• Use unordered array to store the priority queue: Efficiency = Θ(n2)
• Use min-heap to store the priority queue: Efficiency = O(m log n)
42
Downloaded by Shubham Sanodiya ([email protected])