Académique Documents
Professionnel Documents
Culture Documents
FILE
7th Semester
MCE
#include<conio.h>
int main()
int n,v,i,j,cost[10][10],ne=0,no=0,ni,count=0;
scanf("%d",&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++;
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>
int i = 0, j = 0;
else
i++;
while(i < m)
while(j < n)
int main()
int n = sizeof(V2)/sizeof(V2[0]);
int E1[m][m],E2[n][n],E3[m+n][m+n];
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("E1[%d][%d]=",i,j);
scanf("%d",&E1[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("E2[%d][%d]=",i,j);
scanf("%d",&E2[i][j]);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
E3[i][j]=E2[i][j];
else
E3[i][j]=E2[i][j];
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>
int i = 0, j = 0;
i++;
j++;
i++;
}
int main()
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;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("E1[%d][%d]=",i,j);
scanf("%d",&E1[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("E2[%d][%d]=",i,j);
scanf("%d",&E2[i][j]);
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];
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>
int i = 0, j = 0;
else
i++;
}
}
while(i < m)
while(j < n)
int main()
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;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("E1[%d][%d]=",i,j);
scanf("%d",&E1[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("E2[%d][%d]=",i,j);
scanf("%d",&E2[i][j]);
}
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
E3[i][j]=0;
E3[i][j]=E2[i][j];
E3[i][j]=E1[i][j];
else
E3[i][j]=E2[i][j];
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()
scanf("%d",&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)
mincost+=min;
visited[b]=1;} cost[a]
[b]=cost[b][a]=999;
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()
scanf("%d",&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;
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))
mincost +=min;
cost[a][b]=cost[b][a]=999;
getch();
int find(int i)
while(parent[i])
i=parent[i];
return i;
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"
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++)
dist[w]=dist[u]+cost[u][w];
int main()
int n,v,i,j,cost[10][10],dist[10];
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;
scanf("%d",&v);
dij(n,v,cost,dist);
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>
#include<conio.h>
int i,j,k;
cout<<b[i][j]<<"\t";
int main()
int b[7][7],n;
cin>>n;
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>
#include<conio.h>
struct node
int cost;
int value;
int from;
}a[5];
am[src][dest] = cost;
return;
int i, j, k, c = 0, temp;
a[0].cost = 0;
a[0].from = 0;
a[0].value = 0;
a[i].from = 0;
a[i].cost = INFINITY;
a[i].value = 0;
}
while (c < 5)
min = a[i].cost;
else
continue;
break;
else
continue;
temp = i;
a[k].from = temp;
else
continue;
a[temp].value = 1;
c++;
cout<<"Cost"<<"\t"<<"Source Node"<<endl;
cout<<a[j].cost<<"\t"<<a[j].from<<endl;
int main()
am[i][j] = INFINITY;
}}
while (c < 8)
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>
#define M 6
#define N 6
// not visited
// in the following recursive call will not get job 'v' again
matchR[v] = u;
return true;
return false;
// assigned.
int matchR[N];
bool seen[N];
memset(seen, 0, sizeof(seen));
return result;
int main()
{1, 0, 0, 1, 0},
{1, 0, 1, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 1, 0},
};
<<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>
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();
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
int u, v;
int rGraph[6][6];
rGraph[u][v] = graph[u][v];
int parent[6];
int max_flow = 0;
for (v = t; v != s; v = parent[v])
u = parent[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()
{0, 0, 8, 0, 0, 20},
{0, 0, 0, 3, 0, 6},
{0, 0, 0, 0, 0, 0}
};
getch();
Output: