You are on page 1of 61

ALGORITMICA GRAFURILOR

Cuprins

1. LABORATOR 1....................................................................................................................................3
1.1. CREAREA UNUI ARBORE BINAR SI PARCURGEREA SA PRIN CELE 3 FORME: RSD, SRD,SDR.........................3
2. LABORATOR 2....................................................................................................................................5
2.1. CITIREA UNUI GRAF............................................................................................................................5
2.2. OBTINEREA DINTR-UN GRAF A UNUI ALT GRAF PRIN CONTRACTIE................................................................5
3. LABORATOR 3....................................................................................................................................7
3.1. DETERMINAREA UNUI SUBGRAF AL UNUI GRAF.......................................................................................7
3.2. DETERMINAREA UNUI GRAF PARTIAL AL UNUI GRAF ORIENTAT...................................................................8
3.3. DETERMINAREA VECINILOR UNUI VARF AL UNUI GRAF...............................................................................9
3.4. DETERMINAREA GRADELOR VARFURILOR UNUI GRAF,A GRADULUI MINIM SI A GRADULUI MAXIM...................10
3.5. DETERMINAREA MULTIMILOR: W+(A)-MULTIMEA ARCELOR INCIDENTE CU A CATRE EXTERIOR, W- (A)-MULTIMEA
ARCELOR INCIDENTE CU A CATRE INTERIOR SI A VECINILOR LUI A, UNDE A ESTE O SUBMULTIME DE VARFURI ALE
GRAFULUI.11
3.6. VERIFICAREA DACA UN GRAF ESTE SIMETRIC/ANTISIMETRIC.....................................................................12
4. LABORATOR 4..................................................................................................................................14
4.1. VERIFICAREA IZOMORFISMULUI INTRE DOUA GRAFURI.............................................................................14
5. LABORATOR 5..................................................................................................................................16
5.1. ALGORITMUL ROY-WARSHALL..........................................................................................................16
5.2. ALGORITMUL ROY-FLOYD................................................................................................................17
5.3. ALGORITMUL LUI DIJKSTRA...............................................................................................................18
6. LABORATOR 6..................................................................................................................................21
6.1. ALGORITMUL BELLMAN-FORD...........................................................................................................21
6.2. ALGORITMUL LUI PRIM.....................................................................................................................22
6.3. ALGORITMUL LUI KRUSKAL...............................................................................................................24
6.4. ALGORITM PENTRU DETERMINAREA COMPONENTELOR CONEXE.................................................................26
7. LABORATOR 7..................................................................................................................................29
7.1. COLORARAREA SECVENTIALA A UNUI GRAF...........................................................................................29
7.2. COLORAREA SECVENTIALA(ALGORITMUL LARGER FIRST).......................................................................30
7.3. ALGORITMUL DSATUR(DEGREE OF SATURATION) AL LUI BRELAZ........................................................32
8. LABORATOR 8..................................................................................................................................36
8.1. ALGORITMUL FORD-FULKERSON........................................................................................................36
8.2. ALGORITMUL FORD-FULKERSON(METODA ETICHETARII).........................................................................38
9. LABORATOR 9..................................................................................................................................42
9.1. ALGORITMUL LUI FLEURY.................................................................................................................42
9.2. ALGORITMUL LUI TERRY LORING.......................................................................................................44
9.3. ALGORITM BACKTRACKING PENTRU A DETERMINA DACA UN GRAF ESTE EULERIAN.......................................47
9.4. ALGORITM BACKRTACKING PENTRU A DETERMINA DACA UN GRAF ESTE HAMILTONIAN..................................48
10. LABORATOR 10...........................................................................................................................51
10.1. VERIFICAREA DACA UN GRAF NEORIENTAT ESTE BIPARTIT........................................................................51
10.2. ALGORITMUL UNGAR PENTRU DETERMINAREA UNUI CUPLAJ MAXIM DE VALOARE MINIMA INTR-UN GRAF BIPARTIT
53
10.3. ALGORITM GREEDY PENTRU GASIREA UNUI CUPLAJ MAXIMAL INTR-UN GRAF..............................................56
11. LABORATOR 11............................................................................................................................59
11.1. PARCURGEREA IN ADANCIME( DFS) A UNUI GRAF...............................................................................59
11.2. PARCURGEREA IN LATIME( BFS) A UNUI GRAF.....................................................................................60
1. LABORATOR 1

1.1. Crearea unui arbore binar si parcurgerea sa prin cele
3 forme: RSD, SRD,SDR
# include <stdio.h>
# include <conio.h>
# include <alloc.h>
# include <ctype.h>
typedef struct arbore
{
int inf;
struct arbore *st,*dr;
}arbore;
arbore *creare(void)
{
arbore *aux;
char ch;
printf("\n e nod NULL?[d/n]");
ch=getche();
ch=toupper(ch);
if (ch=='N')
{
aux=(arbore *)malloc(sizeof(arbore));
printf("\n informatia=");
scanf("%d",&aux->inf);
printf("\n urmeaza succesorul stang al nodului cu
informatia %d",aux->inf);
aux->st=creare();
printf("\n urmeaza succesorul drept al nodului cu
informatia %d",aux->inf);
aux->dr=creare();
return aux;
}
else return NULL;
}
void RSD(arbore *a)
{
if (a!=NULL)
{
printf("%d ",a->inf);
RSD(a->st);
RSD(a->dr);
}
}
void SRD(arbore *a)
{
if(a!=NULL)
{
SRD(a->st);
printf("%d ",a->inf);
SRD(a->dr);
}
}
void SDR(arbore *a)
{
if (a!=NULL)
{
SDR(a->st);
SDR(a->dr);
printf("%d ",a->inf);
}
}
void main(void)
{
clrscr();
arbore *a;
a=creare();
printf("\n parcurgerea RSD este ");
RSD(a);
printf("\n parcurgerea SRD este ");
SRD(a);
printf("\n parcurgerea SDR este ");
SDR(a);
getch();
}
2. LABORATOR 2

2.1. Citirea unui graf

# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j;
FILE *f;
f=fopen("graf.in","r");
fscanf("f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

2.2. Obtinerea dintr-un graf a unui alt graf prin
contractie.

# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j,x,y;
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n dati nodurile pe care doriti sa le uniti:");
scanf("%d",&x);
scanf("%d",&y);
if(a[x][y]==0) printf("\n nu se poate face
contractarea!");
else
{
for(i=1;i<=n;i++)
if(a[i][x]==1 && i!=x)
{
a[i][y]=a[i][x];
a[y][i]=a[i][y];
}
else
if (i==x)
{
a[i][y]=0;
a[y][i]=0;
}
for(i=x;i<n;i++)
for(j=1;j<=n;j++)
a[i][j]=a[i+1][j];
for(i=1;i<=n;i++)
for(j=x;j<n;j++)
a[i][j]=a[i][j+1];

n=n-1;
printf("\n matricea de adiacenta corespunzatoare
noului graf este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
getch();
}
3. LABORATOR 3

3.1. Determinarea unui subgraf al unui graf.

# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j,m,v[50],k;
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n cate varfuri doriti sa eliminati?");
scanf("%d",&m);
printf("introduceti cele %d varfuri:",m);
for(i=1;i<=m;i++) scanf("%d",&v[i]);
for(k=1;k<=m;k++)
{
for(i=v[k]-k+1;i<n;i++)
for(j=1;j<=n;j++)
a[i][j]=a[i+1][j];
for(i=1;i<=n;i++)
for(j=v[k]-k+1;j<n;j++)
a[i][j]=a[i][j+1];
n=n-1;
}
printf("subgraful rezultat are %d varfuri,matricea sa
fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}

3.2. Determinarea unui graf partial al unui graf orientat.

# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j,m,v[50],k;
FILE *f;
char c;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n pt a abtine un graf partial trebuie sa
eliminati anumite arce!!");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if (a[i][j]==1)
{
printf("\n doriti eliminarea arcului
(%d,%d)?[d/n]",i,j);
c=getche();
if(c=='d')
a[i][j]=0;
}
}
printf("\n graful partial obtinut are matricea de
adiacenta:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
/*am prezentat cazul grafului orientat. In cazul grafului
neorientat algoritmul ar fi:
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
printf("\n doriti eliminarea
muchiei(%d,%d)?[d/n]",i,j);
c==getche();
if (c=='d')
{
a[i][j]=0;
a[j][i]=0;
}
}*/
getch();
}

3.3. Determinarea vecinilor unui varf al unui graf.

# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j,x,v[50],k;
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("dati varful ai carui vecini doriti sa-i aflati:");
scanf("%d",&x);
k=0;
for(i=1;i<=n;i++)
if (a[i][x]==1 || a[x][i]==1)
{
k++;
v[k]=i;
}
printf("\n vecinii lui %d sunt:",x);
for(i=1;i<=k;i++)
printf("%d ",v[i]);
getch();
}

3.4. Determinarea gradelor varfurilor unui graf,a
gradului minim si a gradului maxim.

# include <stdio.h>
# include <conio.h>
struct semigrad
{
int interior;
int exterior;
}semigrad[50];
int minim(int v[50],int n)
{
int i,min=v[1];
for(i=2;i<=n;i++)
if(v[i]<min) min=v[i];
return min;
}
int maxim(int v[50],int n)
{
int i,max=v[1];
for(i=2;i<=n;i++)
if (v[i]>max) max=v[i];
return max;
}
void main(void)
{
int n,a[50][50],i,j,grad[50];
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=1;i<=n;i++) grad[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if (a[i][j]==1 || a[j][i]==1) grad[i]++;
if (a[i][j]==1) semigrad[i].exterior++;
if (a[j][i]==1) semigrad[i].interior++;
}
for(i=1;i<=n;i++)
{
printf("\n pentru varful %d:",i);
printf("\n grad=%d",grad[i]);
printf("\n semigrad
interior=%d",semigrad[i].interior);
printf("\n semigrad
exterior=%d",semigrad[i].exterior);
}
printf(“\n gradul minim este %d,iar gradul maxim este
%d!”,minim(grad,n),maxim(grad,n));
getch();
}

3.5. Determinarea multimilor: w+(A)-multimea arcelor
incidente cu A catre exterior, w- (A)-multimea arcelor
incidente cu A catre interior si a vecinilor lui A, unde
A este o submultime de varfuri ale grafului.

# include <stdio.h>
# include <conio.h>
int in(int v[50],int n,int x)
{
for(int i=1;i<=n;i++)
if (v[i]==x) return 1;
return 0;
}
void main(void)
{
int n,a[50][50],i,j,m,A[50],k,v[50];
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n cate elemente are submultimea de varfuri A?");
scanf("%d",&m);
printf("\n dati cele %d elemente ale submultimii de
varfuri A:",m);
for(i=1;i<=m;i++)
scanf("%d",&A[i]);
printf("\n multimea w_plus(A) este:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if (a[A[i]][j]==1 && in(A,m,j)==0)
printf("(%d,%d);",A[i],j);
printf("\n multimea w_minus(A) este:");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if (a[i][A[j]]==1 && in(A,m,i)==0)
printf("(%d,%d);",i,A[j]);
printf("\n multimea vecinilor lui A este:");
k=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if ((a[A[i]][j]==1 || a[j][A[i]])&& ((in(A,m,j)==0)))
if (in(v,k,j)==0)
{
k++;
v[k]=j;
}
for(i=1;i<=k;i++) printf("%d ",v[i]);
getch();
}

3.6. Verificarea daca un graf este simetric/antisimetric.
# include <stdio.h>
# include <conio.h>
void main(void)
{
int n,a[50][50],i,j,ok;
FILE *f;
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful citit are %d noduri!",n);
printf("\n matricea sa de adiacenta este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
ok=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j]!=a[j][i]) ok=0;
if (ok==1) printf("\n graful este simetric!");
else printf("\n graful nu este simetric!");
ok=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j]==1 && a[j][i]==1) ok=0;
if (ok==1) printf("\n graful este antisimetric!");
else printf("\n graful nu este antisimetric!");
getch();
}
4. LABORATOR 4

4.1. Verificarea izomorfismului intre doua grafuri

# include <stdio.h>
# include <conio.h>
int a[25][25],st[25],n,m,t,b[25][25];
void initializare(void)
{
int i,j;
FILE *f;
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
printf("primul graf are %d noduri,matricea sa de
adicaenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
fscanf(f,"%d",&m);
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
fscanf(f,"%d",&b[i][j]);
printf("al doilea graf are %d noduri,matricea sa de
adiacenta fiind:\n",m);
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
printf("%d ",b[i][j]);
printf("\n");
}
fclose(f);
t=0;
for(i=1;i<25;i++) st[i]=0;
printf("%d %d\n",n,m);
}
void tipar(int k)
{
int i,j,ok=1;
for(i=1;i<=k;i++)
for(j=1;j<=k;j++)
if (b[i][j]!=a[st[i]][st[j]]) ok=0;
if (ok==1) t++;
}
int valid(int k)
{
for(int i=1;i<k;i++)
if (st[i]==st[k]) return 0;
return 1;
}
void bktr(int k)
{
int i;
for(i=1;i<=n;i++)
{
st[k]=i;
if (valid(k)==1)
if (k==n) tipar(k);
else bktr(k+1);
}
}
void main(void)
{
clrscr();
initializare();
if (n!=m) printf("grafurile nu sunt izomorfe!\n");
else
{
bktr(1);
if (t!=0) printf("grafurile sunt izomorfe!\n");
else printf("grafurile nu sunt izomorfe!\n");
}
getch();
}
5. LABORATOR 5

5.1. Algoritmul Roy-Warshall.

# include <stdio.h>
# include <conio.h>
int min(int a,int b)
{
return a<b?a:b;
}
void main(void)
{
int a[50][50],n,i,j,k;
FILE *f;
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful are %d varfuri,matricea sa de adiacenta
fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j]==0 && i!=k && j!=k)
a[i][j]=min(a[i][k],a[k][j]);
printf("\n matricea drumurilor este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}
5.2. Algoritmul Roy-Floyd
# include <stdio.h>
# include <conio.h>
int min(int a,int b)
{
return a<b?a:b;
}
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%4d ",a[i][j]);
printf("\n");
}
}
void main(void)
{
int a[50][50],m[50][50],n,i,j,k;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful are %d varfuri,matricea sa de
adiacenta fiind:\n",n);
afisare(a,n);
printf("\n se va construi matricea distantelor
directe!");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (i==j) m[i][j]=0;
else
if (a[i][j]==0) m[i][j]=1000;
else
{
printf("\n cat este distanta intre %d si
%d?",i,j);
scanf("%d",&m[i][j]);
}
printf("\n matricea drumurilor directe este:\n");
afisare(m,n);
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (i!=k && j!=k && i!=j)
m[i][j]=min(m[i][j],m[i][k]+m[k][j]);
printf("\n matricea distantelor minime este :\n");
afisare(m,n);
getch();
}
5.3. Algoritmul lui Dijkstra
#include <stdio.h>
#include <conio.h>
#include <values.h>
#include <alloc.h>
typedef struct set
{
int inf;
struct set *next;
}set;
int d[50];
int min(set *s)
{
int i,min,t;
min=d[s->inf];t=s->inf;
for(set *p=s;p!=NULL;p=p->next)
if (d[p->inf]<min)
{
min=d[p->inf];
t=p->inf;
}
return t;
}
set *stergere(set *s,int inf)
{
set *p=s,*q;
if (s->inf==inf) s=s->next;
else
{
while(p->next->inf!=inf && p->next!=NULL) p=p->next;
if (p->next!=NULL)
{
q=p->next;
p->next=q->next;
free(q);
}
}
return s;
}
void main(void)
{
int a[25][25],m[50][50],u,i,j,n,x;
FILE *f;
set *s,*aux,*q;
f=fopen("gr.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful are %d varfuri,matricea de adiacenta
fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (i==j) m[i][j]=0;
else
if (a[i][j]==0) m[i][j]=10000;
else
{
printf("dati distanta directa intre %d si
%d!\n",i,j);
scanf("%d",&m[i][j]);
}
printf("dati varful de start :");
scanf("%d",&x);
for(i=1;i<=n;i++)
if (i==x) d[i]=0;
else d[i]=10000;
s=NULL;q=NULL;
for(i=1;i<=n;i++)
{
aux=(set *)malloc(sizeof(set));
aux->inf=i;
aux->next=q;
q=aux;
}
while(q!=NULL)
{
u=min(q);
q=stergere(q,u);
aux=(set *)malloc(sizeof(set));
aux->inf=u;
aux->next=s;
s=aux;
for(i=1;i<=n;i++)
if (a[u][i]==1)
if (d[u]+m[u][i]<d[i])
d[i]=d[u]+m[u][i];
}
printf("vectorul d este:\n");
for(i=1;i<=n;i++) printf("%d ",d[i]);
getch();
}
6. LABORATOR 6
6.1. Algoritmul Bellman-Ford

# include <stdio.h>
# include <conio.h>
void main(void)
{
int a[50][50],m[50][50],d[50],i,j,n,s,k;
FILE *f;
clrscr();
f=fopen("gr.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful are %d varfuri,matricea sa de adiacenta
fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (i==j) m[i][j]=0;
else
if (a[i][j]==0) m[i][j]=1000;
else
{
printf("\n cat este distanta intre %d si
%d?",i,j);
scanf("%d",&m[i][j]);
}
printf("\n matricea drumurilor directe este:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%5d ",m[i][j]);
printf("\n");
}

printf("s=");
scanf("%d",&s);
for(i=1;i<=n;i++)
if (s==i) d[i]=0;
else d[i]=10000;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if (a[i][j]==1)
if (d[j]>d[i]+m[i][j])
d[j]=d[i]+m[i][j];
}
printf("vectorul d este:");
for(i=1;i<=n;i++)
printf("%d ",d[i]);
getch();
}
6.2. Algoritmul lui Prim

# include <stdio.h>
# include <conio.h>
# include <values.h>
typedef struct min
{
int lin,col;
}min;
int n,a[50][50],m[50][50];
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%6d",a[i][j]);
printf("\n");
}
}
int in(int v[50],int n,int x)
{
for(int i=1;i<=n;i++)
if (v[i]==x) return 1;
return 0;
}
min minim(int v[50],int k)
{
min min0;int i,j;
int min1=MAXINT;
for(i=1;i<=k;i++)
for(j=1;j<=n;j++)
if (m[v[i]][j]<min1 && m[v[i]][j]!=0 && in(v,k,j)==0)
{min0.lin=v[i];min0.col=j;min1=m[v[i]][j];}
return min0;
}
void main(void)
{
int t[50][50],vec[50],i,j,v,s,k,x,y;
FILE *f;
clrscr();
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
printf("\n graful are %d noduri,matricea sa de adiacenta
fiind:\n",n);
afisare(a,n);
for(i=1;i<=n;i++) m[i][i]=0;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if (a[i][j]==1)
{
printf("\n cat este costul muchiei (%d %d)?",i,j);
scanf("%d",&m[i][j]);
m[j][i]=m[i][j];
}
else
{
m[i][j]=1000;
m[j][i]=1000;
}
printf("\n matricea costurilor este :\n");
afisare(m,n);
printf("introduceti un nod v (1<=v<=%d)",n);
scanf("%d",&v);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) t[i][j]=0;
k=1;vec[k]=v;
for(i=1;i<n;i++)
{
x=minim(vec,k).lin;
y=minim(vec,k).col;
t[x][y]=m[x][y];
t[y][x]=t[x][y];
m[x][y]=1000;
m[y][x]=1000;
k++;
vec[k]=y;
}
printf("arborele de cost minim rezultat este:\n");
afisare(t,n);
getch();
}

6.3. Algoritmul lui Kruskal

# include <stdio.h>
# include <conio.h>
# include <values.h>
typedef struct min
{
int lin,col;
}min;
int n,a[50][50],m[50][50],t[50][50],k,ok,v[50],st[50];
void initializare(void)
{
for(int i=1;i<50;i++) st[i]=0;
}
int valid(int k)
{
if (k>1 && t[st[k]][st[k-1]]==0) return 0;
for(int i=2;i<k;i++)
if (st[i]==st[k]) return 0;
return 1;
}
void bktr(int k,int n)
{
for(int i=1;i<=n;i++)
{
st[k]=i;
if(valid(k)==1) if(st[1]==st[k] && k>3) ok=1;
else bktr(k+1,n);
}
}
void afisare(int a[50][50],int n)
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%6d",a[i][j]);
printf("\n");
}
}
min minim(int a[50][50],int n)
{
min min0;int i,j;
int min1=MAXINT;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j]<min1 && m[i][j]!=0 )
{
min0.lin=i;
min0.col=j;
min1=a[i][j];
}
return min0;
}
void main(void)
{
int i,j,k,x,y;
FILE *f;
clrscr();
f=fopen("graf.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
printf("\n graful are %d noduri,matricea sa de adiacenta
fiind:\n",n);
afisare(a,n);
for(i=1;i<=n;i++) m[i][i]=0;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if (a[i][j]==1)
{
printf("\n cat este costul muchiei (%d %d)?",i,j);
scanf("%d",&m[i][j]);
m[j][i]=m[i][j];
}
else
{
m[i][j]=1000;
m[j][i]=1000;
}
printf("\n matricea costurilor este :\n");
afisare(m,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
t[i][j]=0;
k=0;
for(i=1;i<n+k;i++)
{
x=minim(m,n).lin;
y=minim(m,n).col;
t[x][y]=m[x][y];
t[y][x]=t[x][y];
if(i>2)
{
ok=0;
bktr(1,y);
if (ok==1)
{
t[x][y]=0;
t[y][x]=0;
k++;
}
}
m[x][y]=1000;
m[y][x]=1000;
}
printf("arborele de cost minim este:\n");
afisare(t,n);
getch();
}

6.4. Algoritm pentru determinarea componentelor conexe

# include <stdio.h>
# include <conio.h>
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) printf("%3d ",a[i][j]);
printf("\n");
}
}
int in(int v[50],int l,int x)
{
for(int i=1;i<=l;i++)
if (v[i]==x) return 1;
return 0;
}
void main(void)
{
int a[50][50],s[50],p[50],comp[50],v[50],n,i,j,x;
int k,k1,t,ok,l;
FILE *f;
clrscr();
f=fopen("comp.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("\n graful are %d noduri,matricea sa de adiacenta
fiind:\n",n);
afisare(a,n);
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j]==0 && i!=k && j!=k)
a[i][j]=a[i][k]*a[k][j];
printf("\n matricea drumurilor este:\n");
afisare(a,n);
printf("\n componentele conexe sunt:\n");
l=0;
// in vectorul v se introduc elementele care sunt incluse
deja intr-o componenta conexa
for(x=1;x<=n;x++)
if (in(v,l,x)==0)
{
k=k1=0;
for(i=1;i<=n;i++)
{
if (a[x][i]==1) s[++k]=i;
if (a[i][x]==1) p[++k1]=i;
}
t=0;
for(i=1;i<=k;i++)
{
ok=0;
for(j=1;(j<=k1 && ok==0);j++)
if (p[j]==s[i]) ok=1;
if (ok==1)
{
comp[++t]=s[i];
v[++l]=s[i];
}
}
printf("\n{ ");
//printf("\n o componenta conexa este:");
if(t!=0)for(i=1;i<=t;i++) printf("%d ",comp[i]);
else printf("%d ",x);
printf("}\n");
}
getch();
}
7. Laborator 7

7.1. Colorararea secventiala a unui graf

# include <stdio.h>
# include <conio.h>
# include <alloc.h>
void stergere(int v[50],int n,int x)
{
int i,j;
for(i=1;i<=n;i++)
if (v[i]==x)
{
for(j=i;j<n;j++)
v[j]=v[j+1];
n=n-1;
}
}
void afisare(int a[50][50],int n)
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
}
int minim(int v[50],int n)
{
int min=v[1];
for(int i=2;i<=n;i++)
if (v[i]<min) min=v[i];
return min;
}
void main(void)
{
int n,i,j,x[50],f[50],k,k1,a[50][50],v[50],v1[50];
FILE *fis;
clrscr();
fis=fopen("graf.txt","r");
fscanf(fis,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(fis,"%d",&a[i][j]);
fclose(fis);
printf("\n graful citit are %d noduri,matricea sa
adiacenta fiind:",n);
afisare(a,n);
printf("introduceti ordinea de colorare a varfurilor:");
for(i=1;i<=n;i++)
{
printf("x%d=",i);
scanf("%d",&x[i]);
}
printf("introduceti nr de culori:");
scanf("%d",&k);
for(i=1;i<=k;i++) v[i]=i;
for(i=1;i<=n;i++) f[x[i]]=0;
f[x[1]]=1;
for(i=1;i<=n;i++)
if (f[x[i]]==0)
{
for(j=1;j<=k;j++) v1[j]=v[j];
k1=k;
for(j=1;j<i;j++)
if (a[x[j]][x[i]]==1)
{
stergere(v1,k1,f[x[j]]);
k1=k1-1;
}
f[x[i]]=minim(v1,k1);
}
printf("varfurile colorate arata astfel:");
for(i=1;i<=n;i++)
printf("\n varful %d a primit culoarea
%d",x[i],f[x[i]]);
getch();
}

7.2. Colorarea secventiala(algoritmul Larger First)

# include <stdio.h>
# include <conio.h>
# include <alloc.h>
typedef struct grad
{
int varf;
int grad;
}gr;
void stergere(int v[50],int n,int x)
{
int i,j;
for(i=1;i<=n;i++)
if (v[i]==x)
{
for(j=i;j<n;j++)
v[j]=v[j+1];
n=n-1;
}
}
void afisare(int a[50][50],int n)
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
}
int minim(int v[50],int n)
{
int min=v[1];
for(int i=2;i<=n;i++)
if (v[i]<min) min=v[i];
return min;
}
int grad(int a[50][50],int n,int x)
{
int i,j=0;
for(i=1;i<=n;i++)
if (a[i][x]==1 || a[x][i]==1) j++;
return j;
}
void main(void)
{
int n,i,j,x[50],f[50],k,k1,a[50][50],v[50],v1[50];
gr d[50],t;
FILE *fis;
clrscr();
fis=fopen("graf.txt","r");
fscanf(fis,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(fis,"%d",&a[i][j]);
fclose(fis);
printf("\n graful citit are %d noduri,matricea sa
adiacenta fiind:",n);
afisare(a,n);
for(i=1;i<=n;i++)
{
d[i].varf=i;
d[i].grad=grad(a,n,i);
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(d[j].grad>d[i].grad)
{
t=d[i];
d[i]=d[j];
d[j]=t;
}
for(i=1;i<=n;i++) x[i]=d[i].varf;
printf("introduceti nr de culori:");
scanf("%d",&k);
for(i=1;i<=k;i++) v[i]=i;
for(i=1;i<=n;i++) f[x[i]]=0;
f[x[1]]=1;
for(i=1;i<=n;i++)
if (f[x[i]]==0)
{
for(j=1;j<=k;j++) v1[j]=v[j];
k1=k;
for(j=1;j<i;j++)
if (a[x[j]][x[i]]==1)
{
stergere(v1,k1,f[x[j]]);
k1=k1-1;
}
f[x[i]]=minim(v1,k1);
}
printf("varfurile colorate arata astfel:");
for(i=1;i<=n;i++)
printf("\n varful %d a primit culoarea
%d",x[i],f[x[i]]);
getch();
}

7.3. Algoritmul DSATUR(Degree of saturation) al lui
Brelaz

# include <stdio.h>
# include <conio.h>
# include <alloc.h>
typedef struct grad
{
int varf;
int grad;
}gr;
int n,i,j,x[50],f[50],k,k1,a[50][50],v[50],v1[50];
gr d[50],aux;
void stergere(int v[50],int n,int x)
{
int i,j;
for(i=1;i<=n;i++)
if (v[i]==x)
{
for(j=i;j<n;j++)
v[j]=v[j+1];
n=n-1;
}
}
void afisare(int a[50][50],int n)
{
int i,j;
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
}
int minim(int v[50],int n)
{
int min=v[1];
for(int i=2;i<=n;i++)
if (v[i]<min) min=v[i];
return min;
}
int in(int v[50],int n,int x)
{
for(int i=1;i<=n;i++)
if (v[i]==x) return 1;
return 0;
}
int grad_saturatie(int x)
{
int gr_sat=0,i,j,s[50];
for(i=1;i<=n;i++)
if ((a[i][x]==1) && (in(s,gr_sat,f[i])==0))
{gr_sat++;s[gr_sat]=f[i];}
return gr_sat;
}
int prim_necol(int x[50])
{
for(int i=1;i<=n;i++)
if (f[x[i]]<0) return i;
return 0;
}
void colorare(void)
{
int i,j,k1,t;
for(i=1;i<=n;i++)
{
d[i].varf=i;
d[i].grad=grad_saturatie(i);
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(d[j].grad>=d[i].grad)
{
aux=d[i];
d[i]=d[j];
d[j]=aux;
}
for(i=1;i<=n;i++) x[i]=d[i].varf;
for(j=1;j<=k;j++) v1[j]=v[j];
k1=k;
t=prim_necol(x);
if(t!=0)
{
for(i=1;i<=n;i++)
if (a[x[i]][x[t]]==1)
{
stergere(v1,k1,f[x[i]]);
k1=k1-1;
}
f[x[t]]=minim(v1,k1);
colorare();
}
else return;
}
void main(void)
{

FILE *fis;
clrscr();
fis=fopen("dsatur.txt","r");
fscanf(fis,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(fis,"%d",&a[i][j]);
fclose(fis);
printf("\n graful citit are %d noduri,matricea sa
adiacenta fiind:",n);
afisare(a,n);
printf("introduceti nr de culori:");
scanf("%d",&k);
for(i=1;i<=k;i++) v[i]=i;
for(i=1;i<=n;i++) f[i]=-100+i;
colorare();
printf("varfurile colorate arata astfel:");
for(i=1;i<=n;i++)
printf("\n varful %d a primit culoarea %d",i,f[i]);
getch();
}
8. LABORATOR 8

8.1. Algoritmul Ford-Fulkerson
# include <stdio.h>
# include <conio.h>
# include <values.h>
struct retea
{
int arc,capacitate,flux;
}a[50][50];
int r[50][50],st[50],n,k,intrare,iesire,flux,t;
void initializare(void)
{
for(int i=1;i<50;i++) st[i]=0;
}
int valid(int p)
{
if (p>1 && r[st[p-1]][st[p]]==0) return 0;
for(int i=1;i<p;i++)
if (st[i]==st[p]) return 0;
return 1;
}
void tipar(int p)
{
int i,j,min=MAXINT,min1=MAXINT;
//metoda backtraking gaseste toate drumurile posibile,dar
se testeaza doar primul drum gasit,adica doar daca t=0;
if(t==0)
{
for(i=1;i<p;i++)
{
if (a[st[i]][st[i+1]].arc!=0 &&
a[st[i]][st[i+1]].capacitate-a[st[i]][st[i+1]].flux<min)
min=a[st[i]][st[i+1]].capacitate-
a[st[i]][st[i+1]].flux;
if (a[st[i+1]][st[i]].arc!=0 &&
a[st[i+1]][st[i]].flux<min1)
min1=a[st[i+1]][st[i]].flux;
}
if (min1<min) min=min1;
for(i=1;i<p;i++)
if (a[st[i]][st[i+1]].arc==1)
a[st[i]][st[i+1]].flux=a[st[i]][st[i+1]].flux+min;
else
a[st[i]][st[i+1]].flux=a[st[i]][st[i+1]].flux-min;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j].arc==1)
{
r[i][j]=a[i][j].capacitate-a[i][j].flux;
r[j][i]=a[i][j].flux;
}
t++;
}
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (p>=3 && st[p]==iesire) {k=1;tipar(p);}
else bktr(p+1);
}
}
void main(void)
{
int i,j,ok,ok1;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j].arc);
printf("reteaua citita are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) printf("%3d ",a[i][j].arc);
printf("\n");
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j].arc==1)
{
printf("capacitatea arcului (%d,%d)=",i,j);
scanf("%d",&a[i][j].capacitate);
a[i][j].flux=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) r[i][j]=0;
//am initializat matricea reziduala cu 0
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j].arc==1)
{
r[i][j]=a[i][j].capacitate-a[i][j].flux;
r[j][i]=a[i][j].flux;
}
for(i=1;i<=n;i++)
{
ok=ok1=0;
for(j=1;j<=n;j++)
{
if (a[j][i].arc!=0) ok=1;
if (a[i][j].arc!=0) ok1=1;
}
if (ok==0) intrare=i;
if (ok1==0) iesire=i;
}
do
{
k=0;t=0;
initializare();
st[1]=intrare;
bktr(2);
}
while(k==1);
flux=0;
for(i=1;i<=n;i++)
if (a[intrare][i].arc==1) flux=flux+a[intrare][i].flux;
printf("\n fluxul maxim este %d!",flux);
getch();
}

8.2. Algoritmul Ford-Fulkerson(metoda etichetarii)

# include <stdio.h>
# include <conio.h>
# include <values.h>
struct retea
{
int arc,capacitate,flux;
}a[50][50];
int etichetat[50],st[50],n,k,intrare,iesire,flux,t;
void initializare(void)
{
for(int i=1;i<50;i++) st[i]=0;
}
int valid(int p)
{
if (p>1 && a[st[p-1]][st[p]].arc==0 && a[st[p]][st[p-
1]].arc==0) return 0;
for(int i=1;i<p;i++)
if (st[i]==st[p]) return 0;
return 1;
}
int eticheta(int v[50],int n)
{
for(int i=1;i<=n;i++)
if (etichetat[v[i]]==0) return 0;
return 1;
}
void tipar(int p)
{
int i,j,ok,min=MAXINT,min1=MAXINT;
//metoda backtraking gaseste toate drumurile posibile,dar
se testeaza doar primul drum gasit,adica doar daca t=0;
if(t==0)
{
for(i=1;i<p;i++)
{
if ( etichetat[st[i]]!=0 && a[st[i]][st[i+1]].arc==1
&& a[st[i]][st[i+1]].capacitate>a[st[i]][st[i+1]].flux)
etichetat[st[i+1]]=st[i];
else if (etichetat[st[i]]!=0 &&
a[st[i+1]][st[i]].arc==1 && a[st[i+1]][st[i]].flux>0)
etichetat[st[i+1]]=-st[i];
if (a[st[i]][st[i+1]].arc!=0 &&
a[st[i]][st[i+1]].capacitate-a[st[i]][st[i+1]].flux<min)
min=a[st[i]][st[i+1]].capacitate-
a[st[i]][st[i+1]].flux;
if (a[st[i+1]][st[i]].arc!=0 &&
a[st[i+1]][st[i]].flux<min1)
min1=a[st[i+1]][st[i]].flux;
}
if (min1<min) min=min1;
if (eticheta(st,p)==1)
{
for(i=1;i<p;i++)
if (a[st[i]][st[i+1]].arc==1)
a[st[i]][st[i+1]].flux=a[st[i]][st[i+1]].flux+min;
else
a[st[i]][st[i+1]].flux=a[st[i]][st[i+1]].flux-min;
t++;
}
}
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (p>=3 && st[p]==iesire) {k=1;tipar(p);}
else bktr(p+1);
}
}
void main(void)
{
int i,j,ok,ok1;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j].arc);
printf("reteaua citita are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) printf("%3d ",a[i][j].arc);
printf("\n");
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a[i][j].arc==1)
{
printf("capacitatea arcului (%d,%d)=",i,j);
scanf("%d",&a[i][j].capacitate);
a[i][j].flux=0;
}
for(i=1;i<=n;i++)
{
ok=ok1=0;
for(j=1;j<=n;j++)
{
if (a[j][i].arc!=0) ok=1;
if (a[i][j].arc!=0) ok1=1;
}
if (ok==0) intrare=i;
if (ok1==0) iesire=i;
}
do
{
k=0;t=0;
for(i=1;i<=n;i++) etichetat[i]=0;
initializare();
st[1]=intrare;
etichetat[st[1]]=1;
bktr(2);
}
while(etichetat[iesire]!=0);
flux=0;
for(i=1;i<=n;i++)
if (a[intrare][i].arc==1) flux=flux+a[intrare][i].flux;
printf("\n fluxul maxim este %d!",flux);
getch();
}
9. LABORATOR 9

9.1. Algoritmul lui Fleury

# include <stdio.h>
# include <conio.h>
int a[50][50],a1[50][50],circuit[50],n,x,t;
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
int grad(int x)
{
int i,gr=0;
for(i=1;i<=n;i++)
if (a1[x][i]!=0) gr=gr+a1[x][i];
return gr;
}
int minim(int a,int b)
{
return a<b?a:b;
}
int conex(int a1[50][50],int x)
{
int k,i,j,a2[50][50];
//a2 este matricea drumurilor corespunzatoare grafului
redus
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a2[i][j]=a1[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a2[i][j]==0 && i!=k && j!=k)
a2[i][j]=minim(a2[i][k],a2[k][j]);
for(i=1;i<=n;i++)
if (i==x || grad(i)>1)
for(j=1;j<=n;j++)
if (grad(j)>1)
if (a2[i][j]==0) return 0;
return 1;
}
void main(void)
{
int i,j,ok,nr_muchii;
FILE *f;
clrscr();
f=fopen("euler.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
afisare(a,n);
nr_muchii=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if (a[i][j]!=0) nr_muchii=nr_muchii+a[i][j];
printf("\n varful de start=");
scanf("%d",&x);
//a1 este matricea de adiacenta corespunzatoare grafului
redus
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a1[i][j]=a[i][j];
t=1;circuit[t]=x;
do
{
ok=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (a1[i][j]!=0) ok=1;
for(i=1;i<=n;i++)
if (a1[x][i]!=0)
if (grad(x)==1)
{
a1[i][x]=a1[i][x]-1;
a1[x][i]=a1[i][x];
circuit[++t]=i;
break;
}
else
{
a1[x][i]=a1[x][i]-1;a1[i][x]=a1[i][x]-1;
if (conex(a1,i)==1)
{
circuit[++t]=i;
break;
}
else
{
a1[i][x]=a1[i][x]+1;
a1[x][i]=a1[x][i]+1;
}
}
x=i;
}
while(ok==1 && i<=n);
if (t==nr_muchii+1)
{
printf("\n graful este eulerian,iar un circuit Euler
este:\n");
for(i=1;i<=t;i++)
printf("%d ",circuit[i]);
}
else printf("graful nu este eulerian!");
getch();
}
9.2. Algoritmul lui Terry Loring

# include <stdio.h>
# include <conio.h>
int a[50][50],st[50],circuit[50],n,t,k,x,ok;
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void initializare(void)
{
for(int i=1;i<30;i++)
st[i]=0;
}
int valid(int p)
{
if(p>1 && a[st[p-1]][st[p]]==0) return 0;
for(int i=2;i<p;i++)
if (st[i]==st[p]) return 0;
return 1;
}
void inserare(int circuit[50],int t,int st[50],int p)
{
int i,j,ok,circuit1[50];
if (t==1)
for (i=1;i<=p;i++) circuit[t++]=st[i];
else
{
ok=0;
for(i=1;i<=t;i++) circuit1[i]=circuit[i];
for(i=1;(i<=t && ok==0);i++)
if (circuit[i]==st[1]) ok=1;
for(j=i;j<=t;j++)
circuit[j+p-1]=circuit1[j];
for(j=2;j<=p;j++)
circuit[i+j-2]=st[j];
}
}
void tipar(int p)
{
int i,j;
if(ok==0)
{
ok++;
inserare(circuit,t,st,p);
t=t+p-1;
for(i=1;i<p;i++)
{
a[st[i]][st[i+1]]=a[st[i]][st[i+1]]-1;
a[st[i+1]][st[i]]=a[st[i]][st[i+1]];
}
for(i=1;(i<=t && k==0);i++)
for(j=1;(j<=n && k==0);j++)
if (a[circuit[i]][j]!=0)
{
k=1;
x=circuit[i];
}
}
}
//functia final() este necesara pt grafurile in care pot
exista mai multe muchii care unesc doua noduri
int final(int p)
{
if (p==3 && a[st[p-1]][st[p]]<2) return 0;
return 1;
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (p>=3 && final(p) && st[p]==st[1])
tipar(p);
else bktr(p+1);
}
}
void main(void)
{
int i,j;
FILE *f;
clrscr();
f=fopen("euler.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
afisare(a,n);
printf("\n varful de start=");
scanf("%d",&x);
t=1;
do
{
initializare();
ok=0;k=0;
st[1]=x;
bktr(2);
}
while(k==1);
k=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]!=0) k=1;
if(k==0)
{
printf("\n un circuit Euler al grafului este:\n");
for(i=1;i<=t;i++)
printf("%d ",circuit[i]);
}
else printf("\n graful nu este eulerian!!!");
getch();
}

9.3. Algoritm backtracking pentru a determina daca un
graf este eulerian

# include <stdio.h>
# include <conio.h>
int a[50][50],st[50],n,t,ok,nr_muchii;
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void initializare(void)
{
for(int i=1;i<50;i++)
st[i]=0;
}
int aparitie(int st[50],int n,int x,int y)
{
int nr=0,i;
for(i=1;i<n;i++)
if ((st[i]==x && st[i+1]==y) || (st[i]==y &&
st[i+1]==x)) nr++;
return nr;
}
int valid(int p)
{
if(p>1 && a[st[p-1]][st[p]]==0) return 0;
if (aparitie(st,p,st[p-1],st[p])>a[st[p-1]][st[p]])
return 0;
return 1;
}
void tipar(int p)
{
int i;
printf("\n");
for(i=1;i<=p;i++) printf("%d ",st[i]);
ok++;
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (p==nr_muchii+1 && st[p]==st[1])
tipar(p);
else bktr(p+1);
}
}
void main(void)
{
int i,j;
FILE *f;
clrscr();
f=fopen("euler.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
afisare(a,n);
nr_muchii=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if (a[i][j]!=0)
nr_muchii=nr_muchii+a[i][j];
bktr(1);
if (ok==0) printf("\ngraful nu este eulerian!!");
else printf("\n Graful este eulerian,posibilitatile de
parcurgere fiind cele prezentate!!!");
getch();
}
9.4. Algoritm backrtacking pentru a determina daca un
graf este hamiltonian

# include <stdio.h>
# include <conio.h>
int a[50][50],st[50],n,t,ok;
void afisare(int a[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void initializare(void)
{
for(int i=1;i<50;i++)
st[i]=0;
}
int valid(int p)
{
if(p>1 && a[st[p-1]][st[p]]==0) return 0;
for(int i=2;i<p;i++)
if (st[i]==st[p]) return 0;
return 1;
}
void tipar(int p)
{
int i;
printf("\n");
for(i=1;i<=p;i++) printf("%d ",st[i]);
ok++;
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (p==n+1 && st[p]==st[1])
tipar(p);
else bktr(p+1);
}
}
void main(void)
{
int i,j;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
afisare(a,n);
bktr(1);
if (ok==0) printf("graful nu este hamiltonian!!!");
else printf("\n Graful este hamiltonian,posibilitatile
de parcurgere fiind cele prezentate!!!");
getch();
}
10. LABORATOR 10

10.1. Verificarea daca un graf neorientat este bipartit

# include <stdio.h>
# include <conio.h>
struct muchii
{
int c1,c2;
}muchii[50],c[50];
int n,t,st[50],a[50][50],m;
void initializare(void)
{
for(int i=1;i<50;i++) st[i]=0;
}
int in(int v[50],int n,int x)
{
for(int i=1;i<=n;i++)
if (v[i]==x) return 1;
return 0;
}
void tipar(int p)
{

int i;
printf("\n");
if (t==0) printf("graful este bipartit!\n");
printf("Prima multime:");
for(i=1;i<=p;i++) printf("%d ",st[i]);
printf(",iar a doaua multime:");
for(i=1;i<=n;i++)
if (in(st,p,i)==0)printf("%d ",i);
t++;
}
int valid(int p)
{
for(int i=1;i<p;i++)
if (st[i]>=st[p] || a[st[i]][st[p]]==1) return 0;
return 1;
}

int final(int p)
{
int i,j;
for(i=1;i<=n;i++)
if(in(st,p,i)==0)
for(j=1;j<=n;j++)
if (in(st,p,j)==0 && a[i][j]==1) return 0;
return 1;
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (final(p)==1)
tipar(p);
else bktr(p+1);
}
}
int adiacent(struct muchii c[50],int t,int x,int y)
{
int i;
for(i=1;i<=t;i++)
if (c[i].c1==x || c[i].c1==y || c[i].c2==x ||
c[i].c2==y) return 1;
return 0;
}
void main(void)
{
int i,j,ok;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
t=0;
bktr(1);
if (t==0) printf("\n graful nu este biparit!");
getch();
}

10.2. Algoritmul ungar pentru determinarea unui cuplaj
maxim de valoare minima intr-un graf bipartit

# include <stdio.h>
# include <conio.h>
typedef struct matrice
{
char marcat;
int inf;
}matrice;
int n,t,st[50];
matrice a[50][50],m[50][50];
void initializare(void)
{
for(int i=1;i<50;i++) st[i]=0;
}
int in(int v[50],int n,int x)
{
for(int i=1;i<=n;i++)
if (v[i]==x) return 1;
return 0;
}
int nemarcat(matrice m[50][50],int n)
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (m[i][j].inf==0 && m[i][j].marcat==' ') return 1;
return 0;
}
void tipar(int p)
{

int i,j,k,min,st1[50],nr,k1;
if (t==0 && p==n/2)
{
t++;
printf("\n se poate aplica algoritmul lui Ungar!!!\n");
k=0;
for(i=1;i<=n;i++)
if (in(st,p,i)==0) st1[++k]=i;
for(i=1;i<=p;i++)
for(j=1;j<=p;j++)
{
if (a[st[i]][st1[j]].inf==0) m[i][j].inf=1000;
else
{
printf("\n cat este valoarea arcului
(%d,%d)?",st[i],st1[j]);
scanf("%d",&m[i][j].inf);
}
m[i][j].marcat=' ';
}
printf("matricea m este:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=p;j++)
printf("%5d",m[i][j].inf);
printf("\n");
}
for(i=1;i<=p;i++)
{
min=m[i][1].inf;
for(j=1;j<=p;j++)
if (m[i][j].inf<min) min=m[i][j].inf;
for(j=1;j<=p;j++)
m[i][j].inf=m[i][j].inf-min;
}
for(j=1;j<=p;j++)
{
min=m[1][j].inf;
for(i=1;i<=p;i++)
if(m[i][j].inf<min) min=m[i][j].inf;
for(i=1;i<=p;i++)
m[i][j].inf=m[i][j].inf-min;
}
do
{
min=p;k=0;
for(i=1;i<=p;i++)
{
nr=0;
for(j=1;j<=p;j++)
if (m[i][j].inf==0 && m[i][j].marcat==' ') nr++;
if (nr!=0 && nr<min)
{
min=nr;
k=i;
}
}
min=p;
for(j=1;j<=p;j++)
if (m[k][j].inf==0 && m[k][j].marcat==' ')
{
nr=0;
for(i=1;i<=p;i++)
if (m[i][j].inf==0 && m[i][j].marcat==' ') nr++;
if(nr<min)
{
min=nr;
k1=j;
}
}
m[k][k1].marcat='*';
for(i=1;i<=p;i++)
{
if (m[k][i].inf==0 && i!=k1) m[k][i].marcat='$';
if (m[i][k1].inf==0 && i!=k) m[i][k1].marcat='$';
}
}
while(nemarcat(m,p)==1);
nr=0;
for(i=1;i<=p;i++)
for(j=1;j<=p;j++)
if (m[i][j].marcat=='*') nr++;
if (nr==p)
{
printf("\n cuplajul maxim de valoare minima este:");
for(i=1;i<=p;i++)
for(j=1;j<=p;j++)
if (m[i][j].marcat=='*') printf("(%d
%d)",st[i],st1[j]);
}
else printf("algoritmul nu poate determina cuplajul
maxim de valoare minima!");
printf("\n");
}
}
int valid(int p)
{
for(int i=1;i<p;i++)
if (st[i]>=st[p] || a[st[i]][st[p]].inf==1) return 0;
return 1;
}

int final(int p)
{
int i,j;
for(i=1;i<=n;i++)
if(in(st,p,i)==0)
for(j=1;j<=n;j++)
if (in(st,p,j)==0 && a[i][j].inf==1) return 0;
return 1;
}
void bktr(int p)
{
int i;
for(i=1;i<=n;i++)
{
st[p]=i;
if (valid(p)==1)
if (final(p)==1)
tipar(p);
else bktr(p+1);
}
}
void main(void)
{
int i,j,k;
FILE *f;
clrscr();
f=fopen("cuplaj.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j].inf);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j].inf);
printf("\n");
}
bktr(1);
if (t==0)
printf("\n nu se poate aplica algoritmul lui Ungar!");
getch();
}

10.3. Algoritm greedy pentru gasirea unui cuplaj maximal
intr-un graf
//programul presupune introducerea unui graf BIPARTIT in
fisier
# include <stdio.h>
# include <conio.h>
struct muchii
{
int c1,c2;
}muchii[50],c[50];
int n,t,a[50][50],m;
int adiacent(struct muchii c[50],int t,int x,int y)
{
int i;
for(i=1;i<=t;i++)
if (c[i].c1==x || c[i].c1==y || c[i].c2==x ||
c[i].c2==y) return 1;
return 0;
}
void main(void)
{
int i,j,ok;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
m=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if (a[i][j]==1)
{
m++;
muchii[m].c1=i;
muchii[m].c2=j;
}
t=0;
do
{
ok=0;
for(i=1;(i<=m && ok==0);i++)
if (adiacent(c,t,muchii[i].c1,muchii[i].c2)==0)
{
ok=1;
t++;
c[t].c1=muchii[i].c1;
c[t].c2=muchii[i].c2;
}
}
while(ok==1);
printf("\n un cuplaj maxim este :");
for(i=1;i<=t;i++)
printf ("(%d %d) ",c[i].c1,c[i].c2);
getch();
}
11. LABORATOR 11

11.1. Parcurgerea in adancime( DFS) a unui graf

# include <stdio.h>
# include <conio.h>
int n,x,a[50][50],viz[50],parcurgere[50],t;
void dfs(int i)
{
int j;
viz[i]=1;
parcurgere[++t]=i;
for(j=1;j<=n;j++)
if (a[i][j]==1)
if (viz[j]==0) dfs(j);
}
void main(void)
{
int i,j;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=1;i<=n;i++) viz[i]=0;
printf("dati varful de start:");
scanf("%d",&x);
t=0;
dfs(x);
printf("Nodurile parcurse sunt: ");
for(i=1;i<=t;i++) printf("%d ",parcurgere[i]);
if (t!=n) printf("\n Nu au fost parcurse toate nodurile
pt ca graful nu este conex!");
getch();
}

11.2. Parcurgerea in latime( BFS) a unui graf

# include <stdio.h>
# include <conio.h>
# include <alloc.h>
typedef struct coada
{
int inf;
struct coada *next;
}coada;
int n,a[50][50],viz[50],x,parcurgere[50],t;
coada *c=NULL;
coada *adaugare(coada *c,int inf)
{
coada *p,*aux;
aux=(coada *)malloc(sizeof(coada));
aux->inf=inf;
aux->next=NULL;
p=c;
if (p!=NULL)
{
while(p->next!=NULL) p=p->next;
p->next=aux;
}
else c=aux;
return c;
}
coada *stergere(coada *c)
{
coada *p=c;
c=c->next;
free(p);
return c;
}
void bfs(int i)
{
int j,v;
viz[i]=1;
parcurgere[++t]=i;
c=adaugare(c,i);
while(c!=NULL)
{
v=c->inf;
c=stergere(c);
for(j=1;j<=n;j++)
if (a[v][j]==1)
if (viz[j]==0)
{
viz[j]=1;
parcurgere[++t]=j;
c=adaugare(c,j);
}
}
}
void main(void)
{
int i,j;
FILE *f;
clrscr();
f=fopen("graf.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
printf("graful citit are %d noduri,matricea sa de
adiacenta fiind:\n",n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=1;i<=n;i++) viz[i]=0;
printf("dati varful de start:");
scanf("%d",&x);
bfs(x);
printf("\n nodurile parcurse sunt: ");
for(i=1;i<=t;i++) printf("%d ",parcurgere[i]);
if (t!=n) printf("\n nu au fost parcurse toate nodurile
pt ca graful nu este conex!");
getch();
}