Esempio n. 1
0
int main(int argc, char* argv[]){
   int i, n=8;
   List S = newList();
   Graph G = newGraph(n);
   Graph T=NULL, C=NULL;

   for(i=1; i<=n; i++) append(S, i);

   addArc(G, 1,2);
   addArc(G, 1,5);
   addArc(G, 2,5);
   addArc(G, 2,6);
   addArc(G, 3,2);
   addArc(G, 3,4);
   addArc(G, 3,6);
   addArc(G, 3,7);
   addArc(G, 3,8);
   addArc(G, 6,5);
   addArc(G, 6,7);
   addArc(G, 8,4);
   addArc(G, 8,7);
   printGraph(stdout, G);

   DFS(G, S);
   fprintf(stdout, "\n");
   fprintf(stdout, "x:  d  f  p\n");
   for(i=1; i<=n; i++){
      fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i));
   }
   fprintf(stdout, "\n");
   printList(stdout, S);
   fprintf(stdout, "\n");

   T = transpose(G);
   C = copyGraph(G);
   fprintf(stdout, "\n");
   printGraph(stdout, C);
   fprintf(stdout, "\n");
   printGraph(stdout, T);
   fprintf(stdout, "\n");

   DFS(T, S);
   fprintf(stdout, "\n");
   fprintf(stdout, "x:  d  f  p\n");
   for(i=1; i<=n; i++){
      fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i));
   }
   fprintf(stdout, "\n");
   printList(stdout, S);
   fprintf(stdout, "\n");

   freeList(&S);
   freeGraph(&G);
   freeGraph(&T);
   freeGraph(&C);
   return(0);
}
/*main------------------------------------------------------------------------*/
int main(int argc, char* argv[]) {

    //input validation
    if (argc != 2) {
        printf("Usage: %s output\n", argv[0]);
        exit(1);
    }

    //open file
    FILE *out;
    out = fopen(argv[1], "w");

    //new graph
    Graph G = newGraph(10);
    Graph T;
    List S = newList();
    int i;
    for (i = 1; i <= 9; i++) {
        addArc(G, i, i + 1);
    }
    addArc(G, 1, 2);

    //add graph and print out the adjacency fields
    for (i = 1; i <= getOrder(G); i++) {
        append(S, i);
    }
    fprintf(out, "\nThe adjacency list of G is:\n");
    printGraph(out, G);

    //Run DFS and get the transpose
    DFS(G, S);
    T = transpose(G);
    fprintf(out, "\nTranspose of G is:\n");
    printGraph(out, T);

    //print out fields
    fprintf(out, "Order of G is %d\n", getOrder(G));
    fprintf(out, "Size of G is %d.\n", getSize(G));
    fprintf(out, "Parent of 10 is %d.\n", getParent(G, 10));
    fprintf(out, "Parent of 3 is %d.\n", getParent(G, 3));
    fprintf(out, "Discover time of 1 is %d.\n", getDiscover(G, 1));
    fprintf(out, "Finish time of 1 is %d.\n", getFinish(G, 1));
    fprintf(out, "Discover time of 9 is %d.\n", getDiscover(G, 9));
    fprintf(out, "Finish time of 9 is %d.\n", getFinish(G, 9));

    //close file
    fclose(out);

    //exit
    return (0);
}
Esempio n. 3
0
int main(int argc, char* argv[]){
   int order = 8;	//the number of nodes in our Graph G
   Graph G = newGraph(order);
   List S = newList();

   //insert numbers into List S
   for(int i=1; i<=8; i++){
      append(S, i);
   }
   
   printf("Here is our Graph G when first initialized\n");
   printGraph(stdout, G);
   printf("Here is our List S\n");
   printList(stdout, S);
   printf("\n\n");


   //calling addArc() to assign edges to our Graph G
   addArc(G, 1, 2);
   addArc(G, 2, 3);
   addArc(G, 3, 4);
   addArc(G, 4, 3);
   addArc(G, 4, 8);
   addArc(G, 8, 8);
   addArc(G, 7, 8);
   addArc(G, 3, 7);
   addArc(G, 7, 6);
   addArc(G, 6, 7);
   addArc(G, 2, 6);
   addArc(G, 5, 6);
   addArc(G, 5, 1);
   addArc(G, 2, 5); 

   printf("Here is the adjacency list representation of G after adding our directed edges\n");
   printGraph(stdout, G);
   printf("\n");
   
   //Test that transpose() works properly
   Graph T = transpose(G);
   printf("Here is the transpose of G:\n");
   printGraph(stdout, T);

   printf("The order of G is: %d\n", getOrder(G));
   printf("The size of G is: %d\n", getSize(G));
   printf("The parent of node 2 is: %d\n", getParent(G, 2));
   printf("The discover time of node 2 is: %d\n", getDiscover(G, 2));
   printf("The finish tim of node 2 is: %d\n\n\n", getFinish(G, 2));

   DFS(G,S);

   printf("Here is our List S after running DFS():\n");
   printList(stdout, S);
   printf("\n");   
   //Test that discover time, finish time, and parent fields correct after call to DFS()
   for(int i = 1; i<=order; i++){
      printf("Node %d: discovery time = %d ; finish time = %d ; parent = %d\n", 
               i, getDiscover(G, i), getFinish(G, i), getParent(G, i));
   }

   //Test that copyGraph() works properly
   Graph C = copyGraph(G);
   printf("Here is the copy of G:\n");
   printGraph(stdout, C);
   for(int i = 1; i<=order; i++){
      printf("Node %d: discovery time = %d ; finish time = %d ; parent = %d\n", 
               i, getDiscover(C, i), getFinish(C, i), getParent(C, i));
   }

   freeList(&S);
   freeGraph(&G);
   freeGraph(&T);
   freeGraph(&C);
   return(0);
}
Esempio n. 4
0
int main(int argc, char* argv[]){
   int i, n=8;
   List S = newList();
   Graph G = newGraph(n);
   Graph T=NULL, C=NULL;

   Graph G_undir = newGraph(35);
   for(i=1; i<35; i++) {
      if( i%7!=0 ) addEdge(G_undir, i, i+1);
      if( i<=28 ) addEdge(G_undir, i, i+7);
   }
   printf("The Order of this undirected graph is: %d\n", getOrder(G_undir));
   printf("The Size of this undirected graph is: %d\n", getSize(G_undir));
   printf("Adjacency list representation of G_undir: \n");
   printGraph(stdout, G_undir);
   printf("\n");

   for(i=1; i<=n; i++) append(S, i);

   addArc(G, 1,2);
   addArc(G, 1,5);
   addArc(G, 2,5);
   addArc(G, 2,6);
   addArc(G, 3,2);
   addArc(G, 3,4);
   addArc(G, 3,6);
   addArc(G, 3,7);
   addArc(G, 3,8);
   addArc(G, 6,5);
   addArc(G, 6,7);
   addArc(G, 8,4);
   addArc(G, 8,7);
   printf("The Order of this graph is: %d\n", getOrder(G));
   printf("The Size of this graph is: %d\n", getSize(G));
   printf("Adjacency list representation of G: \n");
   printGraph(stdout, G);

   DFS(G, S);
   fprintf(stdout, "\n");
   fprintf(stdout, "x:  d  f  p\n");
   for(i=1; i<=n; i++){
      fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i));
   }
   fprintf(stdout, "\n");
   printList(stdout, S);
   fprintf(stdout, "\n");

   T = transpose(G);
   C = copyGraph(G);
   fprintf(stdout, "\n");
   printGraph(stdout, C);
   fprintf(stdout, "\n");
   printGraph(stdout, T);
   fprintf(stdout, "\n");

   DFS(T, S);
   fprintf(stdout, "\n");
   fprintf(stdout, "x:  d  f  p\n");
   for(i=1; i<=n; i++){
      fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i));
   }
   fprintf(stdout, "\n");
   printList(stdout, S);
   fprintf(stdout, "\n");

   freeList(&S);
   freeGraph(&G);
   freeGraph(&T);
   freeGraph(&C);
   return(0);
}