|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | + |
| 4 | +#define MAX 100 |
| 5 | + |
| 6 | +void create_graph(); |
| 7 | +void add(int vertex); |
| 8 | +int del(); |
| 9 | +int isEmpty(); |
| 10 | +int find_indegree_of_vertex(int vertex); |
| 11 | + |
| 12 | +int total_vertices; |
| 13 | +int adjacent_matrix[MAX][MAX]; |
| 14 | +int queue[MAX]; |
| 15 | +int front = -1; |
| 16 | +int rear = -1; |
| 17 | + |
| 18 | +int main() |
| 19 | +{ |
| 20 | + int i, count, topological_sort[MAX], indegree[MAX]; |
| 21 | + create_graph(); |
| 22 | + //calculating the in-degree for all vertices. |
| 23 | + for(i = 0; i < total_vertices; i++) |
| 24 | + { |
| 25 | + indegree[i] = find_indegree_of_vertex(i); |
| 26 | + if(indegree[i] == 0) |
| 27 | + { |
| 28 | + add(i); |
| 29 | + } |
| 30 | + } |
| 31 | + count = 0; |
| 32 | + while(!isEmpty() && count < total_vertices) |
| 33 | + { |
| 34 | + int vertex = del(); |
| 35 | + topological_sort[++count] = vertex; |
| 36 | + for(i = 0; i < total_vertices; i++) |
| 37 | + { |
| 38 | + if(adjacent_matrix[vertex][i] == 1) |
| 39 | + { |
| 40 | + adjacent_matrix[vertex][i] = 0; |
| 41 | + indegree[i] = indegree[i] - 1; |
| 42 | + if(indegree[i] == 0) |
| 43 | + { |
| 44 | + add(i); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + //count < number of vertices implies all vertices could not be covered as there exists a cycle. |
| 50 | + if(count < total_vertices) |
| 51 | + { |
| 52 | + printf("Graph is Cyclic. Therefore, Topological Ordering Not Possible\n"); |
| 53 | + exit(1); |
| 54 | + } |
| 55 | + printf("Topological Order of Vertices\n"); |
| 56 | + for(i = 1; i <= count; i++) |
| 57 | + { |
| 58 | + printf("%3d", topological_sort[i]); |
| 59 | + } |
| 60 | + printf("\n"); |
| 61 | + return 0; |
| 62 | +} |
| 63 | +// Function to add a vertex in the queue |
| 64 | +void add(int vertex) |
| 65 | +{ |
| 66 | + if(rear == MAX - 1) |
| 67 | + { |
| 68 | + printf("Queue Overflow\n"); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + if (front == -1) |
| 73 | + { |
| 74 | + front = 0; |
| 75 | + } |
| 76 | + rear = rear + 1; |
| 77 | + queue[rear] = vertex ; |
| 78 | + } |
| 79 | +} |
| 80 | +//Queue is empty or not |
| 81 | +int isEmpty() |
| 82 | +{ |
| 83 | + if(front == -1 || front > rear ) |
| 84 | + { |
| 85 | + return 1; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + return 0; |
| 90 | + } |
| 91 | +} |
| 92 | +//Delete the front element of the queue |
| 93 | +int del() |
| 94 | +{ |
| 95 | + |
| 96 | + if (front == -1 || front > rear) |
| 97 | + { |
| 98 | + printf("Queue Underflow\n"); |
| 99 | + exit(1); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + int element = queue[front]; |
| 104 | + front = front+1; |
| 105 | + return element; |
| 106 | + } |
| 107 | +} |
| 108 | +//Calculate the in-degree of the vertex |
| 109 | +int find_indegree_of_vertex(int vertex) |
| 110 | +{ |
| 111 | + int count, total_indegree = 0; |
| 112 | + for(count = 0; count < total_vertices; count++) |
| 113 | + { |
| 114 | + if(adjacent_matrix[count][vertex] == 1) |
| 115 | + { |
| 116 | + total_indegree++; |
| 117 | + } |
| 118 | + } |
| 119 | + return total_indegree; |
| 120 | +} |
| 121 | +//Generate the required graph |
| 122 | +void create_graph() |
| 123 | +{ |
| 124 | + int count, maximum_edges, origin_vertex, destination_vertex; |
| 125 | + printf("Enter number of vertices:\t"); |
| 126 | + scanf("%d", &total_vertices); |
| 127 | + //Maximum edges possible with fixed number of vertices. |
| 128 | + maximum_edges = total_vertices * (total_vertices - 1); |
| 129 | + for(count = 1; count <= maximum_edges; count++) |
| 130 | + { |
| 131 | + printf("Enter Edge [%d] co-ordinates (-1 -1 to quit)\n", count); |
| 132 | + printf("Enter Origin Vertex:\t"); |
| 133 | + scanf("%d", &origin_vertex); |
| 134 | + printf("Enter Destination Vertex:\t"); |
| 135 | + scanf("%d", &destination_vertex); |
| 136 | + if((origin_vertex == -1) && (destination_vertex == -1)) |
| 137 | + { |
| 138 | + break; |
| 139 | + } |
| 140 | + if(origin_vertex >= total_vertices || destination_vertex >= total_vertices || origin_vertex < 0 || destination_vertex < 0) |
| 141 | + { |
| 142 | + printf("Edge Co-ordinates are Invalid\n"); |
| 143 | + count--; |
| 144 | + } |
| 145 | + else |
| 146 | + adjacent_matrix[origin_vertex][destination_vertex] = 1; |
| 147 | + } |
| 148 | +} |
0 commit comments