JAVA 实现找出有向图中的所有环路,并输出求指教

-ZHUANGNINGLI- 2016-03-21 10:17:31
package Util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GectorGraph {
private Point root;
private List<List<String>> circlePath;

public GectorGraph(String pointName) {
root=new Point(pointName);
}
public GectorGraph(Point point) {
root=point;
}

public boolean hasCirclePath(){
findCirclePath();
return circlePath.size()>0;
}

public void findCirclePath(){
List<Point> CirclePoints=findCirclePoint();
if(circlePath==null){circlePath=new ArrayList<List<String>>();}
for(Point tempPoint:CirclePoints){
List<String> pointPath=new ArrayList<String>();
findPointPath(tempPoint,root,pointPath);
pointPath.add(root.pointName);
circlePath.add(pointPath);
}
}

public boolean findPointPath(Point target,Point currentPoint,List<String> pointPath){
if(currentPoint.equals(target)){return true;}
if(!currentPoint.hasNext()){return false;}
List<Point> pointList= currentPoint.getNextPointList();
for(Point tempPoint:pointList){
if(tempPoint.equals(root)){continue;}
if(findPointPath(target,tempPoint,pointPath)){
pointPath.add(tempPoint.pointName);
return true;
}
}
return false;
}

private List<Point> findCirclePoint(){
if(!root.hasNext()){return null;}
List<Point> circlePoints=new ArrayList<Point>();
findCirclePoint(root,root,circlePoints);
return circlePoints;
}
private void findCirclePoint(Point root,Point currentPoint,List<Point> circlePoints){
if(!currentPoint.hasNext()){return;}
List<Point> pointList= currentPoint.getNextPointList();
for(Point tempPoint:pointList){
if(tempPoint.equals(root)){circlePoints.add(currentPoint);}
else{findCirclePoint(root,tempPoint,circlePoints);}
}
}
public void showPath(){
if(circlePath==null){System.out.println("Error");}
for(List<String> tempList:circlePath){
StringBuffer pathString=new StringBuffer();
int tempListIndex=tempList.size()-1;
for(;tempListIndex>-1;tempListIndex--){
pathString.append(tempList.get(tempListIndex));
if(tempListIndex!=0){pathString.append("->");}
}
System.out.println(pathString.toString());
}
}
public static void main(String[] args) {

Point p1=new Point("1");
Point p2=new Point("2");
Point p3=new Point("3");
Point p4=new Point("4");
p1.addNextPoint(p2);
p2.addNextPoint(p3);
p2.addNextPoint(p4);
p3.addNextPoint(p1);
p3.addNextPoint(p4);

p4.addNextPoint(p1);
GectorGraph gg=new GectorGraph(p1);
if(gg.hasCirclePath()){
gg.showPath();
}
}
}
class Point{
public String pointName;
private List<Point> nextPointList;
public Point(String pointName) {
this.pointName=pointName;
}
public void addNextPoint(Point p){
if(nextPointList==null){nextPointList=new ArrayList<Point>();}
nextPointList.add(p);
}
public void addNextPointList(List<Point> pList){
if(nextPointList==null){nextPointList=new ArrayList<Point>();}
nextPointList.addAll(pList);
}
public boolean hasNext(){
return nextPointList!=null&&!nextPointList.isEmpty();
}
public List<Point> getNextPointList() {
return nextPointList;
}
public void setNextPointList(List<Point> nextPointList) {
this.nextPointList = nextPointList;
}
//去重复
public int hashCode(){
return pointName.hashCode(); //使用字符串哈希值与Integer的哈希值的组合
}

public boolean equals(Object obj){
if(obj instanceof Point){ //
Point p = (Point)obj;
return(pointName.equals(p.pointName));
}
return super.equals(obj);
}
}



findCirclePath里面哪里写的不合适?我的环路应该是1->2>3,1->2->3->4,1->2->4,才对。。
为嘛答案少了个1->2->4,程序应该怎么改才能让给出所有节点,找出里面的环呢?就不用每次从p1开始了?
改了P1,增加环路之后会报StackOverflowError又是怎么回事?求高手高手指点~
...全文
335 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧