Skip to content

Commit 354fb35

Browse files
authored
Merge pull request #177 from mitali004/Topological_branch
Added topological sort
2 parents 4283667 + eb91529 commit 354fb35

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Topological Sort
2+
3+
--------------------------------------
4+
5+
#### Algorithm Steps
6+
7+
8+
MAX holds the maximum number of vertices possible in the graph, if you want more than 100 vertices chnge the value of MAX on line 4.
9+
The first function called in the main is the create_graph(), which creates i.e. stores every edge along with its direction present in the graph.
10+
11+
After this we calculate the in degree for each vertex and store it in the array indegree[i].
12+
If for any vertex the in-degree is 0 , it signifies that all the prerequisites of that vertex are considered and it is safe to be added in the queue.
13+
14+
Then a loop continues till the queue is empty or count==total vertices.
15+
Inside the loop a vertex is deleted from the queue and all the outgoing edges are considered ,as the indegree of all the destination vertices with this particular vertex(deleted vertex) as origin is decremented by 1.
16+
17+
After the loop is completed it is checked if the count==total vertices.
18+
19+
If yes a proper topological sort obtained is printed else it can be concluded that there exists a cycle in the graph and topological sort can't be obtained.
20+
21+
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

Comments
 (0)