Acasa Tehnologie Contractie graf

Contractie graf

by Dragos Schiopu

>

//contractia grafului
#include <stdio.h>
#include <conio.h>

void main()
{
int n,m1,m2,i,j, a[29][29];
//citire n si matrice din fisier
FILE *f=fopen("c:date.txt","r");
fscanf(f,"%dn",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);

//citire noduri care trebuiesc lipite
printf("Dati nodul 1:");scanf("%d",&m1);
printf("Dati nodul 2:");scanf("%d",&m2);
//contractie
a[m1][m1]=0; a[m1][m2]=0; a[m2][m1]=0; a[m2][m2]=0;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
if((a[m1][j]==1)&&(j!=m2)) a[m2][j]=1;
if((a[m2][j]==1)&&(j!=m1)) a[m1][j]=1;
}

//copiere linie in coloana
for(i=1;i<=n;i++)
{
a[i][m1]=a[m1][i];
a[i][m2]=a[m2][i];
}

//eliminare linie si coloana m2
for(i=1;i<=n;i++)
for(j=m2;j<=n-1;j++)
a[i][j]=a[i][j+1];


for(j=m2;j<=n-1;j++)
for(i=1;i<=n-1;i++)
a[j][i]=a[j+1][i];
n--;

for (i=1;i<=n;i++) {
for (j=1;j<=n;j++)
printf(" %d ",a[i][j]);
printf("n");
}
getch();
}

date.txt
8
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
1 1 0 1 0 0 0 0
0 0 1 0 1 1 0 0
0 0 0 1 0 0 0 0
0 0 0 1 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0

s-ar putea sa-ti placa