当前位置:网站首页>蛮力法/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();
}
}
边栏推荐
- CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
- 72. editing distance ●●
- 割舍绳子/整数分割
- AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
- 终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
- Explain L3 cache to solve circular dependency
- 揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
- 【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
- Cut rope / integer split
- Talk about server performance optimization ~ (recommended Collection)
猜你喜欢

自注意力(self-attention)和多头注意力(multi-head attention)

How to use Diablo immortal database

魔塔类游戏实现源码及关卡生成

分布式服务理论基础

KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~

What is the difference between localhost and 127.0.0.1?

canvas 高级功能(上)

Redis缓存击穿

JD released ted-q, a large-scale and distributed quantum machine learning platform based on tensor network acceleration

Meetup Preview: introduction to the new version of linkis and the application practice of DSS
随机推荐
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
The most common habits from more than 200 English papers written by gradua
Vertical bar of latex tips absolute value \left\right|
Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
72. 编辑距离 ●●●
自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
知识图谱/关系可视化
电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
Deploying static websites using OSS and CDN on Alibaba cloud international
How to use Diablo immortal database
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
Theoretical basis of distributed services
Redis缓存雪崩
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
Niuke.com: numbers that appear more than half of the times in the array
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Recommend a crud tool that may become the best crud tool in 2019 during the National Day