How to implement topological sorting in Java
Foreshadowing
We will introduce algorithms related to directed graphs in this section, so we will first explain some concepts of directed graphs; subsequent articles These concepts will not be explained in detail. First, the nodes of the directed graph are connected by lines with arrows. The out-degree and in-degree of a node can be used to describe it. When the tail of a connection points to the node, its out-degree will increase by 1; and when the arrow of a connection points to the node, its in-degree will increase by 1. Look at the following example, A has an in-degree of 0 and an out-degree of 2, B has an in-degree of 1 and an out-degree of 1, C has an in-degree of 1 and an out-degree of 1, D has an in-degree of 2 and an out-degree is 0.
Adjacency list: The adjacency list is an effective way to store the graph structure. As shown in the figure below, the node array on the left stores all the nodes in the graph, and the adjacency list on the right stores the nodes. adjacent nodes.
Introduction
In this article we are going to talk about topological sorting, which is an algorithm for directed acyclic graphs, mainly to solve the problem of precursors The subsequent relationship, that is, what items we need to complete first when completing the current item, is actually used a lot in our process control. Looking at the picture below, we need to complete item A first, and then we can complete items B and C. Matters B and C are parallel and not in order, but item D can only be completed after items B and C are completed. Topological sorting can help us find a reasonable order for completing items. At the same time, let’s look at the example above. After item A is completed, items B and C are not in order. Whether B or C is completed first, the conditions are met, so topological sorting The sequence of is not entirely certain.
Working process
First, the corresponding operation of topological sorting is a directed acyclic graph. For an acyclic graph, there must be at least one node with indegree 0. In the current situation, we need to find a node with an in-degree of 0 for operation. An in-degree of 0 means that the current node has no predecessor node, or the predecessor node has been processed and can be operated directly. After the operation is completed, all the in-degrees of the current node's successor nodes are reduced by 1, and the node with an in-degree node of 0 is searched again for operation. After that, it is a recursive process, and the nodes with an in-degree of 0 under the current situation are continuously processed until all nodes are processed. complete.
Data structure
The following is the structure of a directed graph, in which node stores all the nodes in the current graph, and adj stores the corresponding subscript node. of adjacent points. When initializing the graph, you need to specify the number of nodes, create a node array and an adjacency array. Provides a method named addEdge, which is used to establish an edge between two nodes, that is, to add the successor node to the adjacency list of the predecessor node.
public static class Graph{ /** * 节点个数 */ private Integer nodeSize; /** * 节点 */ private char[] node; /** * 邻接表 */ private LinkedList[] adj; public Graph(char[] node) { this.nodeSize = node.length; this.node = node; this.adj = new LinkedList[nodeSize]; for (int i = 0 ; i < adj.length ; i++) { adj[i] = new LinkedList(); } } /** * 在节点之间加边,前驱节点指向后继节点 * @param front 前驱节点所在下标 * @param end 后继节点所在下标 */ public void addEdge(int front, int end) { adj[front].add(end); } }
Topological sorting
Topological sorting first initializes two temporary arrays, a queue, and an inDegree array to store the in-degree of the corresponding subscript node, because each node visited requires that the predecessor node has already Completed, that is, the in-degree is 0. With this array, we can find these nodes relatively quickly; the other is the visited array, which marks whether the current node has been visited to prevent multiple visits; a nodes queue is saved in the current situation All nodes with indegree 0. (Note that for the convenience of access, we all store node subscripts. Step1: Initialize the inDegree array and visited array; step2: Traverse the inDegree array and put all nodes with an indegree of 0 into the nodes queue; step3: Exit the node nodes in sequence. Queue; Determine whether the current node has been visited based on visited, if yes, return to step3, if not, proceed to the next step; Set the in-degree of the adjacent node of the current node to -1, determine whether the in-degree of the adjacent node is 0, if it is 0, put it directly into the nodes queue , if it is not 0, return to step3;
/** * @param graph 有向无环图 * @return 拓扑排序结果 */ public List<Character> toPoLogicalSort(Graph graph) { //用一个数组标志所有节点入度 int[] inDegree = new int[graph.nodeSize]; for (LinkedList list : graph.adj) { for (Object index : list) { ++ inDegree[(int)index]; } } //用一个数组标志所有节点是否已经被访问 boolean[] visited = new boolean[graph.nodeSize]; //开始进行遍历 Deque<Integer> nodes = new LinkedList<>(); //将入度为0节点入队 for (int i = 0 ; i < graph.nodeSize; i++) { if (inDegree[i] == 0) { nodes.offer(i); } } List<Character> result = new ArrayList<>(); //将入度为0节点一次出队处理 while (!nodes.isEmpty()) { int node = nodes.poll(); if (visited[node]) { continue; } visited[node] = true; result.add(graph.node[node]); //将当前node的邻接节点入度-1; for (Object list : graph.adj[node]) { -- inDegree[(int)list]; if (inDegree[(int)list] == 0) { //前驱节点全部访问完毕,入度为0 nodes.offer((int) list); } } } return result; }
Test sample 1
public static void main(String[] args) { ToPoLogicalSort toPoLogicalSort = new ToPoLogicalSort(); //初始化一个图 Graph graph = new Graph(new char[]{'A', 'B', 'C', 'D'}); graph.addEdge(0, 1); graph.addEdge(0,2); graph.addEdge(1,3); graph.addEdge(2,3); List<Character> result = toPoLogicalSort.toPoLogicalSort(graph); }
Execution result
Test sample Example 2
public static void main(String[] args) { ToPoLogicalSort toPoLogicalSort = new ToPoLogicalSort(); //初始化一个图 Graph graph = new Graph(new char[]{'A', 'B', 'C', 'D','E','F','G','H'}); graph.addEdge(0, 1); graph.addEdge(0,2); graph.addEdge(0,3); graph.addEdge(1,4); graph.addEdge(2,4); graph.addEdge(3,4); graph.addEdge(4,7); graph.addEdge(4,6); graph.addEdge(7,5); graph.addEdge(6,7); List<Character> result = toPoLogicalSort.toPoLogicalSort(graph); }
Execution result
The above is the detailed content of How to implement topological sorting in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
