Cycle Test I_key

5
n f(n) c*g(n) n0 n f(n ) c1*g(n ) c2*g(n ) n0 KAMARAJ COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 1. Metrics used to measure the efficiency of an algorithm: Time Complexity Space Complexity 2. Divide and Conquer Strategy: Splitting the n inputs into k distinct subsets, 1 < k n, yielding k number of sub problems. These problems must be solved. Type DandC(P) { if small(P) return S(P); else { divide P into smaller instances P1, P2, .. Pk k>=1; Apply DandC to each of these sub problems; return Combine (DandC(P1),..DandC(Pk)); } } 3. Big Omega notation: T (n) = f (n) = (g (n)) - If f (n) >= c*g (n) for all n > n 0 , where c and n 0 are constants > 0 (g (n)): class of functions f (n) that grow at least as fast as g (n) 4. Big Theta notation: T (n) = f (n) = ¨ (g (n)) - If c 1 *g (n) <= f (n) <= c 2 *g (n) for all n > n 0 , where c 1 , c 2 and n 0 are constants > 0. ¨ (g (n)): class of functions f (n) that grow at same rate as g (n)

description

c test series

Transcript of Cycle Test I_key

Page 1: Cycle Test I_key

n

f(n) c*g(n)

n0

n

f(n) c1*g(n

)

c2*g(n)

n0

KAMARAJ COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

1. Metrics used to measure the efficiency of an algorithm:

Time Complexity

Space Complexity

2. Divide and Conquer Strategy:

Splitting the �n� inputs into �k� distinct subsets, 1 < k ≤ n, yielding �k� number of

sub problems. These problems must be solved.

Type DandC(P)

{

if small(P) return S(P);

else {

divide P into smaller instances P1, P2, .. Pk k>=1;

Apply DandC to each of these sub problems;

return Combine (DandC(P1),�..DandC(Pk));

}

}

3. Big � Omega notation:

T (n) = f (n) = Ù (g (n))

- If f (n) >= c*g (n) for all n > n0, where c and n0 are constants > 0

Ù (g (n)): class of functions f (n) that grow at least as fast as g (n)

4. Big Theta notation:

T (n) = f (n) = È (g (n))

- If c1*g (n) <= f (n) <= c2*g (n) for all n > n0, where c1, c2 and n0 are

constants > 0.

È (g (n)): class of functions f (n) that grow at same rate as g (n)

id1716869 pdfMachine by Broadgun Software - a great PDF writer! - a great PDF creator! - http://www.pdfmachine.com http://www.broadgun.com

Page 2: Cycle Test I_key

5. Container Loading Problem:

Large ship is to be loaded with cargos.

Cargo is containerized.

All containers are same in size & different in weights.

wi - weight of the ith container.

Cargo capacity of the ship is �C�.

Problem is to load the ship with maximum number of containers.

Feasible solution

n ∑ wixi ≤ c and xiå {0,1} , 1 ≤i ≤n

i=1 x=1 �> container is to be loaded x= 0 -> container is not to be loaded

Objective function n Maximize ∑ xi

i=1

6. Big Oh notation:

T (n) = f (n) = O (g (n))

- If f (n) <= c*g (n) for all n ≥ n0, where c & n0 are constants > 0

O (g (n)): class of functions f (n) that grow no faster than g (n)

Properties of Big Oh notation:

O (f (n)) + O ( g(n)) = O (max { f (n), g (n)})

f (n) = O (g (n)) and g (n) ≤ h (n) implies f (n) = O ( h (n))

Any function can be said as an order of itself. i.e., f (n) = O (f (n))

Any constant value is equivalent to O (1). i.e., C = O (1)

If limn→∞ {f (n) / g (n)} å R>0 then f (n) å È (g (n)).

If limn→∞ {f (n) / g (n)} = 0 then f (n) å O (g (n)) but f (n) not å È (g (n)).

If limn→∞ {f (n) / g (n)} = +∞ => f (n) å Ù (g(n)) but f (n) not å È (g (n)).

7. a) Solve the following recurrence relation.

n

c*g(n) f(n)

n0

Page 3: Cycle Test I_key

S(K)-4S(K-1)+4S(K-2)=3K+2K

Initial conditions are S(0) = 1, S(1) = 1 Soln:

Characteristic Equation is,

a2 - 4a + 4 = 0

Roots are 2,2

SH(K) = (b0+b1K)2K

Sp1(K)=(K2/2)2K

Sp2(K)=12+3K

S(K)=(-11+(7/2)K) 2K +(K2/2)2K +12+3K 7. b) Merge Sort:

void MergeSort (int low, int 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)

{// If there are more than one element // Divide P into sub problems. // Find where to split the set. int mid = (low+high)/2; // Solve the sub problems MergeSort (low, mid); MergeSort (mid +1, high); // Combine the solutions Merge (low, mid, high);

} }

void Merge (int low, int mid, int high) /* a[low: high] is a global array containing two sorted subsets in a[low: high] and in a [mid +1: high]. The goal is to merge these two sets into a single set residing in a a[low: high]. b [] is an auxiliary global array. { int h = low, i= low, j= mid+1, k; while (( h<= mid) && (j<=high))

{ if (a[h] <= a[j]) { b[i] = a[h]; h++; } else { b[i] = a[j]; j++; } } if ( h > mid) { for ( k=j; k<=high; k++) { b[i] = a[k]; i++; } } else { for ( k=h; k<=mid; k++) { b[i]=a[k];

Page 4: Cycle Test I_key

i++; } } for (k=low; k<=high; k++) { a[k] = b[k]; }

}

Merge sort for the following elements: 310, 285, 179, 652, 351, 423, 861, 254, 450,

and 520.

Solution:

Given a [10] = 310, 285, 179, 652, 351, 423, 861, 254, 450, 520

Step 1: (350 | 285 | 179 | 652, 351 | 423, 861, 254, 450, 520) � split a [] into (a

[1:5] and a [6:10]), then a [1:5] into (a [1:3] and a [4:5]). Then a [1:3] into (a [1:2]

and a [3:3]). The two values in a [1:2] are split a final time into single element sub

arrays.

Step 2: (285, 310 | 179 | 652, 351 | 423, 861, 254, 450, 520) � a [1] and a [2] are

merged.

Step 3: (179, 285, 310 | 652, 351 | 423, 861, 254, 450, 520) � a [3] is merged with

a [1:2]

Step 4: (179, 285, 310 | 351, 652 | 423, 861, 254, 450, 520) � a [4] and a [5] are

merged.

Step 5: (179, 285, 310, 351, 652 | 423, 861, 254, 450, 520) � a [1:3] and a [4:5]

are merged.

Step 6: (179, 285, 310, 351, 652 | 423 | 861 | 254 | 450, 520) � a [6:10] is split

into

a [6:8] and a [9:10], then a [6:8] into a [6:7], a [8:8].Then a [6:7] into individual

element sub arrays.

Step 7: (179, 285, 310, 351, 652 | 423,861 | 254 | 450, 520) � a [6] and a [7] are

merged.

Step 8: (179, 285, 310, 351, 652 | 254, 423, 861| 450, 520) � a [8] is merged with

a [6:7].

Step 9: (179, 285, 310, 351, 652 | 254, 423, 450, 520, 861)

Step 10: (179, 254, 285, 310, 351, 423, 450, 520, 652, 861)

8. Knapsack Problem using Greedy Approach:

� �n� objects & a bag(knapsack)

� wi is weight of the object i.

� If object �i� is place into the bag, it will give pi xi profit, where xi is fraction

value 0≤ xi ≤1

� �m� is capacity of knapsack

Page 5: Cycle Test I_key

Algorithm:

void GreedyKnapsack(float m, int n) //objects are ordered by ratio of p&w

{

for(int i=1; i<=n; i++)

{

x[i]=0.0;

}

float u=m;

for(int i=0; i<=n; i++)

{

if(w[i] > u) break;

x[i]=1.0;

u-=w[i];

}

if(i<=n) x[i] = u/w[i];

}

Problem:

Item Weight Values

1 8 $64

2 7 $63

3 6 $60

4 4 $12

W=16

(p1/w1, p2/w2, p3/w3, p4/w4) = (8, 9, 10, 3)

wi xi wixi pi pixi

w3 6 1 6 60 60

w2 7 1 7 63 63

w1 8 3/8 3 64 24

w4 4 0 0 12 0

Total 16 Total 147

(x1, x2, x3, x4) = (3/8,1,1,0)