Skip to main content

Adjaceny Matrix OR Indegree Outdegree & Adjacency List & BFS or DFS

Adjacency Matrix & Indegree Out degree & Adjacency List & BFS or DFS



CODE

👇


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 20
  4. typedef struct node
  5. {
  6. int vertex;
  7. struct node * next;
  8. }NODE;

  9. NODE *list[10];

  10.    typedef struct 
  11.     {
  12.     int front, rear;
  13.     int info[MAXSIZE];
  14.     }QUEUE;
  15.     
  16.     void addq(QUEUE *pq, int n)
  17.     {
  18.     pq->info[++pq->rear] = n;
  19.     }

  20.     void initq(QUEUE *pq)
  21.     {
  22.     pq->front = pq->rear = -1;
  23.     }


  24.     int removeq(QUEUE *pq)
  25.     {
  26.     return pq->info[++pq->front];
  27.     }

  28.     int isempty(QUEUE *pq)
  29.     {
  30.     return (pq->front == pq->rear);
  31.     }

  32. void createmat(int m[10] [10], int n)
  33. {
  34. int i, j;
  35. //char ans;
  36. for(i=0; i<n; i++)
  37. for(j=0;j<n; j++)
  38. {
  39. m[i] [j] = 0;
  40. if(i != j)
  41. {
  42. printf("Is there an edge between %d->%d (1/0) :",i+1, j+1);
  43. scanf("%d", &m[i][j]);
  44. }
  45. }
  46. }

  47. void dismat( int m[10][10], int n)
  48. {
  49. int i, j;
  50. printf("\n The Adjacency Matrix is:\n");
  51. for(i=0; i<n; i++)
  52. {
  53. for(j=0; j<n; j++)
  54. printf("\t%d", m[i] [j]);
  55. printf("\n");
  56. }
  57. }

  58. void createlist( int m [10] [10], int n)
  59. {
  60. int i, j;
  61. NODE *temp, *nn;
  62. for(i=0; i<n; i++)
  63. {
  64. list[i] = NULL;
  65. for(j=0; j<n; j++)
  66. {
  67. if(m[i][j] == 1)
  68. {
  69. nn=(NODE *)malloc(sizeof(NODE));
  70. nn->vertex=j+1;
  71. nn->next=NULL;
  72. if(list[i] == NULL)
  73. list[i] = temp = nn;
  74. else
  75. {
  76. temp->next=nn;
  77. temp=nn;
  78. }
  79. }
  80. }
  81. }
  82. }

  83. void dislist(int m[10] [10], int n)
  84. {
  85. NODE *temp;
  86. int i;
  87. printf("\n\nThe Adjacency list is:");
  88. for(i=0; i<n; i++)
  89. {
  90. printf("\nv%d->",i+1);
  91. temp=list[i];
  92. while(temp)
  93. {
  94. printf("v%d ->",temp->vertex);
  95. temp = temp->next;
  96. }
  97. printf("NULL");
  98. }
  99. }

  100. void inout(int m[10] [10], int n)
  101. {
  102. int sumin, sumout, v, i, j;
  103. printf("\n\nVertex  Indegree    Outdegree   Total Degree \n");
  104. for(v=0; v<n; v++)
  105. {
  106. sumin = sumout = 0;
  107. for(i=0; i<n; i++)
  108. {
  109. sumin = sumin + m[i][v]; //sum of column v
  110. sumout = sumout + m[v][i]; //sum of row v
  111. }
  112. printf("  %d\t   %d\t\t%d\t   %d\n", v+1, sumin, sumout, sumin+sumout);
  113. }
  114. }

  115. void recdfs(int m[10][10], int n, int v)
  116. {
  117. int w;
  118. static int visited[20]={0};
  119. visited[v]=1;
  120. printf("v%d ",v+1);
  121. for(w=0; w<n; w++)
  122. {
  123. if((m[v][w] == 1) && (visited[w] == 0))
  124. recdfs(m, n, w);
  125. }
  126. }

  127. void bfs(int m[10][10], int n)
  128. {
  129. int  v, w;
  130. int visited[20] = {0};
  131. QUEUE q;
  132. initq(&q);
  133. v=0; //starting vertex is 0
  134. visited[v]=1;
  135. addq(&q,v);
  136. while(!isempty(&q))
  137. {
  138. v=removeq(&q);
  139. printf("v%d ",v+1);
  140. for(w=0; w<n; w++)
  141. if((m[v][w] == 1) && (visited[w] == 0) )
  142. {
  143. addq(&q, w);
  144. visited[w] = 1;
  145. }
  146. }
  147. }

  148. void main()
  149. {
  150. int m[10][10], n,w;
  151. printf("\n Enter the number of vertices:");
  152. scanf("%d",&n);
  153. createmat(m,n);
  154. dismat(m,n);
  155. inout(m,n);
  156. createlist(m,n);
  157. dislist(m,n);
  158. printf("\n\nThe Depth First Search Traversal  is:\t");
  159. recdfs(m,n,w);
  160. printf("\n\nThe Breadth First Traversal is:\t");
  161. bfs(m,n);
  162. }


      //OUTPUT

          /*
            Enter the number of vertices:4
              Is there an edge between 1->2 (1/0) :1
                Is there an edge between 1->3 (1/0) :1
                  Is there an edge between 1->4 (1/0) :0
                    Is there an edge between 2->1 (1/0) :0
                      Is there an edge between 2->3 (1/0) :1
                        Is there an edge between 2->4 (1/0) :1
                          Is there an edge between 3->1 (1/0) :0
                            Is there an edge between 3->2 (1/0) :0
                              Is there an edge between 3->4 (1/0) :1
                                Is there an edge between 4->1 (1/0) :0
                                  Is there an edge between 4->2 (1/0) :0
                                    Is there an edge between 4->3 (1/0) :0

                                         The Adjacency Matrix is:
                                                  0       1       1       0
                                                    0       0       1       1
                                                      0       0       0       1
                                                        0       0       0       0


                                                      Vertex  Indegree    Outdegree   Total Degree
                                                          1               0                   2                   2
                                                            2               1                   2                   3
                                                              3               2                   1                   3
                                                                4              2                    0                   2


                                                                    The Adjacency list is:
                                                                      v1->v2 ->v3 ->NULL
                                                                        v2->v3 ->v4 ->NULL
                                                                          v3->v4 ->NULL
                                                                            v4->NULL

                                                                                The Depth First Search Traversal  is:   v1 v2 v3 v4

                                                                                    The Breadth First Traversal is:     v1 v2 v3 v4
                                                                                      */




                                                                                                                                           //ThE ProFessoR

                                                                                      Comments