Esempio n. 1
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;
}
Esempio n. 2
0
//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;
}
Esempio n. 3
0
int loadEdges(FILE* fp, char* inbuf, IntList* adjVertices, int n) {
  int num;
  Cstring line;

  num=0;
  line = fgets(inbuf, 1024, fp);
  while (line != NULL) {
    Edge e = parseEdge(line, n);
    adjVertices[e.from] = intCons(e.to, adjVertices[e.from]);
    num++;
    line = fgets(inbuf, 1024, fp);
  }
  return num;
}
Esempio n. 4
0
int main(int argc, char* argv[]){
   FILE *fp;

   fp = fopen(argv[1], "r");           //reads file
   if(!fp){                //if no file
      perror("No file specified");        //print error
      return -1;
   }
   
   int size = atoi(fgets(line, 1000, fp));         //get the size of the array
   printf("Array Size = %d \n", size);       //print out first line aka size of array

   //struct IntList * list = NULL;
   IntList* array = calloc(size, sizeof(IntList));       //allocate space for array
   
   char * stuff;
   int m;
   int n;
   int ls;
   int j;
   int num = 0;
   //array declaration start
      int dTime1[size];
      int fTime1[size];
      int parent1[size];
      int finishStk1[size];
      //array declaration end
   
   //initialize to null
   for(j = 0; j <= size; j++){
      array[j] = intNil;
   }

   while(fgets(line, 1000, fp) != NULL){        //while there is a line to read 
      stuff = strtok(line, " \t ");       //stuff
      ls = 0;                 //line stuff
      while(stuff != NULL){            //keep goings
         ls++;             //next part on line
         if(ls == 1){            //1st number
            m = atoi(stuff);
         } 
         if(ls == 2){            //2nd number
            n = atoi(stuff);
            if(m >= 0 && m <= size){
                     array[m-1] = intCons(n, array[m-1]);
                     num++;
                  }else printf("No edge for %d, %d\n", m, n);
         }
         stuff = strtok(NULL, " ,.-");    //stops the loop if nothing else is there
      }  
   }
   
int i;   
dfsSweep(array, size, dTime1, fTime1, parent1, finishStk1);
   printf("Original Graph\n");
   //print times and parent for all edges
   for(i = 0; i < size; i++){
      printf("Discover Time [%d]: %d\t", i+1, dTime1[i]);
      printf("Finish Time [%d]: %d\t", i+1, fTime1[i]);
      printf("Parent [%d]: %d \n", i+1, parent1[i]);
   }

   //print the order edges were visited
   printf("Edges:  ");
   for(i = 0; i < size; i++){
      printf("%d  ", finishStk1[i]);
   }
   printf("\n");

   //transpose the graph
   IntList* transGraph = transposeGraph(array, size);

   //print transposed graph
   printf("\nTransposed Graph:\n");

   //array declaration
   int dTime2[size];
   int fTime2[size];
   int parent2[size];
   int dfstRoot2[size];

   dfsSweepT(transGraph, size, dTime2, fTime2, parent2, finishStk1, dfstRoot2);

   //print times and parent for all edges
   for(i = 0; i < size; i++){
      printf("Discover Time [%d]: %d\t", i+1, dTime2[i]);
      printf("Finish Time [%d]: %d\t", i+1, fTime2[i]);
      printf("Parent [%d]: %d \n", i+1, parent2[i]);
   }

   //print the order edges were visited
   printf("Edges:  ");
   for(i = 0; i < size; i++){
      printf("%d  ", dfstRoot2[i]);
   }
   printf("\n");

   //close file and exit  
   fclose(fp);
   return 0;
}
Esempio n. 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);
}