当前位置:网站首页>蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
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();
}
}
边栏推荐
- typescript13 - type aliases
- 【网络安全】文件上传靶场通关(1-11关)
- Jetpack Compose学习(8)——State及remeber
- 24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios
- 无线模块的参数介绍和选型要点
- 分布式.幂等性
- In Google Cloud API gateway APISIX T2A and T2D performance test
- typescript12 - union types
- Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
- 剑指offer17---打印从1到最大的n位数
猜你喜欢
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
TiDB 在多点数字化零售场景下的应用
ROS Action通信
系统设计.短链系统设计
C language _ structure pointer array function voting system
Problem record in the use of TypeScript
《实战》基于情感词典的文本情感分析与LDA主题分析
ShardingSphere之垂直分库分表实战(五)
Shell变量与赋值、变量运算、特殊变量
tensorflow与GPU版本对应安装问题
随机推荐
调度中心xxl-Job
权限管理怎么做的?
Image processing tool design
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
ROS2系列知识(3):环境配置
Kyushu cloud as cloud computing standardization excellent member unit
勾股数元组 od js
ShardingSphere之公共表实战(七)
使用PageHelper实现分页查询(详细)
ShardingSphere's vertical sub-database sub-table actual combat (5)
小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
MySQL (6)
typescript17 - function optional parameters
typescript10-常用基础类型
Artificial Intelligence and Cloud Security
1782. Count the number of point pairs Double pointer
Can deep learning solve the parameters of a specific function?
Rocky/GNU之Zabbix部署(3)
太阳能板最大面积 od js
数字图像隐写术之JPEG 隐写分析