一、有向线段,存储开始点与结束点

/**
* 有方向的线段
*
* @author Gm
*
*/
public class DirectionLine implements Cloneable { private String beginNode;
private String endNode; public DirectionLine(String beginNode, String endNode) {
this.beginNode = beginNode;
this.endNode = endNode;
} public String toString() {
return beginNode + "->" + endNode;
} public String getBeginNode() {
return beginNode;
} public void setBeginNode(String beginNode) {
this.beginNode = beginNode;
} public String getEndNode() {
return endNode;
} public void setEndNode(String endNode) {
this.endNode = endNode;
} public boolean equals(Object obj) {
DirectionLine dl = (DirectionLine) obj;
if (this.getBeginNode().equals(dl.getBeginNode()) && this.getEndNode().equals(dl.getEndNode())) {
return true;
}
return false;
} public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}

三、过滤无效节点

public class Graph {

	List<DirectionLine> directionLines = null; // 已知的路径(有向线,包含:开始点、结束点)
List<String> visitedList = new ArrayList<String>(); // 存放已经访问过点的节点
Set<String> resultSet = new HashSet<String>(); // 目的访问路径(点的集合)
Set<DirectionLine> loopList = new HashSet<DirectionLine>(); // 已知的回路(有向线,包含:开始点、结束点) public Graph(List<DirectionLine> directionLines) {
this.directionLines = directionLines;
} public List<DirectionLine> getDirectionLines() {
return directionLines;
} public void setDirectionLines(List<DirectionLine> directionLines) {
this.directionLines = directionLines;
} public List<String> getVisitedList() {
return visitedList;
} public void setVisitedList(List<String> visitedList) {
this.visitedList = visitedList;
} public Set<String> getResultSet() {
return resultSet;
} public void setResultSet(Set<String> resultSet) {
this.resultSet = resultSet;
} public Set<DirectionLine> getLoopList() {
return loopList;
} public void setLoopList(Set<DirectionLine> loopList) {
this.loopList = loopList;
} /**
* 路径遍历的核心算法
*
* @param startNode
* @param endNode
*/
public void getAllPaths(String startNode, String endNode) {
visitedList.add(startNode);
// System.out.println("访问的起点->终点:" + startNode + "->" + endNode);
for (int z = 0; z < directionLines.size(); z++) {
// System.out.println("遍历次数:" + z + ",路径:" + directionLines.get(z).toString());
if (directionLines.get(z).getBeginNode().equals(startNode)) { // 寻找找以startNode开始的路径
if (directionLines.get(z).getEndNode().equals(endNode)) { // 如果以endNode结尾,则为一条有效路径
resultSet.add(visitedList.toString().substring(0, visitedList.toString().lastIndexOf("]")) + "," + endNode + "]");
continue;
}
// System.out.println("已访问过的节点:" + visitedList.toString());
if (!visitedList.contains(directionLines.get(z).getEndNode())) {// 此节点仍未遍历,则继续迭代
getAllPaths(directionLines.get(z).getEndNode(), endNode);
} else {// 证明存在回路
loopList.add(directionLines.get(z));
}
}
}
visitedList.remove(startNode);
}
}
public class MapVisit {
/**
* 构造初始化路径--已知
*
*/
public List<DirectionLine> init() {
List<DirectionLine> directionLines = new ArrayList<DirectionLine>();
String str = CommonUtil.readToString("room_layout2.json");
JSONObject jsonObject = JSONObject.parseObject(str);
for (Entry<String, Object> entry : jsonObject.entrySet()) {
String startNode = "node" + entry.getKey();
JSONObject child_nodes = JSONObject.parseObject(entry.getValue().toString());
for (Entry<String, Object> child_entry : child_nodes.entrySet()) {
String endNode = "node" + child_entry.getKey();
// System.out.println(startNode + ":" + endNode);
directionLines.add(new DirectionLine(startNode, endNode));
}
}
return directionLines;
} /**
* 判断所有路径中是否有以beginNode节点为起点的基本路径
*
* @param beginNode
* @param directionLines
* @return
*/
public boolean existBeginNode(String beginNode, List<DirectionLine> directionLines) {
boolean result = false;
for (DirectionLine dl : directionLines) {
if (dl.getBeginNode().equals(beginNode)) {
result = true;
break;
}
}
return result;
} /**
* 判断所有路径中是否有以end节点为终点的基本路径
*
* @param endNode
* @param directionLines
* @return
*/
public boolean existEndNode(String endNode, List<DirectionLine> directionLines) {
boolean result = false;
for (DirectionLine dl : directionLines) {
if (dl.getEndNode().equals(endNode)) {
result = true;
break;
}
}
return result;
} /**
* 根据路径获取到所有的节点
*
* @param directionLines
* @return
*/
public Set<String> getAllNodes(List<DirectionLine> directionLines) {
Set<String> nodes = new HashSet<String>();
for (DirectionLine r : directionLines) {
nodes.add(r.getBeginNode());
nodes.add(r.getEndNode());
}
return nodes;
} /**
* 获取到需要删除的路径
*
* @param beginNodes
* 无效起始节点
* @param endNodes
* 无效终结点
* @param directionLines
*/
public Set<DirectionLine> deleteDirectionLines(Set<String> beginNodes, Set<String> endNodes, List<DirectionLine> directionLines) {
Set<DirectionLine> set = new HashSet<DirectionLine>();
for (String str : beginNodes) {
for (DirectionLine dl : directionLines) {
if (dl.getBeginNode().equals(str)) {
set.add(dl);
}
if (dl.getBeginNode().equals(str)) {
set.add(dl);
}
}
}
return set;
} /**
* 过滤掉无用的节点 获取到无效开始节点和无效结束点
*
* @param all
* @param directionLines
* @param beginNodes
* @param endNodes
* @return
*/
public Set<String> filterInvalidNode(Set<String> allNodes, List<DirectionLine> directionLines, Set<String> beginNodes, Set<String> endNodes) {
Set<String> result = new HashSet<String>();
boolean isBegin = true;
boolean isEnd = true;
for (String node : allNodes) {
if (!existEndNode(node, directionLines)) { // 没有以此节点结尾的路径,则证明此节点为无用节点
isBegin = false;
beginNodes.add(node);
} else if (!existBeginNode(node, directionLines)) {// 没有以此节点开头的路径,则证明此节点为无用节点
isEnd = false;
endNodes.add(node);
} else {
result.add(node); // 有用的节点
}
}
if (isBegin == true && isEnd == true) {
return result;
} else {
return filterInvalidNode(result, directionLines, beginNodes, endNodes);
}
} /**
* @param args
*/
public static void main(String[] args) {
MapVisit visit = new MapVisit();
List<DirectionLine> directionLines = visit.init();
// 构造基本路径--为已知条件
Set<String> invalidBeginNodes = new HashSet<String>(); // 无效的起始节点
Set<String> invalidEndNodes = new HashSet<String>(); // 无效的结束节点
Set<String> allNodes = visit.getAllNodes(directionLines);
visit.filterInvalidNode(allNodes, directionLines, invalidBeginNodes, invalidEndNodes); // 获取到无效开始节点和无效结束点
Set<DirectionLine> invalidRoads = visit.deleteDirectionLines(invalidBeginNodes, invalidEndNodes, directionLines); // 获取需要删除的路径
directionLines.removeAll(invalidRoads); // 删除无效的路径 // System.out.println(directionLines.toString()); Graph pra = new Graph(directionLines);
String begin = "node12"; // 起始点
String end = "node1"; // 终结点
// 获取所有有效路径
pra.getAllPaths(begin, end); Iterator<String> it = pra.getResultSet().iterator();
System.out.println("-----------------从" + begin + "至" + end + "的有效路径如下-----------------");
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

四、数据集

{"1":{"2":1},"2":{"1":1,"3":1,"19":1},"3":{"2":1,"4":1,"9":1},"4":{"3":1,"5":1},"5":{"4":1},"6":{"9":1},"7":{"10":1},"8":{"11":1},"9":{"3":1,"6":1,"10":1},"10":{"7":1,"9":1,"11":1,"12":1},"11":{"8":1,"10":1},"12":{"10":1,"13":1,"17":1},"13":{"12":1,"14":1,"15":1},"14":{"13":1,"16":1},"15":{"13":1},"16":{"14":1},"17":{"12":1,"18":1},"18":{"17":1,"19":1,"20":1},"19":{"2":1,"18":1},"20":{"18":1}}

五、效果展示

-----------------从node12至node1的有效路径如下-----------------

[node12, node17, node18, node19, node2,node1]

[node12, node10, node9, node3, node2,node1]

Java 计算两点间的全部路径(二)的更多相关文章

  1. Java 计算两点间的全部路径(一)

    算法要求: 在一个无向连通图中求出两个给定点之间的所有路径: 在所得路径上不能含有环路或重复的点: 算法思想描述: 整理节点间的关系,为每个节点建立一个集合,该集合中保存所有与该节点直接相连的节点(不 ...

  2. HDOJ2001计算两点间的距离

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. hdu2001 计算两点间的距离【C++】

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. 计算两点间的距离,hdu-2001

    计算两点间的距离 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1 ...

  5. TSQL 根据经纬度计算两点间的距离;返回米(m)

    -- ============================================= -- Author:Forrest -- Create date: 2013-07-16 -- Des ...

  6. 转:Math: Math.atan() 与 Math.atan2() 计算两点间连线的夹角

    我们可以使用正切操作将角度转变为斜率,那么怎样利用斜率来转换为角度呢?可以利用斜率的反正切函数将他转换为相应的角度.as中有两个函数可以计算反正切,我们来看一下. 1.Math.atan() Math ...

  7. 经纬度计算两点间的距离,根据距离排序SQL

    #java的Utilspublic class DistanceUtil { // 地球平均半径 private static final double EARTH_RADIUS = 6378137; ...

  8. J - 计算两点间的距离

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 输入两 ...

  9. 计算两点间的距离-hdu2001

    Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...

随机推荐

  1. Python re 正则表达式【一】【转】

    数量词的贪婪模式与非贪婪模式 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符:非贪婪的则相反,总是尝试匹配尽 ...

  2. 《Structuring Machine Learning Projects》课堂笔记

    Lesson 3 Structuring Machine Learning Projects 这篇文章其实是 Coursera 上吴恩达老师的深度学习专业课程的第三门课程的课程笔记. 参考了其他人的笔 ...

  3. linux下mysql定时备份,数据保存周期一周

    以下脚本来自网络,版权归原作者所有(推荐放在夜间自动备份,用cron制定计划任务) crontab -e 0 3 * * * /var/erp/data/mysql_backup.sh #!/bin/ ...

  4. [笔记] 使用v2r访问外网

    介绍 首先,你需要有一台能上外网的服务器,如AWS,GCP等. 其次,请自行复制全文,然后将FXXK替换为v2r的全称(5个小写字符). 服务器上Docker镜像配置流程 拉取镜像 $ sudo do ...

  5. (“(null)” is of a model that is not supported by this version of Xcode. Please...)

    真机测试遇到以下问题: (还以为手机不支持Xcode的版本呢) 解决方法: 发现只要将XCode重启后就可以真机运行了,碰见这个问题的朋友可以试下,我反正是被坑了半小时...

  6. SQLserver本地数据库开发

    远程端数据库中生成脚本 注意 远程端的数据库 是中文版的还是英文版的,一般我们装的是英文版的, 如果远程端数据库是中文版的,那么我们本地的是英文版,在生成的脚本那需要修改,同时去除相应的路劲代码. 修 ...

  7. ApiDoc 后端接口注释文档的使用

    前端和后端注释文档生成 前端和后端的 函数及api 说明文档生成总结,持续更新中 by Qzx 参考网址 jsDoc - 中文说明 jsDoc 使用教程 后台接口文档生成器 - APIDOC官网 ap ...

  8. Python爬虫学习==>第十章:使用Requests+正则表达式爬取猫眼电影

    学习目的: 通过一个一个简单的爬虫应用,初窥门径. 正式步骤 Step1:流程框架 抓取单页内容:利用requests请求目标站点,得到单个页面的html代码,返回结果: 正则表达式分析:根据html ...

  9. UOJ#152盘子序列

    题面君 那这是一题比较标准的单调栈的题目,维护一下单调栈并访问就好了 int n;//因为我写了十几行头文件..头文件就删了,大家自己加一下吧.. ]; ],s2[],t1,t2; int get() ...

  10. 更改默认浏览器(Windows7)

    更改默认浏览器 第一个方法(最好用): 第一步,先点击左下角WIN 然后点默认程序(画框框的图上) 第二步骤,来到控制面板主页,点击设置程序 第三步骤,左键单击选择要设置为默认浏览器的程序(我用360 ...