I want to discover if a graph (connection matrix) is connected with only one component. The graph is connected when for all two vertices u and v contains a path from u to v. My problem 3 types connection (inhibition(-1), non-connection(0), activation(1)) I suppose if Aij != 0 has connection I use DFS to search how many components are in matrix but he works for some cases and not to others.
Ex my matrix (replacing -1 to 1):
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 1, 1, 1,
here have a representation of graph. When applies the same answer (DFS) of topic created by Wisdom's Wind results in 2 components to workaround this add lines 39-47 there is a way to do without lines 39-47?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define _MIN -1
#define _MAX 1
#define TAM 5
#define MAX TAM*TAM
#define QNT_MATRIX 1
#define SIZE_MATRIX MAX*QNT_MATRIX
void DFS(int *matrix, int *marks, int vertex, int componentes){
int i;
marks[vertex] = componentes;
for(i=0; i<TAM; i++){
if(matrix[vertex*TAM+i] != 0){
if(marks[i] == 0){
DFS(matrix, marks, i, componentes);
}
}
}
}
int testDFS(int *matrix){
int marks[TAM];
int i, k, componentes=0, componentes_total=1;
memset(marks, 0, TAM*sizeof(int));
for(i=0; i<TAM; i++){
if(marks[i] == 0){
++componentes;
DFS(matrix, marks, i, componentes);
}
}
for(i=0; i<TAM-1; i++){//line 39
for(k=i+1; k<TAM; k++){
if(marks[i] != marks[k]){
if(matrix[i*TAM+k] == 0 && matrix[k*TAM+i] == 0){
componentes_total++;//no have way connection
}
}
}
}//line47
printf("testDFS Componentes: %d\n", componentes);
printf("Componentes_total: %d\n", componentes_total);
}
int main(){
int matrix[SIZE_MATRIX];
int i;
srand(time(NULL));
for(i=0; i<SIZE_MATRIX; i++){
scanf("%d,", &matrix[i]);
}
//Print matrix
for(i=0; i<SIZE_MATRIX; i++){
printf("%d ", matrix[i]);
if((i+1)%TAM==0){
printf("\n");
}
if((i+1)%(MAX)==0){
printf("\n");
}
}
testDFS(matrix);
return 0;
}