Btech Degree Examination, May2014 Cs010 601 Design and Analysis of Algorithms Answer Key Part-A 1
Btech Degree Examination, May2014 Cs010 601 Design and Analysis of Algorithms Answer Key Part-A 1
=
(
22 21
12 11
22 21
12 11
22 21
12 11
C C
C C
B B
B B
A A
A A
R = a
11
* (b
12
- b
22
)
S= a
22
* (b
21
- b
11
)
T = (a
11
+ a
12
) * b
22
U= (a
21
- a
11
) * (b
11
+ b
12
)
V = (a
12
- a
22
) * (b
21
+ b
22
)
7 multiplications
18 additions / subtractions
The resulting recurrence relation for T(n) is
Where
n is a power of 2, n=2
k
for some integer k.
a and b are constant.
14.
Divide: divide array A[0..n-1] in two about equal halves and make copies of each half in arrays B
and C
Conquer:
If number of elements in B and C is 1, directly solve it
Sort arrays B and C recursively
Combine: Merge sorted arrays B and C into array A
Repeat the following until no elements remain in one of the arrays:
compare the first elements in the remaining unprocessed portions of the arrays
B and C
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.
Algorithm MergeSort(low,high)
//a[low:high] is a global array to be sorted. Small(P) is true if there is only one element to sort.In this
case the list is already sorted.
{
if(low<high) then //if there are more than one element
{
//Divide P into subproblems
// find where to split the set.
mid:= [(low+high)/2];
//Solve the subproblems
MergeSort(low,mid);
MergeSort(mid+1,high);
// Combine the solutions
Merge(low,mid,high)
}
}
Algorithm Merge(low,mid,high)
//a[low:high] is a global array containing two sorted subset in a[low:mid] and in a[mid+1:high]. The goal
is to merge these two sets into a single set residing in a[low:high]. b[] is an auxilary global array.
{
h:=low; i:=low; j:=mid+1;
while((hmid) and (j high)) do
{
if(a*h+ a*j+) then
{
b[i]:=a[h]; h=h+1;
}
else
{
b[i]=a[j];j=j+1;
}
i=i+1;
}
if(h>mid)then
for k:= j to high do
{
b[i]=a[k];i=i+1;
}
else
for k:=h to mid do
{
b[i]:=a[k]; i=i+1;
}
for k:= low to high do
a[k]:=b[k];
}
If T(n) represent the time for the merging operation is proportional to n then the computing time for
merge sort is ,
Where n is a power of 2, n=2
k
for some integer k.
c and a are constant.
15. Basics of Kruskals Algorithm
Edges are initially sorted by increasing weight
Start with an empty forest ,grow MST one edge at a time
intermediate stages usually have forest of trees (not connected)
at each stage add minimum weight edge among those not yet used that does not create a cycle
at each stage the edge may:
expand an existing tree
combine two existing trees into a single tree
create a new tree
need efficient way of detecting/avoiding cycles
algorithm stops when all vertices are included
Algorithm Kruskal(E,cost,n,t)
// E is the set of edges in G . G has n vertices. Cost[u,v] is the
// cost of edge(u,v). T is the set of edges in the minimum-cost
//spanning tree. The finalcost is returned
{
construct a heap out of the edge costs using heapify;
for i=1 to n do parent[i]=-1;
// each vertex is in a different set.
i=0; mincost=0.0;
while((i<n-1) and (heap not empty)) do
{
Delete a minimum cost edge(u,v) from the heap
and reheapify using Adjust;
j= Find(u); k=Find(v);
if(j k) then
{
i=i+1;
t[i,1]=u; t[i,2]=v;
mincost=mincost + cost[u,v];
Union(j,k)
}
}
if ( i n-1) then write ( no spanning tree);
else return mincost;
}
The final set is
{1 2 3 4 5 6 7}
Analysis of Kruskal
(initialization): O(V)
(sorting): O(E log E)
(set-operation): O(E log E)
Total: O(E log E)