0% found this document useful (0 votes)
244 views

Design and Analysis of Algorithms

The document provides details about implementing stack and queue data structures in C language. It describes the LIFO principle of a stack and the push and pop operations. Sample code snippets show how to implement push, pop and display functions for a stack. Queues follow the FIFO principle and support enqueue and dequeue operations. The document aims to help students understand these basic data structures through practical programming assignments.

Uploaded by

mikdashe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

Design and Analysis of Algorithms

The document provides details about implementing stack and queue data structures in C language. It describes the LIFO principle of a stack and the push and pop operations. Sample code snippets show how to implement push, pop and display functions for a stack. Queues follow the FIFO principle and support enqueue and dequeue operations. The document aims to help students understand these basic data structures through practical programming assignments.

Uploaded by

mikdashe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

1

JawaharlalNehruEngineeringCollege
LaboratoryManual
DESIGN AND ANALYSIS OF ALGORITHMS
For
Third Year Students CSE
Dept: Computer Science & Engineering (NBA Accredited)
Author JNEC, Aurangabad
2
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
3
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 Mrs. Parminder Kaur
HOD, CSE Asst. Prof., CSE Dept
4
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.
5
SUBJECT INDEX
Sr.
No.
Title Page
No.
Introduction 6
1 Program to implement Stack & Queue. 8
2 Program to implement heap sort. 13
3 Program for finding the maximum & minimum using divide &
conquer method.
18
4 Program to implement merge sort or quick sort using divide and
conquer method.
20
5 Program to implement knapsack problem using greedy method. 28
6 Program for finding minimum cost spanning tree using greedy
method.
33
7 Program for finding all pairs shortest paths. 40
8 Program to implement traveling salesperson problem using
dynamic programming.
44
9 Program for finding shortest path for multistage graph using
dynamic programming.
47
10 Program to implement 8queens problem using backtrack method. 50
A practice questionanswer set 54
Note: All the programs should be implemented using C language.
6
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
1Memory
1Communication bandwidth
1Computational time (usually most important)
* Factors affecting the running time
computer
compiler
algorithm used
input to the algorithm
1The content of the input affects the running time
1typically, 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
7
Example
Calculate
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
1E.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.

=
N
i
i
1
3
8
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 seeing the figure above you can see that this data structure is really a restricted list.
Youhaverestrictedtheaccesstooneendofthelistbyusingthepopandpushoperations.
Theresultofthisrestrictionisthatitemsinthelistwillbestoredoneontopoftheother.
Wemustfirstremovealltheitemsaboveittillyougettothebottomitem."LastIn,First
Out"orLIFO,whichisusedtodescribethebehavior,sincethelastitemtoenterthestack
isthe first itemtoleavethe stack.Thetopitem isthe item alwaysthe last itemtoenter
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.
9
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)
{
10
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 is a data structure which works based on principle of first in first out (FIFO). In
computing world, queue data structure can be applied in many applications FCFS
schedulingalgorithm,bufferofprinter.
Figure1.2:Queueoperations(ShowsInsertandDelete)
By seeing the figure above you can see that this data structure is really a restricted 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
11
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
12
}
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).
13
2.Programtoimplement heapsort.
Aim:Writeaprogram toimplementheapsort.
Theory:
Heapsort(method)isacomparisonbased sortingalgorithm,and ispartoftheselection
sort family. Although somewhat slower in practice on most machines than a good
implementation of quick sort, it has the advantage of a worstcase (n log n) runtime.
Heapsortisaninplacealgorithm.
It begins by building a heap outof the data set, and then removing the largest item and
placing it at the end of the sorted array. After removing the largest item, it reconstructs
theheap,removesthelargestremainingitem,andplacesitinthenextopenpositionfrom
theendofthesortedarray.This isrepeateduntil thereareno items left inthe heap and
thesortedarray is full.Elementary implementationsrequiretwoarraysonetoholdthe
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
14
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
15
Figure2.1:BuildingMaxHeaptree
16
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)
17
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 10 14 8 7
ResultantArray :
1 2 3 4 7 8 9 10 14 16
Conclusion:
Complexity ofheapsortis:(n)+O(nlgn)=O(nlgn).
18
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 sub problems are compared to the
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, we see fromthe forloopthat N1comparisonswill be made. Thealgorithm
forfindingtheminimumfollowsinasimilarway.Ifwe findtheminimumafterfinding
the maximum, it is enough to find the minimum among the remaining N1 numbers
(assuming there are at least 2 numbers otherwise, maximum and minimum will be the
same).Soto findthe maximum and minimumofN numbers,we need(N1)+(N2)=
2N 3 comparisons. Now, let us see whether we can develop an algorithm whose
complexityisbetterthanthatofsolution1usingthedivideandconquermethod.
Thiswillbethebasicprincipleofthealgorithmtofindthemaximumandminimumofan
array of numbers. The main idea of using the divideandconquer strategy is described
below:
DividearrayAintotwosubpartsA1andA2suchthatA1andA2together
form A.
Find the maximumand minimum of each by recursive application of the
algorithm.
The maximum and minimum of A can be computed from the maximum
andminimumof A1andA2bymakingtwocomparisons.
19
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.Ifwe solvethesameproblemusingdivideandconquertechnique number
ofcomparisonsarereducedto3n/22.Thissaves25%comparisons.
20
4. Programtoimplementmergesortorquicksort using
divide&conquermethod.
Aim: Write a program to 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 sub problems are compared to the
originalproblem.
Youhavealreadyseenanexampleofadivideandconqueralgorithmin3rdassignment.
FirstwewillseeMERGESORT.
The idea behind mergesortistotakea list,divide it intotwosmallersublists,conquer
each sublistbysortingit,andthencombinethetwosolutionsforthesub problemsintoa
single solution. These three basic steps {divide, conquer, and combine {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.
Mergesortis basedonthedivideandconquerparadigm.The Mergesortalgorithm can
bedescribedingeneraltermsasconsistingofthefollowingthreesteps:
1.DivideStep
If given array A has zero or one element, return S it is already sorted.
Otherwise,divideAintotwoarrays,A
1
andA
2
,eachcontainingabouthalf
oftheelementsof A.
2.RecursionStep
Recursivelysortarray A
1
andA
2
.
3.ConquerStep
Combine the elements back in A by merging the sorted arrays A
1
and A
2
intoasortedsequence.
We can visualize Mergesort by means of binary tree where each node of the tree
representsarecursivecallandeachexternalnoderepresentindividualelementsofgiven
arrayA. Such a tree is called Mergesort tree. The heart of the Mergesort algorithm is
conquerstep,whichmergetwosortedsequencesintoasinglesortedsequence.
21
Figure4.1:Binarytreeformergesort
To begin, suppose that we have two sorted arrays A
1
[1], A
1
[2], . . , A
1
[M] and A
2
[1],
A
2
[2], . . . , A
2
[N]. The following is a direct algorithm of the obvious strategy of
successively choosingthesmallestremainingelementsfrom A
1
toA
2
andputtingitin 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]
22
}
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
23
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: 13 5 8 15 60 17 31 47
Output:
Sortednumbersare:8 5 13 15 17 31 47 60
Conclusion:
PerformanceAnalysisof MergeSort
LetT(n)bethetimetakenbythisalgorithmtosortanarrayof nelementsdividingAinto
sub arrays A
1
and A
2
takes linear time. It is easy to see that the Merge (A
1
,A
2
, 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.
24
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.
The Quick sort algorithm is fastest when the median of the array is chosen as the pivot
value.Thatisbecausetheresultingpartitionsareofverysimilarsize.Eachpartitionsplits
itselfintwoandthusthebasecaseisreachedveryquickly.
In practice, the Quick sort algorithm becomes very slow when the array passed to it is
alreadyclosetobeing sorted. Becausethere is noefficientway forthecomputertofind
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 much deeper than the other, and causes execution time to go up.Thus, it is
said that the more random the arrangement of the array, the faster the Quicksort
Algorithmfinishes.
Figure4.2:TheidealQuicksortonarandomarray
25
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.
Then array is then partitioned on either side of the pivot. Elements that are less than or
equal to pivot will move toward the 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
}
26
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: 13 5 8 15 60 17 31 47
Output:
Sortednumbersare:8 5 13 15 17 31 47 60
27
Conclusion:
PerformanceAnalysisof QuickSort
Therunningtimeofquicksortdependsonwhetherpartition is balancedorunbalanced,
which in turn depends on which elements of an array to be sorted are used for
partitioning.
A verygoodpartitionsplitsanarrayup intotwoequalsizedarrays. A badpartition,on
otherhand,splitsanarrayup intotwoarraysof verydifferentsizes.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
divides the array exactly in half. In other words,the best to be a median of the keys in
A[p..r]everytimeprocedure'Partition' iscalled.Theprocedure'Partition'always split
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)
28
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
GreedyAlgorithmworksbymaking thedecision thatseemsmostpromising atany
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 is greedy because at every stage it chooses the largest coin without worrying
abouttheconsequences.Moreover,itneverchangesitsmindinthesensethatonceacoin
hasbeenincludedinthesolutionset,itremainsthere.
Toconstructthesolutioninanoptimalway.Algorithmmaintainstwosets.Onecontains
chosenitemsandtheothercontainsrejecteditems.
Thegreedyalgorithmconsistsoffour(4)function.
1. Afunctionthatcheckswhetherchosensetofitemsprovideasolution.
2. Afunctionthatchecksthefeasibilityofaset.
29
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
merelya solution,butanoptimalsolution totheproblem.Inparticular,theempty
setisalwayspromisingwhy?(becauseanoptimalsolutionalwaysexists)
A greedy 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.Objectihasaweightw
i
and
theknapsackhasacapacitym.
Therearetwoversionsofproblem
I. Fractionalknapsackproblem
Thesetupissame,wecantakefractionsofitems,meaningthattheitemscan
bebrokenintosmallerpiecessothatwemaydecidetocarryonlyafractionof
x
i
of item i, where 0 x
i
1. If a fraction x
i
of object i is placed into the
knapsack,thenaprofitp
i
x
i
isearned.
The objective is to obtain a filling of the knapsack that maximizes the total
profitearned.
30
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 may decide either totake an item or to leave it (binary choice), 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
}
31
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$.
32
Conclusion:
PerformanceAnalysis
Iftheitemsarealreadysortedintodecreasingorderofv
i
/w
i,
thenthewhilelooptakesa
timein O(n)
Therefore,thetotaltimeincludingthesortisin O(nlogn).
Ifwekeeptheitemsinheapwithlargestv
i
/w
i
attheroot.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 by the weight of the item just pick. The second item to pick is the most
valuable item among those remaining. Keep follow the same strategy until we cannot
carrymoreitem(duetoweight).
33
6. Programfor findingminimumcostspanningtreeusinggreedy
method.
Aim:Writeaprogramforfindingminimumcostspanningtreeusinggreedy
method.
Theory:
Aspanningtreeofagraphisanytreethatincludeseveryvertexinthegraph.Littlemore
formally,aspanningtreeofagraphG isasubgraphofGthatisatreeandcontainsall
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.
Some hardproblems(e.g.,Steinertreeproblem andtravelingsalesmanproblem)
canbesolvedapproximatelybyusingspanningtrees.
Spanningtreeshavewideapplicationsinmanyareas,suchasnetworkdesign,etc.
Herearesomeexamples:
AgraphG: Three(ofthemanypossible)spanningtreesfromgraphG:
AweightedgraphG: TheminimumspanningtreefromweightedgraphG:
Figure6.1:GraphGanditsspanningtrees
ToexplainfurtherupontheMinimumSpanningTree(MST),andwhatitappliesto,let's
consideracoupleofrealworldexamples:
34
1. One practical application of a MST would be in the design of a network. For
instance,agroupof individuals,whoareseparatedby varyingdistances,wishto
beconnectedtogetherinatelephonenetwork. AlthoughMSTcannotdoanything
aboutthedistancefromoneconnectiontoanother,itcanbeusedtodeterminethe
leastcostly pathswithnocyclesinthisnetwork,therebyconnectingeveryoneata
minimumcost.
2. Another useful application of MST would be finding airline routes. The vertices
ofthegraphwouldrepresentcities,andtheedgeswouldrepresentroutesbetween
thecities. Obviously,thefurtheronehastotravel,themoreitwillcost,soMST
canbeappliedtooptimizeairlineroutesbyfindingtheleastcostlypathswithno
cycles.
ToexplainhowtofindaMinimumSpanningTree,wewilllookattwoalgorithms:the
KruskalalgorithmandthePrimalgorithm. Bothalgorithmsdifferintheirmethodology,
but both eventually end up with the MST. Kruskal's algorithm uses edges, and Prims
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)edges have
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: f = T .
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
35
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. The obvious advantage of greedy approach is that we do not have to spend
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.
36
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()
37
/*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()
}
38
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)
39
{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) .
40
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
solutionsofanoptimalsolutionoftheproblemarethemselvesoptimal solutions
fortheirsub problems.
Examples:
o Theshortestpathproblem satisfiesthePrincipleofOptimality.
o Thisisbecauseifa,x1,x2,...,xn,bisashortestpathfromnodeatonodeb
inagraph,thentheportionofxitoxjonthatpathisashortestpathfrom
xitoxj.
o Thelongestpathproblem,ontheotherhand,doesnotsatisfythePrinciple
ofOptimality.TakeforexampletheundirectedgraphGofnodesa,b,c,d,
and e,andedges(a,b)(b,c)(c,d)(d,e)and(e,a).That is,G isaring.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.
41
DynamicprogrammingDesign:
o Notation:A
(k)
(i,j)=lengthoftheshortestpathfromnodeitonodejwhere
thelabelofeveryintermediarynodeis<=k.
A
(0)
(i,j)=W[i,j].
o Principle of Optimality: We already saw that any subpath of a shortest
pathisashortestpathbetweenitsendnodes.
o Dividethepathsfromitojwhereeveryintermediarynodeisoflabel<=k
intotwogroups:
1. Thosepathsthatdogothrough nodek
2. Thosepathsthatdonotgothroughnodek.
o theshortestpathinthefirstgroupistheshortestpathfromitojwherethe
labelofeveryintermediarynodeis<=k1.
o therefore,thelengthoftheshortestpathofgroup1isA
(k1)
(i,j)
o Eachpathingrouptwoconsistsoftwoportions:Thefirstisfromnodeito
nodek,andthesecondisfromnodektonodej.
o the shortest path in group 2 does not go through K more than once, for
otherwise,thecyclearoundKcanbeeliminated,leadingtoashorterpath
ingroup2.
o Therefore, the two portions of the shortest path in group 2 have their
intermediarylabels<=k1.
o Eachportionmustbetheshortestofitskind.Thatis,theportionfromito
kwhereintermediarynodeis<=k1mustbetheshortestsuchapathfrom
itok.Ifnot,wewouldgetashorterpathingroup2.Samethingwiththe
secondportion(fromjtok).
o Therefore,the lengthofthe firstportionoftheshortestpathingroup2is
A
(k1)
(i,k)
o Therefore,the lengthofthe2ndportionofthe shortestpath ingroup2 is
A
(k1)
(k,j)
o Hence,thelengthoftheshortestpathingroup2isA
(k1)
(i,k)+A
(k1)
(k,j)
o Sincetheshortestpathinthetwogroupsistheshorteroftheshortestpaths
ofthetwogroups,weget
o A
(k)
(i,j)=min(A
(k1)
(i,j),A
(k1)
(i,k)+A
(k1)
(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
}
42
Thefollowingprocedurecomputestheabovesequenceofmatricesbyusingthis
techniqueof repeatedsquaring.
Figure7.1GraphGandmatrixD
(4)
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]
}
}
}
43
}
Input:A weightedgraph,representedbyitsweightmatrixW.
Output:Aweightmatrixrepresentingshortestdistancebetweeneverypairofnodes.
Conclusion:
TimeComplexityAnalysis:
o The tripleforloop that follows has a constanttime body, and thus takes
O(n
3
)time.
o Thus,thewholealgorithmtakesO(n
3
)time.
44
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 (or nodes) cyclically. In one tour he visits
eachcity justonce,and finishesupwhere hestarted.Inwhatordershould hevisitthem
tominimizethedistancetraveled?
ManyTSP'saresymmetricthatis,foranytwocitiesAandB,thedistancefromAtoB
isthesameasthatfromBtoA.Inthiscase youwillgetexactlythesametourlength if
you reverse the order in which they are visited so there is no need to distinguish
betweenatouranditsreverse,andyoucanleaveoffthearrowsonthetourdiagram.
If there are only 2 cities then the problem is trivial, since only one tour is possible. For
thesymmetriccasea3cityTSPisalsotrivial.Ifalllinksarepresentthenthereare(n1)!
DifferenttoursforanncityasymmetricTSP.Toseewhythisisso,pickanycityasthe
firstthentherearen1choicesforthesecondcityvisited,n2choicesforthethird,and
soon.Forthesymmetriccasetherearehalfasmanydistinctsolutions(n1)!/2forann
cityTSP.Ineithercasethe numberofsolutionsbecomesextremely large forlarge n,so
thatanexhaustivesearchisimpractical.
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
1
4
2
3
45
k=1,considersetsof1element:
Set{2}: 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
Set{3}: 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
Set{4}: 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
Set{2,4}: 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
Set{3,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
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)
46
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 =
Totaltime=O(n
2
2
n
)
Thisisbetterthanenumeratingalln!differenttours
2
2
0
2 ) 1 (
2
) 1 (
-
-
=
- =







-
-

n
n
k
n
k
n
n
47
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)
ThevertexsinV
1
iscalledthesourcethevertextinV
K
iscalledthesink.Gisusually
assumedtobeaweightedgraph.Thecostofapathfromnodevtonodewissumofthe
costsof edges in the path. The "multistage graph problem" is to find the minimum cost
pathfromstot.EachsetV
i
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
48
DPformulation
Everystotpathistheresultofasequenceofk2decisions
Theprincipleofoptimalityholds(Why?)
p(i,j)=aminimumcostpathfromvertexjinVitovertext
cost(i,j)=costofpathp(i,j)
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
)} , 1 ( cos ) , ( { min ) , ( cos
,
1
l i t l j c j i t
E l j
V l
i
+ + =
> <

+
49
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 2 7 10 12
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]]
}
Conclusion:
Timecomplexity
50
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
make sure that if the problem is finite we will eventually try all possibilities (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 brute force algorithm, you have to form all the m ntuples to determine the
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
*x
i
>=0,orallnonnegativerealnumbers
*x
i
={0,1}
*l
i
<=x
i
<=u
i
Definition 2 Implicit constraints are rules that determine which of the tuples in the
solutionspaceofIsatisfythecriterionfunction.
51
Implicitconstraintsdescribethewayinwhichthexismustrelatetoeachother.
Determineproblemsolutionbysystematicallysearchingthesolutionspaceforthegiven
probleminstance
Useatreeorganizationforsolutionspace
8queensproblem
Place eight queens on an 8 8 chessboard so that 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
Solutionspaceof8
8
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)
Reducesthesizeofsolutionspacefrom8
8
to8!tuples
Notwoqueenscanbeonthesamediagonal
Thesolutionaboveisexpressedasan8tupleas4,6,8,2,7,1,3,5
1 2 3 4 5 6 7 8
1 Q
2 Q
3 Q
4 Q
5 Q
6 Q
7 Q
8 Q
Figure10.1(OneSolutionto8queensproblem)
The8QueensProblem:
Givenisachessboard.Achessboardhas8x8 fields.Isitpossibletoplace8queenson
thisboard,sothatnotwoqueenscanattackeachother?
ThenQueensproblem:
Given isa boardofnbynsquares.Is it possible toplacenqueens(thatbehaveexactly
likechessqueens)onthisboard,withouthavinganyoneofthemattackany otherqueen?
1. Anotherexampleofaproblemthatcanbesolvedwith backtracking:
o Place 8 queens on a 8x8 chess board so that no queen attack each other
(findallsolutions)
52
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++)
{
53
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)
}
}
}
54
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.
2. Listouttheareasinwhichdatastructuresareappliedextensively?
CompilerDesign,
OperatingSystem,
DatabaseManagementSystem,
Statisticalanalysispackage,
NumericalAnalysis,
Graphics,
ArtificialIntelligence,
Simulation
3. Whatarethemajordatastructuresusedinthefollowingareas:RDBMS,Network
datamodel& Hierarchicaldatamodel?
RDBMS Array(i.e.Arrayofstructures)
Networkdatamodel Graph
Hierarchicaldatamodel Trees
4. If you are using C language to implement the heterogeneous linked list, what
pointertypewillyouuse?
Theheterogeneouslinkedlistcontainsdifferentdatatypesinitsnodesand
weneedalink,pointertoconnectthem.Itisnotpossibletouseordinarypointers
for this. So we go for void pointer. Void pointeris capable of storing pointerto
anytypeasitisagenericpointertype.
5. Minimumnumberofqueuesneededtoimplementapriorityqueue?
Two.Onequeue isused foractual storingofdataandanother forstoring
priorities.
6. 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 to return.
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.
7. WhatarethenotationsusedinEvaluationofArithmeticExpressionsusingprefix
andpostfixforms?
PolishandReversePolishnotations.
8. Convert the expression ((A + B) * C (D E)^ (F + G)) to equivalent Prefix
andPostfixnotations.
55
PrefixNotation:^*+ABCDE+FG
PostfixNotation:AB+C*DE FG+^
9. 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.
10. Abinarytreewith20nodeshasnullbranches.
Answer:
21
Explanation:
Letustakeatreewith5nodes(n=5)
Itwillhaveonly6(ie,5+1)nullbranches.Ingeneral,abinarytreewithn
nodeshasexactlyn+1nullnodes.
11. Whatarethemethodsavailableinstoringsequential files?
Straightmerging,
Naturalmerging,
Polyphasesort,
DistributionofInitialruns.
12. Howmanydifferenttreesarepossiblewith10nodes?
Answer:
1014
Explanation:
Forexample,consideratreewith3nodes(n=3),itwillhavethemaximum
combinationof5different(ie,2
3
3=5)trees.
i ii iii iv v
NullBranches
56
Ingeneral:
Iftherearennodes,thereexist2
n
n differenttrees.
13. List out a few of the applications of tree datastructure in compilers/compiler
design.
ThemanipulationofArithmeticexpression,
SymbolTableconstruction,
Syntaxanalysis.
14. ListoutafewoftheapplicationsthatmakeuseofMultilinkedStructures.
Sparsematrix,
Indexgeneration.
15. Intreeconstructionwhichisthesuitableefficientdatastructure?
(a)Array
(b)Linkedlist
(c)Stack
(d)Queue
(e)None
Answer:
(b)Linkedlist
16. Whattypeofthealgorithmisusedinsolvingthe8Queensproblem?
Backtracking
17. InanAVLtree,whatistheconditionforbalancingtobedone?
Thepivotal value(ortheHeightfactor)isgreaterthan1orlessthan1.
18. Whatisthebucketsize,whenoverlappingandcollisionoccuratthesametime?
One. If there is only one entry possible in the bucket, when the collision
occurs, there is no way to accommodate the colliding value. This results in the
overlappingofvalues.
19. TraversethegiventreeusingInorder,PreorderandPostordertraversals.
Inorder: DHBEAFCIGJ
Preorder: ABDHECFGIJ
A
B C
D
E F G
H I
J
Giventree:
57
Postorder: HDEBFIJGCA
20. Thereare8,15,13,14nodesweretherein4differenttrees.Whichofthemcould
haveformedafullbinarytree?
15.
Ingeneral:
Thereare2
n
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?
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
pointerareindicatedusing
L
and
R
respectively.
65 70
L
75 80 85 60 55 50 45
R
Since pivot is not yet changed the same process is continued after
interchangingthevaluesat
L
and
R
positions
65 45 75
L
80 85 60 55 50
R
70
65 45 50 80
L
85 60 55
R
75 70
65 45 50 55 85
L
60
R
80 75 70
65 45 50 55 60
R
85
L
80 75 70
1
2 3
4
5
58
When the L and R pointers cross each other the pivot value is
interchangedwiththe valueatrightpointer.Ifthepivotischanged it meansthat
the pivot has occupied its original position in the sorted order (shown in bold
italics) and hence two different arrays are formed, one from startof the original
arraytothepivotposition1andtheotherfrompivotposition+1toend.
60
L
45 50 55
R
65 85
L
80 75 70
R
55
L
45 50
R
60 65 70
R
80
L
75 85
50
L
45
R
55 60 65 70 80
L
75
R
85
Inthenextpasswegetthesortedformofthearray.
45 50 55 60 65 70 75 80 85
23. Forthegivengraph,drawtheDFSandBFS?
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.
A
X
G P
Y
M J
Thegivengraph:
59
27. Draw the Btree of order 3 created by inserting the following data arriving in
sequence922467118224516192078
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 spanning tree is a tree associated with a network. All the nodes of the
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.
1 3
2
4
5
410
600
200
400
310
1421
2985
612
1
1

5 7 19 24
4

1
6

20 22 78 92
60
Theequivalentminimalspanningtreeis:
32. Whichisthesimplestfilestructure?
(a) Sequential
(b) Indexed
(c) Random
Answer:
(a)Sequential
33. Islinkedlistalinearornonlineardatastructure?
AccordingtoAccessstrategiesLinkedlistisalinearone.
AccordingtoStorage,LinkedListisaNonlinearone.
34. DrawabinaryTreefortheexpression:
A*B (C+D)*(P/Q)
35. ForthefollowingCOBOLcode,drawtheBinarytree?
01STUDENT_REC.
02NAME.
03FIRST_NAMEPICX(10).

*
*
A
B
+
/
C
P D
Q
1
2
3
4
5
410
612
200
310
61
03LAST_NAMEPICX(10).
02YEAR_OF_STUDY.
03FIRST_SEMPICXX.
03SECOND_SEMPICXX.
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.
3. Define recursivealgorithm.
Analgorithmissaidtoberecursiveifthesamealgorithmisinvokedinthe
body of the algorithm. Recursive algorithms can be divided into direct recursive
andindirectrecursivealgorithms.
Directrecursive:
Analgorithmthatcallsitself.
IndirectRecursive:
An algorithm A is said to be indirect recursive if it calls another
algorithmwhichinturncallsalgorithmA.
4. Howcanyouclassifyperformanceanalysis?
Performanceanalysiscanbeclassifiedas:
i. priorianalysis
ii. posteriorianalysis
STUDENT_REC
NAME YEAR_OF_STUDY
FIRST_NAME LAST_NAME FIRST_SEM SECOND_SEM
01
02 02
03 03 03 03
62
PrioriAnalysis:
The boundsofalgorithmscomputingtimeareobtained by formulatinga
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.
6. Givevariouscomputingtimesandtheirmeaning.
Fewoftheimportantcomputingtimesare:
ComputingTime Meaning
O(1) : constantcomputingtime
O(n) : linearcomputingtime
O(n*n) : quadraticcomputingtime
O(n*n*n) : cubiccomputingtime
O(2*2*2*2*..............*n) : exponentialcomputingtime
7. Givethemostimportantbasicdesignsofalgorithms.
Therearefiveimportantbasicdesignsforalgorithms.Theyare:
i. Divideandconquer,
ii. Thegreedymethod,
iii. Dynamicprogramming,
iv. Backtracking,
v. Branchandbound.
8. 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).
9. WhatisGreedyMethod?
The greedy method suggests thatone can devise an algorithm that works
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.
63
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. An example for algorithm using dynamic programming is multistage
graphs.
11. Whatarethetimecomplexitiesforthefollowingalgorithms?
Binarysearch : O(logn)
Findingmaximumandminimumforagivensetofnumbers
: O(3n/22)
MergeSort : O(nlogn)
InsertionSort : O(n*n)
QuickSort : O(nlogn)
SelectionSort : O(n)
12. 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.
13. 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.
14. Defineminimumcostspanningmethod.
LetG=(V,E)beanundirectedconnectedgraph.Asubgrapht=(V,E)of
GisaspanningtreeofGifandonlyiftisatree.
Tofindoutminimumcostspanningmethodwehavefollowingmethods
PrimsAlgorithm : O(n*n)
KruskalsAlgorithm : O(eloge)
15. Definearticulationpoints.
A VertexV in aconnectedgraphG isanarticulationpoint ifandonly if
thedeletionofvertexVtogetherwillalledgesincidentfordisconnectsthegraph
intotwoormorenonemptyComponents.
16. Definebiconnectedgraph.
AgraphGisbiconnectedifandonlyifitcontainsnoarticulationpoints.
17. Whatareexplicitandimplicitconstraints?
Explicit constraints are rules that restrict each x
i
totake on values only
fromagivenset.Implicitconstraintsarerulesthatdeterminewhichofthetuples
inthesolutionspaceofisatisfythecriterionfunction.
64
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
examination system to become popular amongst the students. It is a wrong
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.

You might also like