路径规划。求解题思路!!!通过给出点和路径,计算最优路径。如何避免环路?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了路径规划。求解题思路!!!通过给出点和路径,计算最优路径。如何避免环路?相关的知识,希望对你有一定的参考价值。

路径规划。求解题思路!!!通过给出点和路径,计算最优路径。如何避免环路?假设现在有一个有向无环图,每个节点上都带有正数权重。我们希望找到一
条最优路径,使得这个路径上经过的节点的权重之和最大。
输入:n个节点,m个路径,起点
输出:最优路径的权重值之和
举例:
3个节点:
A1
B2
C2
3条路径:
A->B
B->C
A->C
起点:
A
输出:5(最优路径是A->B->C,权重之和是1+2+2=5)
•附加问题:我们要求的输入是有向无环图,但是没人知道实际使用的时
候会有什么数据输入进来,如何避免输入了带环路的图导致的死循环
呢?

public class Edge private String startNode; private String endNode; public String getStartNode() return startNode; public void setStartNode(String startNode) this.startNode = startNode; public String getEndNode() return endNode; public void setEndNode(String endNode)
this.endNode = endNode;
public class Node private String name; private Integer weight; private List<Edge> edges = new ArrayList<Edge>(); public String getName() return name; public void setName(String name) this.name = name; public Integer getWeight() return weight; public void setWeight(Integer weight) this.weight = weight; public List<Edge> getEdges() return edges; public void addEdges(String endNode) Edge edge = new Edge(); edge.setStartNode(this.getName()); edge.setEndNode(endNode); this.edges.add(edge);
public class Graph private Map<String,Node> nodeMap = new HashMap<String, Node>();
public Map<String, Node> getNodeMap() return nodeMap; public void addNode(Node node) if(node==null) throw new RuntimeException("bad Node"); this.nodeMap.put(node.getName(),node); public void addEdge(String startNodeName,String endNodeName) if(nodeMap==null) throw new RuntimeException("nodeMap is null"); if(nodeMap.get(startNodeName)==null) throw new RuntimeException("startNode is null"); if(nodeMap.get(endNodeName)==null) throw new RuntimeException("endNode is null"); nodeMap.get(startNodeName).addEdges(endNodeName); /** * 此方法返回fromNode到toNode的最短路径,但是如果有环路则会陷入死循环 * @param fromNodeName * @param toNodeName * @return */ public int getMostPath(String fromNodeName,String toNodeName) Node fromNode = nodeMap.get(fromNodeName); List<Edge> edges = fromNode.getEdges(); if(edges==null) return -1; int path = 0; for (Edge edge :edges)
int tempPath = fromNode.getWeight(); String endNodeName = edge.getEndNode(); if(!endNodeName.equals(toNodeName)) int mostPath = getMostPath(endNodeName, toNodeName); if(mostPath==-1) continue; tempPath+=mostPath; else tempPath+=nodeMap.get(endNodeName).getWeight(); if(path<tempPath) path=tempPath; return path; /** * 可跳过闭环并且返回路径list的方法 * @param fromNodeName * @param toNodeName * @param pathList * @return */ public int getMostPathResult(String fromNodeName,String toNodeName,LinkedHashSet<String> pathList) if(fromNodeName.equals(toNodeName)) System.out.println("ERR : fromNode == toNode"); return -1; Node fromNode = nodeMap.get(fromNodeName); List<Edge> edges = fromNode.getEdges(); if(edges==null) return -1; boolean add = pathList.add(fromNodeName); if(!add) System.out.println("有闭环!"+"node:"+fromNodeName+",path:"+pathList); return -1;
int path = 0; LinkedHashSet<String> temp = new LinkedHashSet<String>(); temp.addAll(pathList); for (Edge edge :edges) LinkedHashSet<String> temp2 = new LinkedHashSet<String>(); temp2.addAll(temp); int tempPath = fromNode.getWeight(); String endNodeName = edge.getEndNode(); if(!endNodeName.equals(toNodeName)) int mostPath = getMostPathResult(endNodeName, toNodeName,temp2); if(mostPath==-1) continue; tempPath+=mostPath; else tempPath+=nodeMap.get(endNodeName).getWeight(); temp2.add(toNodeName); if(path<tempPath) path=tempPath; pathList.clear(); pathList.addAll(temp2); return path;
参考技术A 查算法吧,估计没人会给你答案追问

能给个思路就行

以上是关于路径规划。求解题思路!!!通过给出点和路径,计算最优路径。如何避免环路?的主要内容,如果未能解决你的问题,请参考以下文章

配送路径规划基于matlab遗传算法求解单配送中心多客户多车辆最短路径规划问题含Matlab源码 1602期

路径规划基于粒子群遗传求解多无人机三维路径规划matlab源码

配送路径规划基于matlab遗传算法求解无人机和货车配送路径规划问题含Matlab源码 361期

VRP基于matlab遗传算法求解多车辆路径规划问题含Matlab源码 1249期

MDVRP基于matlab遗传算法求解多仓库车辆路径规划问题含Matlab源码 1481期

MDVRP基于matlab遗传算法求解多仓库车辆路径规划问题含Matlab源码 1481期