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

Applied Graph Theory File 7 Semester MCE: Submitted By:-Ankit Jain 2K14/MC/011 Batch R1

The document contains a program to find the minimum spanning tree of a graph using Kruskal's algorithm. It includes functions to find the parent node of each node and to union two nodes. It initializes data structures to store the cost matrix, parent nodes, and minimum cost. It then iterates through the edges, prints the minimum cost edge between unconnected components, updates the minimum cost and unions the nodes.

Uploaded by

Jain Pradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views35 pages

Applied Graph Theory File 7 Semester MCE: Submitted By:-Ankit Jain 2K14/MC/011 Batch R1

The document contains a program to find the minimum spanning tree of a graph using Kruskal's algorithm. It includes functions to find the parent node of each node and to union two nodes. It initializes data structures to store the cost matrix, parent nodes, and minimum cost. It then iterates through the edges, prints the minimum cost edge between unconnected components, updates the minimum cost and unions the nodes.

Uploaded by

Jain Pradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

APPLIED GRAPH THEORY

FILE
7th Semester
MCE

Submitted by :- Ankit Jain


2K14/MC/011
BATCH R1
INDEX
S.No. TOPIC DATE TEACHER’S
SIGNATURE
1. Write a program to find the number of
vertices , even vertices , odd vertices and
the number of edges in a graph.
2. Write a program to find UNION,
INTERSECTION and RING SUM of two
graphs.
3. Write a program to find minimum spannin
tree of a graph using Prim’s Algorithm.

4. Write a program to find minimum


spanning tree of a graph using Kruskal’s
Algorithm.
5. between 2 vertices in a graph using
Disjkstra’s Algorithm.

6. between every pair of vertices in a graph


using Floyd warshall’s algorithm.

7. Write a program to find shortest path


between every pair of vertices in a graph
using Bellman Ford’s algorithm.
8. Write a program to find maximum
matching in a bipartite graph.
9. Write a program to find maximum
matching for general graph.
10. from source node to sink using ford-
fulkerson algorithm.
1. Write a program to find the number of vertices , even vertices , odd vertices and the
number of edges in a graph.
#include<stdio.h>

#include<conio.h>

int main()

int n,v,i,j,cost[10][10],ne=0,no=0,ni,count=0;

printf("\n Enter the number of nodes:");

scanf("%d",&n);

printf("\n Enter the adjacency matrix:\n");

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

for(j=0;j<n;j++)

{scanf("%d",&cost[i][j]);

if(cost[i][j]==1) count+

+;

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

{ ni=0;

for(j=0;j<n;j++)

if(cost[i][j]=1)

ni++;

if((ni%2==0)&&(ni!=0))

ne++;

else

no++;

printf("\nNo of vertices is %d",n);

printf("\nNo of edges is %d",count/2);


printf("\nNo of odd vertices is %d",no);

printf("\nNo of even vertices is %d",ne);

getch();

Output:
2. Write a program to find UNION, INTERSECTION and RING SUM of two graphs.
//UNION

#include<stdio.h>

#include<iostream>

#include<conio.h>

using namespace std;

int printUnion(int arr1[], int arr2[], int m, int n)

int i = 0, j = 0;

while(i < m && j < n)

if(arr1[i] < arr2[j])

printf(" %d ", arr1[i++]);

else if(arr2[j] < arr1[i])

printf(" %d ", arr2[j++]);

else

printf(" %d ", arr2[j++]);

i++;

while(i < m)

printf(" %d ", arr1[i++]);

while(j < n)

printf(" %d ", arr2[j++]);

int main()

int V1[] = {0, 1};

int V2[] = {0, 1, 2};


int m = sizeof(V1)/sizeof(V1[0]);

int n = sizeof(V2)/sizeof(V2[0]);

int E1[m][m],E2[n][n],E3[m+n][m+n];

int i,j,k;

printf("Enter the adjacency matrix(symmetric) for graph G1:\n");

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

for(j=0;j<m;j++)

printf("E1[%d][%d]=",i,j);

scanf("%d",&E1[i][j]);

printf("Enter the adjacency matrix(symmetric) for graph G2:\n");

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

for(j=0;j<n;j++)

printf("E2[%d][%d]=",i,j);

scanf("%d",&E2[i][j]);

printf("\nSet of vertices in union of the graphs G1 and G2 is:\n");

printUnion(V1, V2, m, n);

printf("\n");

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

for(j=0;j<n;j++)

if(E1[i][j]>E2[i][j]&& i<m && j<m)


E3[i][j]=E1[i][j];

else if(E1[i][j]<E2[i][j]&& i<m && j<m)

E3[i][j]=E2[i][j];

else

E3[i][j]=E2[i][j];

printf("Adjacency matrix of union of graphs G1 and G2 is:\n\t");

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

printf("%d\t",i);

printf("\n\t");

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

printf(" ");

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

printf("\n%d|\t",i);

for(j=0;j<n;j++)

printf("%d\t",E3[i][j]);

getch();

Output:
//Intersection.

#include<stdio.h>

#include<iostream>

#include<conio.h>

using namespace std;

int printIntersection(int arr1[], int arr2[], int m, int n)

int i = 0, j = 0;

while(i < m && j < n)

if(arr1[i] < arr2[j])

i++;

else if(arr2[j] < arr1[i])

j++;

else /* if arr1[i] == arr2[j] */

printf(" %d ", arr2[j++]);

i++;
}

int main()

int V1[] = {0, 1};

int V2[] = {0, 1, 2};

int m = sizeof(V1)/sizeof(V1[0]);

int n = sizeof(V2)/sizeof(V2[0]);

int E1[m][m],E2[n][n],E3[m+n][m+n];

int i,j,k;

printf("Enter the adjacency matrix(symmetric) for graph G1:\n");

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

for(j=0;j<m;j++)

printf("E1[%d][%d]=",i,j);

scanf("%d",&E1[i][j]);

printf("Enter the adjacency matrix(symmetric) for graph G2:\n");

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

for(j=0;j<n;j++)

printf("E2[%d][%d]=",i,j);

scanf("%d",&E2[i][j]);

printf("\nSet of vertices in intersection of the graphs G1 and G2 is:\n");


printIntersection(V1, V2, m, n);

printf("\n");

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

for(j=0;j<m;j++)

if(E1[i][j]>E2[i][j])

E3[i][j]=E1[i][j];

else

E3[i][j]=E2[i][j];

printf("Adjacency matrix of intersection of graphs G1 and G2 is:\n\t");

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

printf("%d\t",i);

printf("\n\t");

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

printf(" ");

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

printf("\n%d|\t",i);

for(j=0;j<m;j++)

printf("%d\t",E3[i][j]); }

getch(); }
output:

//RingSum:

#include<stdio.h>

#include<iostream>

#include<conio.h>

using namespace std;

int printUnion(int arr1[], int arr2[], int m, int n)

int i = 0, j = 0;

while(i < m && j < n)

if(arr1[i] < arr2[j])

printf(" %d ", arr1[i++]);

else if(arr2[j] < arr1[i])

printf(" %d ", arr2[j++]);

else

printf(" %d ", arr2[j++]);

i++;

}
}

while(i < m)

printf(" %d ", arr1[i++]);

while(j < n)

printf(" %d ", arr2[j++]);

int main()

int V1[] = {0, 1};

int V2[] = {0, 1, 2};

int m = sizeof(V1)/sizeof(V1[0]);

int n = sizeof(V2)/sizeof(V2[0]);

int E1[m][m],E2[n][n],E3[m+n][m+n];

int i,j,k;

printf("Enter the adjacency matrix(symmetric) for graph G1:\n");

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

for(j=0;j<m;j++)

printf("E1[%d][%d]=",i,j);

scanf("%d",&E1[i][j]);

printf("Enter the adjacency matrix(symmetric) for graph G2:\n");

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

for(j=0;j<n;j++)

printf("E2[%d][%d]=",i,j);

scanf("%d",&E2[i][j]);
}

printf("\nSet of vertices in ring sum of the graphs G1 and G2 is:\n");

printUnion(V1, V2, m, n);

printf("\n");

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

for(j=0;j<n;j++)

if(E1[i][j]==E2[i][j]&& i<m && j<m)

E3[i][j]=0;

else if(E1[i][j]<E2[i][j]&& i<m && j<m)

E3[i][j]=E2[i][j];

if(E1[i][j]<E2[i][j]&& i<m && j<m)

E3[i][j]=E1[i][j];

else

E3[i][j]=E2[i][j];

printf("Adjacency matrix of ring sum of graphs G1 and G2 is:\n\t");

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

printf("%d\t",i);

printf("\n\t");

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

printf(" ");

for(i=0;i<n;i++)
{

printf("\n%d|\t",i);

for(j=0;j<n;j++)

printf("%d\t",E3[i][j]);

getch();

Output:
3. Write a program to find minimum spanning tree of a graph using Prim’s Algorithm.

#include<stdio.h>

#include<conio.h>

int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];

int main()

printf("\n Enter the number of nodes:");

scanf("%d",&n);

printf("\n Enter the weighted matrix:\n");

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

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

visited[1]=1;

printf("\n");

while(ne<n)

for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++) if(cost[i]

[j]<min) if(visited[i]!=0)

min=cost[i][j];

a=u=i;

b=v=j;

}
if(visited[u]==0 || visited[v]==0)

printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);

mincost+=min;

visited[b]=1;} cost[a]

[b]=cost[b][a]=999;

printf("\n Minimun cost=%d",mincost);

getch();

Output:
4. Write a program to find minimum spanning tree of a graph using Kruskal’s Algorithm.

//krushkal.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[9][9],parent[9];

int find(int);

int uni(int,int);

int main()

printf("\n\n\tImplementation of Kruskal's algorithm\n\n");

printf("\nEnter the no. of vertices\n");

scanf("%d",&n);

printf("\nEnter the cost adjacency matrix\n");

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

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");

while(ne<n)

for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++)
{

if(cost[i][j]<min)

min=cost[i][j];

a=u=i;

b=v=j;

u=find(u);

v=find(v);

if(uni(u,v))

printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost);

getch();

int find(int i)

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)
{

parent[j]=i;

return 1;

getch();

Output:
5. Write a program to find shortest path between 2 vertices in a graph using Disjkstra’s Algorithm.

//dijkstra.

#include "stdio.h"

#include "conio.h"

#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[])

int i,u,count,w,flag[10],min;

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

flag[i]=0,dist[i]=cost[v][i];

count=2;

while(count<=n)

min=99; for(w=1;w<=n;w+

+) if(dist[w]<min && !

flag[w]) min=dist[w],u=w;

flag[u]=1;

count++;

for(w=1;w<=n;w++)

if((dist[u]+cost[u][w]<dist[w]) && !flag[w])

dist[w]=dist[u]+cost[u][w];

int main()

int n,v,i,j,cost[10][10],dist[10];

printf("\n Enter the number of nodes:");

scanf("%d",&n);
printf("\n Enter the cost matrix:\n");

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

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0) cost[i]

[j]=infinity;

printf("\n Enter the source vertex:");

scanf("%d",&v);

dij(n,v,cost,dist);

printf("\n Shortest path:\n");

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

if(i!=v)

printf("%d->%d,cost=%d\n",v,i,dist[i]);

getch();

Output:
6. Write a program to find shortest path between every pair of vertices in a graph using Floyd
warshall’s algorithm.

#include<iostream>

using namespace std;

#include<conio.h>

using namespace std;

void floyds(int b[][7],int n)

int i,j,k;

for (k = 0; k < n; k++)

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

for (j = 0; j < n; j++)

if ((b[i][k] * b[k][j] != 0) && (i != j))

if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))

b[i][j] = b[i][k] + b[k][j];

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

cout<<"\nMinimum Cost With Respect to Node:"<<i<<endl;

for (j = 0; j < n; j++)


{

cout<<b[i][j]<<"\t";

int main()

int b[7][7],n;

cout<<"\n Enter the number of nodes:";

cin>>n;

cout<<"ENTER VALUES OF ADJACENCY MATRIX\n\n";

for (int i = 0; i< n; i++)

cout<<"enter values for "<<(i+1)<<" row"<<endl;

for (int j = 0; j < n; j++)

cin>>b[i][j];

}}

floyds(b,n);

getch();}
Output:
7. Write a program to find shortest path between every pair of vertices in a graph using Bellman
Ford’s algorithm.

//bellmanford.

#include<iostream>

#include<stdio.h>

using namespace std;

#include<conio.h>

#define INFINITY 999

struct node

int cost;

int value;

int from;

}a[5];

void addEdge(int am[][5],int src,int dest,int cost)

am[src][dest] = cost;

return;

void bell(int am[][5])

int i, j, k, c = 0, temp;

a[0].cost = 0;

a[0].from = 0;

a[0].value = 0;

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

a[i].from = 0;

a[i].cost = INFINITY;

a[i].value = 0;
}

while (c < 5)

int min = 999;

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

if (min > a[i].cost && a[i].value == 0)

min = a[i].cost;

else

continue;

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

if (min == a[i].cost && a[i].value == 0)

break;

else

continue;

temp = i;

for (k = 0; k < 5; k++)

if (am[temp][k] + a[temp].cost < a[k].cost)


{

a[k].cost = am[temp][k] + a[temp].cost;

a[k].from = temp;

else

continue;

a[temp].value = 1;

c++;

cout<<"Cost"<<"\t"<<"Source Node"<<endl;

for (j = 0; j < 5; j++)

cout<<a[j].cost<<"\t"<<a[j].from<<endl;

int main()

int n, am[5][5], c = 0, i, j, cost;

for (int i = 0; i< 5; i++)

for (int j = 0; j < 5; j++)

am[i][j] = INFINITY;

}}

while (c < 8)

cout<<"Enter the source, destination and cost of edge\n";


cin>>i>>j>>cost;

addEdge(am, i, j, cost);

c++;}

bell(am);

getch();}

output:
8. Write a program to find maximum matching in a bipartite graph.

#include <iostream>

#include <string.h>

#include<conio.h>

#include<stdio.h>

using namespace std;

// M is number of applicants and N is number of jobs

#define M 6

#define N 6

// A DFS based recursive function that returns true if a

// matching for vertex u is possible

bool bpm(bool bpGraph[M][N], int u, bool seen[], int matchR[])

// Try every job one by one

for (int v = 0; v < N; v++)

// If applicant u is interested in job v and v is

// not visited

if (bpGraph[u][v] && !seen[v])

seen[v] = true; // Mark v as visited

// If job 'v' is not assigned to an applicant OR

// previously assigned applicant for job v (which is matchR[v])

// has an alternate job available.

// Since v is marked as visited in the above line, matchR[v]

// in the following recursive call will not get job 'v' again

if (matchR[v] < 0 || bpm(bpGraph, matchR[v], seen, matchR))


{

matchR[v] = u;

return true;

return false;

// Returns maximum number of matching from M to N

int maxBPM(bool bpGraph[M][N])

// An array to keep track of the applicants assigned to

// jobs. The value of matchR[i] is the applicant number

// assigned to job i, the value -1 indicates nobody is

// assigned.

int matchR[N];

// Initially all jobs are available

memset(matchR, -1, sizeof(matchR));

int result = 0; // Count of jobs assigned to applicants

for (int u = 0; u < M; u++)

// Mark all jobs as not seen for next applicant.

bool seen[N];

memset(seen, 0, sizeof(seen));

// Find if the applicant 'u' can get a job

if (bpm(bpGraph, u, seen, matchR))


result++;

return result;

// Driver program to test above functions

int main()

// Let us create a bpGraph shown in the above example

bool bpGraph[M][N] = { {0, 1, 1, 0, 0},

{1, 0, 0, 1, 0},

{1, 0, 1, 0, 1},

{1, 0, 1, 1, 0},

{0, 1, 0, 1, 0},

};

cout<< "Maximum number of applicants that can get job is "

<<maxBPM(bpGraph);

getch();

Output:
10. Write a program to find maximum flow from source node to sink using ford-fulkerson
algorithm.

#include <iostream>

#include <string.h>

#include <queue>

#include<stdio.h>

#include<conio.h>

using namespace std;

bool bfs(int rGraph[][6], int s, int t, int parent[])

bool visited[6];

memset(visited, 0, sizeof(visited));

queue<int> q;

q.push(s);

visited[s] = true;

parent[s] = -1;

while (!q.empty())

int u = q.front();

q.pop();

for (int v = 0; v < 6; v++)

if (visited[v] == false &&rGraph[u][v] > 0)

q.push(v);

parent[v] = u;

visited[v] = true;

}
}

return (visited[t] == true);

int fordFulkerson(int graph[6][6], int s, int t)

int u, v;

int rGraph[6][6];

for (u = 0; u < 6; u++)

for (v = 0; v < 6; v++)

rGraph[u][v] = graph[u][v];

int parent[6];

int max_flow = 0;

while (bfs(rGraph, s, t, parent))

int path_flow = INT_MAX;

for (v = t; v != s; v = parent[v])

u = parent[v];

path_flow = min(path_flow, rGraph[u][v]);

for (v = t; v != s; v = parent[v])

u = parent[v];

rGraph[u][v] -= path_flow;

rGraph[v][u] += path_flow;
}

max_flow += path_flow;

return max_flow;

int main()

int graph[6][6] = { {0, 10, 7, 0, 0, 0},

{0, 0, 10, 9, 0, 0},

{0, 7, 0, 0, 14, 0},

{0, 0, 8, 0, 0, 20},

{0, 0, 0, 3, 0, 6},

{0, 0, 0, 0, 0, 0}

};

cout<< "The maximum possible flow is " <<fordFulkerson(graph, 0, 5);

getch();

Output:

You might also like