search
HomeJavajavaTutorialHow 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.

    How to implement topological sorting in Java

    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.

    How to implement topological sorting in Java

    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.

    How to implement topological sorting in Java

    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[]{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;});
           graph.addEdge(0, 1);
           graph.addEdge(0,2);
           graph.addEdge(1,3);
           graph.addEdge(2,3);
           List<Character> result = toPoLogicalSort.toPoLogicalSort(graph);
      }

    Execution result

    How to implement topological sorting in Java

    Test sample Example 2

    public static void main(String[] args) {
           ToPoLogicalSort toPoLogicalSort = new ToPoLogicalSort();
           //初始化一个图
           Graph graph = new Graph(new char[]{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;,&#39;H&#39;});
           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

    How to implement topological sorting in Java

    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!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

    Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

    完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

    一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

    详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

    本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

    Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

    java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

    封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

    归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

    See all articles

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools