Advertisement
Guest User

DFS

a guest
Nov 25th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. // 9181320149_SimeonVarbanov_A
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct Vertex {
  7.     unsigned int vertex_no;
  8.     unsigned int color; // 0: unvisited, 1: visited, 2: finished
  9.     unsigned int time_stamp_visit;
  10.     unsigned int time_stamp_finish;
  11.     struct Vertex * adj_vertex;
  12. } Vertex;
  13.  
  14. void DFS(Vertex *);
  15. Vertex * getLastVertex(Vertex *);
  16. Vertex * graph;
  17. void printResult();
  18. unsigned int edge[1000][1000];
  19.  
  20. unsigned int time_stamp = 0;
  21. unsigned int num_of_vertices;
  22.  
  23. int main(void) {
  24.    
  25.     Vertex * last_vertex;
  26.     Vertex * new_vertex;
  27.     int i, x, y, tmp1;
  28.  
  29.     scanf("%d", &num_of_vertices);
  30.  
  31.     graph = (Vertex *) malloc((num_of_vertices+1) * sizeof(Vertex));
  32.     for (i = 1; i <= num_of_vertices; i++) {
  33.         (graph+i)->vertex_no = i;
  34.         (graph+i)->adj_vertex = NULL;
  35.         (graph+i)->color = 0;
  36.         (graph+i)->time_stamp_visit = 0;
  37.         (graph+i)->time_stamp_finish = 0;
  38.     }
  39.  
  40.     while (1) {
  41.         tmp1 = scanf("%d", &x);
  42.         if (tmp1 == EOF) // fix here
  43.             break;
  44.         scanf("%d", &y);
  45.  
  46.         new_vertex = (Vertex *) malloc(sizeof(Vertex));
  47.         new_vertex->adj_vertex = NULL;
  48.         new_vertex->vertex_no = y;
  49.         new_vertex->time_stamp_visit = 0;
  50.         new_vertex->time_stamp_finish = 0;
  51.  
  52.         last_vertex = getLastVertex((graph+x));
  53.         last_vertex->adj_vertex = new_vertex;
  54.     }
  55.  
  56.     for (i = 1; i <= num_of_vertices; i++) {
  57.         if ((graph+i)->color != 2)
  58.             DFS((graph+i));
  59.     }
  60.    
  61.     printResult();
  62.     return 0;
  63. }
  64.  
  65. void DFS(Vertex * vertex) {
  66.     Vertex * tmp_vertex = (graph+vertex->vertex_no);
  67.  
  68.     // can only visit if it has not been visited yet
  69.     (graph+vertex->vertex_no)->time_stamp_visit = ++time_stamp;
  70.     (graph+vertex->vertex_no)->color = 1; // be gray, mark visited
  71.  
  72.     while (tmp_vertex->adj_vertex != NULL) {
  73.         tmp_vertex = tmp_vertex->adj_vertex;
  74.         if ((graph+tmp_vertex->vertex_no)->color == 0) { // if vertex is White (not visited)
  75.  
  76.             edge[vertex->vertex_no][tmp_vertex->vertex_no] = 1; // tree edge
  77.             DFS(tmp_vertex); // visit and DFS from this vertex
  78.  
  79.         } else if ((graph+tmp_vertex->vertex_no)->color == 1) { // if vertex is Gray (visited)
  80.  
  81.             edge[vertex->vertex_no][tmp_vertex->vertex_no] = 2; // back edge
  82.  
  83.         } else if ((graph+tmp_vertex->vertex_no)->color == 2) {  // if vertex is Black (finished)
  84.    
  85.             edge[vertex->vertex_no][tmp_vertex->vertex_no] = 3; // check later
  86.         }
  87.     }
  88.  
  89.     (graph+vertex->vertex_no)->color = 2; //finished
  90.     (graph+vertex->vertex_no)->time_stamp_finish = ++time_stamp;
  91. }
  92.  
  93. Vertex * getLastVertex(Vertex * vertex) {
  94.     Vertex *tmp = vertex;
  95.  
  96.     while (tmp->adj_vertex != NULL)
  97.         tmp = tmp->adj_vertex;
  98.     return tmp;
  99. }
  100.  
  101. void printResult() {
  102.     int i;
  103.     Vertex *tmp;
  104.     for (i = 1; i <= num_of_vertices; i++) {
  105.         tmp = (graph+i);
  106.         while (tmp->adj_vertex != NULL) {
  107.             tmp = tmp->adj_vertex;
  108.             if (edge[i][tmp->vertex_no] != 3)
  109.                 printf("%d %d %d\n", i, tmp->vertex_no, edge[i][tmp->vertex_no]);
  110.             else {
  111.                 if (((graph+tmp->vertex_no)->time_stamp_visit > (graph+i)->time_stamp_visit)            // check if it is forward or cross edge by time interval
  112.                     && ((graph+tmp->vertex_no)->time_stamp_finish < (graph+i)->time_stamp_finish))
  113.                     printf("%d %d %d\n", i, tmp->vertex_no, 3);
  114.                 else
  115.                     printf("%d %d %d\n", i, tmp->vertex_no, 4);
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement