基于JGraphT实现的路径探寻
基于JGraphT实现的路径探寻
业务中提出基于内存,探寻的两点间的有向以及无向路径,多点间的最小子图等需求,以下记录使用JGraphT的实现过程。
GraphT是免费的Java类库,提供数学图论对象和算法,本文只涉及路径探寻中的部分内容。
图实例简介
以下资料来源graph-structures
可用图概览
图类 边方向 自环 顶点对间多边 加权 SimpleGraph undirected no no no Multigraph undirected no yes no Pseudograph undirected yes yes no DefaultUndirectedGraph undirected yes no no SimpleWeightedGraph undirected no no yes WeightedMultigraph undirected no yes yes WeightedPseudograph undirected yes yes yes DefaultUndirectedWeightedGraph undirected yes no yes SimpleDirectedGraph directed no no no DirectedMultigraph directed no yes no DirectedPseudograph directed yes yes no DefaultDirectedGraph directed yes no no SimpleDirectedWeightedGraph directed no no yes DirectedWeightedMultigraph directed no yes yes DirectedWeightedPseudograph directed yes yes yes DefaultDirectedWeightedGraph directed yes no yes 结构特性
无向边(undirected edges):一条边只连接一个顶点对,不施加方向。
有向边(directed edges):边具有起点和终点。
自环(self-loops):是否允许顶点的边连接到自身。
同向多边(multiple edges):是否在同一顶点对之间存在多个边(有向图中,同一顶点对之间,方向相反的两条边不计为多边)。
加权(weighted):边是否具有浮点值权重(对于该类图,通常用DefaultWeightedEdge作边类型),
未加权图被视为有统一的边权重1.0,这使它们可以用于算法中,例如查找最短路径。
业务类
抽象顶点类
public class Node{
//顶点id
private String id;
...
}
抽象边缘类
public class Link{
//边缘id
private String id;
//起始点id
private String source;
//终止点id
private String target;
...
}
抽象图数据类
public class GraphDescription{
//顶点集合
private List<Node> nodes;
//边缘集合
private List<Link> links;
...
}
两点有向路径探寻
使用业务中的UID(String)作为顶点类,边构造使用默认边类DefaultEdge。
Graph<String, DefaultEdge> directedGraph = new DirectedMultigraph<>(DefaultEdge.class);
graphDescription.getNodes().forEach(node -> directedGraph.addVertex(node.getId()));
graphDescription.getLinks().forEach(link -> directedGraph.addEdge(link.getSource(), link.getTarget()));
最短路径探寻,先找出有向最短路径长度,最短路径长度小于限制时,按照最短路径跳数找出所有非自环路径
DijkstraShortestPath<String, DefaultEdge> dijkstraAlg = new DijkstraShortestPath<>(directedGraph);
GraphPath<String, DefaultEdge> shorest = dijkstraAlg.getPath(start, end);
if (shorest != null && shorest.getLength() <= hopsLimit) {
AllDirectedPaths allPaths = new AllDirectedPaths(directedGraph);
fullRes = allPaths.getAllPaths(start, end, true, shorest.getLength());
}
全路径探寻,按照跳数限制直接探寻结果
AllDirectedPaths allPaths = new AllDirectedPaths(directedGraph);
fullRes = allPaths.getAllPaths(start, end, true, hopsLimit);
两点无向路径探寻
我们构造支持无向及多边的Multigraph
Graph<String, DefaultEdge> multiGraph = new Multigraph<>(DefaultEdge.class);
graphDescription.getNodes().forEach(node -> graph.addVertex(node.getId()));
graphDescription.getLinks().forEach(link -> graph.addEdge(link.getSource(), link.getTarget()));
无向路径探寻类似于有向步骤,确认最短路径跳数探寻,结果数限制k设置为整型最大值
BidirectionalDijkstraShortestPath<String, DefaultEdge> dijkstraAlg = new BidirectionalDijkstraShortestPath<>(multiGraph);
GraphPath<String, DefaultEdge> shorest = dijkstraAlg.getPath(start, end);
if (shorest != null && shorest.getLength() <= hopsLimit) {
KShortestSimplePaths simplePaths = new KShortestSimplePaths(multiGraph, shorest.getLength());
fullRes = simplePaths.getPaths(start, end, Integer.MAX_VALUE);
}
全路径探寻,按照跳数限制直接探寻结果
KShortestSimplePaths simplePaths = new KShortestSimplePaths(graph, hopsLimit);
fullRes = simplePaths.getPaths(start, end, Integer.MAX_VALUE);
多点最小子图探寻
基于MultiGraph,多个点形成的集合,与自身作笛卡尔积,两两探寻最短路径后加入fullRes并去重,形成的边集即为最小子图。
基于JGraphT实现的路径探寻的更多相关文章
- Mininet实验 基于Mininet实现BGP路径挟持攻击实验
参考:基于Mininet实现BGP路径挟持攻击实验 实验目的: 掌握如何mininet内模拟AS. 掌握BGP路径挟持的原理和分析过程. 实验原理: 互联网是由相互连接的自治系统AS组成的,通过一个通 ...
- 4.6 基于STM32+MC20地图显示路径功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- C#路径总结
[相对路径] Request.ApplicationPath /src Path.GetDirectoryName(HttpContext.Current.Request.RawUrl ) //s ...
- 【转】 ASP.NET网站路径中~(波浪线)解释
刚开始学习ASP.NET的朋友可能会不理解路径中的-符代表什么,例如ImageUrl=”~/Images/SampleImage.jpg” 现在我们看看-代表什么意思.-是ASP.NET 的Web 应 ...
- asp.net后台获取路径的各种方法归纳
asp.net后台获取路径的各种方法归纳 1.Request.CurrentExecutionFilePath 获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...
- 使用Class.getResource和ClassLoader.getResource方法获取文件路径
自从转投Java阵营后,一直发下Java程序的路径读取异常麻烦,因此查阅了比较多的版本内容,整合了一份自己的学习笔记.主要使用Class及通过ClassLoader来动态获取文件路径. 查阅链接如下: ...
- 关于ASP.Net中路径的问题
比如你的工程是Webapplication1(url是:http://localhost/webapplication1/webform1.aspx) Request.ApplicationPath ...
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1
以下是一些设计略显繁琐,有必要清除思维. 下一个主要的成就,当我们点击Gobutton后,得到一个随机数骰子,是走了几步,它是基于以下步骤行走路径的数目,然后移动位置的基于角色的路径. 流程如图普遍认 ...
- 一种基于路网图层的GPS轨迹优化方案
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 GPS数据正常情况下有20M左右的偏移,在遇到高楼和桥梁等情况 ...
随机推荐
- 微信接口开发报错invalid credential, access_token is invalid or not latest hint
微信接口凭证access_token一定要全局管理 我们的查酒后台集成了微信公众平台的客服API接口,不用登录微信公众号的后台就可以直接给用户发送消息.最近,运营的同事反馈,通过微信查酒,后台无法直接 ...
- PHP 试题(1)
1.__FILE__表示什么意思?(5分)文件的完整路径和文件名.如果用在包含文件中,则返回包含文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一 ...
- Unity 鼠标控制视角功能和动画播放冲突解决办法
环境是 unity 5.6.4 情况说明: 1 模型动画单独播放是没问题的. 2 鼠标控制模型是没问题的. 3 在start中播放模型动画,即使鼠标控制视角代码还挂载着,但是模型却无法用鼠标旋转等操作 ...
- 怎样打开.jar格式文件,怎样运行.jar格式文件
当时第一次看到.jar文件不知道是什么鬼,以为是压缩包,直接就解压了,但是并没有什么用.所以在下为大家详细介绍如何打开.jar文件以及如何运行.jar文件.什么是.jar文件,简单的说就是java压缩 ...
- ThinkPHP商城实战
ThinkPHP3.2.3商城实战教程,需要的联系我,QQ:1844912514 千万级php电商秒杀项目实战 ,需要的联系我,QQ:1844912514
- svn 删除、移动和改名
删除.移动和改名 Subversion allows renaming and moving of files and folders. So there are menu entries for d ...
- React MVC框架 <某某后台商品管理开源项目> 完成项目总结
**百货后台商品信息开源项目 1.利用React app脚手架 2.封装打包 buid 3.更偏向于后台程序员开发思维 4.利用的 react -redux react-router-dom ...
- 2018-10-19-jekyll-添加-Valine-评论
title author date CreateTime categories jekyll 添加 Valine 评论 lindexi 2018-10-19 09:10:40 +0800 2018-2 ...
- vue-learning:2 - template - directive
指令 directive 在上一节我们知道,VUE的template模板通过VUE指令API实现与页面的视图层联系.所以本节将聚集在实现视图层交互的VUE指令系统directive的基础使用. 我们先 ...
- Strongly Connected Tournament
题解: 有一个很重要的性质就是 对于一张完全强联通图来说 一定有一个强联通分量入度为0(或者出度为0) 然后就一些计数题的基本套路 https://www.cnblogs.com/onioncyc/p ...