iasatan

Untitled

Nov 20th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Edge {
  5.     int vertex;
  6.     struct Edge * next;
  7. };
  8.  
  9. // Inserts Node to the Linked List by Head Insertion - O(1)
  10. // Returns address of head which is the newly created node.
  11. struct Edge * AddEdge(struct Edge * currentHead, int newVertex)
  12. {
  13.     struct Edge * newHead
  14.                  = (struct Edge *) malloc(sizeof(struct Edge));
  15.  
  16.     newHead->vertex = newVertex;
  17.     newHead->next = currentHead;
  18.  
  19.     return newHead;
  20. }
  21.  
  22. void BreadthFirstSearch(
  23.                         struct Edge * adjacencyList[],
  24.                         int vertices,
  25.                         int parent[],
  26.                         int level[],
  27.                         int startVertex
  28.                        )
  29. {
  30.     struct Edge * traverse;
  31.     int i, par, lev, flag = 1;
  32.     // 'lev' represents the level to be assigned
  33.     // 'par' represents the parent to be assigned
  34.     // 'flag' used to indicate if graph is exhausted
  35.  
  36.     lev = 0;
  37.     level[startVertex] = lev;
  38.     // We start at startVertex
  39.  
  40.     while (flag) {
  41.         flag = 0;
  42.         for (i = 1; i <= vertices; ++i) {
  43.             if (level[i] == lev) {
  44.                 flag = 1;
  45.                 traverse = adjacencyList[i];
  46.                 par = i;
  47.  
  48.                 while (traverse != NULL) {
  49.                     if (level[traverse->vertex] != -1) {
  50.                         traverse = traverse->next;
  51.                         continue;
  52.                     }
  53.  
  54.                     level[traverse->vertex] = lev + 1;
  55.                     parent[traverse->vertex] = par;
  56.                     traverse = traverse->next;
  57.                 }
  58.             }
  59.         }
  60.  
  61.         ++lev;
  62.     }
  63. }
  64.  
  65. int main()
  66. {
  67.     int vertices, edges, i, v1, v2;
  68.  
  69.     printf("Enter the Number of Vertices -\n");
  70.     scanf("%d", &vertices);
  71.  
  72.     printf("\nEnter the Number of Edges -\n");
  73.     scanf("%d", &edges);
  74.  
  75.     struct Edge * adjacencyList[vertices + 1];
  76.     // Size is made (vertices + 1) to use the
  77.     // array as 1-indexed, for simplicity
  78.  
  79.     int parent[vertices + 1];
  80.     // Each element holds the Node value of its parent
  81.     int level[vertices + 1];
  82.     // Each element holds the Level value of that node
  83.  
  84.     // Must initialize your array
  85.     for (i = 0; i <= vertices; ++i) {
  86.         adjacencyList[i] = NULL;
  87.         parent[i] = 0;
  88.         level[i] = -1;
  89.     }
  90.  
  91.     for (i = 1; i <= edges; ++i) {
  92.         scanf("%d%d", &v1, &v2);
  93.  
  94.         // Adding edge v1 --> v2
  95.         adjacencyList[v1] = AddEdge(adjacencyList[v1], v2);
  96.  
  97.         // Adding edge v2 --> v1
  98.         // Remove this if you want a Directed Graph
  99.         adjacencyList[v2] = AddEdge(adjacencyList[v2], v1);
  100.     }
  101.  
  102.     // Printing Adjacency List
  103.     printf("\nAdjacency List -\n\n");
  104.     for (i = 1; i <= vertices; ++i) {
  105.         printf("adjacencyList[%d] -> ", i);
  106.  
  107.         struct Edge * traverse = adjacencyList[i];
  108.  
  109.         while (traverse != NULL) {
  110.             printf("%d -> ", traverse->vertex);
  111.             traverse = traverse->next;
  112.         }
  113.  
  114.         printf("NULL\n");
  115.     }
  116.  
  117.     printf("\nEnter a Start Vertex - ");
  118.     scanf("%d", &v1);
  119.  
  120.     BreadthFirstSearch(adjacencyList, vertices, parent, level, v1);
  121.  
  122.     // Printing Level and Parent Arrays
  123.     printf("\nLevel and Parent Arrays -\n");
  124.     for (i = 1; i <= vertices; ++i) {
  125.         printf("Level of Vertex %d is %d, Parent is %d\n",
  126.                                   i, level[i], parent[i]);
  127.     }
  128.  
  129.     return 0;
  130. }
Add Comment
Please, Sign In to add comment