TEST(DFS, general){ TestingVisitor visitor; /* on small graphs */ for (int i = 1; i <= 5; ++i){ visitor.setGraphSize(i); for (auto &graph : getAllGraphs(i)){ runDFS(graph.get(), &visitor); EXPECT_TRUE(visitor.isGood()); visitor.reset(); } } /* on big graphs */ int n = 100; visitor.setGraphSize(n); for (int i = 0; i <= 100; ++i){ runDFS(getRandomGraph(n, 0.01*i).get(), &visitor); EXPECT_TRUE(visitor.isGood()); visitor.reset(); } }
int main(int argc, char **argv) { int i, j, k; FILE* fptr; fptr = fopen(argv[1], "r"); fscanf(fptr,"%d",&n); Node = calloc(n, sizeof(node)); //declare node array decolour(); //set all colours to 2 (no colour) matrix = createMatrix(n); //scan file for(i = 0; i < n * n; i++) { fscanf(fptr, "%d", &matrix[i]); } fclose(fptr); makeSymmetric(matrix, n); //assign neighbours for(j = 0; j < n; j++) { Node[j].nbr = calloc(n, sizeof(int)); for(k = 0; k < n; k++) { Node[j].nbr[k] = matrix[k + n*j]; } } printMatrix(matrix, n); //running DFS now beginning from every vertex for(i=0; i<n; i++){ counter = 1; //set visited counter to 1 decolour(); //decolour all Node[i].colour = 0; runDFS(i, -1); } if(cyclic == 1) printf("Cyclic\n"); else printf("Not Cyclic\n"); if(counter != n){ printf("Not Connected\n"); printf("Not Tree\n"); } else{ printf("Connected\n"); if(cyclic == 0) printf("Tree\n"); else printf("Not Tree\n"); } if(bipartite == 1) printf("Bipartite\n"); else printf("Not Bipartite\n"); free(matrix); free(Node); return 0; }
void runDFS(int i, int c){ //i = current node to check, c = from where the pointer has come int k; for(k = 0; k < n; k++){ //run the loop for every neighbour if(k != c){ // skip itself if(Node[i].nbr[k] == 1){ //if there is an edge then go ahead if(Node[k].colour == 2){ //if uncoloured, colour it with the opposite parity if(Node[i].colour == 0) Node[k].colour = 1; else Node[k].colour = 0; runDFS(k, i); //since it was uncoloured, we now shift our pointer to the next one counter++; //since we have discovered a new (amd unconnected vertex, counter increases } else{ cyclic = 1; //if the next vertex is already coloured, it is also visited, hence cyclic graph if(Node[k].colour == Node[i].colour){ bipartite = 0; //if the neighbour is of the same colour, two colouring is not possible and so it is not bipartite } } } } } }