Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Competitive Coding/Graphs/Topological Sort/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Topological Sort

--------------------------------------

#### Algorithm Steps


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.
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.

After this we calculate the in degree for each vertex and store it in the array indegree[i].
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.

Then a loop continues till the queue is empty or count==total vertices.
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.

After the loop is completed it is checked if the count==total vertices.

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.


148 changes: 148 additions & 0 deletions Competitive Coding/Graphs/Topological Sort/topological.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include<stdio.h>
#include<stdlib.h>

#define MAX 100

void create_graph();
void add(int vertex);
int del();
int isEmpty();
int find_indegree_of_vertex(int vertex);

int total_vertices;
int adjacent_matrix[MAX][MAX];
int queue[MAX];
int front = -1;
int rear = -1;

int main()
{
int i, count, topological_sort[MAX], indegree[MAX];
create_graph();
//calculating the in-degree for all vertices.
for(i = 0; i < total_vertices; i++)
{
indegree[i] = find_indegree_of_vertex(i);
if(indegree[i] == 0)
{
add(i);
}
}
count = 0;
while(!isEmpty() && count < total_vertices)
{
int vertex = del();
topological_sort[++count] = vertex;
for(i = 0; i < total_vertices; i++)
{
if(adjacent_matrix[vertex][i] == 1)
{
adjacent_matrix[vertex][i] = 0;
indegree[i] = indegree[i] - 1;
if(indegree[i] == 0)
{
add(i);
}
}
}
}
//count < number of vertices implies all vertices could not be covered as there exists a cycle.
if(count < total_vertices)
{
printf("Graph is Cyclic. Therefore, Topological Ordering Not Possible\n");
exit(1);
}
printf("Topological Order of Vertices\n");
for(i = 1; i <= count; i++)
{
printf("%3d", topological_sort[i]);
}
printf("\n");
return 0;
}
// Function to add a vertex in the queue
void add(int vertex)
{
if(rear == MAX - 1)
{
printf("Queue Overflow\n");
}
else
{
if (front == -1)
{
front = 0;
}
rear = rear + 1;
queue[rear] = vertex ;
}
}
//Queue is empty or not
int isEmpty()
{
if(front == -1 || front > rear )
{
return 1;
}
else
{
return 0;
}
}
//Delete the front element of the queue
int del()
{

if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
else
{
int element = queue[front];
front = front+1;
return element;
}
}
//Calculate the in-degree of the vertex
int find_indegree_of_vertex(int vertex)
{
int count, total_indegree = 0;
for(count = 0; count < total_vertices; count++)
{
if(adjacent_matrix[count][vertex] == 1)
{
total_indegree++;
}
}
return total_indegree;
}
//Generate the required graph
void create_graph()
{
int count, maximum_edges, origin_vertex, destination_vertex;
printf("Enter number of vertices:\t");
scanf("%d", &total_vertices);
//Maximum edges possible with fixed number of vertices.
maximum_edges = total_vertices * (total_vertices - 1);
for(count = 1; count <= maximum_edges; count++)
{
printf("Enter Edge [%d] co-ordinates (-1 -1 to quit)\n", count);
printf("Enter Origin Vertex:\t");
scanf("%d", &origin_vertex);
printf("Enter Destination Vertex:\t");
scanf("%d", &destination_vertex);
if((origin_vertex == -1) && (destination_vertex == -1))
{
break;
}
if(origin_vertex >= total_vertices || destination_vertex >= total_vertices || origin_vertex < 0 || destination_vertex < 0)
{
printf("Edge Co-ordinates are Invalid\n");
count--;
}
else
adjacent_matrix[origin_vertex][destination_vertex] = 1;
}
}