Está en la página 1de 64

JawaharlalNehruEngineeringCollege

LaboratoryManual

DESIGN AND ANALYSIS OF ALGORITHMS

For Third Year Students CSE Dept: Computer Science & Engineering (NBA Accredited)

Author JNEC, Aurangabad

FOREWORD
It is my great pleasure to present this laboratory manual for Third Year engineering students for the subject of DESIGN AND ANALYSIS OF ALGORITHMS keeping in view the vast coverage required for analysis of computer algorithms.

As a student, many of you may be wondering about the subject and exactly that has been tried through this manual.

As you may be aware that MGM has already been awarded with ISO 9000 certification and it is our aim to technically equip students taking the advantage of the procedural aspects of ISO 9000 Certification.

Faculty members are also advised that covering these aspects in initial stage itself, will relive them in future as much of the load will be taken care by the enthusiastic energies of the students once they are conceptually clear.

Dr. S.D.Deshmukh Principal

LABORATORY MANUAL CONTENTS


This manual is intended for the Third Year students of Computer Science and Engineering in the subject of Design and Analysis of Algorithms. This manual typically contains practical/Lab Sessions related to the subject to enhance understanding.

Algorithmic is a branch of computer science that consists of designing and analyzing computer algorithms 1. The design pertain to i. The description of algorithm at an abstract level by means of a pseudo language, and ii. Proof of correctness that is, the algorithm solves the given problem in all cases. 2. The analysis deals with performance evaluation (complexity analysis). Students are advised to thoroughly go though this manual rather than only topics mentioned in the syllabus as practical aspects are the key to understanding and conceptual visualization of theoretical aspects covered in the books.

Good Luck for your Enjoyable Laboratory Sessions

Prof. D.S.Deshpande HOD, CSE

Mrs. Parminder Kaur Asst. Prof., CSE Dept

DOs and DONTs in Laboratory:

1. Make entry in the Log Book as soon as you enter the Laboratory.

2. All the students should sit according to their roll numbers starting from their left to right.

3. All the students are supposed to enter the terminal number in the log book.

4. Do not change the terminal on which you are working.

5. All the students are expected to get at least the algorithm of the program/concept to be implemented.

6. Strictly observe the instructions given by the teacher/Lab Instructor.

Instruction for Laboratory Teachers::

1.

Submission related to whatever lab work has been completed should be

done during the next lab session. The immediate arrangements for printouts related to submission on the day of practical assignments.

2. Students should be taught for taking the printouts under the observation of lab teacher.

3. The promptness of submission should be encouraged by way of marking and evaluation patterns that will benefit the sincere students.

SUBJECT INDEX

Sr. No. 1 2 3 4 5 6 7 8 9 10

Title

Page No. 6 Introduction Program to implement Stack & Queue. 8 Program to implement heap sort. 13 Program for finding the maximum & minimum using divide & 18 conquer method. Program to implement merge sort or quick sort using divide and 20 conquer method. Program to implement knapsack problem using greedy method. 28 Program for finding minimum cost spanning tree using greedy 33 method. Program for finding all pairs shortest paths. 40 Program to implement traveling salesperson problem using 44 dynamic programming. Program for finding shortest path for multistage graph using 47 dynamic programming. Program to implement 8queens problem using backtrack method. 50 A practice questionanswer set 54

Note: All the programs should be implemented using C language.

Introduction
1 What is Algorithm? * a clearly specified set of simple instructions to be followed to solve a problem Takes a set of values, as input and produces a value, or set of values, as output * may be specified In English as a computer program As a pseudocode 2 Data structures * Methods of organizing data 3 Program = algorithms + data structures 4 Why need algorithm analysis? a. writing a working program is not good enough b. The program may be inefficient! c. If the program is run on a large data set, then the running time becomes an issue Algorithm Analysis * We only analyze correct algorithms / programs * An algorithm is correct If, for every input instance, it halts with the correct output * Incorrect algorithms Might not halt at all on some input instances Might halt with other than the desired answer * Analyzing an algorithm Predicting the resources that the algorithm requires Resources include 1 Memory 1 Communication bandwidth 1 Computational time (usually most important) * Factors affecting the running time computer compiler algorithm used input to the algorithm 1 The content of the input affects the running time 1 typically, the input size (number of items in the input) is the main consideration E.g. sorting problem the number of items to be sorted E.g. multiply two matrices together the total number of elements in the two matrices * Machine model assumed Instructions are executed one after another, with no concurrent operations Not parallel computers 6

Example Calculate
N

i
i= 1

Algorithm to solve this example: int sum(int n) { int psum psum=0 // Line 1 for(int i=0i<=ni++) // Line 2 psum=psum+i*i*i // Line 3 return psum // Line 4 } This is how we calculate time complexity * Lines 1 and 4 count for one unit each * Line 3: executed N times, each time four units * Line 2: (1 for initialization, N+1 for all the tests, N for all the increments) total 2N + 2 * total cost: 6N + 4 O(N) Time complexity can be classified as: * Worstcase running time of an algorithm The longest running time for any input of size n An upper bound on the running time for any input guarantee that the algorithm will never take longer Example: Sort a set of numbers in increasing order and the data is in decreasing order The worst case can occur fairly often 1 E.g. in searching a database for a particular piece of information * Bestcase running time sort a set of numbers in increasing order and the data is already in increasing order * Averagecase running time May be difficult to define what average means, usually it is taken as average of best and worst values.

1.Program toimplementStack&Queue.
Aim:Writeaprogram toimplementstack&queue. Theory: 1. ImplementationofStack
Stack is a data structure which works based on principle of last in first out (LIFO). In computing world, stack data structure can be applied in many applications such as parsing syntax of expressions, runtime memory management (used in Java virtual machine)andsolvingsearchproblem.

Figure1.1:Stackoperations(A,B,CshowspushandD,EshowsPop) By seeingthe figureabove youcanseethatthis datastructureisreallyarestricted list. Youhaverestrictedtheaccesstooneendofthelistbyusingthepopandpushoperations. Theresultofthisrestrictionisthatitemsinthelistwillbestoredoneontopoftheother. Wemustfirstremovealltheitemsaboveittillyougettothebottomitem."LastIn,First Out"orLIFO,whichisusedtodescribethebehavior,sincethelastitemtoenterthestack isthefirstitemtoleavethestack.Thetopitemistheitemalwaysthelastitemtoenter the stack and it is always the first item to leave the stack since no other items can be removeduntilthetopitemisremoved. StackOperations: push(newitem :itemtype) Addsanitemontothestack. top():itemtype Returnsthelastitempushedontothestack. pop() Removesthemostrecentlypusheditemfromthestack. isempty():Boolean Trueiffnomoreitemscanbepoppedandthereisnotopitem. isfull():Boolean Trueiffnomoreitemscanbepushed. getsize():Integer Returnsthenumberofelementsonthestack.

Algorithm:
PUSH Step1: Incrementthe'top'by1 Step2: Checkforthe'stackoverflow'condition.If stacknotoverflowingthengotostep 3elsesay"stack overflow"andquit Step3: Putthenewelementatthepositionpointedby'top' Program Snippets: voidpush() { if(top==SIZE 1) { printf("Stackisfull(overflow)) getch() return } top++ printf("EntertheelementtoPUSH:") scanf("%d",&stack[top]) } POP Step1: Checkforthe'stackunderflow'condition.If stacknotempty thengotostep2 elsesay"stackunderflow"andquit Step2: Displayandremoveelementatthepositionpointedby'top' Step3: Decrementthe topby1. Program Snippets: voidpop() { if(top== 1) { printf("Stackisempty(underflow)) getch() return } printf("ThePOPPEDelementis:%d,stack[top]) getch() top } voiddisplay() { if(top== 1) { 9

printf("Stackisempty(underflow)) getch() return } printf("Theelementsinstackfrom TOPare:") for(i=topi>=0i) printf("%d",stack[i]) getch() }

Output:
ForPushoperation:Entertheelementtopush:10 ForPushoperation:Entertheelementtopush:20 ForPushoperation:Entertheelementtopush:30 ForPop operation:ThePOPPEDelementis:30 ForDisplay operation:TheelementsinstackfromTOPare: 20 10

Conclusion:
Alloperationsexceptgetsize()canbeperformedinO(1)time.getsize()runsinatworst O(N).

2. ImplementationofQueue
Queue isadatastructurewhichworksbasedon principleof first in firstout(FIFO).In computing world, queue data structure can be applied in many applications FCFS schedulingalgorithm,bufferofprinter.

Figure1.2:Queueoperations(ShowsInsertandDelete) By seeingthe figureabove youcanseethatthis datastructureisreallyarestricted list. You have restricted the access to two ends of the list by using the insert and delete operations. The result of this restriction is that items in the list will be stored from the REARendandcanberemovedfromtheotherendcalledFRONT. QueueOperations: insert(newitem :itemtype) Addsanitemontothequeuefromrearend. delete() Removesthelastentereditemfromthequeue. isempty():Boolean Trueiffnomoreitemscanbedeleted. isfull():Boolean 10

Trueiffnomoreitemscanbeinserted. getsize():Integer Returnsthenumberofelementsinthequeue.

Algorithm:
INSERT Step1: Incrementthe'rear'by1 Step2: Checkforthe'queueoverflow'condition.Ifqueuenotoverflowingthengoto step3elsesay"queueoverflow"andquit Step3: Putthenewelementatthepositionpointedby'rear' Program Snippets: voidinsert() { if(rear==SIZE 1) { printf("Queueisfull(overflow)) getch() return } rear++ printf("Entertheelementtoinsert:") scanf("%d",&queue[rear]) } DELETE Step1: Checkforthe'queueunderflow'condition.Ifqueuenotempty thengotostep2 elsesay"queueunderflow"andquit Step2: Displayandremoveelementatthepositionpointedby'front' Step3: Incrementthefrontby1. Program Snippets: voiddelete() { if(front==1) { printf("Queueisempty(underflow)) getch() return } printf("Thedeletedelementis:%d,queue[front]) getch() front 11

} voiddisplay() { if(front==1) { printf("Queueisempty(underflow)) getch() return } printf("Theelementsin Queueare:") for(i=reari>=fronti) printf("%d",queue[i]) getch() }

Output:
ForInsert operation:Entertheelementtoinsert:10 ForInsert operation:Entertheelementtoinsert:20 ForInsert operation:Entertheelementtoinsert:30 ForDeleteoperation:Thedeletedelementis:10 ForDisplay operation:Theelementsin queueare:20 30

Conclusion:
Alloperationsexceptgetsize()canbeperformedinO(1)time.getsize()runsinatworst O(N).

12

2.Programtoimplement heapsort.
Aim:Writeaprogram toimplementheapsort. Theory:
Heapsort(method)isacomparisonbasedsortingalgorithm,andispartoftheselection sort family. Although somewhat slower in practice on most machines than a good implementationofquicksort,ithastheadvantageofaworstcase(n logn)runtime. Heapsortisaninplacealgorithm. Itbeginsby buildingaheapoutofthedataset,andthenremovingthe largestitemand placing itattheendofthe sortedarray.Afterremovingthe largest item, itreconstructs theheap,removesthelargestremainingitem,andplacesitinthenextopenpositionfrom theendofthesortedarray.Thisisrepeateduntiltherearenoitemsleftintheheapand thesortedarrayisfull.Elementaryimplementationsrequiretwoarraysonetoholdthe heapandtheothertoholdthesortedelements. Heapsortinsertstheinputlistelementsintoaheapdatastructure.Thelargestvalue(ina maxheap) or the smallest value (in a minheap) are extracted until none remain, the valueshavingbeenextractedinsortedorder.Theheap'sinvariantispreservedaftereach extraction,sotheonlycostisthatofextraction. During extraction, the only space required is that needed to store the heap. In order to achieveconstantspaceoverhead,theheapisstoredinthepartoftheinputarraythathas notyetbeensorted. MaxHeap: Thevaluesortedinanodeisgreaterthanorequaltothevaluesstoredatits children .Sincetherearenonodesatlevellunlesslevell 1iscompletelyfilled,aheap canbestoredinanarraylevelbylevel (beginningwiththeroot),lefttorightwithineach level. TheROOTisalwaysstoredatA[1] PARENT(i)=[i/2] LEFTCHILD(i)=2i RIGHTCHILD(i)=2i+1 Length[A]:numberofelementsinthearrayA Heapsize[A]:numberofelementsintheheapstoredwithin arrayA. Restoretheheapproperty:IfA[i]sleftsubtreeandrightsubstreeareMaxHeaps,but A[i]violatestheheapproperty,i.e.,A[i]issmallerthanitschildren,MaxHeapify(A,i)is called to let A[i] float down in the maxheap so that the subtree rooted at index i becomesaMaxHeap. Algorithm MaxHeapify(A,i)//Restoretheheappropertyforthetree rootedati.When called,theleftandrightsubtreesmustbeMaxHeaps. if(A[i]A[2i])ANDA[i]>=A[2i+1]) return else letjbethelargestchildofi 13

exchangeA[i]andA[j] MaxHeapify(A,j) Buildaheap: UseMaxHeapifyinabottomupfashiontoconvertA[1..n]toaMaxHeap. Algorithm BuildMaxHeap(A) Heapsize[A]=Length[A] fori:=Length[A]/2 downto1 MaxHeapify(A,i) HeapSort: Algorithm Heapsort(A) BuildMaxHeap(A) (n) fori:=nto2 exchangeA[1]andA[i] Heapsize[A]:=Heapsize[A]1 MaxHeapify(A,1) (lgn) RemoveMaxelementfromthemaxheaptree: Algorithm ExtractMax(A)//Removeandreturnthemax. MAX:=A[1] ExchangeA[1]andA[Heapsize[A]] Heapsize:=Heapsize1 MaxHeapify(A,1) returnMAX

14

Figure2.1:BuildingMaxHeaptree

15

Figure2.2:Sorting using Heapsort

ProgramSnippets: voidmain() { int*x,i,n inttemp voidheap(int*,int) clrscr() fflush(stdin) printf(" HeapSort") printf("EnterHowmanyNumbers:") scanf("%d",&n) x=(int*)malloc(n*sizeof(int)) for(i=0i<ni++) { fflush(stdin) 16

scanf("%d",&x[i]) } heap(x,n) for(i=n1i>=1i) { temp=x[i] x[i]=x[0] x[0]=temp heap(x,i1) } printf("ResultantArray") for(i=0i<ni++) printf("%d ",x[i]) free(x) getch() } voidheap(int*a,intn) { inti,temp for(i=n/2i>=0i) { if(a[(2*i)+1]<a[(2*i)+2]&&(2*i+1)<=n&&(2*i+2)<=n) { temp=a[(2*i)+1] a[(2*i)+1]=a[(2*i)+2] a[(2*i)+2]=temp } if(a[(2*i)+1]>a[i]&&(2*i+1)<=n&&i<=n) { temp=a[(2*i)+1] a[(2*i)+1]=a[i] a[i]=temp } } }

Output:
HeapSort EnterHowmanyNumbers:10 4 1 3 2 16 9 ResultantArray : 1 2 3 4 7 8

10 9

14 10

8 14

7 16

Conclusion:
Complexity ofheapsortis:(n)+O(nlgn)=O(nlgn). 17

3.Programforfindingthemaximum&minimumusing divide&conquermethod.
Aim:Writeaprogramforfindingthemaximum&minimumusingdivide& conquermethod. Theory:
DivideandConquer Algorithms: These algorithms have the following outline: To solveaproblem,divideitintosubproblems.Recursivelysolvethesubproblems.Finally glue the resulting solutions together to obtain the solution to the original problem. Progress here is measured by how much smaller the subproblems are compared tothe originalproblem. Finding Maximum/minimum numbersinalist: Suppose we wish to find the minimum and maximum items in a list of numbers. How many comparisonsdoesittake? AlgorithmStraightMaxMin(a,n,max,min) { max=min=a[1] fori=2tondo { if(a[i]>max)thenmax=a[i] if(a[i]<min)thenmin=a[i] } }

Letusanalyzethenumberofcomparisonsmadeforfindingthemaximum.Itcanbeseen that the complexity will be proportional to the number of comparisons. To find the maximum,weseefromtheforloopthatN1comparisonswillbemade.Thealgorithm forfindingtheminimumfollowsinasimilarway.Ifwefindtheminimumafterfinding the maximum, it is enough to find the minimum among the remaining N1 numbers (assuming there are at least 2 numbers otherwise, maximumand minimum will be the same).SotofindthemaximumandminimumofNnumbers,weneed(N1)+(N2)= 2N 3 comparisons. Now, let us see whether we can develop an algorithm whose complexityisbetterthanthatofsolution1usingthedivideandconquermethod. Thiswillbethebasicprincipleofthealgorithmtofindthemaximumandminimumofan arrayof numbers.The main idea ofusingthedivideandconquerstrategy isdescribed below: DividearrayAintotwosubpartsA1andA2suchthatA1andA2together form A. Findthe maximumand minimumof each byrecursiveapplicationofthe algorithm. The maximum and minimum ofA can be computed fromthe maximum andminimumof A1andA2bymakingtwocomparisons. 18

Algorithm MINMAX(A,L,U,mx,mn) (Aisanarrayrangingbetween L andUmx andmn willhavethemaximum andminimumvaluerespectivelywhentheprocedureterminates) varl1,l2,U1,U2:integer(LocalVariables) mx1,mx2,mn1,mn2:integer(LocalVariables) { If(U==L)thenmax=min=a[L] elseIf (UL+1)=2 then { ifA[L]>A[U]thenmx:=A[L](maximum) mn:=A[U](minimum ) else mx:=A[U](maximum) mn:=A[L](minimum) } else { //SplitAintotwohalvesA1andA2withlowerandupperindicestobel1,U1 andl2, U2respectively call MINMAX(A,l1,U1, mx1,mn1) call MINMAX(A,l2,U2,mx2,mn2) mx :=maximum(mx1,mx2)(maximumreturnsthemaximumofthetwo values) mn :=minimum(mn1,mn2)(minimumreturnstheminimumofthetwo values) } }

Input:
22 13 5 8 15 60 17 31 47

Output:
Maximum:60 Minimum:8

Conclusion:
Thus we saw using StraightMaxMin algorithm, we need (N1) + (N2) = 2N 3 comparisons.Ifwesolvethesameproblemusingdivideandconquertechniquenumber ofcomparisonsarereducedto3n/22.Thissaves25%comparisons.

19

4. Programtoimplementmergesortorquicksort using divide&conquermethod.


Aim: Write a programto implement merge sort or quick sort using divide andconquermethod. Theory:
DivideandConquer Algorithms: These algorithms have the following outline: To solveaproblem,divideitintosubproblems.Recursivelysolvethesubproblems.Finally glue the resulting solutions together to obtain the solution to the original problem. Progress here is measured by how much smaller the subproblems are compared tothe originalproblem. Youhavealreadyseenanexampleofadivideandconqueralgorithmin3rdassignment. FirstwewillseeMERGESORT. Theideabehindmergesortistotakealist,divideitintotwosmallersublists,conquer each sublistbysortingit,andthencombinethetwosolutionsforthesub problemsintoa singlesolution.Thesethreebasicsteps{divide,conquer,andcombine{lie behind most divideandconqueralgorithms. With merge sort, we kept dividing the list into halves until there was just one element left. In general, we may divide the problem into smaller problems in any convenient fashion. Also, in practice it may not be best to keep dividing until the instances are completely trivial. Instead, it may be wise to divide until the instances are reasonably small,andthenapplyan algorithmthatisfasteronsmallinstances. Mergesortisbasedonthedivideandconquerparadigm.TheMergesortalgorithmcan bedescribedingeneraltermsasconsistingofthefollowingthreesteps: 1.DivideStep If given array A has zero or one element, return S it is already sorted. Otherwise,divideAintotwoarrays,A1 andA2,eachcontainingabouthalf oftheelementsof A. 2.RecursionStep Recursivelysortarray A1 andA2. 3.ConquerStep Combinetheelements back inA by mergingthe sortedarraysA1 andA2 intoasortedsequence. We can visualize Mergesort by means of binary tree where each node of the tree representsarecursivecallandeachexternalnoderepresentindividualelementsofgiven arrayA. Such atree is called Mergesorttree.The heartof the Mergesort algorithm is conquerstep,whichmergetwosortedsequencesintoasinglesortedsequence.

20

Figure4.1:Binarytreeformergesort To begin, suppose that we have two sorted arrays A1[1], A1[2], . . , A1[M] and A2[1], A2[2], . . . , A2[N]. The following is a direct algorithm of the obvious strategy of successively choosingthesmallestremainingelementsfrom A1toA2andputtingitin A. Mergingtwosortedsubarrays: Algorithm Merge(low,high,mid) //a[low:high]isaglobal arraycontainingtwosortedsubsetsina[low:mid}andin a [mid+1: high]. The goal is to merge these two sets into a single set residing in a [low: high].b[]isanauxiliaryglobalarray. { h=lowi=lowj=mid+1 while((h<=mid)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=jtohighdo { b[i]=a[k]i=i+1 } else for(k=htomiddo { b[i]=a[k]i=i+1 } fork=lowtohighdo a[k]=b[k] 21

} MergeSort Algorithm MergeSort(low,high) //a[low:high]isaglobalarray tobesorted. { if(low<high)then { //dividearrayintosubarrays mid=[(low+high)/2] MergeSort(low,mid) MergeSort(mid+1,high) //combinethesortedsubarrays Merge(low,mid,high) } }

ProgramSnippets: voidmergeSort(intnumbers[],int temp[],int array_size) { m_sort(numbers,temp,0,array_size1) } voidm_sort(intnumbers[],int temp[],intleft,int right) { intmid if (right>left) { mid=(right+left)/2 m_sort(numbers,temp,left,mid) m_sort(numbers,temp,mid+1,right) merge(numbers,temp,left,mid+1,right) } } voidmerge(intnumbers[],int temp[],intleft,int mid,int right) { inti,left_end,num_elements,tmp_pos left_end=mid1 tmp_pos=left num_elements=rightleft+1 while((left<=left_end)&&(mid<=right)) { if (numbers[left]<=numbers[mid]) { temp[tmp_pos]=numbers[left] tmp_pos=tmp_pos+1 22

left=left+1 } else { temp[tmp_pos]=numbers[mid] tmp_pos=tmp_pos+1 mid=mid+1 } } while(left<=left_end) { temp[tmp_pos]=numbers[left] left=left+1 tmp_pos=tmp_pos+1 } while(mid<=right) { temp[tmp_pos]=numbers[mid] mid=mid+1 tmp_pos=tmp_pos+1 } for (i=0i<=num_elementsi++) { numbers[right]=temp[right] right=right1 } }

Input: Output:

13

15

60

17

31

47

Sortednumbersare:8

13

15

17

31

47

60

Conclusion:
PerformanceAnalysisof MergeSort LetT(n)bethetimetakenbythisalgorithmtosortanarrayof nelementsdividingAinto sub arrays A1 and A2 takes linear time. It is easy to see thatthe Merge (A1,A2,A) also takesthelineartime.Consequently, T(n)= T(n/2)+ T(n/2)+(n) forsimplicity T(n)=2T (n/2)+(n) The total running time of Merge sort algorithm is O(n lg n), which is asymptotically optimal like Heap sort, Merge sort has a guaranteed n lg n running time. Merge sort required (n) extra space. Merge is not inplace algorithm. The only known ways to merge inplace (without any extra space) are too complex to be reduced to practical program.

23

NowwewillstudyQuickSort The basic concept is to pick one of the elements in the array as a pivot value around whichtheotherelementswillberearranged.Everythinglessthanthepivotismovedleft ofthepivot(intotheleftpartition).Similarly,everythinggreaterthanthepivotgoesinto therightpartition.Atthispointeachpartitionisrecursivelyquicksorted. TheQuicksortalgorithm is fastestwhenthe medianofthearray ischosenasthepivot value.Thatisbecausetheresultingpartitionsareofverysimilarsize.Eachpartitionsplits itselfintwoandthusthebasecaseisreachedveryquickly. In practice, the Quick sort algorithm becomes very slow when the array passed to it is alreadyclosetobeingsorted.Becausethereisnoefficientwayforthecomputertofind themedianelementtouseasthepivot,thefirstelementofthearrayisusedasthepivot. So when the array is almost sorted, quick sort doesn't partition it equally. Instead, the partitions are asymmetrical like in Figure 4.2. This means that one of the recursion branches is muchdeeperthantheother,andcausesexecutiontimetogoup.Thus, it is said that the more random the arrangement of the array, the faster the Quicksort Algorithmfinishes.

Figure4.2:TheidealQuicksortonarandomarray

24

Quicksortworksbypartitioningagivenarray A[p..r]intotwononemptysubarray A[p ..q]andA[q+1..r]suchthateverykeyinA[p..q]islessthanorequaltoeverykeyin A[q+1..r].Thenthetwosub arraysaresortedbyrecursivecallstoQuicksort.Theexact positionofthepartitiondependsonthegivenarrayandindexqiscomputedasapartof thepartitioningprocedure. Algorithm QuickSort(p,q) //Sortstheelementsa[p],,a[q]whichresideintheglobalarraya[1:n]into ascendingorder. { if(p<r) then // iftherearemorethanoneelement { // dividePintotwosubproblems j=Partition(a,p,q+1) // jisthepositionofthepartitioningelement // solvethesubproblems QuickSort(p, j 1) QuickSort(j +1,q) } } As a first step, Quick Sort chooses as pivot one of the items in the array to be sorted. Thenarray isthenpartitionedoneithersideofthepivot.Elementsthatarelessthanor equal to pivot will move towardthe left and elements that are greater than or equal to pivotwillmovetowardtheright. PartitioningtheArray Partitioningprocedurerearrangesthesub arraysinplace. AlgorithmPartition(a,m,p) // Within a[m],a[m+1],.,a[p1] the elements are rearranged in such a manner that if initiallyt=a[m]thenaftercompletiona[q]=tforsomeqbetweenmandp1,a[k]|<=tfor m<=k<q,anda[k]>=t forq<k<p.qisreturned.Seta[p]= . { v =a[m]i=mj=p repeat { repeat i=i+1 until a[i]>=v repeatj=j1 until a[j]<=v if i <j thenexchangeA[i]A[j] }until(i>=j) a[m]=a[j]a[j]=vreturnj }

25

Partitionselectsthefirstkey,a[p]asapivotkeyaboutwhichthearraywillpartitioned: Keysa[p]willbemovedtowardstheleft. Keysa[p]willbemovedtowardstheright. ProgramSnippets: voidquickSort(int numbers[],int array_size) { q_sort(numbers,0,array_size1) } voidq_sort(intnumbers[],intleft,int right) { intpivot,l_hold,r_hold l_hold=left r_hold=right pivot=numbers[left] while(left<right) { while((numbers[right]>=pivot)&&(left<right)) right if (left!=right) { numbers[left]=numbers[right] left++ } while((numbers[left]<=pivot)&&(left<right)) left++ if (left!=right) { numbers[right]=numbers[left] right } } numbers[left]=pivot pivot=left left=l_hold right=r_hold if (left<pivot) q_sort(numbers,left,pivot1) if (right>pivot) q_sort(numbers,pivot+1,right) }

Input: Output:

13

15

60

17

31

47

Sortednumbersare:8

13

15

17

31

47

60 26

Conclusion:
PerformanceAnalysisof QuickSort Therunningtimeofquicksortdependsonwhetherpartitionisbalancedorunbalanced, which in turn depends on which elements of an array to be sorted are used for partitioning. Averygoodpartitionsplitsanarrayupintotwoequalsizedarrays.Abadpartition,on otherhand,splitsanarrayupintotwoarraysofverydifferentsizes.Theworstpartition puts only one element in one array and all other elements in the other array. If the partitioningisbalanced,theQuicksortrunsasymptoticallyasfastasmergesort.Onthe other hand, if partitioning is unbalanced, the Quick sort runs asymptotically as slow as insertionsort. BestCase The best thing that could happen in Quick sort would be that each partitioning stage dividesthearrayexactly in half.Inotherwords,thebesttobea medianofthekeys in A[p..r]everytimeprocedure'Partition'iscalled.Theprocedure'Partition'alwayssplit thearraytobesortedintotwoequalsizedarrays. If the algorithm 'Partition' produces two regions of size n/2. The recurrence relation is then T(n)=T(n/2)+T(n/2)+ (n) =2T(n/2)+ (n) OR T(n)= (nlgn)

27

5. Programtoimplement knapsackproblemusinggreedy method.


Aim: Write a program to implement knapsack problem using greedy method. Theory:
Greedy algorithms are simple and straightforward. They are shortsighted in their approachinthesensethattheytakedecisionsonthebasisofinformationathandwithout worryingabouttheeffectthesedecisionsmayhaveinthefuture.Theyareeasytoinvent, easytoimplementandmostofthetimequiteefficient.Manyproblemscannotbesolved correctlybygreedyapproach.Greedyalgorithmsareusedtosolveoptimizationproblems GreedyApproach GreedyAlgorithmworksbymakingthedecisionthatseemsmostpromisingatany momentitneverreconsidersthisdecision,whateversituationmayariselater. Asanexampleconsidertheproblemof "MakingChange". Coinsavailableare: dollars(100cents) quarters(25cents) dimes(10cents) nickels(5cents) pennies(1cent) Problem Make a change of a given amount using the smallest possible number of coins. InformalAlgorithm Startwithnothing. ateverystagewithoutpassingthegivenamount. o addthelargesttothecoinsalreadychosen. Example Makeachangefor2.89(289cents)heren=2.89andthesolutioncontains2 dollars(200cents)3quarters(75cents),1dime(10cents)and4pennies(4cents).The algorithm isgreedy because atevery stage itchoosesthe largestcoinwithoutworrying abouttheconsequences.Moreover,itneverchangesitsmindinthesensethatonceacoin hasbeenincludedinthesolutionset,itremainsthere. Toconstructthesolutioninanoptimalway.Algorithmmaintainstwosets.Onecontains chosenitemsandtheothercontainsrejecteditems. Thegreedyalgorithmconsistsoffour(4)function. 1. Afunctionthatcheckswhetherchosensetofitemsprovideasolution. 2. Afunctionthatchecksthefeasibilityofaset. 28

3. Theselectionfunctiontellswhichofthecandidatesisthemostpromising. 4. An objective function, which does not appear explicitly, gives the value of a solution. StructureGreedyAlgorithm

Initiallythesetofchosenitemsisemptyi.e.,solutionset. Ateachstep
o itemwillbeaddedinasolutionsetbyusingselectionfunction. o IFthesetwouldnolongerbefeasible

rejectitemsunderconsideration(andisneverconsideragain).

o ELSEIFsetisstillfeasibleTHEN

addthecurrentitem.

Definitionsoffeasibility A feasible set (of candidates) is promising if it can be extended to produce not merelyasolution,butanoptimalsolutiontotheproblem.Inparticular,theempty setisalwayspromisingwhy?(becauseanoptimalsolutionalwaysexists) Agreedy strategy usually progresses in a topdown fashion, making one greedy choice afteranother,reducingeachproblemtoasmallerone. GreedyChoiceProperty The "greedychoice property" and "optimal substructure" are two ingredients in the problemthatlendtoagreedystrategy. GreedyChoiceProperty It says that a globally optimal solution can be arrived at by making a locally optimal choice.

KnapsackProblem
Statement Wearegivennobjectsandaknapsackorbag.Objectihasaweightwi and theknapsackhasacapacitym. Therearetwoversionsofproblem I. Fractionalknapsackproblem

Thesetupissame,wecantakefractionsofitems,meaningthattheitemscan bebrokenintosmallerpiecessothatwemaydecidetocarryonlyafractionof xi of item i, where 0 xi 1. If a fraction xi of object i is placed into the knapsack,thenaprofitpi xiisearned. The objective is to obtain a filling of the knapsack that maximizes the total profitearned.

29

Theideaistocalculateforeachobjecttheratioof value/cost,andsortthemaccordingto thisratio.Thenyoutaketheobjectswiththehighestratiosandaddthemuntilyoucant addthenextobjectaswhole.Finallyaddasmuchasyoucanofthenextobject. So,forourexample: v(weight)={4,2,2,1,10} c(profit)={12,1,2,1,4} r={1/3,2,1,1,5/2} Fromthisitsobviousthatyoushouldaddtheobjects:5,2,3,and4andthenasmuchas possibleof1. Wecanchooseobjectslikethis: Addedobject5(10$,4Kg)completely inthebag.Spaceleft:11. Addedobject2(2$,1Kg)completely inthebag.Spaceleft:10. Addedobject3(2$,2Kg)completely inthebag.Spaceleft:8. Addedobject4(1$,1Kg)completely inthebag.Spaceleft:7. Added58%(4$,12Kg)ofobject1inthebag. Filledthebagwithobjectsworth15.48$. II. 01knapsackproblem

Thesetupisthesame,buttheitemsmaynotbebrokenintosmallerpieces,so we maydecideeithertotakean itemortoleave it(binarychoice),but may nottakeafractionofanitem.

Algorithmfractionalknapsack(w,v,W) { fori=1ton dox[i]=0 weight=0 whileweight<W doi=bestremainingitem if weight+w[i]W then x[i]=1 weight=weight+w[i] else x[i]=(w weight)/w[i] weight=W return x }

30

ProgramSnippets: intn=5/*Thenumberofobjects*/ intc[10]={12,1,2,1,4}/*c[i]isthe*COST*oftheith object i.e.whatYOUPAYtotaketheobject*/ intv[10]={4,2,2,1,10}/*v[i]isthe*VALUE*oftheithobject i.e.whatYOUGETfortakingtheobject*/ intW=15/*Themaximumweightyoucantake*/ voidsimple_fill() { intcur_w floattot_v inti,maxi intused[10] for(i=0i<n++i) used[i]=0/*Notusedtheithobjectyet*/ cur_w=W while(cur_w>0) {/*whilethere'sstillroom*/ /*Findthebestobject*/ maxi =1 for(i=0i<n++i) if((used[i]==0)&&((maxi==1)||((float)v[i]/c[i]> (float)v[maxi]/c[maxi]))) maxi=i used[maxi]=1/*markthemaxithobjectasused*/ cur_w =c[maxi]/*withtheobjectinthebag,Icancarryless*/ tot_v+=v[maxi] if(cur_w>=0) printf("Addedobject%d(%d$,%dKg)completlyinthebag.Spaceleft: %d.\n",maxi+1,v[maxi],c[maxi],cur_w) else{ printf("Added%d%%(%d$,%dKg)ofobject%dinthebag.\n",(int)((1 +(float)cur_w/c[maxi])*100),v[maxi],c[maxi],maxi+1) tot_v =v[maxi] tot_v+=(1+(float)cur_w/c[maxi])*v[maxi] } } printf("Filledthebagwithobjectsworth%.2f$.\n",tot_v) }

Output:
Addedobject5(10$,4Kg)completely inthebag.Spaceleft:11. Addedobject2(2$,1Kg)completely inthebag.Spaceleft:10. Addedobject3(2$,2Kg)completely inthebag.Spaceleft:8. Addedobject4(1$,1Kg)completely inthebag.Spaceleft:7. Added58%(4$,12Kg)ofobject1inthebag. Filledthebagwithobjectsworth15.48$. 31

Conclusion:
PerformanceAnalysis Iftheitemsarealreadysortedintodecreasingorderofvi /wi, thenthewhilelooptakesa timein O(n) Therefore,thetotaltimeincludingthesortisin O(nlogn). Ifwekeeptheitemsinheapwithlargestvi/wiattheroot.Then creatingtheheaptakesO(n)time whileloop now takes O(log n) time (since heap property must be restored after theremovalofroot) Althoughthisdatastructuredoesnotaltertheworstcase,itmaybefasterifonlyasmall numberofitemsareneededtofilltheknapsack. Onevariantofthe01knapsackproblemiswhenorderofitemsaresortedbyincreasing weightisthesameastheirorderwhensortedbydecreasingvalue. The optimal solution to this problem is to sort by the value of the item in decreasing order. Then pick up the most valuable item which also has a least weight. First, if its weightislessthanthetotalweightthatcanbecarried.Thendeductthetotalweightthat can be carried bytheweightofthe item justpick.Thesecond itemtopick isthe most valuable item among those remaining. Keep follow the same strategy until we cannot carrymoreitem(duetoweight).

32

6. Programfor findingminimumcostspanningtreeusinggreedy method.


Aim:Writeaprogramforfindingminimumcostspanningtreeusinggreedy method. Theory:
Aspanningtreeofagraphisanytreethatincludeseveryvertexinthegraph.Littlemore formally,aspanningtreeofagraphGisasubgraphofGthatisatreeandcontainsall theverticesofG.Anedgeofaspanningtreeiscalledabranchanedgeinthegraphthat is not in the spanning tree is called a chord. We construct spanning tree whenever we want to find a simple, cheap and yet efficient way to connect a set of terminals (computers, cites, factories, etc.). Spanning trees are important because of following reasons. Spanning trees construct a sparse sub graph that tells a lot about the original graph. Spanningtreesaveryimportantindesigningefficientroutingalgorithms. Somehardproblems(e.g.,Steinertreeproblemandtravelingsalesmanproblem) canbesolvedapproximatelybyusingspanningtrees. Spanningtreeshavewideapplicationsinmanyareas,suchasnetworkdesign,etc.

Herearesomeexamples: AgraphG: Three(ofthemanypossible)spanningtreesfromgraphG:

AweightedgraphG:

TheminimumspanningtreefromweightedgraphG:

Figure6.1:GraphGanditsspanningtrees ToexplainfurtherupontheMinimumSpanningTree(MST),andwhatitappliesto,let's consideracoupleofrealworldexamples:

33

1. One practical application of a MST would be in the design of a network. For instance,agroupof individuals,whoareseparatedbyvaryingdistances,wishto beconnectedtogetherinatelephonenetwork. AlthoughMSTcannotdoanything aboutthedistancefromoneconnectiontoanother,itcanbeusedtodeterminethe leastcostly pathswithnocyclesinthisnetwork,therebyconnectingeveryoneata minimumcost. 2. Anotheruseful applicationofMSTwould be findingairlineroutes. Thevertices ofthegraphwouldrepresentcities,andtheedgeswouldrepresentroutesbetween thecities. Obviously,thefurtheronehastotravel,themoreitwillcost,soMST canbeappliedtooptimizeairlineroutesbyfindingtheleastcostlypathswithno cycles. ToexplainhowtofindaMinimumSpanningTree,wewilllookattwoalgorithms:the KruskalalgorithmandthePrimalgorithm. Bothalgorithmsdifferintheirmethodology, butbotheventually endupwiththe MST. Kruskal's algorithmusesedges,andPrims algorithmusesvertexconnectionsindeterminingtheMST. Kruskal'sAlgorithm: Thisisagreedyalgorithm.Agreedyalgorithmchoosessomelocaloptimum(ie.picking anedgewiththeleastweightinaMST). Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep adding the shortest(leastcost)edge,whileavoidingthecreationofcycles,until(n1)edgeshave beenadded.(NOTE:Sometimestwoormoreedgesmayhavethesamecost.Theorderin whichtheedgesarechosen,inthiscase,doesnotmatter.DifferentMSTsmayresult,but they will all have the same total cost, which will always be the minimum cost) AlgorithmKruskalMST Input:Aweighted,connectedandundirectedgraph G=(V,E). Output:Aminimalspanningtreeof G. Step1: T =f . Step2: whileTcontainslessthan n1edgesdo Chooseanedge(v,w)from Eofthesmallestweight. Delete(v,w)from E. If theaddingof(v,w)doesnotcreatecyclein Tthen Add(v,w)toT. ElseDiscard(v,w). endwhile Prim'sAlgorithm: This algorithm builds the MST one vertex at a time. It starts at any vertex in a graph (vertexA,forexample),andfindstheleastcostvertex(vertexB,forexample)connected to the start vertex. Now, from either 'A' or 'B', it will find the next least costly vertex connection,withoutcreatingacycle(vertexC,forexample).Now,from either'A','B',or 'C',itwillfindthenextleastcostlyvertexconnection,withoutcreatingacycle,andsoon it goes. Eventually, all the vertices will be connected, without any cycles, and an MST willbetheresult.(NOTE:Twoormoreedgesmayhavethesamecost,sowhenthereisa 34

choicebytwoormoreverticesthatisexactlythesame,thenonewillbechosen,andan MSTwillstillresult) Someimportantfactsaboutspanningtreesareasfollows:


Anytwoverticesinatreeareconnectedbyauniquepath. LetTbeaspanningtreeofagraphG,andletebeanedge ofGnotinT.TheT+econtain auniquecycle.

Greediness: It is easy to see that this algorithm has the property that each edge is examined at most once. Algorithms, like this one, which examine each entity at most once and decide its fate once and for all during that examination, are called greedy algorithms.Theobviousadvantageofgreedyapproach isthatwedo nothavetospend timereexaminingentities.

Figure6.2:GraphG ApplyingKruskal'sAlgorithm onthegraphshowninfigure6.2 Usingtheabovegraph,herearethestepstotheMST,usingKruskal'sAlgorithm: 1. N1toN2 costis1addtotree 2. N7toN8 costis1addtotree 3. N2toN3 costis2addtotree 4. N1toN6 costis3addtotree 5. N2toN6 costis4rejectbecauseitformsacircuit 6. N3toN4 costis4addtotree 7. N2toN7 costis5addtotree 8. N3toN7 costis6rejectbecauseitformsacircuit 9. N4toN8 costis6rejectbecauseitformsacircuit 10. N4toN7 costis7rejectbecauseitformsacircuit 11. N4toN5 costis7addtotree We stop here, because n 1 edges have been added. We are left with the minimum spanningtree,withatotalweightof23. 35

ProgramSnippets: #defineN6/*numberofvertices*/ #defineM15/*numberofedgesingraph*/ intU[N] /*functionprototypes*/ voidmakeset(inti) intfind(inti) voidmerge(intp,intq) intequal(intp,intq) voidinitial(intn) voidtest_univ(void) voidpause(void)/*usedmainlyfortestpurposes*/ /*functiondefinitions*/ intmain() {intW[N][N]={0,2,4,1,3,2,/*weightedgraph*/ 2,0,6,4,5,1, 4,6,0,4,2,1, 1,4,4,0,5,4, 3,5,2,5,0,6, 2,1,1,4,6,0} intE[M][3]/*completesetofedges*/ intF[N1][3]/*setofedgesinmin.span.tree*/ intnum_edges=0/*numofedgesinmin.span.tree*/ intnext_edge=0/*nextedgenotyetconsidered*/ intweight=0 /*minimalspanningtreeweight*/ inta,b,c,i,j,k/*counter/placeholdervariables*/ /*initializesetofedges*/ k=0 for(i=0i<Ni++) for(j=0j<Nj++) if(j>i) {E[k][0]=i/*firstvertexofedge*/ E[k][1]=j/*secondvertexofedge*/ E[k][2]=W[i][j]/*weightofedge*/ k++ } /*displaysetofedgesbeforesort*/ for(i=0i<Mi++) {for(j=0j<3j++) printf("%3d",E[i][j]) printf("\n") }

pause() 36

/*sortsetofedgesinnondecreasingorderbyweightbubblesort*/ for(i=M1i>0i) for(j=0j<ij++) if(E[j][2]>E[j+1][2]) {a=E[j][0] b=E[j][1] c=E[j][2] E[j][0]=E[j+1][0] E[j][1]=E[j+1][1] E[j][2]=E[j+1][2] E[j+1][0]=a E[j+1][1]=b E[j+1][2]=c } /*displaysetofedgesaftersort*/ for(i=0i<Mi++) {for(j=0j<3j++) printf("%3d",E[i][j]) printf("\n") } /*createndisjointsubsets*/ initial(N) /*initializesetofedgesinmin.span.treetoempty*/ for(i=0i<N1i++) for(j=0j<3j++) F[i][j]=1/*'1'denotes'empty'*/ test_univ() /*findminimalspanningtree*/ while(num_edges<N1) {a=E[next_edge][0] b=E[next_edge][1] i=find(a) j=find(b) if(!equal(i,j)) {merge(i,j) F[num_edges][0]=E[next_edge][0] F[num_edges][1]=E[next_edge][1] F[num_edges][2]=E[next_edge][2] num_edges++ test_univ() } 37

next_edge++ } /*displayedgescomprisingminimalspanningtree*/ printf("\nMinimalSpanningTreeEdges:\n") printf("F=(") for(i=0i<N1i++) {printf("(V%d,V%d)",F[i][0],F[i][1]) if(i<N2) printf(",") weight=weight+F[i][2] } printf(")\n") printf("MinimalSpanningTreeWeight=%d\n",weight) return(0) } /***************makeset()***************/ voidmakeset(inti) {U[i]=i } /***************find()***************/ intfind(inti) {intj j=i while(U[j]!=j) j=U[j] return(j) } /***************merge()***************/ voidmerge(intp,intq) {if(p<q) U[q]=p else U[p]=q } /***************equal()***************/ intequal(intp,intq) {if(p==q) return(1) else return(0) } /***************initial()***************/ voidinitial(intn) 38

{inti for(i=0i<ni++) makeset(i) } /***************test()***************/ voidtest_univ(void) /*testuniversevalues*/ {inti printf("\nThedisjointsubsetsare:\n") for(i=0i<Ni++) printf("%3d",U[i]) printf("\n") } /***************pause()***************/ voidpause(void) {inti printf("PressENTERtocontinue...\n") i=getchar() }

Conclusion:
PerformanceanalysisofKruskalsalgorithm:

Creationof thepriorityqueue Ifthereareeedges,itiseasytoseethatittakesO(eloge)timetoinserttheedges intoapartiallyorderedtree

EachdeleteminoperationtakesO(loge)timeintheworstcase.Thusfindingand deletingleastcostedges,overthewhileiterationscontributeO(loge)intheworst case. Thetotaltimeforperformingallthemergeandfindis:O(eloge) .

39

7. Programfor findingallpairsshortestpaths.
Aim:Writeaprogram forfindingallpairsshortestpaths. Theory:
Dynamicprogrammingisanoptimizationtechnique. Greedyvs.DynamicProgramming: o Both techniques are optimization techniques, and both build solutions fromacollectionofchoicesofindividualelements. o Thegreedymethodcomputesitssolutionbymakingitschoicesinaserial forwardfashion,neverlookingbackorrevisingpreviouschoices. o Dynamic programming computes its solution bottom up by synthesizing them from smaller sub solutions, and by trying many possibilities and choicesbeforeitarrivesatthe optimalsetofchoices. o ThereisnoapriorilitmustestbywhichonecantelliftheGreedymethod willleadtoanoptimalsolution. o By contrast,there is a litmus test for Dynamic Programming, called The PrincipleofOptimality DivideandConquervs.DynamicProgramming: o Bothtechniquessplittheirinputintoparts,findsubsolutionstotheparts, andsynthesizelargersolutionsfrom smallerones. o Divide and Conquer splits its input at pre specified deterministic points (e.g.,alwaysinthemiddle) o DynamicProgrammingsplitsitsinputateverypossiblesplitpointsrather than at prespecified points. After trying all split points, it determines whichsplitpointisoptimal.

PrincipleofOptimality Definition: A problem is said to satisfy the Principle of Optimality if the sub solutionsofanoptimalsolutionoftheproblemarethemselvesoptimalsolutions fortheirsub problems. Examples: o Theshortestpathproblem satisfiesthePrincipleofOptimality. o Thisisbecauseifa,x1,x2,...,xn,bisashortestpathfromnodeatonodeb inagraph,thentheportionofxitoxjonthatpathisashortestpathfrom xitoxj. o Thelongestpathproblem,ontheotherhand,doesnotsatisfythePrinciple ofOptimality.TakeforexampletheundirectedgraphGofnodesa,b,c,d, ande,andedges(a,b)(b,c)(c,d)(d,e)and(e,a).Thatis,Gisaring.The longest(noncyclic)pathfromatodtoa,b,c,d.Thesubpathfrombtocon thatpathissimplytheedgeb,c.Butthatisnotthelongestpathfrombto c.Rather,b,a,e,d,cisthelongestpath.Thus,thesubpathonalongestpath isnotnecessarilyalongestpath.

TheAllPairsShortestPathProblem

Input:Aweightedgraph,representedbyitsweightmatrixW. Problem:findthedistancebetweeneverypairofnodes. 40

DynamicprogrammingDesign: (k) o Notation:A (i,j)=lengthoftheshortestpathfromnodeitonodejwhere thelabelofeveryintermediarynodeis<=k.


(0) A (i,j)=W[i,j].

o Principle of Optimality: We already saw that any subpath of a shortest o

o o o o

o o

o o o o o

pathisashortestpathbetweenitsendnodes. Dividethepathsfromitojwhereeveryintermediarynodeisoflabel<=k intotwogroups: 1. Thosepathsthatdogothrough nodek 2. Thosepathsthatdonotgothroughnodek. theshortestpathinthefirstgroupistheshortestpathfromitojwherethe labelofeveryintermediarynodeis<=k1. (k1) therefore,thelengthoftheshortestpathofgroup1isA (i,j) Eachpathingrouptwoconsistsoftwoportions:Thefirstisfromnodeito nodek,andthesecondisfromnodektonodej. the shortest path in group 2 does not go through K more than once, for otherwise,thecyclearoundKcanbeeliminated,leadingtoashorterpath ingroup2. Therefore, the two portions of the shortest path in group 2 have their intermediarylabels<=k1. Eachportionmustbetheshortestofitskind.Thatis,theportionfromito kwhereintermediarynodeis<=k1mustbetheshortestsuchapathfrom itok.Ifnot,wewouldgetashorterpathingroup2.Samethingwiththe secondportion(fromjtok). Therefore,thelengthofthefirstportionoftheshortestpathingroup2is (k1) A (i,k) Therefore,thelengthofthe2ndportionoftheshortestpathingroup2is (k1) A (k,j) (k1) (k1) Hence,thelengthoftheshortestpathingroup2isA (i,k)+A (k,j) Sincetheshortestpathinthetwogroupsistheshorteroftheshortestpaths ofthetwogroups,weget (k) (k1) (k1) (k1) A (i,j)=min(A (i,j),A (i,k)+A (k,j)).

Algorithm APSP(input:W[1:n,1:n]A[1:n,1:n]) { fori=1tondo forj=1tondo A(i,j):=W[i,j] endfor endfor fork=1tondo fori=1tondo forj=1tondo A(i,j)=min(A(i,j),A(i,k)+A(k,j)) endfor endfor endfor } 41

Thefollowingprocedurecomputestheabovesequenceofmatricesbyusingthis techniqueof repeatedsquaring.

(4) Figure7.1GraphGandmatrixD showsshortestdistancebetweeneverypairof nodes

ProgramSnippets: intd[n][n] /*initializedsothateachedge(i,j)hasthecorrectweightatd[i][j]andallotherelements ofthearrayaresettoINFINITY*/ /*nisnumberofvertices*/ for(intk=0k<n k++) { for(inti=0i<ni++) { for(intj=0j<nj++) { if(d[i][j]>d[i][k]+d[k][j]) { d[i][j]=d[i][k]+d[k][j] } } } 42

Input:A weightedgraph,representedbyitsweightmatrixW. Output:Aweightmatrixrepresentingshortestdistancebetweeneverypairofnodes. Conclusion:

TimeComplexityAnalysis: o The tripleforloopthat follows has a constanttime body, and thus takes 3 O(n )time. 3 o Thus,thewholealgorithmtakesO(n )time.

43

8. Programtoimplementtravelingsalespersonproblem usingdynamicprogramming.
Aim: Write a program to implement traveling salesperson problem using dynamicprogramming. Theory:
TheTravelingSalesmanProblem(TSP)isadeceptivelysimplecombinatorialproblem.It canbestatedverysimply: A salesman spends his time visiting n cities (ornodes) cyclically. In one tour he visits eachcityjustonce,andfinishesupwherehestarted.Inwhatordershouldhevisitthem tominimizethedistancetraveled? ManyTSP'saresymmetricthatis,foranytwocitiesAandB,thedistancefromAtoB isthesameasthatfromBtoA.Inthiscaseyouwillgetexactlythesametourlengthif you reverse the order in which they are visited so there is no need to distinguish betweenatouranditsreverse,andyoucanleaveoffthearrowsonthetourdiagram. Ifthereareonly2citiesthentheproblem istrivial,sinceonlyonetourispossible.For thesymmetriccasea3cityTSPisalsotrivial.Ifalllinksarepresentthenthereare(n1)! DifferenttoursforanncityasymmetricTSP.Toseewhythisisso,pickanycityasthe firstthentherearen1choicesforthesecondcityvisited,n2choicesforthethird,and soon.Forthesymmetriccasetherearehalfasmanydistinctsolutions(n1)!/2forann cityTSP.Ineithercasethenumberofsolutionsbecomesextremelylargeforlargen,so thatanexhaustivesearchisimpractical. 2

1
4 Figure8.1GraphG

ForthegraphGshowninfigure8.1,distancematrix: D={02 9 10, 10 64, 15708, 63120} Letg(i,S)bethelengthofshortestpathstartingatvertexi,goingthroughallverticesinS, andterminatingatvertex1. g(2,)=c21=1 g(3,)=c31=15 g(4,)=c41=6 44

k=1,considersetsof1element: Set{2}: Set{3}: Set{4}: g(3,{2})=c32+g(2, )=c32+c21=7+1=8p(3,{2})=2 g(4,{2})=c42+g(2, )=c42+c21=3+1=4p(4,{2})=2 g(2,{3})=c23+g(3, )=c23+c31=6+15=21p(2,{3})=3 g(4,{3})=c43+g(3, )=c43+c31=12+15=27p(4,{3})=3 g(2,{4})=c24+g(4,)=c24+c41=4+6=10p(2,{4}) =4 g(3,{4})=c34+g(4,)=c34+c41=8+6=14p(3,{4})=4

k=2,considersetsof2elements: Set{2,3}: g(4,{2,3})=min{c42+g(2,{3}),c43+g(3,{2})}=min{3+21,12+8}= min{24,20}=20 p(4,{2,3}=3 g(3,{2,4}) = min {c32 + g(2,{4}), c34 + g(4,{2})} = min {7+10, 8+4}= min{17,12}=12 p(3,{2,4}=4 g(2,{3,4})=min{c23+g(3,{4}),c24+g(4,{3})}=min{6+14,4+27}= min{20,31}=20 p(2,{3,4}=3

Set{2,4}:

Set{3,4}:

Lengthofanoptimaltour: f=g(1,{2,3,4})=min{c12+g(2,{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}=min{2+ 20,9+12,10+20}=min{22,21,30}=21 Successorofnode1:p(1,{2,3,4})=3 Successorofnode3:p(3,{2,4})=4 Successorofnode4:p(4,{2})=2 OptimalTSPtour:1 3 4 2 1 AlgorithmTSP Input:Numberofcitiesnandarrayofcostsc(i,j) i,j=1,..n(Webeginfromcitynumber1) Output:Vectorofcitiesandtotalcost. (*startingvalues*) C=0 cost=0 visits=0 e=1(*e=pointerofthevisitedcity) (*determinationofroundandcost) forr=1ton1do chooseofpointerjwith minimum=c(e,j)=min{c(e,k)visits(k)=0andk=1,..,n} cost=cost+minimum e=j C(r)=j endrloop C(n)=1 cost=cost+c(e,1)

45

ProgramSnippets: voidtravel(intn,constnumberW[][],indexP[][],number&minlength) { indexi,j,k numberD[1..n][subsetofV{v1}] for(i=2i<=ni++) D[i][]=W[i][1] for(k=1k<=n 2k++) for(allsubsetsA V {v1} V {v1}containingkvertices) for(isuchthati 1andviisnotinA){ D[i][A]=minimum(W[i][j]+D[j][A {vj}]) j:[vj A P[i][A]=valueofjthatgavetheminimum } D[1][V {v1}]=minimum(W[1][j]+D[j][V{v1,vj}]) 2jn P[1][V {v1}]=valueofjthatgavetheminimum minlength=D[1][V {v1}] }

Conclusion:
Timecomplexity LetNbethenumberofg(i,S)thathavetobecomputed
n 2 -

) (n - 1
k= 0

n- 2 n = ( - 1 2 - 2 n ) k

2 n Totaltime=O(n 2 ) Thisisbetterthanenumeratingalln!differenttours

46

9. Programto findshortestpathformultistagegraph using dynamicprogramming.


Aim: Write a program to find shortest path for multistage graph using dynamicprogramming. Theory:
Definition:multistagegraphG(V,E) Adirectedgraphinwhichtheverticesarepartitionedintok2disjointsetsVi, 1ik If<u,v>E,thenuViandvVi+1forsomeI,1i<k |V1|=|Vk|=1,ands(source)V1andt(sink)Vk c(i,j)=costofedge<i,j> Findaminimumcostpathfromstot

Figure9.1(5stagegraph) ThevertexsinV1 iscalledthesourcethevertextinVK iscalledthesink.Gisusually assumedtobeaweightedgraph.Thecostofapathfromnodevtonodewissumofthe costsofedges inthepath.The"multistagegraphproblem" istofindthe minimumcost pathfromstot.EachsetVi iscalledastageinthegraph. Manyproblemscanbeformulatedasmultistagegraphproblem Anexample:resourceallocationproblem nunitsofresourcearetobeallocatedtorprojects N(i,j)=netprofitwhenjunitsofresourceallocatedtoprojecti V(i,j) = vertex representing the state in which a total of j units have alreadybeenallocatedtoprojects1,2,..,i1 47

DPformulation Everystotpathistheresultofasequenceofk2decisions Theprincipleofoptimalityholds(Why?) p(i,j)=aminimumcostpathfromvertexjinVitovertext cost(i,j)=costofpathp(i,j)

cos ( , j = min { (j l)+ cos( + 1l t i ) c , t i , )}


l V+1 i < j, > E l

cost(k1,j)=c(j,t)if<j,t> E,otherwise Thencomputingcost(k2,j)forallj Vk2 Thencomputingcost(k3,j)forallj Vk3 Finallycomputingcost(1,s)

Tofindtheshortestpath of multistagegraph showninfigure9.1. Stage5 cost(5,12)=0.0 Stage4 cost(4,9)=min{4+cost(5,12)}=4 cost(4,10)=min{2+cost(5,12)}=2 cost(4,11)=min{5+cost(5,12)}=5 Stage3 cost(3,6)=min{6+cost(4,9),5+cost(4,10)}=7 cost(3,7)=min{4+cost(4,9),3+cost(4,10)}=5 cost(3,8)=min{5+cost(4,10),6+cost(4,11)}=7 Stage2 cost(2,2)=min{4+cost(3,6),2+cost(3,7),1+cost(3,8)}=7 cost(2,3)=min{2+cost(3,6),7+cost(3,7)}=9 cost(2,4)=min{11+cost(3,8)}=18 cost(2,5)=min{11+cost(3,7),8+cost(3,8)}=15 Stage1 cost(1,1) = min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4), 2+cost(2,5)}=16 Recordingthepath d(i,j) = value of l (l is a vertex) that minimizes c(j,l)+cost(i+1,l) in equation(5.5) InFigure9.1 d(3,6)=10d(3,7)=10d(3,8)=10 d(2,2)=7d(2,3)=6d(2,4)=8d(2,5)=8 48

d(1,1)=2 Whenlettingtheminimumcostpath1,v2,v3,,vk1,t, v2=d(1,1)=2 v3=d(2,d(1,1))=7 v4=d(3,d(2,d(1,1)))=d(3,7)=10 Sothesolution(minimumcostpath)is1 anditscostis16 Algorithm /ProgramSnippets: Voidmultistage(graphG,intk,intn,intp[] ) //TheinputisakstagegraphG=(V,E)withnverticesindexedinorder //ofstages.Eisasetofedgesandc[i][j]isthecostof<i,j>. //p[1:k]isaminimumcostpath. { floatcost[MAXSIZE]intd[MAXSIZE],r cost[n]=0.0 for(intj=n1j>=1j){//Computecost[j]. letrbeavertexsuchthat<j,r>isanedge ofGandc[j][r]+cost[r]isminimum cost[j]=c[j][r]+cost[r] d[j]=r } //Findaminimumcostpath. p[1]=1p[k]=n for(j=2j<=k1j++)p[j]=d[p[j1]] } 2 7 10 12

Conclusion:
Timecomplexity

49

10.Program to implement8queensproblemusing backtrack method.


Aim: Write a program to implement 8queens problem using backtrack method. Theory:
Backtracking is kind of solving a problem by trial and error. However, it is a well organizedtrialanderror.Wemakesurethatwenevertrythesamethingtwice.Wealso makesurethat iftheproblem is finitewe willeventuallytryallpossibilities(assuming thereisenoughcomputingpowertotryall possibilities). Generalmethod Usefultechniqueforoptimizingsearchundersomeconstraints Express the desired solution as an ntuple (x1, . . . , xn) where each xi 2 Si, Si beingafiniteset Thesolutionisbasedonfindingoneormorevectorsthatmaximize,minimize,or satisfyacriterionfunctionP(x1,...,xn) Sortinganarraya[n] Findanntuplewheretheelementxiistheindexofith smallestelementina Criterionfunctionisgivenbya[xi]_a[xi+1]for1_i<n SetSiisafinitesetofintegersintherange[1,n] Bruteforceapproach LetthesizeofsetSibemi Therearem=m1m2mnntuplesthatsatisfythecriterionfunctionP In bruteforcealgorithm, you havetoformallthe m ntuplestodeterminethe optimalsolutions Backtrackapproach Requireslessthanmtrialstodeterminethesolution Formasolution(partialvector)andcheckateverystepifthishasanychanceof success Ifthesolutionatanypointseemsnotpromising,ignoreit Ifthepartialvector(x1,x2,...,xi)doesnotyieldanoptimalsolution,ignore mi+1mnpossibletestvectors evenwithoutlookingatthem All the solutions require a set of constraints divided into two categories: explicit and implicitconstraints Definition1Explicitconstraintsarerulesthatrestricteachxitotakeonvaluesonlyfrom agivenset. ExplicitconstraintsdependontheparticularinstanceIofproblembeingsolved Alltuplesthatsatisfytheexplicitconstraintsdefineapossiblesolutionspacefor I Examplesofexplicitconstraints *xi>=0,orallnonnegativerealnumbers *xi ={0,1} *li<=xi<=ui Definition 2 Implicit constraints are rules that determine which of the tuples in the solutionspaceofIsatisfythecriterionfunction. 50

Implicitconstraintsdescribethewayinwhichthexismustrelatetoeachother. Determineproblemsolutionbysystematicallysearchingthesolutionspaceforthegiven probleminstance Useatreeorganizationforsolutionspace 8queensproblem Place eight queens on an 8 8 chessboard sothat no queen attacks another queen *Identifydatastructurestosolvetheproblem *Firstpass:Definethechessboardtobean88array *Secondpass:Sinceeachqueenisinadifferentrow,definethechessboard solutiontobean8tuple(x1,...,x8), wherexiisthecolumnforithqueen Identifyexplicitconstraints *Explicitconstraintsusing8tupleformulationareSi={1,2,3,4,5,6,7, 8},1 <=i<=8 8 Solutionspaceof8 8tuples Identifyimplicitconstraints * No two xi can be the same, or all the queens must be in different columns Allsolutionsarepermutationsofthe8tuple(1,2,3,4,5,6,7,8) 8 Reducesthesizeofsolutionspacefrom8 to8!tuples Notwoqueenscanbeonthesamediagonal Thesolutionaboveisexpressedasan8tupleas4,6,8,2,7,1,3,5 1 1 2 3 4 5 6 7 8 2 3 4 Q 5 6 Q Q Q Q Q Q Q Figure10.1(OneSolutionto8queensproblem) The8QueensProblem: Givenisachessboard.Achessboardhas8x8fields.Isitpossibletoplace8queenson thisboard,sothatnotwoqueenscanattackeachother? ThenQueensproblem: Givenisaboardofnbynsquares.Isitpossibletoplacenqueens(thatbehaveexactly likechessqueens)onthisboard,withouthavinganyoneofthemattackany otherqueen? 1. Anotherexampleofaproblemthatcanbesolvedwith backtracking: o Place 8 queens on a 8x8 chess board sothat no queen attack each other (findallsolutions) 7 8

51

ExamplesSolutions:

Figure10.2(Solutionsto8queensproblem)

Algorithm: TheBacktrackingAlgorithmfornQueens Problem: Position nqueensonachessboardsothatnotwoareinthesamerow,column, ordiagonal. Inputs:positiveintegern. Outputs:allpossiblewaysnqueenscanbeplacedonan nx nchessboardsothatnotwo queensthreateneachother.Eachoutputconsistsofan arrayofintegerscolindexedfrom 1 ton,wherecol[i] isthecolumnwherethequeenintheithrowisplaced. ProgramSnippets: include<stdio.h> defineTRUE1 defineFALSE0 int*board intmain() { board=(int*)calloc(8+1,sizeof(int)) board++ //heregoestheuserinputnoofqueensdoesntmatterinthiscode intcol intqueens=2//examplefromuserinput for(col=1col<=8col++) placeQueens(1,col,queens) } voidplaceQueens(introw,intcol,intqueens) { inti for(i=1i<rowi++) { 52

if((board[i]==col)||((row+col)==(i+board[i]))||((rowcol)==(iboard[i]))) { check=FALSE } else check=TRUE } if(check==TRUE) { board[row]=col if(row==8) { for(i=1i<=queensi++) printf("(%d,%d)",i,board[i]) printf("\n") } else { for(i=1i<=8i++) placeQueens(row+1,i) } } }

53

A practice questionanswerset
1. Whatisdatastructure? A data structure is a way of organizing data that considers not only the itemsstored,butalsotheirrelationshiptoeachother.Advanceknowledgeabout the relationship between data items allows designing of efficient algorithms for themanipulationofdata. Listouttheareasinwhichdatastructuresareappliedextensively? CompilerDesign, OperatingSystem, DatabaseManagementSystem, Statisticalanalysispackage, NumericalAnalysis, Graphics, ArtificialIntelligence, Simulation Whatarethemajordatastructuresusedinthefollowingareas:RDBMS,Network datamodel& Hierarchicaldatamodel? RDBMS Array(i.e.Arrayofstructures) Networkdatamodel Graph Hierarchicaldatamodel Trees If you are using C language to implement the heterogeneous linked list, what pointertypewillyouuse? Theheterogeneouslinkedlistcontainsdifferentdatatypesinitsnodesand weneedalink,pointertoconnectthem.Itisnotpossibletouseordinarypointers forthis.Sowegoforvoidpointer.Voidpointeris capableofstoringpointerto anytypeasitisagenericpointertype. Minimumnumberofqueuesneededtoimplementapriorityqueue? Two.Onequeueisusedforactualstoringofdataandanotherforstoring priorities. Whatisthedatastructureusedtoperformrecursion? Stack. Because of its LIFO (Last In First Out) property it remembers its caller so the function knows where to return when the function has toreturn. Recursion makes use of system stack for storing the return addresses of the functioncalls. Every recursive function has its equivalent iterative (nonrecursive) function. Even when such equivalent iterative procedures are written, explicit stackistobeused. WhatarethenotationsusedinEvaluationofArithmeticExpressionsusingprefix andpostfixforms? PolishandReversePolishnotations. Converttheexpression((A+B)*C(DE)^(F+G))toequivalentPrefix andPostfixnotations. 54

2.

3.

4.

5.

6.

7.

8.

9.

PrefixNotation:^*+ABCDE+FG PostfixNotation:AB+C*DE FG+^ Sortingisnotpossiblebyusingwhichofthefollowingmethods? (a)Insertion (b)Selection (c)Exchange (d)Deletion Answer: (d)deletion Explanation: Using insertion we can perform insertion sort, using selection we can performselectionsort,usingexchangewecanperformthebubblesort(andother similarsortingmethods).Butnosortingmethodcanbedonejustusingdeletion. Abinarytreewith20nodeshasnullbranches. Answer: 21 Explanation: Letustakeatreewith5nodes(n=5)

10.

NullBranches

Itwillhaveonly6(ie,5+1)nullbranches.Ingeneral,abinarytreewithn nodeshasexactlyn+1nullnodes. 11. Whatarethemethodsavailableinstoringsequential files? Straightmerging, Naturalmerging, Polyphasesort, DistributionofInitialruns. Howmanydifferenttreesarepossiblewith10nodes? Answer: 1014 Explanation: Forexample,consideratreewith3nodes(n=3),itwillhavethemaximum 3 combinationof5different(ie,2 3=5)trees.

12.

ii

iii

iv

v 55

Ingeneral: n Iftherearennodes,thereexist2 n differenttrees. 13. List out a few of the applications of tree datastructure in compilers/compiler design. ThemanipulationofArithmeticexpression, SymbolTableconstruction, Syntaxanalysis. 14. ListoutafewoftheapplicationsthatmakeuseofMultilinkedStructures. Sparsematrix, Indexgeneration. Intreeconstructionwhichisthesuitableefficientdatastructure? (a)Array (b)Linkedlist (c)Stack (d)Queue (e)None Answer: (b)Linkedlist Whattypeofthealgorithmisusedinsolvingthe8Queensproblem? Backtracking InanAVLtree,whatistheconditionforbalancingtobedone? Thepivotal value(ortheHeightfactor)isgreaterthan1orlessthan1. Whatisthebucketsize,whenoverlappingandcollisionoccuratthesametime? One.Ifthere isonlyoneentrypossible inthe bucket,whenthe collision occurs, there is no way to accommodate the colliding value. This results in the overlappingofvalues. TraversethegiventreeusingInorder,PreorderandPostordertraversals. Giventree:
A

15.

16.

17.

18.

19.

H Inorder: Preorder:

I DHBEAFCIGJ ABDHECFGIJ

56

Postorder:

HDEBFIJGCA

20. Thereare8,15,13,14nodesweretherein4differenttrees.Whichofthemcould haveformedafullbinarytree? 15. Ingeneral: n Thereare2 1nodesinafullbinarytree. Bythemethodofelimination: Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a completebinarytreebutnotafullbinarytree.Sothecorrectansweris15. Note: Full and Complete binary trees are different. All full binary trees are completebinarytreesbutnotviceversa. 21. Inthegivenbinarytree,usingarray,atwhichlocationcanyoustorethenode4? 1

4 5 Answer: Atlocation6 Explanation: 1 2 3 4 5 Root LC1 RC1 LC2 RC2 LC3 RC3 LC4 RC4 Where LCn means Left Child of node n and RCn means Right Child of noden 22. SortthegivenvaluesusingQuickSort? 65 70 75 80 85 60 55 50 45 Sorting takes place from the pivot value, which is the first value of the given elements, this is marked bold. The values at the left pointer and right pointerareindicatedusingL andR respectively. L R 70 75 80 85 60 55 50 45 65 Since pivot is not yet changed the same process is continued after interchangingthevaluesatL andR positions 65 65 65 65 45 45 45 45 75L 50 50 50 80 80L 55 55 85 85 85L 60R 60 60 60R 85L 55 55R 80 80 50R 75 75 75 70 70 70 70 57

When the L and R pointers cross each other the pivot value is interchangedwiththevalueatrightpointer.Ifthepivotischangeditmeansthat the pivot has occupied its original position in the sorted order (shown in bold italics)and hencetwodifferentarrays are formed,one fromstartoftheoriginal arraytothepivotposition1andtheotherfrompivotposition+1toend. 60L 55L 50L 45 45 45R 50 50R 55 55R 60 60 65 65 65 85L 70R 70 80 80L 80L 75 75 75R 70R 85 85

Inthenextpasswegetthesortedformofthearray. 45 23. 50 55 60 65 70 75 80 85

Forthegivengraph,drawtheDFSandBFS?

A X G P

Thegivengraph:

Y M J

BFS: AXGHPEMYJ DFS: AXHPEYMJG

24. Classify the Hashing Functions based on the various methods by which the key valueisfound. Directmethod, Subtractionmethod, ModuloDivisionmethod, DigitExtractionmethod, MidSquaremethod, Foldingmethod, Pseudorandommethod. 25. What are the types of Collision Resolution Techniques and the methods used in eachofthetype? Openaddressing(closedhashing). Themethodsusedinclude: Overflowblock. Closedaddressing(openhashing) Themethodsusedinclude: Linkedlist,Binarytree 26. In RDBMS, what is the efficient data structure used in the internal storage representation? B+tree.BecauseinB+tree,allthedataarestoredonlyinleafnodes,that makessearchingeasier.Thiscorrespondstotherecordsthatshallbestoredinleaf nodes. 58

27. Draw the Btree of order 3 created by inserting the following data arriving in sequence922467118224516192078
1 1

19

24

1 6

20

22

78

92

28. Of the following tree structure, which is efficient considering space andtimecomplexities?
(a)IncompleteBinaryTree (b)CompleteBinaryTree (c) FullBinaryTree Answer: (b)CompleteBinaryTree. Explanation: Bythemethodofelimination: Fullbinarytreelosesitsnaturewhenoperationsofinsertionsanddeletions are done. For incomplete binary trees, extra storage is required and overhead of NULLnodecheckingtakesplace.Socompletebinarytreeisthebetteronesince the property of complete binary tree is maintained even after operations like additionsanddeletionsaredoneonit. 29. WhatisaspanningTree? A spanningtree isatreeassociatedwitha network.Allthe nodesofthe graph appear on the tree once. A minimal spanning tree is a spanning tree organizedsothatthetotaledgeweightbetweennodesisminimized.

30. Doestheminimalspanningtreeofagraphgivetheshortestdistancebetweenany 2specifiednodes? Answer: No. Explanation: Minimalspanningtreeassuresthatthetotalweightofthetreeiskeptatits minimum.Butitdoesntmeanthatthedistancebetweenanytwonodesinvolved intheminimumspanningtreeisminimum. 31. Convertthegivengraphwithweightededgestominimalspanningtree.
600

1
612
410 2985

3
310

200

5
400

59

1421

Theequivalentminimalspanningtreeis:

3 200 612 310 4 5

410 2

32. Whichisthesimplestfilestructure? (a)Sequential (b)Indexed (c) Random Answer: (a)Sequential 33. Islinkedlistalinearornonlineardatastructure? AccordingtoAccessstrategiesLinkedlistisalinearone. AccordingtoStorage,LinkedListisaNonlinearone. DrawabinaryTreefortheexpression: A*B (C+D)*(P/Q)

34.

35.

ForthefollowingCOBOLcode,drawtheBinarytree? 01STUDENT_REC. 02NAME. 03FIRST_NAMEPICX(10). 60

03LAST_NAMEPICX(10). 02YEAR_OF_STUDY. 03FIRST_SEMPICXX. 03SECOND_SEMPICXX. 01 STUDENT_REC

02
NAME

02
YEAR_OF_STUDY

03
FIRST_NAME

03
LAST_NAME

03
FIRST_SEM

03
SECOND_SEM

SectionIIAlgorithms 1. Whatisanalgorithm? Analgorithmconsistsofafinitesetofstepsthatmayrequireoneormore operations. These operations should be definite and effective. An algorithm shouldproduceoneormoreoutputsandmayhavezeroormoreinputs. Thisconsistsoffivedistinctareas: 1.todevicealgorithms 2.tovalidatethealgorithms 3.toexpressthealgorithms 4.toanalyzethealgorithms 5.totesttheprogramsforthealgorithms 2. Whatisacomputational procedure? Analgorithmthatdoesnotterminateiscalledcomputationalprocedure. Exampleforsuchcomputationalprocedureisanoperatingsystem. Define recursivealgorithm. Analgorithmissaidtoberecursiveifthesamealgorithmisinvokedinthe bodyofthealgorithm.Recursivealgorithmscan bedivided intodirectrecursive andindirectrecursivealgorithms. Directrecursive: Analgorithmthatcallsitself. IndirectRecursive: An algorithm A is said to be indirect recursive if it calls another algorithmwhichinturncallsalgorithmA. Howcanyouclassifyperformanceanalysis? Performanceanalysiscanbeclassifiedas: i. priorianalysis ii. posteriorianalysis 61

3.

4.

PrioriAnalysis: Theboundsofalgorithmscomputingtimeareobtainedbyformulatinga function. PosterioriAnalysis: Testing the actual computation of space and time are recorded while the algorithmisexecuting. 5. Define Big O. Forthefunctionf(n) f(n)=O(g(n)) iffthereexistpositiveconstantscanddsuchthat: f(n)<=c*g(n) foralln,n>=d. Thisisdefinedtobetheworsttimecomplexityofthefunctionf(n). Forexample: O(n)=3n+2because, 3n+2<=4nforalln>=2. Givevariouscomputingtimesandtheirmeaning. Fewoftheimportantcomputingtimesare: ComputingTime Meaning O(1) O(n) O(n*n) O(n*n*n) O(2*2*2*2*..............*n) 7. : : : : : constantcomputingtime linearcomputingtime quadraticcomputingtime cubiccomputingtime exponentialcomputingtime

6.

Givethemostimportantbasicdesignsofalgorithms. Therearefiveimportantbasicdesignsforalgorithms.Theyare: i. Divideandconquer, ii. Thegreedymethod, iii. Dynamicprogramming, iv. Backtracking, v. Branchandbound. How dodivideandconqueralgorithmswork? For a function to compute on n inputs the divide and conquer strategy suggests the inputs into a k distinct subsets, 1<k<=n, yielding k subproblems. Thesesubproblemsmustbesolvedandthenamethodmustbefoundtocombine thesubsolutionsintoasolutionofthewhole. An example for this approach is binary search algorithm. The time complexityofbinarysearchalgorithmisO(logn). WhatisGreedyMethod? Thegreedy methodsuggeststhatonecandevise analgorithmthatworks in stages, considering one input at a time. At each stage, a decision is made regarding whether a particular input is an optimal solution. An example for solutionusinggreedymethodisknapsackproblem.

8.

9.

62

10.

WhatisDynamicProgramming? Dynamic Programming is an algorithm design method that can be used when the solution to a problem can be viewed as the result of a sequence of decisions. Anexample foralgorithmusingdynamicprogramming ismultistage graphs. Whatarethetimecomplexitiesforthefollowingalgorithms? Binarysearch : O(logn) Findingmaximumandminimumforagivensetofnumbers : O(3n/22) MergeSort : O(nlogn) InsertionSort : O(n*n) QuickSort : O(nlogn) SelectionSort : O(n) WhatisthedifferencebetweenMergeSortandQuicksort? BothMergesortandQuicksorthavesametimecomplexityi.e.O(nlogn). Inmergesortthefilea[1:n]wasdividedatitsmidpointintosubarrayswhichare independently sorted and later merged. Whereas, in quick sort the division into two subarrays is made so that the sorted subarrays do not need to be merged latter. IsthereanyoptimumsolutionforMatrixmultiplication? Yes.DivideandconquermethodsuggestsStrassensmatrixmultiplication method to be used. If we follow this method, the time complexity is O(n*n*n..*2.81)timesratherO(n*n*n**3)times. Defineminimumcostspanningmethod. LetG=(V,E)beanundirectedconnectedgraph.Asubgrapht=(V,E)of GisaspanningtreeofGifandonlyiftisatree. Tofindoutminimumcostspanningmethodwehavefollowingmethods PrimsAlgorithm : O(n*n) KruskalsAlgorithm : O(eloge) Definearticulationpoints. AVertexVinaconnectedgraphGisanarticulationpointifandonly if thedeletionofvertexVtogetherwillalledgesincidentfordisconnectsthegraph intotwoormorenonemptyComponents. Definebiconnectedgraph. AgraphGisbiconnectedifandonlyifitcontainsnoarticulationpoints. Whatareexplicitandimplicitconstraints? Explicitconstraints arerulesthatrestricteach xi totakeonvaluesonly fromagivenset.Implicitconstraintsarerulesthatdeterminewhichofthetuples inthesolutionspaceofisatisfythecriterionfunction.

11.

12.

13.

14.

15.

16.

17.

63

Evaluation and marking system:

Basic honesty in the evaluation and marking system is absolutely essential and in the process impartial nature of the evaluator is required in the examinationsystem tobecomepopularamongstthestudents.It isawrong approach or concept to award the students by way of easy marking to get cheap popularity among the students to which they do not deserve. It is a primary responsibility of the teacher that right students who are really putting up lot of hard work with right kind of intelligence are correctly awarded. The marking patterns should be justifiable to the students without any ambiguity and teacher should see that students are faced with unjust circumstances. The assessment is done according to the directives of the Principal/ Vice Principal/DeanAcademics.

64

También podría gustarte