当前位置:网站首页>Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
2022-06-10 21:09:00 【xcrj】
package xcrj.kchalgorithm.bruteForce.graph;
import javafx.scene.shape.Arc;
import java.util.ArrayList;
import java.util.List;
/** * Adjacency table stores graph */
public class DFSAdjTable {
static int n = 5;
static int[] visited = new int[n];
/** * initial :vertex Feel free to look 1 A little bit */
static void dfs(ALGraph alGraph, int vertex) {
visited[vertex] = 1;
// take vertex First edge point
ArcNode p = alGraph.getAdjList().get(vertex).getFirstArcNode();
while (p != null) {
// Not visited
if (0 == visited[p.getAdjVertex()]) {
// Depth takes down an edge point
System.out.println(" spot " + vertex + "- A weight " + p.getWeight() + "-> spot " + p.getAdjVertex());
dfs(alGraph, p.getAdjVertex());
}
// Depth first , Breadth second
p = p.getNextArcNode();
}
}
/** * Directed weighted graph */
public static void directedWeightedGraph() {
// Initialize adjacency table
int n = DFSAdjTable.n;
int e = 6;
ALGraph alGraph = new ALGraph();
alGraph.setN(n);
alGraph.setE(e);
// adjList,AVNode The vertices ,ArcNode Edge point
List<AVNode> adjList = new ArrayList<>(n);
// 0 To 4
AVNode avNode0 = new AVNode();
avNode0.setNo(0);
ArcNode arcNode04 = new ArcNode(6, 4, null);
avNode0.setFirstArcNode(arcNode04);
adjList.add(avNode0);
// 1 To 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 To 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 To 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
dfs(alGraph, 1);
}
/** * Undirected weighted graph */
public static void undirectedWeightedGraph() {
// Initialize adjacency table
int n = DFSAdjTable.n;
int e = 6;
ALGraph alGraph = new ALGraph();
alGraph.setN(n);
alGraph.setE(e);
// adjList,AVNode The vertices ,ArcNode Edge point
List<AVNode> adjList = new ArrayList<>(n);
// 0 And 1 2 4 Connected to a
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 And 0 2 Connected to a
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 And 0 1 3 Connected to a
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 And 2 4 Connected to a
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 And 0 3 Connected to a
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);
// Depth-first traversal
dfs(alGraph, 0);
}
public static void main(String[] args) {
// undirectedWeightedGraph();
directedWeightedGraph();
}
}
边栏推荐
- AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
- 中衍期货公司是国内的正规平台吗?开户安全吗?想开个期货账户
- 自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
- 中衍期货靠谱吗?是不是正规期货公司?开户安全吗?
- Vertical bar of latex tips absolute value \left\right|
- Use DAP link to download the executable file separately to the mm32f5 microcontroller
- JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation
- 蛮力法/1~n个整数中取k个整数
- Theoretical basis of distributed services
- Service management and communication, basic principle analysis
猜你喜欢

AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘

72. editing distance ●●

Pytorch deep learning -- neural network convolution layer conv2d

vulnhub-The Planets: Earth

LeetCode:497. 非重叠矩形中的随机点————中等

一、Vulkan开发理论基础知识

六级考试-商务英语-考前最后一背

Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.

Talk about server performance optimization ~ (recommended Collection)

pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
随机推荐
Niuke.com: numbers that appear more than half of the times in the array
Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
72. editing distance ●●
Meetup预告:Linkis新版本介绍以及DSS的应用实践
农产品期货开户的条件是什么?现在开户的手续费是多少?
LeetCode:1037. 有效的回旋镖————简单
Magic tower game implementation source code and level generation
LeetCode:497. 非重叠矩形中的随机点————中等
KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
^30H5 Web Worker多线程
蛮力法/1~n的幂集 v4 递归
pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
canvas 高级功能(中)
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
【电脑使用】如何设置没有自启项的软件开机启动
連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
2台电脑共享一套键盘鼠标
MySQL service startup failed
蛮力法/u到v是否存在简单路径