Home Common Problem How is topological sort sorted?

How is topological sort sorted?

Jul 02, 2021 pm 02:19 PM
topological sort

Method: 1. Find a node in the graph with an in-degree of 0, remove this node from the graph and add it to the sequence E; 2. Associate all the nodes found in 1 The edge is removed from the graph; 3. Repeat steps 1 and 2 until all nodes in the graph are removed or a node with an in-degree of 0 cannot be found.

How is topological sort sorted?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

  1. Find a node in the graph with an in-degree of 0, remove this node from the graph and add it to the sequence E

  2. Remove all associated edges of the nodes found in 1 from the graph

  3. Repeat 1 and 2 until all nodes in the graph are removed or a node with an in-degree of 0 cannot be found Click until

If the number of nodes in the graph at this time is 0, the topological sequence has been found. If the number of nodes in the graph at this time is not 0, it means that there is a cycle in the graph and topological sorting cannot be performed. .

Extended information:

To perform topological sorting on a directed acyclic graph (DAG for short) G is to arrange all the vertices in G into a linear sequence, such that for any pair of vertices u and v in the graph, if the edge ∈E(G), then u appears before v in the linear sequence. Usually, such a linear sequence is called a sequence that satisfies the topological order, or is referred to as a topological sequence. Simply put, from a partial order on a set to a total order on the set, this operation is called topological sorting.

Execution steps

The topological sorting algorithm that constructs the topological sequence from the AOV network mainly performs the following two steps in a loop until there is no vertex with an in-degree of 0.

(1) Select a vertex with indegree 0 and output it;

(2) Delete this vertex and all outgoing edges from the network.

After the loop ends, if the number of output vertices is less than the number of vertices in the network, the "loop" information will be output, otherwise the output vertex sequence is a topological sequence.

How is topological sort sorted?

For more computer-related knowledge, please visit the FAQ column!

The above is the detailed content of How is topological sort sorted?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)