• Java数据结构——应用DFS算法计算流程图下游节点(1)


    问题描述:

    前端在绘制流程图的时候,某些情况需要对某个节点之后的流程图进行折叠,因此需要得到某个节点的ID后,计算出这个ID下游之后的所有节点(找到的节点,边也就找到了)

    已知条件:

    某个节点的ID,流程图解析成对应的JSON对象文件(有的是将流程图解析成XML文件)

    例如:

    1. {
    2. "nodes": [
    3. {
    4. "id": "A"
    5. },
    6. {
    7. "id": "B"
    8. },
    9. {
    10. "id": "C"
    11. }
    12. ],
    13. "edges": [
    14. {
    15. "sourceNode": "A",
    16. "targetNode": "B"
    17. },
    18. {
    19. "sourceNode": "B",
    20. "targetNode": "C"
    21. }
    22. ]
    23. }

    问题解决:

    1. import com.alibaba.fastjson2.JSONArray;
    2. import com.alibaba.fastjson2.JSONObject;
    3. import java.util.*;
    4. public class FlowChartDFS {
    5. // 定义节点类
    6. static class Node {
    7. public String id; // 节点ID
    8. public Set nextNodes = new HashSet<>(); // 存放下游节点id的集合
    9. public Node(String id) {
    10. this.id = id;
    11. }
    12. }
    13. // 定义边类
    14. static class Edge {
    15. public String sourceNode; // 边的起始节点ID
    16. public String targetNode; // 边的目标节点ID
    17. public Edge(String sourceNode, String targetNode) {
    18. this.sourceNode = sourceNode;
    19. this.targetNode = targetNode;
    20. }
    21. }
    22. // 解析JSON数据,构建节点和边的关系图
    23. public static Map buildGraphFromJson(String jsonStr) {
    24. JSONObject flowChartJson = JSONObject.parseObject(jsonStr);
    25. JSONArray nodesJson = flowChartJson.getJSONArray("nodes");
    26. JSONArray edgesJson = flowChartJson.getJSONArray("edges");
    27. Map graph = new HashMap<>();
    28. for (int i = 0; i < nodesJson.size(); i++) {
    29. JSONObject nodeJson = nodesJson.getJSONObject(i);
    30. String nodeId = nodeJson.getString("id");
    31. Node node = new Node(nodeId);
    32. graph.put(nodeId, node);
    33. }
    34. for (int i = 0; i < edgesJson.size(); i++) {
    35. JSONObject edgeJson = edgesJson.getJSONObject(i);
    36. String sourceNodeId = edgeJson.getString("sourceNode");
    37. String targetNodeId = edgeJson.getString("targetNode");
    38. Node sourceNode = graph.get(sourceNodeId);
    39. sourceNode.nextNodes.add(targetNodeId);
    40. }
    41. return graph;
    42. }
    43. // DFS深度优先递归搜索指定节点的下游所有节点和边
    44. public static void dfs(String startNodeId, Set visitedNodeIds, List edges, Map graph) {
    45. Node node = graph.get(startNodeId);
    46. if (node == null || visitedNodeIds.contains(startNodeId)) {
    47. return;
    48. }
    49. visitedNodeIds.add(startNodeId);
    50. for (String nextNodeId : node.nextNodes) {
    51. Edge edge = new Edge(startNodeId, nextNodeId); // 记录边的信息
    52. edges.add(edge);
    53. // 递归循环
    54. dfs(nextNodeId, visitedNodeIds, edges, graph);
    55. }
    56. }
    57. public static void main(String[] args) {
    58. // JSON数据示例
    59. String jsonStr = "{\"nodes\": [{\"id\": \"A\"},{\"id\": \"B\"},{\"id\": \"C\"}], \"edges\": [{\"sourceNode\": \"A\", \"targetNode\": \"B\"},{\"sourceNode\": \"B\",\"targetNode\": \"C\"}]}";
    60. // 构建关系图
    61. Map graph = buildGraphFromJson(jsonStr);
    62. // 指定起始节点ID
    63. String startNodeId = "A";
    64. // 初始化已访问节点集合和边列表
    65. Set visitedNodeIds = new HashSet<>();
    66. List edges = new ArrayList<>();
    67. // 进行DFS深度优先搜索
    68. dfs(startNodeId, visitedNodeIds, edges, graph);
    69. // 输出搜索结果
    70. System.out.println("节点" + startNodeId + "的下游节点和边:");
    71. for (Edge edge : edges) {
    72. System.out.println(edge.sourceNode + "->" + edge.targetNode);
    73. }
    74. }
    75. }

    补充:gson/UserGuide.md at main · google/gson · GitHub

    引入Gson依赖

    1. <dependency>
    2. <groupId>com.google.code.gsongroupId>
    3. <artifactId>gsonartifactId>
    4. <version>2.10.1version>
    5. dependency>

    在线json格式化,json在线解析格式化,在线json验证,json格式化工具,json压缩-在线JSON (zxjson.com)

    JSON代码工具 - 代码工具 - 脚本之家在线工具 (jb51.net)

    使用Gson将JSON对象转换为字符串

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


     

  • 相关阅读:
    10月27日,每日信息差
    Nginx优化与防盗链
    有Mac或无Mac电脑通用的获取安卓公钥的方案
    zabbix监控华为路由器
    21 - 数据接口与selenium的基本用法
    雷达人体存在感应器成品,广泛应用于感应灯控制,实时精准感知方案
    kubernetes 起几个节点,就会有几个flannel pod
    2019 CSP J2入门组 CSP-S2提高组 第2轮 视频与题解
    基于SSM的智慧作业试题管理系统(有报告)。Javaee项目。
    Hive 中的各种常用set设置
  • 原文地址:https://blog.csdn.net/weixin_49171365/article/details/133854623