Key Concepts of Topological Sort
- Directed Acyclic Graph (DAG): Topological sorting can only be applied to DAGs. If the graph has cycles, topological sorting is not possible.
- DFS-Based Approach:
- Perform DFS traversal on the graph.
- Use a stack to keep track of the order of vertices.
- After visiting all vertices, the stack will contain the topological sort.
C Program for Topological Sort Using DFS
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
// Structure for the adjacency list node
struct Node {
int vertex;
struct Node* next;
};
// Structure for the graph
struct Graph {
int numVertices;
struct Node* adjList[MAX];
};
// Function to create a new adjacency list node
struct Node* createNode(int vertex) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
// Function to create a graph with a given number of vertices
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = vertices;
for (int i = 0; i < vertices; i++) {
graph->adjList[i] = NULL; // Initialize adjacency list
}
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int src, int dest) {
struct Node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
}
// DFS utility function for topological sort
void topologicalSortUtil(struct Graph* graph, int vertex, int visited[], int stack[], int* stackIndex) {
visited[vertex] = 1; // Mark the current vertex as visited
// Recur for all the vertices adjacent to this vertex
struct Node* temp = graph->adjList[vertex];
while (temp) {
int adjVertex = temp->vertex;
if (!visited[adjVertex]) {
topologicalSortUtil(graph, adjVertex, visited, stack, stackIndex);
}
temp = temp->next; // Move to the next adjacent vertex
}
// Push current vertex to stack which stores the result
stack[(*stackIndex)++] = vertex;
}
// Function to perform topological sort
void topologicalSort(struct Graph* graph) {
int visited[MAX] = {0}; // Array to keep track of visited vertices
int stack[MAX]; // Stack to store the topological order
int stackIndex = 0; // Index for the stack
// Call the utility function for each vertex
for (int i = 0; i < graph->numVertices; i++) {
if (!visited[i]) {
topologicalSortUtil(graph, i, visited, stack, &stackIndex);
}
}
// Print the contents of the stack
printf("Topological Sort: ");
for (int i = stackIndex - 1; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
// Main function
int main() {
struct Graph* graph = createGraph(6); // Create a graph with 6 vertices
// Adding edges to the graph
addEdge(graph, 5, 2);
addEdge(graph, 5, 0);
addEdge(graph, 4, 0);
addEdge(graph, 4, 1);
addEdge(graph, 2, 3);
addEdge(graph, 3, 1);
// Perform topological sort
topologicalSort(graph);
return 0;
}
Explanation of the Code
- Graph Representation:
- The graph is represented using an adjacency list, where each vertex points to a list of its neighbors. The
Node
structure is used to represent each vertex’s adjacent nodes.
- The graph is represented using an adjacency list, where each vertex points to a list of its neighbors. The
- Graph Functions:
- createNode: Creates a new node for the adjacency list.
- createGraph: Initializes a graph with a specified number of vertices.
- addEdge: Adds a directed edge from
src
todest
.
- Topological Sort using DFS:
- The
topologicalSortUtil
function performs a DFS traversal. - It marks each vertex as visited and recurses through its adjacent vertices.
- Once all adjacent vertices are processed, the current vertex is pushed onto a stack.
- The
- Main Function:
- In the
main
function, we create a graph with 6 vertices and add directed edges. Then, we call thetopologicalSort
function to perform the sorting and print the result.
- In the
Input and Output
Input (Edges of the graph):
The directed edges added to the graph are:
5 → 2
5 → 0
4 → 0
4 → 1
2 → 3
3 → 1
Output
When you run the program, you will see the following output:
Topological Sort: 4 5 0 2 3 1
Conclusion
This program demonstrates how to perform topological sorting using DFS on a directed graph. It effectively handles the ordering of vertices to satisfy the prerequisite conditions. You can modify the graph structure by changing the edges to observe different topological sorts. This implementation serves as a good introduction to graph algorithms and their applications in real-world problems, such as scheduling tasks and resolving dependencies.