对于加权图最短路径(最少节点)

编程入门 行业动态 更新时间:2024-10-26 03:34:46
本文介绍了对于加权图最短路径(最少节点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图建立它返回一个非加权图中从一个节点到另一个节点的最短路径的方法。我认为使用Dijkstra的,但是这似乎有点矫枉过正,因为我只想要一对。相反,我实现了一个广度优先搜索,但麻烦的是,我在返回列表中包含了一些,我不希望节点 - ?我该如何修改我的code来实现我的目标。

公开名单<节点> getDirections(节点开始,节点完成){     名单<节点>方向=新的LinkedList<节点>();     队列<节点> Q =新的LinkedList<节点>();     节点电流=启动;     q.add(电流);     而(!q.isEmpty()){         电流= q.remove();         directions.add(电流);         如果(current.equals(完成)){             打破;         }其他{             为(节点节点:current.getOutNodes()){                 如果(!q.contains(节点)){                     q.add(节点);                 }             }         }     }     如果(!current.equals(完成)){         的System.out.println(无法到达目的地);     }     返回的方向; }

解决方案

其实你code将无法完成在循环图,考虑图表1 - > 2 - > 1。你必须有一些数组,你可以标记哪些节点的你已经访问过。并且还为每个节点可以节省previous节点,从中你来了。因此,这里是正确的code:

私人地图<节点,布尔>>可见=新的HashMap<节点,布尔>(); 私人地图<节点,节点> preV =新的HashMap<节点,节点>(); 公开名单getDirections(节点开始,节点完成){     清单方向=新的LinkedList();     队列Q =新的LinkedList();     节点电流=启动;     q.add(电流);     vis.put(电流,真正的);     而(!q.isEmpty()){         电流= q.remove();         如果(current.equals(完成)){             打破;         }其他{             为(节点节点:current.getOutNodes()){                 如果(!vis.contains(节点)){                     q.add(节点);                     vis.put(节点,真正的);                     prev.put(节点,电流);                 }             }         }     }     如果(!current.equals(完成)){         的System.out.println(无法到达目的地);     }     对于(节点node =光洁度;!节点= NULL;节点= prev.get(节点)){         directions.add(节点);     }     directions.reverse();     返回的方向; }

I'm trying build a method which returns the shortest path from one node to another in an unweighted graph. I considered the use of Dijkstra's but this seems a bit overkill since I only want one pair. Instead I have implemented a breadth-first search, but the trouble is that my returning list contains some of the nodes that I don't want - how can I modify my code to achieve my goal?

public List<Node> getDirections(Node start, Node finish){ List<Node> directions = new LinkedList<Node>(); Queue<Node> q = new LinkedList<Node>(); Node current = start; q.add(current); while(!q.isEmpty()){ current = q.remove(); directions.add(current); if (current.equals(finish)){ break; }else{ for(Node node : current.getOutNodes()){ if(!q.contains(node)){ q.add(node); } } } } if (!current.equals(finish)){ System.out.println("can't reach destination"); } return directions; }

解决方案

Actually your code will not finish in cyclic graphs, consider graph 1 -> 2 -> 1. You must have some array where you can flag which node's you've visited already. And also for each node you can save previous nodes, from which you came. So here is correct code:

private Map<Node, Boolean>> vis = new HashMap<Node, Boolean>(); private Map<Node, Node> prev = new HashMap<Node, Node>(); public List getDirections(Node start, Node finish){ List directions = new LinkedList(); Queue q = new LinkedList(); Node current = start; q.add(current); vis.put(current, true); while(!q.isEmpty()){ current = q.remove(); if (current.equals(finish)){ break; }else{ for(Node node : current.getOutNodes()){ if(!vis.contains(node)){ q.add(node); vis.put(node, true); prev.put(node, current); } } } } if (!current.equals(finish)){ System.out.println("can't reach destination"); } for(Node node = finish; node != null; node = prev.get(node)) { directions.add(node); } directions.reverse(); return directions; }

更多推荐

对于加权图最短路径(最少节点)

本文发布于:2023-11-30 10:43:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   最短   路径

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!