//origGraph is array of IntLists to be transposed //n is the size of origGraph IntList* transposeGraph(IntList* origGraph, int n){ int i; int s = 0; IntList curr; //find length of graph2 by finding max element of origGraph for(i = 0; i < n; i++){ curr = origGraph[i]; while(curr != intNil){ if(intFirst(curr) > s) s = intFirst(curr); curr = intRest(curr); } } //sets new size equal to the max element of the old array if(s > size) size = s; //make and initialize graph2 IntList* graph2 = calloc(size , sizeof (IntList)); for(i = 0; i < n; i++){ graph2[i] = intNil; } //read origGraph and transpose to graph2 for(i = 0; i < n; i++){ curr = origGraph[i]; while(curr != intNil){ graph2[intFirst(curr)-1] = intCons(i + 1, graph2[intFirst(curr) - 1]); curr = intRest(curr); } }return graph2; }
int dfs(IntList* adjVertices, IntList* trees, int* color, int v, int t) { int w; IntList* remAdj; int ans = 0; color[v] = grey; time++; discoverTime[v] = time; remAdj = adjVertices[v]; int wAns = 0; while (remAdj != intNil) { w = intFirst(remAdj); wAns = 0; if(color[w] == white) { parent[w] = v; trees[t] = intCons(v , trees); wAns = dfs(adjVertices, trees, color, w, t); } else { if(parent[w] == -1) wAns = 1; break; } remAdj = intRest(remAdj); } time++; finishTime[v] = time; ans = wAns; color[v] = black; return ans; }
Cstring toCharR(Cstring prefix, IntList L) { Cstring s = calloc(1024, sizeof(char)); if(L == intNil) { s[0] = ']'; } else { strncpy(s, prefix, 1024); strncat(s, (Cstring)intFirst(L), 1024); strncat(s, toCharR(", ", intRest(L)), 1024); return s; } }
Cstring toCharR(Cstring prefix, IntList L) { Cstring s = calloc(1024, sizeof(char)); if(L == intNil) { s = "]"; } else { char intF[10]; sprintf(intF, "%d", intFirst(L)); strncpy(s, prefix, 1024); strncat(s, intF, 1024); strncat(s, toCharR(", ", intRest(L)), 1024); } return s; }
int main (int argc, char** argv) { if (argc == 1) { printf("Usage: graph01 input.data\n"); return 0; } /* CREATE ADJACENCY LISTS */ int n = 0, m = 0, result; IntList* adj_list; IntList* transpose_list; adj_list = loadGraph(&n, &m, argc, argv); transpose_list = fix_graph(transposeGraph(adj_list, n), n); int discoverTime1[n], finishTime1[n], finishStk[n], parent1[n], counter = 1, stackspot = n; memset(discoverTime1, 0, n * sizeof(int)); memset(finishTime1, 0, n * sizeof(int)); memset(finishStk, 0, n * sizeof(int)); memset(parent1, -1, n * sizeof(int)); int discoverTime2[n], finishTime2[n], dfstRoot[n], parent2[n]; memset(discoverTime2, 0, n * sizeof(int)); memset(finishTime2, 0, n * sizeof(int)); memset(parent2, -1, n * sizeof(int)); memset(dfstRoot, 0, n * sizeof(int)); for (int i = 1; i <= n; i++) result = dfsTrace1(adj_list, discoverTime1, finishTime1, finishStk, parent1, &counter, i, &stackspot); counter = 1; for (int i = 0; i < n; i++) result = dfsTrace2(transpose_list, discoverTime2, finishTime2, finishStk, parent2, &counter, finishStk[i], dfstRoot, finishStk[i]); result = result; /* ADJACENCY LIST */ printf("\n__Adjacency List__\n"); // print list print_list(adj_list, n, m); // display stack printf("stack: ["); for (int i = 0; i < n; i++) { if (i != 0) printf(", "); printf("%d", finishStk[i]); } printf("]\n\n"); // table printf("vertex |dtime |ftime |parent\n"); printf("-------|------|------|------\n"); for (int i = 0; i < n; i++) printf("%7d|%6d|%6d|%6d\n", i + 1, discoverTime1[i], finishTime1[i], parent1[i]); printf("\n\n"); /* TRANSPOSE LIST */ printf("__Transpose List__\n"); // print transpose list print_list(transpose_list, n, m); printf("\n"); // table printf("vertex |dtime |ftime |parent |root\n"); printf("-------|------|------|-------|-----\n"); for (int i = 0; i < n; i++) printf("%7d|%6d|%6d|%7d|%5d\n", i + 1, discoverTime2[i], finishTime2[i], parent2[i], dfstRoot[i]); printf("\n"); /* FREE MEMORY */ for (int i = 1; i <= n; i++) { for (IntList ptr = adj_list[i]; ptr != intNil; ptr = intRest(ptr)); for (IntList ptr = transpose_list[i]; ptr != intNil; ptr = intRest(ptr)); } free(adj_list); free(transpose_list); }
int main (int argc, char** argv) { if (argc == 1) { printf("Usage: graph01 input.data\n"); return 0; } // Create Array char buffer [100]; bool in = false; int n = 0, m = 0, to = 0, from = 0; IntList* adjVertices; FILE* the_file; if (argc == 2 && strcmp(argv[1], "-") == 0) { the_file = stdin; in = true; } else the_file = fopen(argv[1], "r"); fgets(buffer, sizeof (buffer), the_file); sscanf(buffer, "%d", &n); adjVertices = calloc(n + 1, sizeof (struct IntListNode*)); for (int i = 1; i <= n; i++) adjVertices[i] = intNil; while (fgets(buffer, sizeof (buffer), the_file)) { sscanf(buffer, "%d %d", &from, &to); if (from < n + 1 && from > 0) { m++; adjVertices[from] = intCons(to, adjVertices[from]); } } if (in == false) fclose(the_file); // Print Array printf("n = %d\nm = %d\n", n, m); for (int i = 1; i <= n; i++) { printf("%d\t", i); if (adjVertices[i] == intNil) { printf("null\n"); continue; } for (bool first = true; adjVertices[i] != intNil; adjVertices[i] = intRest(adjVertices[i])) { if (first == true) { first = false; printf("[%d", intFirst(adjVertices[i])); } else printf(", %d", intFirst(adjVertices[i])); } printf("]\n"); } free(adjVertices); }