algo assignment

11
ALGORITHM ASSIGNMENT QUESTION PAPER SOLUTION 8836 SUBMITTED BY: RAHUL BHATIA (15) SONU KUMAR (50)

Upload: abhishekkumar

Post on 16-Jan-2016

218 views

Category:

Documents


0 download

DESCRIPTION

assignment of algorithm chap-3 and chap- 11

TRANSCRIPT

Page 1: Algo Assignment

ALGORITHM ASSIGNMENT QUESTION PAPER SOLUTION 8836

SUBMITTED BY:

RAHUL BHATIA (15)

SONU KUMAR (50)

Page 2: Algo Assignment

Ans 1:

a)

Brute force example that will give worst case time is:

Brute-Force String MatchingSearching for a pattern, P[0...m-1], in text, T[0...n-1]

Algorithm BruteForceStringMatch(T[0...n-1], P[0...m-1])for i ← 0 to n-m do

j ← 0while j < m and P[j] = T[i+j] do

j++if j = m then return i

return -1

Worst case when a shift is not made until the m-th comparison, so Θ(nm)Typically shift is made early then Average case Θ(n) or for m << nI.E.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAB

c)x=0

y=1;

for(i=0;i<n;i++)//n times

for(j=0;j<n;j++)//n times

{

Page 3: Algo Assignment

X++;

Y=x*x;

}

So, the total time to run/no. of steps is n^2+2 i.e O(n^2).

d)Insertion sort and heap sort are in place algorithms as these algorithms does not require any extra space to run perfectly .

f)RB TREE INSERTION:

Page 4: Algo Assignment

3. a)

Algorithm for heap sort

while end > 0 do(a[0] is the root and largest value. The swap moves it in front of the sorted elements.)swap(a[end], a[0])(the heap size is reduced by one)end ← end - 1(the swap ruined the heap property, so restore it)siftDown(a, 0, end)Max_heapify(a,i)

from above you can get O(nlogn) as build max heap takes O(n) to the heapfiy which takes O(logn) time.

b)

Page 5: Algo Assignment

After 17 delete

After 7, 8 delete

Ans4

a)

BED BAD HID BID DIG BAG CAB AIDCHOSING LAST LETTER

Page 6: Algo Assignment

CAB BED BAD HID BID AID

DIG BAG

CAB BED BAD HID BID AID DIG BAG

CHOSING 2ND

CAB BAD BAG

BED HID BID AID DIG

CAB BAD BAG BED HID BID AID DIG

CHOSING FIRST

AID BAD BAG BED BIG

CAB DIG HID

AID BAD BAG BED BIG CAB DIG HID

Page 7: Algo Assignment

b)

Page 8: Algo Assignment

The edges of the original graph can be divided into three classes: forward edges, which point from a node of the tree to one of its descendants, back edges, which point from a node to one of its ancestors, and cross edges, which do neither. Sometimes tree edges, edges which belong to the spanning tree itself, are classified separately from forward edges. If the original graph is undirected then all of its edges are tree edges or back edges.

Tree edges = {(v5, v4), (v4, v2), (v2, v3), (v3, v0), (v0, v1)}

Back edge= {(v1, v2), (v3, v5), (v1, v4)}

Cross edge = {(v5, v1)}

Forward edge = {(v3, v5)}

Page 9: Algo Assignment

Ans 5 a)

Minimum spanning tree: 18+21+25+29+31+34+46

=204

Ans 7c)

Dynamic programming is applicable for an optimization problem only if

1. The sub problems are not independent i.e. sub problems are related to another sub problems.

2. Dynamic programming algorithm solves every sub problems just once and then saves its answer in the table.