zoukankan      html  css  js  c++  java
  • [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:
    ");
    51     vector<GraphNode*> topo_sort = topological_sort(graph);
    52     for (GraphNode* node : topo_sort)
    53         printf("%d ", node -> label);
    54     printf("
    ");
    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.

  • 相关阅读:
    python 进度条
    linux中利用Shell脚本实现自动安装部署weblogic服务
    Linux虚拟机如何上网
    常用Python脚本
    Allure测试框架
    软件评测师 第二小时
    保险项目测试流程(一)
    电子商务网站测试总结
    Python随机生成电话号码&号码段分析
    Python中的*args和**kwargs
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4604248.html
Copyright © 2011-2022 走看看