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又是怎么回事?求高手高手指点~
...全文
431 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文介绍了一个基于Java与Vue的中小银行信贷材料结构化审核与尽调要点抽取系统的设计与实现。系统通过OCR识别、版面解析、关键字段抽取、实体归一化、规则引擎和风险评分等技术,将非结构化的信贷材料(如营业执照、审计报告、银行流水、合同等)转化为可查询、可校验的结构化数据,实现材料自动分类、字段提取、完整性检查、跨文档一致性比对及风险线索识别。系统涵盖文件接入、文档识别、字段抽取、规则校验、风险评分与前端交互六大层次,提升了信贷审核效率、统一了审查标准、增强了风险预警能力,并建立了可追溯的数据资产体系。文中还提供了企业名称清洗、统一社会信用代码校验、金额抽取、材料完整性校验、名称相似度计算和风险评分等核心功能的模型描述与Java代码示例。; 适合人群:具备Java和Vue开发基础,从事金融科技、信贷系统开发或智能文档处理相关工作的研发人员,尤其是关注OCR、NLP与规则引擎在金融风控中应用的工程师和技术负责人。; 使用场景及目标:① 实现中小银行信贷材料的自动化结构化处理与智能审核;② 提升信贷审批效率,降低人工核验成本;③ 统一尽调标准,增强风险识别与预警能力;④ 构建可追溯、可审计的信贷数据资产,支持贷后管理与监管合规。; 阅读建议:此资源以实际项目为导向,结合业务需求与技术实现,建议开发者结合代码示例深入理解各模块设计逻辑,重点关注实体归一化、规则引擎配置与前后端协同机制,并可在类似场景中复用核心算法与架构设计。

67,535

社区成员

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

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