Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

LabAssignment‌‌

   ‌

DesignandAnalysisof‌‌
 
Algorithms‌‌
   ‌

SubmittedBy:‌‌    ‌
‌Name-Rojbela‌‌Kisku‌  ‌
‌Univ.RegistrationNo.BBMK–‌‌    ‌
S
‌ 1904753‌‌Univ.Roll‌‌no.‌‌    ‌
‌200081018215‌  ‌
Course:‌‌    ‌
BCA‌‌    ‌
Semester:‌‌    ‌
th‌
4‌   ‌ ‌

DepartmentofBCA‌‌ 
P.K.RoyMemorial‌‌
 
College,Dhanbad–‌  ‌
1|‌Page‌‌
   ‌

826004‌  ‌
2|‌Page‌‌
   ‌
INDEX‌  ‌
Sl.No‌‌
   Experiment‌‌
   ‌ PageNo‌  ‌

1‌‌
   ‌ QUICKSORT‌‌
   ‌ 03‌  ‌
2‌‌
   ‌ MERGESORT‌‌
   ‌ 06‌  ‌

3‌‌
   ‌ DEPTHFIRSTSEARCH(GRAPHTRAVERSALS)‌‌
   ‌ 09‌  ‌

4‌‌
   ‌ SUMOFSUBSETSPROBLEM‌‌
   ‌ 12‌  ‌

5‌‌
   ‌ MINIMUMCOSTSPANNINGTREE‌‌
   ‌ 14‌  ‌
 ‌
 ‌
3|‌Page‌‌
   ‌
EXPERIMENTNO-1‌‌
   ‌

QUICKSORT‌‌
   ‌
OBJECTIVE:‌‌
   ‌

SortagivensetofelementsusingtheQuicksortmethodanddeterminethetime‌‌  
requiredtosorttheelements.Repeattheexperimentfordifferentvaluesofn,the‌‌
 
numberofelementsinthelisttobesortedandplotagraphofthetimetakenversusn.‌‌  
Theelementscanbereadfrom‌‌afileorcanbegeneratedusingtherandom‌‌number‌‌generator.‌‌
   ‌

PACKAGES/SOFTWAREREQUIRED:‌‌
   ‌

DevC++‌‌
   ‌

THEORY:‌‌
   ‌

QuickSortisaDivideandConqueralgorithm.Itpicksanelementaspivotandpartitions‌‌
 
thegivenarrayaroundthepickedpivot.‌‌
   ‌

TherearemanydifferentversionsofQuickSortthatpickpivotindifferentways.‌(‌ i)‌‌
 
Alwayspickfirstelementaspivot.‌‌   ‌
(ii)‌A
‌ lwayspicklastelementaspivot(implementedbelow)‌‌
   ‌
(iii)‌P
‌ ickarandomelementaspivot.‌‌   ‌
(iv)‌P‌ ickmedianaspivot.‌‌
   ‌

ThekeyprocessinQuickSortispartition.Targetofpartitionsis,givenanarrayandan‌‌
 
elementxofarrayaspivot,putxatitscorrectpositioninsortedarrayandputall‌‌
 
smallerelements(smallerthanx)beforex,andputallgreaterelements(greaterthanx)‌‌afterx.‌‌
   ‌

PROGRAMMINGLOGIC:‌  ‌

4|‌Page‌‌
   ‌
PROCEDURE:‌‌
   ‌

1.‌Create:OpenDevC++,writeaprogramwithQuickSortprogramlogic,afterthatsave‌‌
 
theprogramwith.cextension.‌‌
   ‌
2.‌Compile:Alt+F9‌‌
   ‌
3.‌Execute:Ctrl+F10‌‌
   ‌

SOURCECODE:‌‌
   ‌

#include<stdio.h>‌‌
   ‌

voidswap(int*a,int*b)‌‌
   ‌
{‌‌
   ‌
intt=*a;‌‌   ‌
*a=*b;‌‌   ‌
*b=t;‌‌
   ‌
}‌‌
   ‌

intpartition(intarr[],intlow,inthigh)‌‌
   ‌
{‌‌
   ‌
intpivot=‌‌
   ‌
arr[high];inti=‌‌
   ‌
(low-1);‌‌
   ‌

for(intj=low;j<=high-1;j++)‌‌
   ‌
{‌‌
   ‌

if(arr[j]<=pivot)‌‌   ‌
{‌‌
   ‌
i++;‌‌
   ‌
swap(&arr[i],&arr[j]);‌‌
   ‌
}‌‌
   ‌
}‌‌
   ‌
swap(&arr[i+1],&arr[high]);‌‌
   ‌
return(i+1);‌‌
   ‌
}‌‌
   ‌

voidquickSort(intarr[],intlow,inthigh)‌‌   ‌
{‌‌
   ‌
if(low<high)‌‌
   ‌
{‌‌
   ‌
intpi=partition(arr,low,high);‌‌
   ‌

quickSort(arr,low,pi-1);‌‌
   ‌
quickSort(arr,pi+1,‌‌
   ‌
high);‌‌
   ‌
}‌‌
   ‌
}‌  ‌

5|‌Page‌‌
   ‌
voidprintArray(intarr[],intsize)‌‌
   ‌
{‌‌
   ‌

inti;‌‌
   ‌
for(i=0;i<size;i++)‌‌
   ‌
printf("%d",arr[i]);‌‌
   ‌
printf("\n");‌‌
   ‌
}‌‌
   ‌

intmain()‌‌    ‌
{‌‌
   ‌
intarr[]={10,7,8,9,1,5};‌‌   ‌
intn=‌‌
   ‌
sizeof(arr)/sizeof(arr[0]);‌‌    ‌
quickSort(arr,0,n-1);‌‌   ‌
printf("Sortedarray:\n");‌‌    ‌
printArray(arr,n);‌‌
   ‌
return0;‌‌    ‌
}‌‌
   ‌

1.6INPUT/OUTPUT‌  ‌

6|‌Page‌‌
   ‌

EXPERIMENT-2‌‌
   ‌

MERGESORT‌‌
   ‌

OBJECTIVE:‌‌   ‌
Implementmergesortalgorithm‌  ‌tosortagivensetofelementsanddeterminethetime‌‌
 
requiredtosorttheelements.Repeattheexperimentfordifferentvaluesofn,thenumberof‌‌
 
elementsinthelisttobesortedandplotagraphofthetimetakenversusn.Theelements‌‌ 
canbereadfromafileorcanbegeneratedusingtherandomnumbergenerator.‌‌    ‌

PACKAGES/SOFTWAREREQUIRED:‌‌
   ‌

DevC++‌‌
   ‌

THEORY:‌‌
   ‌

MergeSortisaDivideandConqueralgorithm.Itdividestheinputarrayintotwohalves,calls‌‌
 
itselfforthetwohalves,andthenmergesthetwosortedhalves.Themerge()functionis‌‌  
usedformergingtwohalves.Themerge(arr,l,m,r)isakeyprocessthatassumesthat‌‌  
arr[l..m]andarr[m+1..r]aresortedandmergesthetwosortedsub-arraysintoone.‌‌
   ‌

PROGRAMMINGLOGIC:‌‌
   ‌
 ‌
PROCEDURE:‌‌
   ‌

Create:OpenDevC++,writeaprogramwithMergeSortlogicafterthatsavetheprogramwith.c‌‌
 
extension.Compile:Alt+F9‌‌
   ‌
Execute:Ctrl+F10‌  ‌

7|‌Page‌‌
   ‌
SOURCECODE:‌‌
   ‌

/*CprogramforMergeSort‌‌   ‌
*/#include<stdio.h>‌‌
   ‌
#include<stdlib.h>‌‌
   ‌

voidmerge(intarr[],intl,intm,intr)‌{‌   
‌‌ ‌
inti,j,k;‌‌
   ‌
intn1=m-l+1;‌‌    ‌
intn2=r-m;‌‌    ‌

intL[n1],R[n2];‌‌
   ‌

for(i=0;i<n1;i++)‌‌
   ‌
L[i]=arr[l+i];‌‌
   ‌
for(j=0;j<n2;j++)‌‌
   ‌
R[j]=arr[m+1+j];‌‌    ‌

i=0;‌‌
   ‌
j=0;‌‌
   ‌
k=l;‌‌
  
while(i<n1&&j<n2)‌‌    ‌
{if(L[i]<=R[j]){‌‌    ‌
arr[k]=L[i];‌‌
   ‌
i++;‌‌
   ‌
}‌‌
   ‌
else{‌‌   ‌
arr[k]=R[j];‌‌
   ‌
j++;‌‌
   ‌
}‌‌
   ‌
k++;‌‌   ‌
}‌‌
   ‌

while(i<n1){‌‌    ‌
arr[k]=L[i];‌‌
   ‌
i++;‌‌
   ‌
k++;‌‌
   ‌
}‌‌
   ‌
while(j<n2){‌‌    ‌
arr[k]=R[j];‌‌
   ‌
j++;‌‌
   ‌
k++;‌‌
   ‌
}‌‌
   ‌
}‌‌
   ‌
voidmergeSort(intarr[],intl,intr)‌{‌   
‌‌ ‌
if(l<r){‌‌
   ‌
intm=l+(r-l)/2;‌  ‌

8|‌Page‌‌
   ‌
mergeSort(arr,l,m);‌‌
   ‌

mergeSort(arr,m+1,r);‌‌
   ‌

merge(arr,l,m,r);‌‌
   ‌
}‌‌
   ‌
}‌‌
   ‌

voidprintArray(intA[],intsize)‌‌    ‌
{‌‌
   ‌
inti;‌‌
   ‌
for(i=0;i<size;i++)‌‌    ‌
printf("%d",A[i]);‌‌
   ‌
printf("\n");‌‌   ‌
}‌‌
   ‌

intmain()‌‌
   ‌
{‌‌
   ‌
intarr[]={12,11,13,5,6,7};‌‌
   ‌
intarr_size=sizeof(arr)/sizeof(arr[0]);‌‌
   ‌

printf("Givenarrayis\n");‌‌
   ‌
printArray(arr,arr_size);‌‌
   ‌

mergeSort(arr,0,arr_size-1);‌‌
   ‌

printf("\nSortedarrayis\n");‌‌   ‌
printArray(arr,arr_size);‌‌
   ‌
return0;‌‌
   ‌
}‌‌
   ‌

INPUT/OUTPUT‌  ‌
9|‌Page‌‌
   ‌
EXPERIMENT-3‌‌
   ‌

DEPTHFIRSTSEARCH(GRAPHTRAVERSAL)‌‌
   ‌
OBJECTIVE:‌‌
   ‌
TocheckconnectivityofgivengraphusingDFSmethod‌‌
   ‌

 ‌
PACKAGES/SOFTWAREREQUIRED:‌‌
   ‌

DevC++‌‌
   ‌

THEORY:‌‌
   ‌

DepthFirstSearch(DFS)algorithmtraversesagraphinadepthwardmotionanduses‌‌
 
astacktoremembertogetthenextvertextostartasearch.‌‌
   ‌

(i)‌V
‌ isittheadjacentunvisitedvertex.Markitasvisited.Displayit.Pushitinastack.‌(‌ ii)‌‌
 
Ifnoadjacentvertexisfound,popupavertexfromthestack.(Itwillpopup‌‌  
alltheverticesfromthestack,whichdonothaveadjacentvertices.)‌‌   ‌
(iii)‌R
‌ epeatRule1andRule2untilthestackisempty.‌‌    ‌

PROGRAMMINGLOGIC:‌  ‌
10|‌Page‌‌
   ‌
PROCEDURE:‌‌
   ‌

Create:OpenDevC++,writeaprogramafterthatsavetheprogramwith.c‌‌
 
extension.Compile:Alt+F9‌‌
   ‌
Execute:Ctrl+F10‌‌
   ‌

SOURCECODE:‌‌
   ‌

#include<iostream>‌   ‌
#defineNODE5‌‌    ‌
usingnamespacestd;‌‌    ‌
intgraph[NODE][NODE]={{0,1,1,0,0},‌‌    ‌
{1,0,1,1,0},‌‌    ‌
{1,1,0,1,1},‌‌    ‌
{0,1,1,0,1},‌‌    ‌
{0,0,1,1,0}};‌‌    ‌
voidtraverse(intu,boolvisited[])‌‌    ‌
{visited[u]=true;//markvas‌‌    ‌
visitedfor(intv=0;v<NODE;v++)‌‌    ‌
{‌‌
   ‌
if(graph[u][v]){‌‌    ‌
if(!visited[v])‌‌   
{traverse(v,‌‌    ‌
visited);‌‌    ‌
}‌‌
   ‌
}‌‌
   ‌
}‌‌
   ‌
boolisConnected(){‌‌    ‌
bool*vis=newbool[NODE];‌‌    ‌
for(intu;u<NODE;u++)‌‌    ‌
{for(inti=0;i<NODE;i++)‌‌    ‌
vis[i]=false;‌‌    ‌
traverse(u,vis);‌‌    ‌
for(inti=0;i<NODE;i++)‌‌    ‌
{if(!vis[i])‌‌    ‌
returnfalse;‌‌    ‌
}‌‌
   ‌
}‌‌
   ‌
returntrue;‌‌    ‌
}‌‌
   ‌

intmain(){‌‌
   ‌
if(isConnected()){‌‌
   ‌
cout<<"TheGraphisconnected.";‌‌
   ‌
}‌‌
   ‌
else‌‌   ‌
cout<<"TheGraphisnotconnected.";‌  ‌

11|‌Page‌‌
   ‌
}‌  ‌

12|‌Page‌‌
   ‌
INPUT/OUTPUT‌‌
   ‌
CheckingwhetheragivengraphisconnectedornotusingDFSmethod‌  ‌

13|‌Page‌‌
   ‌

EXPERIMENT-4‌‌
   ‌

SUMOFSUBSETSPROBLEM‌‌
   ‌

OBJECTIVE:‌‌
   ‌

FindasubsetofagivensetS={sl,s2‌‌s‌ n}ofnpositiveintegerswhosesumisequaltoagiven‌‌
 
positiveintegerd.Forexample,ifS={1,2,5,6,8}andd=9therearetwosolutions{1,2,6}‌‌
 
and{1,8}.Asuitablemessageistobedisplayedifthegivenprobleminstancedoesn'thavea‌‌solution.‌‌
   ‌

PACKAGES/SOFTWAREREQUIRED:‌‌
   ‌

DevC++‌‌
   ‌

THEORY:‌‌
   ‌

Givenasetofnon-negativeintegers,andavaluesum,determineifthereisasubsetofthe‌‌
 
givensetwithsumequaltogivensum.‌‌
   ‌

PROGRAMMINGLOGIC:‌‌
   ‌
PROCEDURE:‌‌
   ‌
Create:OpenDevC++,writeaprogramafterthatsavetheprogramwith.cpp‌‌
 
extension.Compile:Alt+F9‌‌
   ‌
Execute:Ctrl+F10‌  ‌

12|‌Page‌‌
   ‌
SOURCECODE:‌‌
   ‌

#include<iostream>‌‌
   ‌
usingnamespacestd;‌‌   ‌

voiddisplaySubset(intsubSet[],intsize)‌‌
   ‌
{for(inti=0;i<size;i++){‌‌
   ‌
cout<<subSet[i]<<"";‌‌    ‌
}‌‌
   ‌
cout<<endl;‌‌    ‌
}‌‌
   ‌

voidsubsetSum(intset[],intsubSet[],intn,intsubSize,inttotal,intnodeCount,intsum)‌‌
 
{if(total==sum){‌‌
   ‌
displaySubset(subSet,subSize);‌‌    ‌
subsetSum(set,subSet,n,subSize-1,total-set[nodeCount],nodeCount+1,sum);‌retur‌
n;‌‌}else{‌‌   ‌
for(inti=nodeCount;i<n;i++){‌‌subSet[subSize]=set[i];‌‌
   ‌
subsetSum(set,subSet,n,subSize+1,total+set[i],i+1,sum);‌‌}  
‌‌ ‌
}‌‌
   ‌
}‌‌
   ‌

voidfindSubset(intset[],intsize,intsum)‌‌
   ‌
{int*subSet=newint[size];‌‌
   ‌
subsetSum(set,subSet,size,0,0,0,sum);delete[]‌‌
   ‌
subSet;‌‌
   ‌
}‌‌
   ‌

intmain(){‌‌
   ‌
intweights[]={10,7,5,18,12,20,15};‌‌
   ‌
intsize=7;‌‌
   ‌
findSubset(weights,size,35);‌‌
   ‌

}‌‌
   ‌

INPUT/OUTPUT‌  ‌

13|‌Page‌‌
   ‌
EXPERIMENT-5‌‌
   ‌

MINIMUMCOSTSPANNINGTREE‌‌
   ‌

OBJECTIVE:‌‌
   ‌

FindMinimumCostSpanningTreeofagivenundirectedgraphusingPrim'salgorithm.‌‌
 

 ‌

PACKAGES/SOFTWAREREQUIRED:‌‌
   ‌

DevC++‌‌
   ‌

THEORY:‌‌
   ‌

Prim’salgorithmisalsoa‌GreedyAlgorithm.‌Itstartswithanemptyspanningtree.Theideaisto‌‌  
maintaintwosetsofvertices.ThefirstsetcontainstheverticesalreadyincludedintheMST,‌‌
 
theothersetcontainstheverticesnotyetincluded.Ateverystep,itconsidersalltheedgesthat‌‌ 
connectthetwosets,andpickstheminimumweightedgefromtheseedges.Afterpickingthe‌‌  
edge,itmovestheotherendpointoftheedgetothesetcontainingMST.‌‌    ‌

PROGRAMMINGLOGIC:‌‌
   ‌
PROCEDURE:‌‌    ‌
Create:OpenDevC++,writeaprogramafterthatsavetheprogramwith.c‌‌
   ‌
extension.Compile:Alt+F9‌‌
   ‌
Execute:Ctrl+F10‌  ‌

14|‌Page‌‌
   ‌
SOURCECODE:‌‌
   ‌

#include<limits.h>‌‌
   ‌
#include<stdbool.h>‌‌    ‌
#include<stdio.h>‌‌
   ‌
#defineV5‌‌
   ‌

intminKey(intkey[],boolmstSet[])‌‌
   ‌
{‌‌
   ‌
intmin=INT_MAX,min_index;‌‌   ‌

for(intv=0;v<V;v++)‌‌
  
if(mstSet[v]==false&&key[v]<min)‌‌
   ‌
min=key[v],min_index=v;‌‌
   ‌

returnmin_index;‌   ‌
}‌‌
   ‌

intprintMST(intparent[],intgraph[V][V])‌‌   ‌
{‌‌
   ‌
printf("Edge\tWeight\n");‌‌
   ‌
for(inti=1;i<V;i++)‌‌
   ‌
printf("%d-%d\t%d\n",parent[i],i,graph[i][parent[i]]);‌‌}  
‌‌ ‌

voidprimMST(intgraph[V][V])‌‌
   ‌
{‌‌
   ‌
intparent[V];‌‌
   ‌

intkey[V];‌‌
   ‌

boolmstSet[V];‌‌
   ‌

for(inti=0;i<V;i++)‌‌
   ‌
key[i]=INT_MAX,mstSet[i]=false;‌‌
   ‌

key[0]=0;‌‌
   ‌
parent[0]=-1;‌‌
   ‌

for(intcount=0;count<V-1;count++){‌‌
   ‌

intu=minKey(key,mstSet);‌‌
   ‌

mstSet[u]=true;‌‌
   ‌

for(intv=0;v<V;v++)‌  ‌

15|‌Page‌‌
   ‌
if(graph[u][v]&&mstSet[v]==false&&graph[u][v]<key[v])‌‌
   ‌
parent[v]=u,key[v]=graph[u][v];‌‌
   ‌
}‌‌
   ‌

printMST(parent,graph);‌‌
   ‌
}‌‌
   ‌

intmain()‌‌
   ‌
{‌‌
   ‌
intgraph[V][V]={{0,2,0,6,0},‌‌
   ‌
{2,0,3,8,5},‌‌
   ‌
{0,3,0,0,7},‌‌
   ‌
{6,8,0,0,9},‌‌
   ‌
{0,5,7,9,0}};‌‌
   ‌

primMST(graph);‌‌
   ‌

return0;‌‌
   ‌

INPUT/OUTPUT:‌  ‌

16|‌Page‌‌
   ‌

You might also like