0% found this document useful (0 votes)
71 views5 pages

TEMA

The document contains code for several graph algorithms and data structures: 1) Functions for checking if a graph is complete, solving a dinner party seating problem, checking if a graph is bipartite, generating all bipartite complete graphs, solving another bipartite graph problem, and generating all graphs. 2) The algorithms use common graph representations like adjacency matrices and lists, and techniques like backtracking to solve problems like finding if a graph is bipartite. 3) The code includes input/output streams, functions for core graph problems, and main functions to test the algorithms.

Uploaded by

Mihai
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)
71 views5 pages

TEMA

The document contains code for several graph algorithms and data structures: 1) Functions for checking if a graph is complete, solving a dinner party seating problem, checking if a graph is bipartite, generating all bipartite complete graphs, solving another bipartite graph problem, and generating all graphs. 2) The algorithms use common graph representations like adjacency matrices and lists, and techniques like backtracking to solve problems like finding if a graph is bipartite. 3) The code includes input/output streams, functions for core graph problems, and main functions to test the algorithms.

Uploaded by

Mihai
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/ 5

**************GRAF COMPLET*******************************

#include <fstream>
using namespace std;

ifstream fin("graf_complet.in");
ofstream fout("graf_complet.out");

int n,nr,x;

void graf(int nr) {int i,j,ok=1;


for(i=1;i<=nr;i++)
for(j=1;j<=nr;j++) {fin>>x; if(x==0 && i!=j) ok=0;}
if(ok==1)fout<<"DA"<<'\n';
else fout<<"NU"<<'\n';
}

int main ()
{
int k;
fin>>n;
for(k=1; k<=n; k++)
{
fin>>nr;
graf(nr);
}
}
**************DINEU**********************10 P ******************
#include <fstream>
using namespace std;

ifstream fin("dineu.in");
ofstream fout("dineu.out");

int n,k,g[22][12],S[22],s[22],NS,l;

int suma () {
int i,su=0;
for(i=1;i<=n;i++) su+=s[i];
return su;
}

void afisare() {
int ns=suma(),i;
if(ns>NS) {NS=ns;
for(i=1;i<=n;i++) S[i]=s[i];}
}

bool corect(int k) {
int i,j, ok;
for(i=1;i<k;i++) {
if(s[i]) {
ok=0;
for(j=1;j<=l&& !ok ;j++) if(g[i][j]==1 && g[k][j]==1) ok=1;
if (ok==0) return 0;
}
}
return 1;
}
void bkt(int k) {
int i;
for(i=1;i>=0;i--)
{
s[k]=i;
if(corect(k)) {
if(k==n) afisare();
else bkt(k+1);
}
}
}

int main()
{int i,li;
fin>>n>>l;
for(i=1;i<=n;i++)
for(fin>>k;k;k--) {fin>>li;g[i][li]=1;}
bkt(1);
fout<<NS<<'\n';
for(i=1;i<=n;i++) if(S[i])fout<<i<<" ";
return 0;
}
********************BIPARTIT**********************************
#include <fstream>
using namespace std;

ifstream fin("bipartit.in");
ofstream fout("bipartit.out");

int n,k,i,j,mi[102],mo[102],p[102],np;

int main()
{
fin>>n>>k;
for (i=1;i<=k;i++) {
fin>>mi[i]>>mo[i];
}
fin>>np;
for (i=1;i<=np;i++) {fin>>j; p[j]=1;}

for (i=1;i<=k;i++)
if (p[mi[i]]+p[mo[i]]!=1) {fout<<"NU"; return 0;}
fout<<"DA";
}
*******************BIPARTITCOMPLET***********************************
#include <fstream>
using namespace std;

ifstream fin("bipartitcomplet.in");
ofstream fout("bipartitcomplet.out");

int n,k,a[101];
bool g[101][101];

bool ok(int m,int k) {


int i;
if(m==k) return 0;
for(i=1;i<=k;i++) if(k==a[i]) return 0;
return 1;
}

int main () {int i,j=1,x;


fin>>n>>k;
for(i=1;i<=k;i++) {fin>>x; a[x]=1;}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(a[i]+a[j]==1) g[i][j]=g[j][i]=1;
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)fout<<g[i][j]<<" ";
fout<<'\n';
}
}
******************************BIPARTIT2****************************
#include <fstream>
using namespace std;
ifstream fin("bipartit2.in");
ofstream fout("bipartit2.out");

int n,m,c[101],s[101];
bool g[101][101],ok;

void afisare() {
int i;ok=1;
fout<<"DA"<<'\n';
for(i=1;i<=n;i++) if (s[i]) fout<<i<<" ";
fout<<'\n';
for(i=1;i<=n;i++) if (!s[i]) fout<<i<<" ";
}

bool corect(int k) {
int i;
//o solutie
if(ok) return 0;
//muchie in aceeasi multime
for (i=1;i<k;i++) if(s[i]==s[k] && g[i][k]) return 0;
//ok
return 1;
}

void bkt(int k) {
int i;
for(i=0;i<=1;i++)
{
s[k]=i;
if(corect(k)) {
if(k==n) afisare();
else bkt(k+1);
}
}
}

int main ()
{
int i,a,b;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>a>>b;
g[a][b]=g[b][a]=1;
}
s[1]=1;
bkt(2);

if(ok==0) fout<<"NU";
}
****************************BIPARTIT1MARE******************************
#include <fstream>
using namespace std;
ifstream fin("bipartit1mare.in");
ofstream fout("bipartit1mare.out");

int n,m,c[101],s[101];
bool g[101][101],ok;

void afisare() {
int i;ok=1;
fout<<"DA"<<'\n';
for(i=1;i<=n;i++) if (s[i]) fout<<i<<" ";
fout<<'\n';
for(i=1;i<=n;i++) if (!s[i]) fout<<i<<" ";
}

bool corect(int k) {
int i;
//o solutie
if(ok) return 0;
//muchie in aceeasi multime
for (i=1;i<k;i++) if(s[i]==s[k] && g[i][k]) return 0;
//ok
return 1;
}

void bkt(int k) {
int i;
for(i=0;i<=1;i++)
{
s[k]=i;
if(corect(k)) {
if(k==n) afisare();
else bkt(k+1);
}
}
}

int main ()
{
int i,a,b;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>a>>b;
g[a][b]=g[b][a]=1;
}s[1]=1;
bkt(2);
if(ok==0) fout<<"NU";
}
*************************************GENGRAF**********************************
#include <fstream>
using namespace std;
ifstream f("gengraf.in");
ofstream g("gengraf.out");
int n, i, j, y, x, a[105][105], p=1;
int main()
{
f>>n;
for(i=1; i<=n*(n-1)/2; i++) p*=2;
g<<p<<endl;
p--;
for(y=0; y<=p; y++)
{
x=y;
for(i=n-1; i>=1; i--)
for(j=n; j>i; j--)
{a[i][j]=a[j][i]=x%2;
x/=2;
}
//afis
for(i=1; i<=n; i++)
{for(j=1; j<=n; j++) g<<a[i][j]<<" ";
g<<endl;
}
g<<endl;
}
}

You might also like