当前位置:网站首页>Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
2022-07-31 01:52: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) {
// Record the points that have been visited
int[] visited = new int[g.getN()];
// 创建循环
int[] que = new int[g.getN()];
// point to the front1个元素
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);
// Undirected connected graph with weights
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();
}
}
边栏推荐
猜你喜欢
MySQL installation tutorial (detailed, package teaching package~)
软件测试要达到一个什么水平才能找到一份9K的工作?
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
关于Redis相关内容的基础学习
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
Manchester City confuses fans with smart scarf that detects emotions
观察者(observer)模式(一)
类似 MS Project 的项目管理工具有哪些
What are the project management tools like MS Project
Can an inexperienced college graduate switch to software testing?my real case
随机推荐
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
[1153]mysql中between的边界范围
leetcode-399: division evaluation
充电效果模拟
Fiddler captures packets to simulate weak network environment testing
CV-Model【3】:MobileNet v2
The PC side determines the type of browser currently in use
What have I experienced when I won the offer of BAT and TMD technical experts?
两个有序数组间相加和的Topk问题
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
ShardingJDBC usage summary
二层广播风暴(产生原因+判断+解决)
VSCode插件:嵌套注释
JPEG Steganalysis of Digital Image Steganography
打印任务排序 js od华为
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
[1153] The boundary range of between in mysql
Crypto Life, a day in the life of a Web3 project partner
Simple confession page
来自一位女测试工程师的内心独白...