Sunteți pe pagina 1din 30

ALGORITMICA GRAFURILOR

Cuprins
1.LABORATOR 1....................................................................................................................................................................2
1.1.CREAREA UNUI ARBORE BINAR SI PARCURGEREA SA PRIN CELE 3 FORME: RSD, SRD,SDR .........................................2
2.LABORATOR 2....................................................................................................................................................................4
2.1.CITIREA UNUI GRAF..........................................................................................................................................................4
2.2.OBTINEREA DINTR-UN GRAF A UNUI ALT GRAF PRIN CONTRACTIE..................................................................................4
3.LABORATOR 3....................................................................................................................................................................6
3.1.AVAND DAT UN GRAF,DETERMINATI UN SUBGRAF AL SAU..............................................................................................6
3.2.AVAND DAT UN GRAF,DETERMINATI UN GRAF PARTIAL AL SAU.....................................................................................6
3.3.DETERMINATI VECINII UNUI VARF AL UNUI GRAF............................................................................................................8
3.4. DETERMINATI GRADELE VARFURILOR UNUI GRAF,GRADUL MINIM SI GRADUL MAXIM..................................................8
3.5. DETERMINATI W+(A)-MULTIMEA ARCELOR INCIDENTE CU A CATRE EXTERIOR, W- (A)-MULTIMEA ARCELOR
INCIDENTE CU A CATRE INTERIOR SI VECINII LUI A, UNDE A ESTE O SUBMULTIME DE VARFURI ALE GRAFULUI...............10
3.6. VERIFICATI DACA UN GRAF ESTE SIMETRIC/ANTISIMETRIC...........................................................................................11
4.LABORATOR 4..................................................................................................................................................................12
4.1. DETERMINATI DACA DOUA GRAFURI SUNT IZOMORFE..................................................................................................12
5. LABORATOR 5.................................................................................................................................................................14
5.1. ALGORITMUL ROY-WARSHALL...................................................................................................................................14
5.2.ALGORITMUL ROY-FLOYD.............................................................................................................................................14
5.3.ALGORITMUL LUI DIJKSTRA...........................................................................................................................................16
6.LABORATOR 6..................................................................................................................................................................18
6.1.ALGORITMUL BELLMAN-FORD......................................................................................................................................18
6.2.ALGORITMUL LUI PRIM..................................................................................................................................................19
6.3.ALGORITMUL LUI KRUSKAL...........................................................................................................................................20
7.LABORATOR 7..................................................................................................................................................................24
7.1.COLORARAREA SECVENTIALA A UNUI GRAF..................................................................................................................24
7.2.COLORAREA SECVENTIALA(ALGORITMUL LARGER FIRST)............................................................................................25
8.LABORATOR 8..................................................................................................................................................................28
8.1.ALGORITMUL FORD-FULKERSON...................................................................................................................................28

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);
2

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:");
4

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.

Avand dat un graf,determinati un subgraf al sau.

# 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.

Avand dat un graf,determinati un graf partial al sau.


6

# 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;
7

a[j][i]=0;
}
}*/
getch();
}

3.3.

Determinati vecinii 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.

Determinati gradele varfurilor unui graf,gradul minim si gradul


maxim.

# include <stdio.h>
# include <conio.h>
struct semigrad
{
int interior;
8

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
%d!,minim(grad,n),maxim(grad,n));

este
9

getch();
}

3.5.

Determinati w+(A)-multimea arcelor incidente cu A catre exterior,


w- (A)-multimea arcelor incidente cu A catre interior si vecinii 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)))
10

if (in(v,k,j)==0)
{
k++;
v[k]=j;
}
for(i=1;i<=k;i++) printf("%d ",v[i]);
getch();
}

3.6.

Verificati 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();
}

11

4. LABORATOR 4
4.1.

Determinati daca doua grafuri sunt izomorfe.

# 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++;
12

}
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();
}

13

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>
14

# 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();
}
15

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);
16

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();
}

17

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++)
18

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;
19

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>
20

# 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;
21

}
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++;
}
}
22

m[x][y]=1000;
m[y][x]=1000;
}
printf("arborele de cost minim este:\n");
afisare(t,n);
getch();
}

23

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);
24

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;
25

}
}
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)
{
26

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();
}

27

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,min,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;
//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==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++)
28

{
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");
}
min=MAXINT;
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);
if (a[i][j].capacitate<min) min=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;
29

}
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();
}

30