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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值