0

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;
}
Community
  • 1
  • 1
realbas
  • 3
  • 6

1 Answers1

0

Just a small tweak in your code and it will do the job

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);
        }
        else if(marks[i] != marks[vertex]){
            marks[vertex] = marks[i];
            componentes = marks[i];
        }
    }
}

}

actually your code was right.But consider the case , as in your above test case, node 2 can be visit by 3,4,5. But 2 cannot be visited from any where. So its connected property will be 2 by your logic. But, the check I have added ensured that , if 'a' node is visiting 'b' node and 'b' is already visited , so 'a' becomes connected with 'b' and has same connected value.

rajni kant
  • 106
  • 1
  • 6
  • thanks man. Can u explain why marks[vertex] receives marks[i] and componentes receives marks[i]? – realbas Jul 28 '16 at 06:00
  • actually your code was right.But consider the case , as in your above test case, node 2 can visit 3,4,5. But 2 cannot be visited from any where. So its connected property will be 2 by your logic. But, the check I have added ensured that , if 'a' node is visiting 'b' node and 'b' is already visited , so 'a' becomes connected with 'b' and has same connected value. – rajni kant Jul 28 '16 at 08:25
  • i'm testing with matrix3x3={1, 0, 0, 0, 1, 1, 1, 1, 1} and results in 2 components (wrong). When (testDFS i == 1) have more call DFS(i=2, C=2) and ends with C=2 (without call DFS for i=2 because marks[2] is 2) that is wrong. Can I help? – realbas Jul 29 '16 at 22:20
  • I change != by conditional changing marks[i], marks[vertex] to get minor value of component once 'a' visits 'b' and 'b' is already visited and 'a', 'b' must have lowest value of component – realbas Jul 31 '16 at 05:13