当前位置:网站首页>蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
2022-07-31 01:25:00 【xcrj】
思路
- 队列结构 先进先出 对头出 队尾入
- 循环队列:
- 入队:rear=(rear+1)%que.length;
- 出队:front=(front+1)%que.length;
- 队列空:front!=rear
- 队列满:front=(rear+1)%arraySize
- while出队列{while广度}
代码
顶点
package xcrj.kchalgorithm.graphAlgorithm.graph;
/** * 点 */
public class MVertex {
// 顶点编号,edge[][]邻接矩阵 数组下标
private int no;
// 顶点信息,城市A,城市B
private char data;
public MVertex() {
}
public MVertex(int no, char data) {
this.no = no;
this.data = data;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public char getData() {
return data;
}
public void setData(char data) {
this.data = data;
}
}
邻接矩阵
package xcrj.kchalgorithm.graphAlgorithm.graph;
/** * 邻接矩阵存储图 */
public class MGraph {
// 顶点数量
private int n;
// 边数量
private int e;
// 顶点们
private MVertex[] vertices;
// 边们
private int[][] edges;
public MGraph() {
}
public MGraph(int n, int e, MVertex[] vertices, int[][] edges) {
this.n = n;
this.e = e;
this.vertices = vertices;
this.edges = edges;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getE() {
return e;
}
public void setE(int e) {
this.e = e;
}
public MVertex[] getVertices() {
return vertices;
}
public void setVertices(MVertex[] vertices) {
this.vertices = vertices;
}
public int[][] getEdges() {
return edges;
}
public void setEdges(int[][] edges) {
this.edges = edges;
}
}
bfs
package xcrj.kchalgorithm.bruteForce.graph;
/** * 广度优先遍历,以邻接矩阵为存储结构 */
public class BFSAdjMatrix {
/** * 队列结构 先进先出 对头出 队尾入 * 循环队列: * 入队:rear=(rear+1)%que.length; * 出队:front=(front+1)%que.length; * 队列空:front!=rear * 队列满:front=(rear+1)%arraySize * <p> * while出队列{while广度} * * @param g 邻居矩阵 * @param v 起始点 */
public static void bfs(MGraph g, int v) {
// 记录已经访问的点
int[] visited = new int[g.getN()];
// 创建循环
int[] que = new int[g.getN()];
// 指向队头的前1个元素
int front = 0;
// 指向队尾元素
int rear = 0;
// 入队v访问
rear = (rear + 1) % que.length;
que[rear] = v;
visited[v] = 1;
System.out.println(v + "-");
// while出队列,队列非空
while (front != rear) {
// 出队
front = (front + 1) % que.length;
int a = que[front];
// while广度,出度看行, 1行有g.getN()个点
int[] as = g.getEdges()[a];
for (int i = 0; i < g.getN(); i++) {
if (0 == visited[i] && as[i] != 0) {
// 入队访问
int b = i;
rear = (rear + 1) % g.getN();
que[rear] = b;
visited[b] = 1;
System.out.println("权重" + as[i] + "-" + b + ',');
}
}
System.out.println();
}
}
/** * 无方向带权图 */
public static void undirectedWeightedGraph() {
int n = DFSAdjMatrix.n;
int e = 6;
MVertex[] mVertices = new MVertex[n];
for (int i = 0; i < n; i++) {
mVertices[i] = new MVertex(i, (char) i);
}
int[][] edges = new int[n][n];
edges[0][4] = 6;
edges[4][0] = 6;
edges[0][1] = 9;
edges[1][0] = 9;
edges[1][2] = 3;
edges[2][1] = 3;
edges[0][2] = 2;
edges[2][0] = 2;
edges[2][3] = 5;
edges[3][2] = 5;
edges[3][4] = 1;
edges[4][3] = 1;
MGraph mGraph = new MGraph(n, e, mVertices, edges);
// 无方向带权值连通图
bfs(mGraph, 1);
}
/** * 有方向带权图 */
public static void directedWeightedGraph() {
int n = DFSAdjMatrix.n;
int e = 6;
MVertex[] mVertices = new MVertex[n];
for (int i = 0; i < n; i++) {
mVertices[i] = new MVertex(i, (char) i);
}
int[][] edges = new int[n][n];
edges[0][4] = 6;
edges[1][0] = 9;
edges[1][2] = 3;
edges[2][0] = 2;
edges[2][3] = 5;
edges[3][4] = 1;
MGraph mGraph = new MGraph(n, e, mVertices, edges);
// 有方向带权值连通图
bfs(mGraph, 1);
}
public static void main(String[] args) {
directedWeightedGraph();
// undirectedWeightedGraph();
}
}
边栏推荐
- typescript11-数据类型
- typescript16-void
- 24. 请你谈谈单例模式的优缺点,注意事项,使用场景
- TiDB之rawkv升级之路v5.0.4--&gt;v6.1.0
- 小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
- Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
- Bert usage and word prediction based on Keras_bert model
- typescript9 - common base types
- Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
- "Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
猜你喜欢
手把手教你配置Jenkins自动化邮件通知
使用docker安装mysql
ROS Action通信
typescript13 - type aliases
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
数字图像隐写术之JPEG 隐写分析
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
JS逆向之浏览器补环境(一)
JPEG Steganalysis of Digital Image Steganography
typescript16-void
随机推荐
35. 反转链表
link与@import的区别
设置浏览器滚动条样式
Jetpack Compose学习(8)——State及remeber
Parameter introduction and selection points of wireless module
typescript18-对象类型
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
1782. Count the number of point pairs Double pointer
typescript15-(同时指定参数和返回值类型)
87. 把字符串转换成整数
C语言_结构体指针数组函数选票系统
This project is so geeky
斩获BAT、TMD技术专家Offer,我都经历了什么?
Teach you how to configure Jenkins automated email notifications
typescript10-常用基础类型
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
ShardingSphere之水平分库实战(四)
typescript14-(单独指定参数和返回值的类型)
Why use high-defense CDN when financial, government and enterprises are attacked?