Hướng dẫn topological sort python - phân loại topo python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    For example, a topological sorting of the following graph is “5 4 2 3 1 0”. There can be more than one topological sorting for a graph. For example, another topological sorting of the following graph is “4 5 2 3 1 0”. The first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no in-coming edges).
     

    Hướng dẫn topological sort python - phân loại topo python

    Bàn luận
     

    Sắp xếp cấu trúc liên kết cho biểu đồ acyclic có hướng (DAG) là thứ tự tuyến tính của các đỉnh sao cho đối với mỗi UV cạnh có hướng, Vertex U đến trước V theo thứ tự. Việc sắp xếp tôpô cho một biểu đồ là không thể nếu biểu đồ không phải là DAG. Ví dụ, một phân loại tôpô của biểu đồ sau là Hồi 5 4 2 3 1 0. Có thể có nhiều hơn một phân loại tôpô cho một biểu đồ. Ví dụ, một phân loại tôpô khác của biểu đồ sau đây là 4 5 2 3 1 0. Đỉnh đầu tiên trong phân loại tô pô luôn luôn là một đỉnh với độ trong số là 0 (một đỉnh không có các cạnh trong đến). & Nbsp;

    Python3

    Sắp xếp tôpô có thể được thực hiện đệ quy và không được công nhận. Đầu tiên, chúng tôi hiển thị phiên bản đệ quy rõ ràng hơn, sau đó cung cấp phiên bản không nhận được phân tích. & NBSP;

    Sắp xếp cấu trúc liên kết đệ quy

    from collections import defaultdict

    class Graph:

        def

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    2

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    5
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    7
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    8
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    9

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1collections 2

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from22216 from4

        def from7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from9

        def collections 5

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1collections 7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3collections 9
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 import1

    defaultdict5

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1defaultdict7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3defaultdict9class0class1

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3import3 import4import5
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1import7

    import8import9 defaultdict0

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 defaultdict3defaultdict4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3Graph:7
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6Graph:9

        def class4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1class6

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3collections 9
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 import1

    defaultdict5

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1defaultdict7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3import3 import4import5
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1import7

    import8import9 defaultdict0

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 defaultdict3defaultdict4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    07
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    09
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    07
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08class0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    17
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08class0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

        def class4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1class6

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    09
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    29
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    29
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    24
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3class8
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 Graph:0defaultdict3Graph:2Graph:3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    40

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3import3 import4import5     4    55
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1____87

    Following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    

    The following is a Topological Sort of the given graph [5, 4, 2, 3, 1, 0] 3def9 The following is a Topological Sort of the given graph [5, 4, 2, 3, 1, 0] 00

    Algorithm:

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    01
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    03
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    044____105
    After all the nodes have been visited, what remains is the final result which is found by printing the stack result in reverse.

    Python3

    Sắp xếp tôpô có thể được thực hiện đệ quy và không được công nhận. Đầu tiên, chúng tôi hiển thị phiên bản đệ quy rõ ràng hơn, sau đó cung cấp phiên bản không nhận được phân tích. & NBSP;

    Sắp xếp cấu trúc liên kết đệ quy

    from collections import defaultdict

    class Graph:

        def

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    2

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    5
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    7
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    8
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    9

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1collections 2

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from22216 from4

        def from7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from9

        def collections 5

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1collections 7

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3collections 9
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 import1

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3import3 import4import5
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1import7

    import8import9 defaultdict0

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 defaultdict3defaultdict4

        def class4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1class6

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3class8
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 Graph:0defaultdict3Graph:2Graph:3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3import3 import4import5     4    55
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1____87

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3def9
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    00

    from17from18

    from17from20

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from22

    from17from24

    import8from26defaultdict4

    defaultdict5from29

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    01
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    03
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    044____105

    import8import9 defaultdict0

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 defaultdict3defaultdict4

        def class4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1class6

        def class4

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1class6

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3class8
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6 Graph:0defaultdict3Graph:2Graph:3
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1

    defaultdict5

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    1from62

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3from64

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    3def9
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    00

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    01
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    6
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    03
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    044____105

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    07
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    09
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    07
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08class0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    17
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08class0
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    17
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    24
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    09
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    29
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    06
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    29
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    08
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    24
    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    10

    def9    5collections 05

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    
    05

    collections 07

    Đầu ra

    The following is a Topological Sort of the given graph
    [5, 4, 2, 3, 1, 0]
    

    Phân tích độ phức tạp:

    • Độ phức tạp về thời gian: O (V + E): Thuật toán trên chỉ đơn giản là DFS với ngăn xếp làm việc và ngăn xếp kết quả.Không giống như giải pháp đệ quy, độ sâu đệ quy không phải là vấn đề ở đây. The above algorithm is simply DFS with a working stack and a result stack. Unlike the recursive solution, recursion depth is not an issue here.
    • Không gian phụ trợ: O (v): Không gian thêm cần thiết cho 2 ngăn xếp được sử dụng. The extra space is needed for the 2 stacks used.

    Vui lòng tham khảo hoàn thành bài viết về phân loại tôpô để biết thêm chi tiết. & NBSP;