当前位置:网站首页>蛮力法/u到v是否存在简单路径
蛮力法/u到v是否存在简单路径
2022-06-10 19:58:00 【xcrj】
深度优先看看从u出发是否能够到达v
package xcrj.kchalgorithm.bruteForce.graph;
import java.util.ArrayList;
import java.util.List;
/** * u到v的简单路径(路径上的顶点不重复) * 利用深度优先遍历 */
public class UVSimplePath {
static int n = 5;
static int[] visited = new int[n];
/** * 从u到v是否存在简单路径 */
static boolean dfsSimplePath(ALGraph alGraph, int u, int v) {
visited[u] = 1;
if (u == v) {
return true;
}
// 取vertex第一个边点
ArcNode p = alGraph.getAdjList().get(u).getFirstArcNode();
while (p != null) {
// 未被访问
if (0 == visited[p.getAdjVertex()]) {
// 深度取下一个边点
System.out.println(" 点" + u + "-权值" + p.getWeight() + "->点" + p.getAdjVertex());
boolean flag = dfsSimplePath(alGraph, p.getAdjVertex(), v);
if (true == flag) {
return true;
}
}
// 深度优先,广度次之
p = p.getNextArcNode();
}
return false;
}
/** * 有方向带权图 */
public static void directedWeightedGraph() {
// 初始化邻接表
int n = DFSAdjTable.n;
int e = 6;
ALGraph alGraph = new ALGraph();
alGraph.setN(n);
alGraph.setE(e);
// adjList,AVNode顶点,ArcNode边点
List<AVNode> adjList = new ArrayList<>(n);
// 0到4
AVNode avNode0 = new AVNode();
avNode0.setNo(0);
ArcNode arcNode04 = new ArcNode(6, 4, null);
avNode0.setFirstArcNode(arcNode04);
adjList.add(avNode0);
// 1到0 2
AVNode avNode1 = new AVNode();
avNode1.setNo(1);
ArcNode arcNode12 = new ArcNode(3, 2, null);
ArcNode arcNode10 = new ArcNode(9, 0, arcNode12);
avNode1.setFirstArcNode(arcNode10);
adjList.add(avNode1);
// 2到0 3
AVNode avNode2 = new AVNode();
avNode2.setNo(2);
ArcNode arcNode23 = new ArcNode(5, 3, null);
ArcNode arcNode20 = new ArcNode(2, 0, arcNode23);
avNode2.setFirstArcNode(arcNode20);
adjList.add(avNode2);
// 3到4
AVNode avNode3 = new AVNode();
avNode3.setNo(3);
ArcNode arcNode34 = new ArcNode(1, 4, null);
avNode3.setFirstArcNode(arcNode34);
adjList.add(avNode3);
// 4
AVNode avNode4 = new AVNode();
avNode4.setNo(4);
avNode4.setFirstArcNode(null);
adjList.add(avNode4);
alGraph.setAdjList(adjList);
// dfs
dfsSimplePath(alGraph, 1, 4);
}
/** * 无方向带权图 */
public static void undirectedWeightedGraph() {
// 初始化邻接表
int n = DFSAdjTable.n;
int e = 6;
ALGraph alGraph = new ALGraph();
alGraph.setN(n);
alGraph.setE(e);
// adjList,AVNode顶点,ArcNode边点
List<AVNode> adjList = new ArrayList<>(n);
// 0与1 2 4相连
AVNode avNode0 = new AVNode();
avNode0.setNo(0);
ArcNode arcNode04 = new ArcNode(6, 4, null);
ArcNode arcNode02 = new ArcNode(2, 2, arcNode04);
ArcNode arcNode01 = new ArcNode(9, 1, arcNode02);
avNode0.setFirstArcNode(arcNode01);
adjList.add(avNode0);
// 1与0 2相连
AVNode avNode1 = new AVNode();
avNode1.setNo(1);
ArcNode arcNode12 = new ArcNode(3, 2, null);
ArcNode arcNode10 = new ArcNode(9, 0, arcNode12);
avNode1.setFirstArcNode(arcNode10);
adjList.add(avNode1);
// 2与0 1 3相连
AVNode avNode2 = new AVNode();
avNode2.setNo(2);
ArcNode arcNode23 = new ArcNode(5, 3, null);
ArcNode arcNode21 = new ArcNode(3, 1, arcNode23);
ArcNode arcNode20 = new ArcNode(2, 0, arcNode21);
avNode2.setFirstArcNode(arcNode20);
adjList.add(avNode2);
// 3与2 4相连
AVNode avNode3 = new AVNode();
avNode3.setNo(3);
ArcNode arcNode34 = new ArcNode(1, 4, null);
ArcNode arcNode32 = new ArcNode(5, 2, arcNode34);
avNode3.setFirstArcNode(arcNode32);
adjList.add(avNode3);
// 4与0 3相连
AVNode avNode4 = new AVNode();
avNode4.setNo(4);
ArcNode arcNode43 = new ArcNode(1, 3, null);
ArcNode arcNode40 = new ArcNode(6, 0, arcNode43);
avNode4.setFirstArcNode(arcNode40);
adjList.add(avNode4);
alGraph.setAdjList(adjList);
// 深度优先遍历
dfsSimplePath(alGraph, 0, 4);
}
public static void main(String[] args) {
undirectedWeightedGraph();
// directedWeightedGraph();
}
}
边栏推荐
- Identity and access management (IAM)
- Canvas advanced functions (Part 1)
- "Bug" problem analysisruntimeerror:which is output 0 of resubackward0
- Mba-day21 linear programming problem
- Talk about server performance optimization ~ (recommended Collection)
- Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
- 揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
- Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
- 暗黑破坏神不朽WIKI地址 暗黑破坏神不朽数据库地址分享
- Quick start to elastic job, three minutes to experience distributed scheduled tasks
猜你喜欢

LeetCode:1037. Effective boomerang - simple
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation

Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
![[technical fragment] implementation of renaming and filtering duplicate name files with suffixes](/img/be/d33cd20ae37dfb5d3ea59ce8b1e568.png)
[technical fragment] implementation of renaming and filtering duplicate name files with suffixes

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch. cuda. device_ count() is 1.

牛客网:数组中出现次数超过一半的数字

服务管理与通信,基础原理分析

You have to learn math to play art?

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

Service management and communication, basic principle analysis
随机推荐
How to use Diablo immortal database
Theoretical basis of distributed services
Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
Read the source code of micropyton - add the C extension class module (1)
Service management and communication, basic principle analysis
Vertical bar of latex tips absolute value \left\right|
请问九洲期货是正规的吗?开户安不安全
六级考试-商务英语-考前最后一背
详解三级缓存解决循环依赖
LeetCode:1037. 有效的回旋镖————简单
Elastic-Job的快速入门,三分钟带你体验分布式定时任务
table设置超出部分隐藏,鼠标移上去显示全部
面试必备——synchronized底层原理的基础知识
保姆级教程:如何成为Apache Linkis文档贡献者
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
Redis缓存击穿
Build a BPMN modeling Web Service
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
MySQL - common functions
PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)