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.

  • 相关阅读:
    day 29 什么是元类、class底层原理分析、通过元类来控制类的产生、通过元类控制类的调用过程、有了元类之后的属性查找
    day 28 断点调试,反射,内置方法
    day 26 绑定方法,非绑定方法,面向对象串讲
    day 25 类的组合、多太与多态性、封装
    day 24 类的继承
    函数进阶(1)
    函数基础
    文件修改及函数定义
    文件处理
    字典类型内置方法
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4604248.html
Copyright © 2011-2022 走看看