一、引入
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/52276d9585f74206b423b0d0ba260e50.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/a4583ff0aba54d9583d7d35c113ff96e.png)
二、基本概念
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/42db35bf66e9453ca0afff11277d9228.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ae89a63157b24be9ba655399f41e56fc.png)
三、图的表示
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/52bf14552c6d49c9a0437cf64c4e42e5.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/bd13fb1727d24175aeab0f4046ca98ce.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/3ae16b81d12045bda3c237a60d71b205.png)
package com.gyh.grapg;
import java.util.ArrayList;
import java.util.Arrays;
public class Graph {
private ArrayList<String> vertexList;
private int[][] edges;
private int numOfEdges = 0;
public static void main(String[] args) {
int n = 5;
String[] VertexValue = {"A", "B", "C", "D", "E"};
Graph graph = new Graph(5);
for (String s : VertexValue) {
graph.insertVertex(s);
}
graph.insertEdge(0,1,1);
graph.insertEdge(0,2,1);
graph.insertEdge(1,2,1);
graph.insertEdge(1,3,1);
graph.insertEdge(1,4,1);
graph.showGraph();
}
public Graph(int n) {
this.edges = new int[n][n];
vertexList = new ArrayList<>();
}
public int getNumOfVertex() {
return vertexList.size();
}
public int getNumOfEdges() {
return numOfEdges;
}
public void showGraph() {
for (int[] edge : edges) {
System.out.println(Arrays.toString(edge));
}
}
public int getWeight(int v1, int v2) {
return edges[v1][v2];
}
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
public void insertEdge(int v1, int v2, int weight) {
if (v1 >= edges.length || v2 >= edges.length) {
throw new RuntimeException("该结点标号越界");
}
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
}
四、图的遍历
4.1 图的深度优先遍历(DFS)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e60de0a99a954d5ab85d5db4bc1244b7.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/a04caae4dea24b86a9e73b2f7afd513a.png)
4.2 图的广度优先遍历(BFS)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/bdffab65ee304374b2d1d4a68776c63f.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/233beb7444764146a95a72603ff1fbd6.png)
package com.gyh.grapg;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Graph {
private ArrayList<String> vertexList;
private int[][] edges;
private int numOfEdges = 0;
private boolean[] isVisited;
public static void main(String[] args) {
int n = 5;
String[] VertexValue = {"A", "B", "C", "D", "E"};
Graph graph = new Graph(n);
for (String s : VertexValue) {
graph.insertVertex(s);
}
graph.insertEdge(0, 1, 1);
graph.insertEdge(0, 2, 1);
graph.insertEdge(1, 2, 1);
graph.insertEdge(1, 3, 1);
graph.insertEdge(1, 4, 1);
graph.showGraph();
graph.bfs(2, new LinkedList<>());
}
public void bfs(int v, Queue<Integer> queue) {
queue.add(v);
isVisited[v] = true;
while (queue.size() > 0) {
v = queue.poll();
System.out.println(vertexList.get(v));
for (int i = 0; i < edges.length; i++) {
if (edges[v][i] != 0 && !isVisited[i]) {
queue.add(i);
isVisited[i] = true;
}
}
}
}
public void dfs(int v) {
if (v < 0 || v >= edges.length) {
return;
}
System.out.println(vertexList.get(v));
isVisited[v] = true;
for (int i = 0; i < edges.length; i++) {
if (edges[v][i] != 0 && !isVisited[i]) {
dfs(i);
}
}
}
public Graph(int n) {
this.edges = new int[n][n];
vertexList = new ArrayList<>();
isVisited = new boolean[n];
}
public int getNumOfVertex() {
return vertexList.size();
}
public int getNumOfEdges() {
return numOfEdges;
}
public void showGraph() {
for (int[] edge : edges) {
System.out.println(Arrays.toString(edge));
}
}
public int getWeight(int v1, int v2) {
return edges[v1][v2];
}
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
public void insertEdge(int v1, int v2, int weight) {
if (v1 >= edges.length || v2 >= edges.length) {
throw new RuntimeException("该结点标号越界");
}
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
}
4.3 图的深度优先 VS 广度优先
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e8138d1ed1fb402e82c9f9d53327c0b8.png)