Java data structure - applying DFS algorithm to calculate downstream nodes of flow chart (1)

Problem Description:

When the front-end draws the flow chart, in some cases it is necessary to fold the flow chart after a certain node. Therefore, after getting the ID of a certain node, it needs to calculate all the nodes downstream of this ID (the found node, the edge is also found it)

known conditions:

The ID of a node, the flow chart is parsed into the corresponding JSON object file (some parse the flow chart into an XML file)

For example:

{
    "nodes": [
        {
            "id": "A"
        },
        {
            "id": "B"
        },
        {
            "id": "C"
        }
    ],
    "edges": [
        {
            "sourceNode": "A",
            "targetNode": "B"
        },
        {
            "sourceNode": "B",
            "targetNode": "C"
        }
    ]
}

problem solved:

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;

public class FlowChartDFS {

    // 定义节点类
    static class Node {
        public String id; // 节点ID
        public Set<String> nextNodes = new HashSet<>(); // 存放下游节点id的集合

        public Node(String id) {
            this.id = id;
        }
    }

    // 定义边类
    static class Edge {
        public String sourceNode; // 边的起始节点ID
        public String targetNode; // 边的目标节点ID

        public Edge(String sourceNode, String targetNode) {
            this.sourceNode = sourceNode;
            this.targetNode = targetNode;
        }
    }

    // 解析JSON数据,构建节点和边的关系图
    public static Map<String, Node> buildGraphFromJson(String jsonStr) {

        JSONObject flowChartJson = JSONObject.parseObject(jsonStr);
        JSONArray nodesJson = flowChartJson.getJSONArray("nodes");
        JSONArray edgesJson = flowChartJson.getJSONArray("edges");

        Map<String, Node> graph = new HashMap<>();
        for (int i = 0; i < nodesJson.size(); i++) {
            JSONObject nodeJson = nodesJson.getJSONObject(i);
            String nodeId = nodeJson.getString("id");
            Node node = new Node(nodeId);
            graph.put(nodeId, node);
        }
        for (int i = 0; i < edgesJson.size(); i++) {
            JSONObject edgeJson = edgesJson.getJSONObject(i);
            String sourceNodeId = edgeJson.getString("sourceNode");
            String targetNodeId = edgeJson.getString("targetNode");
            Node sourceNode = graph.get(sourceNodeId);
            sourceNode.nextNodes.add(targetNodeId);
        }
        return graph;
    }

    // DFS深度优先递归搜索指定节点的下游所有节点和边
    public static void dfs(String startNodeId, Set<String> visitedNodeIds, List<Edge> edges, Map<String, Node> graph) {
        Node node = graph.get(startNodeId);
        if (node == null || visitedNodeIds.contains(startNodeId)) {
            return;
        }
        visitedNodeIds.add(startNodeId);
        for (String nextNodeId : node.nextNodes) {
            Edge edge = new Edge(startNodeId, nextNodeId); // 记录边的信息
            edges.add(edge);
            // 递归循环
            dfs(nextNodeId, visitedNodeIds, edges, graph);
        }
    }

    public static void main(String[] args) {
        // JSON数据示例
        String jsonStr = "{\"nodes\": [{\"id\": \"A\"},{\"id\": \"B\"},{\"id\": \"C\"}], \"edges\": [{\"sourceNode\": \"A\", \"targetNode\": \"B\"},{\"sourceNode\": \"B\",\"targetNode\": \"C\"}]}";

        // 构建关系图
        Map<String, Node> graph = buildGraphFromJson(jsonStr);

        // 指定起始节点ID
        String startNodeId = "A";

        // 初始化已访问节点集合和边列表
        Set<String> visitedNodeIds = new HashSet<>();
        List<Edge> edges = new ArrayList<>();

        // 进行DFS深度优先搜索
        dfs(startNodeId, visitedNodeIds, edges, graph);

        // 输出搜索结果
        System.out.println("节点" + startNodeId + "的下游节点和边:");
        for (Edge edge : edges) {
            System.out.println(edge.sourceNode + "->" + edge.targetNode);
        }
    }
}

Payment:gson/UserGuide.md at main · google/gson · GitHub

Introducing Gson dependencies

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>

Online json formatting, json online parsing and formatting, online json verification, json formatting tool, json compression - online JSON (zxjson.com)

JSON Code Tool - Code Tool - Script House Online Tool (jb51.net)

Convert JSON object to string using Gson

Gson gson = new Gson();
String jsonString = gson.toJson();


 

Guess you like

Origin blog.csdn.net/weixin_49171365/article/details/133854623