示例#1
0
文件: scc03.c 项目: doitles/CS101
//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;
}
示例#2
0
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;
}
示例#3
0
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;
  }
}
示例#4
0
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;
}
示例#5
0
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);
}