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

Grafuri

This C++ program reads in data from a file and performs several graph operations: 1. It reads in the number of nodes and edges of a graph and builds the adjacency matrix. 2. It performs depth-first search to label the connected components and finds properties like the largest component. 3. It checks if two nodes are in the same connected component and prints all pairs of nodes that are in the same component.

Uploaded by

The Rebel Hunter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Grafuri

This C++ program reads in data from a file and performs several graph operations: 1. It reads in the number of nodes and edges of a graph and builds the adjacency matrix. 2. It performs depth-first search to label the connected components and finds properties like the largest component. 3. It checks if two nodes are in the same connected component and prints all pairs of nodes that are in the same component.

Uploaded by

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

#include <iostream>

#include <fstream>
#include <cstring>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int i,j,n,m,viz[100],a[100][100],x,y,s,maxi,maxx,nr,t,k,cmax,p;
void df(int i,int nr)
{
int j;
viz[i]=nr;
for(j=1; j<=n; j++)
if(a[i][j]==1 && viz[j]==0)
df(j,nr);
}
int main()
{
f>>n>>m;
for(i=1; i<=m; i++)
{
f>>x>>y;
a[x][y]=a[y][x]=1;
}
g<<"a) ";
for(i=1; i<=n; i++)
{
s=0;
for(j=1; j<=n; j++)
s=s+a[i][j];
if(s==0) g<<i<<endl;
}
g<<"b) ";
for(i=1; i<=n; i++)
{
s=0;
for(j=1; j<=n; j++)
s=s+a[i][j];
g<<i<<" "<<s<<endl;
}
g<<"c) ";
for(i=1; i<=n; i++)
{
s=0;
for(j=1; j<=n; j++)
s=s+a[i][j];
if(s>maxi)
maxi=s;
}
g<<maxi<<endl;
g<<"d) ";
for(i=1; i<=n; i++)
{
s=0;
for(j=1; j<=n; j++)
s=s+a[i][j];
if(s==maxi)
g<<i<<" ";
}
g<<endl<<"e) ";
f>>x;
for(j=1; j<=n; j++)
if(a[x][j]==1) g<<j<<" ";
g<<endl<<"f) ";
for(i=1; i<=n; i++)
if(viz[i]==0)
{
nr++;
df(i,nr);
}
g<<nr<<endl;
g<<"g) ";
for(k=1; k<=nr; k++)
{
t=0;
for(i=1; i<=n; i++)
if(viz[i]==k)
t++;
if(t>maxx)
{
maxx=t;
cmax=k;
}
}
g<<maxx<<endl;
g<<"h) ";
for(k=1; k<=nr; k++)
{
g<<k<<": ";
for(i=1; i<=n; i++)
if(viz[i]==k)
g<<i<<" ";
g<<endl;
}
g<<"i) "<<nr-1<<endl;
p=nr-1;
g<<"j) ";
while(p)
{
for(i=1; i<n; i++)
if(viz[i]!=viz[i+1])
{
g<<i<<" "<<i+1;
t=viz[i+1];
for(j=1; j<=n; j++)
if(viz[j]==t)
viz[j]=1;
p--;
g<<endl;
}
}

return 0;
}

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int i,j,n,m,viz[100],a[100][100],x,y,s,maxi,maxx,nr,t,k,cmax,p;
void df(int i,int nr)
{
int j;
viz[i]=nr;
for(j=1; j<=n; j++)
if(a[i][j]==1 && viz[j]==0)
df(j,nr);
}
int main()
{
f>>n>>m;
for(i=1; i<=m; i++)
{
f>>x>>y;
a[x][y]=a[y][x]=1;
}
g<<"a) ";
f>>x>>y;;
for(i=1; i<=n; i++)
if(viz[i]==0)
{
nr++;
df(i,nr);
}
if(viz[x]==viz[y])
g<<"DA";
else g<<"NU";
g<<endl;
g<<"b) ";
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(viz[i]==viz[j])
g<<i<<" "<<j<<endl;

return 0;
}

You might also like