Você está na página 1de 10

ASSESSMENT 1 1. Amortized analysis: it is a method of analyzing algorithms that considers the entire sequence of operations of the program.

It allows for the establishment of a worst-case bound for the performance of an algorithm irrespective of the inputs by looking at all of the operations. It is particularly useful because it guarantees worst-case performance while accounting for the entire set of operations in an algorithm.It is used to analyze the average time required to perform each operation involved in a sequence of data structure operation. 2. Run time for algorithm 1 x=100n Run time for algorithm 2 y=2n The value of n such that x<y are greater than or equal to 15 The minimum value of n such that x<y will be equal to15 3. T(n)=a n=1,a is a constant 2T(n/2)+cn n>1,c is a constant
2

Where n is a power of 2,n=2K


T(n) =2(2T(n/4)+cn/2)+cn =4T(n/4)+2cn =4(2T(n/8)+cn/4)+2cn : :

=2kT(1)+kcn
=an+cnlogn It is easy to see that if 2 <n<=2

k+1 k+1 , then T(n)<=T(2 )

Therefore T(n)=O(nlogn).

4. For any lower triangular matrix ai j we can compute the transpose by getting the values in inverse order,(i.e) get value(j,i); i-1 L.M= a+ k + j k=1 int Index(int i,int j) { int k=1 int diff=1; for( k<i-1;i++) diff + = k; return diff + (j-1); } int Setvalue (i,j)

{ return a[Index(i,j)]; } int Print Transpose() { for(int j=1;j<= rowcount;j++) { for(int i=j;i<= rowcount;i++) Cout<< SetValue(j,i); Cout<<end l; } 5.Function to insert: void Insert(int *h,int x) { if(n==Maxsize) // n-> size of heap { cout<<Heap is Full; return; } int p=n/2 //p is the parent of new node if(!p) { h[1]=x; return; } Switch(level(p)) { Case Min: if(x<h[p]) { h[n]=h[p]; Verify Min(p,x); } else Verify Max(n,x); break; Case Max: if(x>h[p]) { h[n]=h[p] verify Max (p,x); } else Verify Min (h,x); }

} Insert complexity is O (log 2 n ) ) 6. To find whether P is in Min Heap or not, void Insert(int *h,int x) { int i; if(n==Maxsize) // n-> size of heap { cout<<Deap is Full; return; } n++ if(n==1) {d[2]=x; return} int p=n+1; switch(MaxHeap(p)) case TRUE: i=MinPartner(p); if(x.key<d[i].key) { d[p]=d[i]; MinInsert(i,x); } else MaxInsert(p,x); break; case FALSE: i=MinPartner(p); if(x.key>d[i].key) { d[p]=d[i]; MaxInsert(i,x); } else MinInsert(p,x); } 7. (i) Deap : Values to be inserted; 20,7,9,45,63,90,18,33,1,5,41,30,65,

Insert 20:

2 20 0

Insert 7:
7
2 0

Insert 9:
7 9
2 0

Insert 45:
7 9
4 5 2 0

Insert 63:
7
9

6 3 4 5 2 0

2 0

Insert 90:
7
9 2 0 4 5 9 0 6 3

Insert 18:

7
9 1 8 2 0 4 5

9 0 6 3

Insert 33:
7
9 1 8 2 0 3 3 4 5 9 0 6 3

Insert 1:
1
9 1 8 3 3 2 0 7 4 5 9 0 6 3

Insert 5:

1
1 8 9 3 3

2 0 5 7

4 5

9 0 6 3

Insert 41:
1
9 1 8 3 3 2 0 5 4 1 7 4 5 9 0 6 3

Insert 30:
1
9 1 2 0 5 4 1 7 4 5 9 0 6 3 3 3 9 0 4 5 2 0 3 0 5 4 1 7 6 3 3 3 6 5

Insert 65: Final Deap. 8


3 0

1
9 1 8

(ii) Min Binomial heap

Insert 20: Insert 7:

2 0

7 2 0

Insert 9:
2 0

Insert 45:
7 2 0

Insert 63:
2 0

4 5

6 3

4 5 7 6 3 9 0 9

Insert 90:

2 0

4 5

Insert 18:
2 0

7 9 0 9

6 3

1 8

4 5

Insert 33:
7 2 0

1 3 8 3 9 6 9 3 0

Insert 1:

4 5 2 0

1 3 8 3 6 9 3 0

4 5 1

Insert 5:
7 5 2 0 4 9 5 3 3 1 8 9 0 6 3

Insert 41:
5 2 0

4 1

1 3 8 3 6 9 3 0

4 5

Insert 30:
7

5 3 4 0 1

2 0 9

1 3 8 3 6 9 3 0

4 5

Insert 65: Final Min binomial


7 6 5

5 3 4 0 1

2 0 9

1 3 8 3 6 9 3 0

4 5

Você também pode gostar