Advertisement
ruhul0

DFS

Aug 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define size 50
  4.  
  5. char visited_array[size];
  6. int visited_seq = -1, graph[50][50], n,top = -1;
  7. char q[50];
  8. char vertex[26],stack[100];
  9.  
  10. void visit(char ch)
  11. {
  12.     visited_seq++;
  13.     visited_array[visited_seq] = ch;
  14. }
  15.  
  16. bool is_visit(char ch)
  17. {
  18.     int i;
  19.     for(i = 0 ; i <= visited_seq ; i++)
  20.     {
  21.         if(ch == visited_array[i])
  22.             return true; //Already visited
  23.     }
  24.     return false; //Not visited before
  25. }
  26.  
  27. void push(char ch)
  28. {
  29.     stack[++top] = ch;
  30. }
  31.  
  32. char pop()
  33. {  
  34.     if (top == -1)
  35.     {
  36.         return '\0';
  37.     }  
  38.     else
  39.         return stack[top--];
  40. }
  41.  
  42. void find_adjecency(char ch)
  43. {
  44.     int i, j;
  45.     for(i = 0 ; i < n ; i++)
  46.     {
  47.         if(ch == vertex[i])
  48.         {
  49.             for(j = 0 ; j < n ; j++)
  50.             {
  51.                 if(graph[i][j] == 1)
  52.                 {
  53. //                  printf("%c ", vertex[j]);
  54.                     if(is_visit(vertex[j]) == false)
  55.                         push(vertex[j]);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61. }
  62.  
  63. void dfs()
  64. {
  65.     char ch;
  66.     int x;
  67.     printf("\nFrom which vertex you want to start?(1-50): ");
  68.     scanf("%d",&x);
  69.     push(vertex[x-1]);
  70.  
  71.     while(stack[top] != '\0')
  72.     {
  73.         ch = pop();      
  74.         if(is_visit(ch) == false)
  75.             {
  76.                 visit(ch);
  77.             }
  78.         find_adjecency(ch);
  79.     }
  80. }
  81.  
  82. void displayVisitedArray()
  83. {
  84.     int i;
  85.     printf("\nVisited Sequence is:  ");
  86.     for(i=0; i<= visited_seq ; i++)
  87.         printf("%c ", visited_array[i]);
  88. }
  89.  
  90. void createGraph()
  91. {
  92.     int i, j;
  93.     for(i = 0 ; i < 26 ; i++)
  94.         vertex[i] = i+65;
  95.  
  96.     printf("How many Vertex: ");
  97.     scanf("%d", &n);
  98.     for(i = 0 ; i < n ; i++)
  99.     {
  100.         for(j = 0 ; j < n ;j++)
  101.         {
  102.             graph[i][j] = 0;
  103.         }
  104.     }
  105.  
  106.     for(i = 0 ; i < n ; i++)
  107.     {
  108.         for(j = i+1 ; j < n ;j++)
  109.         {
  110.             printf("%c-%c exist: ", vertex[i], vertex[j]);
  111.             scanf("%d", &graph[i][j]);
  112.             if(graph[i][j] == 1)
  113.                 graph[j][i] = 1;
  114.         }
  115.     }
  116. }
  117. int main()
  118. {
  119.     createGraph();
  120.     dfs();
  121.     displayVisitedArray();
  122.     getch();
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement