[Algorithms] Topological Sort

简介: Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge (u, v) from node u to node v in the graph, u must appear before v in the topological sort.

Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge (u, v) from node u to node v in the graph, u must appear before v in the topological sort.

Topological sort has many interesting applications. One of them is job scheduling, in which you are assigned many jobs, each of the job has some prerequisite jobs, that means some jobs must be finished before you can move on to a particular job. If we express this dependency using a DAG (each job is represented as a node; if job u must be finished before job v, there is a directed edge from node u to node v), the topological sort of the graph will be a feasible schedule for the jobs.

The implementation of topogical sort is based on the previous graph representation and the DFS algorithms in this passage. I also implement a function read_digraph to input a directed graph manually. It is basically the same to the function read_graph in the above link. You may refer to it for the formatting style of the graph and more information.

The following graph is used to test the code.

The code is as follows.

 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 #include <unordered_set>
 5 
 6 using namespace std;
 7 
 8 struct GraphNode {
 9     int label;
10     vector<GraphNode*> neighbors;
11     GraphNode(int _label) : label(_label) {}
12 };
13 
14 vector<GraphNode*> read_digraph(void) {
15     int num_nodes, num_edges;
16     scanf("%d %d", &num_nodes, &num_edges);
17     vector<GraphNode*> graph(num_nodes);
18     for (int i = 0; i < num_nodes; i++)
19         graph[i] = new GraphNode(i);
20     int node, neigh;
21     for (int i = 0; i < num_edges; i++) {
22         scanf("%d %d", &node, &neigh);
23         graph[node] -> neighbors.push_back(graph[neigh]);
24     }
25     return graph;
26 }
27 
28 void topological(vector<GraphNode*>& graph, GraphNode* node, \
29                  unordered_set<GraphNode*>& visited, vector<GraphNode*>& nodes) {
30     visited.insert(node);
31     for (GraphNode* neigh : node -> neighbors)
32         if (visited.find(neigh) == visited.end())
33             topological(graph, neigh, visited, nodes);
34     nodes.push_back(node);
35 }
36 
37 vector<GraphNode*> topological_sort(vector<GraphNode*>& graph) {
38     vector<GraphNode*> nodes;
39     unordered_set<GraphNode*> visited;
40     for (GraphNode* node : graph)
41         if (visited.find(node) == visited.end())
42             topological(graph, node, visited, nodes);
43     reverse(nodes.begin(), nodes.end());
44     return nodes;
45 }
46 
47 void graph_test(void) {
48     vector<GraphNode*> graph = read_digraph();
49     // Topological Sort
50     printf("Topological Sort:\n");
51     vector<GraphNode*> topo_sort = topological_sort(graph);
52     for (GraphNode* node : topo_sort)
53         printf("%d ", node -> label);
54     printf("\n");
55 }
56 
57 int main(void) {
58     graph_test();
59     system("pause");
60     return 0;
61 }

The above graph is input as as follows:

9 9
0 1
1 4
1 2
2 7
3 4
0 4
5 2
5 6
6 7

The output is as follows:

Topological Sort:
8 5 6 3 0 1 2 7 4

Now if you reorder the nodes of the graph in the order of the topological sort, all the edges will be pointing from left to right, as shown in the graph below.

 

目录
相关文章
|
算法 搜索推荐 数据库
一个有点咬文嚼字的 sorting 和 ordering
为什么排序算法的英文是 sorting 而不是 ordering。
138 0
|
算法 Java
简单选择排序(Simple Selection Sort)
算法介绍 算法描述 动图演示 算法分析 代码实现 算法优化 参考
简单选择排序(Simple Selection Sort)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
109 0
|
C++
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
113 0
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
104 0
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
117 0
Data Structures and Algorithms (English) - 6-1 Deque(25 分)
Data Structures and Algorithms (English) - 6-1 Deque(25 分)
100 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
109 0
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
104 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
114 0