0% found this document useful (0 votes)
73 views15 pages

Q. Write A Program To Obtain The GCD of Two Numbers Recursively. Program Code

The document provides code to implement Prim's algorithm to find the minimum spanning tree of a weighted undirected graph. It defines a Prims class with methods to read the graph adjacency matrix, perform Prim's algorithm starting from a source vertex, and display the resulting minimum spanning tree and its total cost. The main function gets the source vertex from the user, calls the spanning tree method to find the MST, and displays the output.

Uploaded by

vaibhav30388
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views15 pages

Q. Write A Program To Obtain The GCD of Two Numbers Recursively. Program Code

The document provides code to implement Prim's algorithm to find the minimum spanning tree of a weighted undirected graph. It defines a Prims class with methods to read the graph adjacency matrix, perform Prim's algorithm starting from a source vertex, and display the resulting minimum spanning tree and its total cost. The main function gets the source vertex from the user, calls the spanning tree method to find the MST, and displays the output.

Uploaded by

vaibhav30388
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Q. Write a program to obtain the GCD of two numbers recursively.

Program Code:

#include <stdio.h>

intgcd(intn,int m)

{ if(m<=n &&n%m == 0)

return m;

if(n < m)

returngcd(m,n);

else

returngcd(m,n%m);

intmain()

{ intn,m,divisor;

printf("Enter the two numbers : ");

scanf("%d%d",&n,&m);

divisor=gcd(n,m);

printf("The Greatest Common factor of %d and %d is %d",n,m,divisor);

return 0;

}
Output:
Q.Write a program to compute the Binary Search.

Program Code:

#include<iostream>

using namespace std;

int binary(intnum[10],intbeg,intend,int y)

intmid,z,pos=0;

if(beg<=end)

mid=(beg+end)/2;

if(num[mid]==y)

pos=mid;

}else{

if(y<num[mid])

end=mid-1;

pos=binary(num,beg,end,y);

else

beg=mid+1;

pos=binary(num,beg,end,y);

}
else

pos=-1;

returnpos;

int main()

int a[10],i,item,pos;

cout<<"Binary Search for 10 numbers \n";

cout<<"=============================\n";

cout<<"Enter 10 numbers sorted in ascending order :\n";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"\nEnter the number to search :";

cin>>item;

pos=binary(a,0,9,item);

if(pos==-1){

cout<<"Number not found\n\n";

}else{
cout<<"Number found at position : "<<(pos+1)<<"\n\n";

return 0;

Outputs:

Number Found
Number Not Found
Q. Write a program to compute the Quick Sort.

Program Code:

#include<iostream>

using namespace std;

void quicksort(int*,int,int);

int split(int*,int,int);

int main()

intarr[10];

int i;

cout<<"\nEnter the elements to be sorted\n";

for(i=0;i<10;i++)

cin>>arr[i];

cout<<"\n array before sorting\n";

for(i=0;i<=9;i++)

cout<<"%d\t",arr[i];

quicksort(arr,0,9);

cout<<"\n array after sorting\n";

for(i=0;i<=9;i++)

cout<<"%d\t",arr[i];

return 0;

void quicksort(int a[],intlower,int upper)


{

int i;

if(upper>lower)

i=split(a,lower,upper);

quicksort(a,lower,i-1);

quicksort(a,i+1,upper);

}}

int split(int a[],intlower,int upper)

intp,q,t;

p=lower+1;

q=upper;

int i=a[lower];

while(q>=p)

while(a[p]<i)

p++;

while(a[q]>i)

q--;

if(q>p)

t=a[p];

a[p]=a[q];

a[q]=t;

}
t=a[lower];

a[lower]=a[q];

a[q]=t;

return q;

Output:
Q. Write a program to compute the Merge Sort.

Program Code:

#include <iostream>

using namespace std;

int a[50];

void merge(int,int,int);

voidmerge_sort(intlow,int high)

int mid;

if(low<high)

mid=(low+high)/2;

merge_sort(low,mid);

merge_sort(mid+1,high);

merge(low,mid,high);

void merge(intlow,intmid,int high)

inth,i,j,b[50],k;

h=low;

i=low;

j=mid+1;

while((h<=mid)&&(j<=high))

{
if(a[h]<=a[j])

b[i]=a[h];

h++;

else

b[i]=a[j];

j++;

i++;

if(h>mid)

for(k=j;k<=high;k++)

b[i]=a[k];

i++;

else

for(k=h;k<=mid;k++)

b[i]=a[k];

i++;

}
}

for(k=low;k<=high;k++) a[k]=b[k];

int main()

intnum,i;

cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort\t";

cin>>num;

cout<<endl;

cout<<"Enter The Elements:"<<endl;

for(i=1;i<=num;i++)

cin>>a[i] ;

merge_sort(1,num);

cout<<endl;

cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;

cout<<endl<<endl;

for(i=1;i<=num;i++)

cout<<a[i]<<" ";

cout<<endl<<endl<<endl<<endl;

return 0;

}
Output:
Ques –Wap to implement prims algo?

#include<iostream.h>

#define MAX 10

class prims
{
private : int cost[MAX][MAX], tree[MAX][MAX];
int n;
public : void readmatrix();
int spanningtree(int);
void display(int);
};

void prims :: readmatrix()


{
int i, j;
cout << "\nEnter the number of vertices in the Graph : ";
cin >> n;
cout << "\nEnter the Cost matrix of the Graph\n\n";
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
cin >> cost[i][j];
}

int prims :: spanningtree(int src)


{
int visited[MAX], d[MAX], parent[MAX];
int i, j, k, min, u, v, stcost;
for (i = 1; i <= n; i++)
{
d[i] = cost[src][i];
visited[i] = 0;
parent[i] = src;
}
visited[src] = 1;
stcost = 0;
k = 1;
for (i = 1; i < n; i++)
{
min = 999;
for (j = 1; j <= n; j++)
{
if (!visited[j] && d[j] < min)
{
min = d[j];
u = j;
}
}
visited[u] = 1;
stcost = stcost + d[u];
tree[k][1] = parent[u];
tree[k++][2] = u;
for (v = 1; v <= n; v++)
if (!visited[v] && (cost[u][v] < d[v]))
{
d[v] = cost[u][v];
parent[v] = u;
}
}
return (stcost);
}

void prims :: display(int cost)


{
int i;
cout << "\nThe Edges of the Mininum Spanning Tree are\n\n";
for (i = 1; i < n; i++)
cout << tree[i][1] << " " << tree[i][2] << endl;
cout << "\nThe Total cost of the Minimum Spanning Tree is : " << cost;
}

int main()
{
int source, treecost;
prims pri;
pri.readmatrix();
cout << "\nEnter the Source : ";
cin >> source;
treecost = pri.spanningtree(source);
pri.display(treecost);
return 0;
}

You might also like