- 图的基本介绍:
-
图的代码实现:
-
-
package com.model.graph; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/17 15:16 * 演示图的实现,图的快速入门 */ public class GraphDemo01 { public static void main(String[] args) { Graph graph = new Graph(5); graph.insertVertex("A"); graph.insertVertex("B"); graph.insertVertex("C"); graph.insertVertex("D"); graph.insertVertex("E"); graph.addEdges(0, 1, 1); graph.addEdges(0, 2, 1); graph.addEdges(1, 2, 1); graph.addEdges(1, 3, 1); graph.addEdges(1, 4, 1); graph.showGraph(); } } class Graph{ private List<String> vertexList=null;//保存节点的list private int[][] edges=null;//保存边的矩阵 private int numEdges=0;//边的数目 public Graph(int n) { vertexList=new ArrayList<>(n); edges=new int[n][n]; numEdges=0; } // 保存节点的值,第一个节点的名字 public void insertVertex(String vertex){ vertexList.add(vertex); } // 添加边 public void addEdges(int v1,int v2,int weight){ edges[v1][v2]=weight; edges[v2][v1]=weight; numEdges++; } public int getNumOfVertex(){ return vertexList.size(); } public int getNumEdges(){ return numEdges; } // 返回节点的值,名字 public String getVertex(int index){ return vertexList.get(index); } public int getWeight(int v1,int v2){ return edges[v1][v2]; } public void showGraph(){ for (int[] arr:edges){ System.out.println(Arrays.toString(arr)); } } }
-
-
图的遍历-深度优先(DFS):
-
public void dfs(){ for (int i = 0; i < getNumEdges(); i++) { if (!isVisited[i]){ dfs(isVisited, i); } } } // 深度优先算法 private void dfs(boolean[] isVisited, int index) { isVisited[index] = true; System.out.print(vertexList.get(index)+"->"); int neighbor = getFirstNeighbor(index); while (neighbor != -1) { if (!isVisited[neighbor]) { dfs(isVisited, neighbor); } neighbor = getNextNeighbor(index, neighbor); } }
-
图的遍历-广度优先算法(BFS):
-
// 广度优先遍历算法 public void bfs(){ isVisited = new boolean[getNumOfVertex()]; for (int i = 0; i < isVisited.length; i++) { if (!isVisited[i]){ bfs(isVisited,i); } } } // 广度优先遍历算法 public void bfs(boolean[] isVisited, int index){ int u; //队列的头节点的小标 int w;//邻接节点 LinkedList queue=new LinkedList();//记录节点的访问顺序 System.out.print(getVertex(index)+"->"); isVisited[index]=true; queue.addLast(index); while(!queue.isEmpty()){ u= (int) queue.removeFirst(); w=getFirstNeighbor(u); while (w!=-1){ if (!isVisited[w]){ System.out.print(getVertex(w)+"->"); isVisited[w]=true; queue.addLast(w); }else { w=getNextNeighbor(u, w); } } } }
- 深度优先算法和广度优先算法的比较;
-
Graph graph1 = new Graph(8); graph1.insertVertex("1"); graph1.insertVertex("2"); graph1.insertVertex("3"); graph1.insertVertex("4"); graph1.insertVertex("5"); graph1.insertVertex("6"); graph1.insertVertex("7"); graph1.insertVertex("8"); graph1.addEdges(0, 1, 1); graph1.addEdges(0, 2, 1); graph1.addEdges(1, 3, 1); graph1.addEdges(1, 4, 1); graph1.addEdges(3, 7, 1); graph1.addEdges(4, 7, 1); graph1.addEdges(2, 5, 1); graph1.addEdges(2, 6, 1); graph1.addEdges(5, 6, 1); graph1.showGraph(); System.out.println("深度优先算法"); graph1.dfs(); System.out.println(); System.out.println("广度优先算法"); graph1.bfs();
-
相关文章
- 02-11Python3求解字符串滤值与百元买百鸡算法
- 02-11Python 中 plt 画柱状图和折线图
- 02-11用KMP算法实现strStr()
- 02-11LeetCode Implement strStr()(Sunday算法)
- 02-11VueEasySlider - 基于 Vue.js 的简洁开源轮播图组件
- 02-11matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)
- 02-11UML指南系列——活动图
- 02-11UML建模之活动图介绍(Activity Diagram)
- 02-11****** 五十 ******、软设笔记【UML分析和意义】-类图、对象图、状态图、活动图、顺序图、协作图、构件图、部署图,动静态模式
- 02-11制作H5像一个div中一张长图,里边是一条一条信息,需要点击的响应式方法