图是一种数据结构,其中结点可以具有零个或多个相邻元素。两个结点之间的连接称为边。 结点也可以称为
顶点。
图的表示方式有两种:二维数组表示(邻接矩阵);链表表示(邻接表)。
邻接矩阵
邻接矩阵是表示图形中顶点之间相邻关系的矩阵,对于 n 个顶点的图而言,矩阵是的 row 和 col 表示的是 1…n 个点。
邻接表
代码实现如下图结构.
思路:存储顶点 String 使用 ArrayList (2) 保存矩阵 int[][] edges存储顶点 String 使用 ArrayList (2) 保存矩阵 int[][] edges
所谓图的遍历,即是对结点的访问。一个图有那么多个结点,如何遍历这些结点,需要特定策略,一般有两种
访问策略: (1)深度优先遍历 (2)广度优先遍历
图的深度优先搜索(Depth First Search) 。
//深度优先遍历方法
public void dfs(boolean[] isVisted, int i) {
// 首先输出该节点
System.out.print(getValueByindex(i) + "->");
// 将该节点设置为已经访问过
isVisted[i] = true;
//查找节点i 的第一个邻结节点
int w = getFirstNeighbor(i);
while (w != -1) {
if (!isVisted[w]) {
dfs(isVisted, w);
}
// 如果w节点已经被访问过了,那么我
w = getNexttNeighbor(i, w);
}
}
//对dfs进行重载,遍历我们所有的节点并且进行dfs
public void dfs() {
for (int i = 0; i < getNumOFVertex(); i++) {
if (!isVisted[i]) {
dfs(isVisted, i);
}
}
}
广度优先遍历基本思想:
//对一个节点进行广度优先搜索遍历
public void bfs(boolean[] isVisted, int i) {
//表示队列的头节点的对应下标
int u;
//邻节点w
int w;
//模拟队列记录节点访问的顺序
LinkedList<Object> queue = new LinkedList<>();
//输出节点信息
System.out.print(getValueByindex(i) + "->");
// 标记为已访问
isVisted[i] = true;
// 将节点加入队列
queue.addLast(i);
//判断只要非空就一直找
while (!queue.isEmpty()) {
// 取出队列头节点下标
u = (Integer) queue.removeFirst();
w = getFirstNeighbor(u);
while (w != -1) {
// 是否访问过
if (!isVisted[w]) {
System.out.print(getValueByindex(w) + "->");
// 标记已经访问
isVisted[w] = true;
// 入队
queue.addLast(w);
}
// 如果访问过 以u 为前驱点 找w后面的第一个节点
w = getNexttNeighbor(u, w);//体现出广度优先
}
}
}
//遍历所有的节点都进行广度优先搜索
public void bfs() {
for (int i = 0; i < getNumOFVertex(); i++) {
if (!isVisted[i]) {
bfs(isVisted, i);
}
}
}
package com.hyc.DataStructure.garph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* @projectName: DataStructure
* @package: com.hyc.DataStructure.garph
* @className: Graph
* @author: 冷环渊 doomwatcher
* @description: TODO
* @date: 2022/2/22 17:52
* @version: 1.0
*/
public class Graph {
//存储顶点结合
private ArrayList<String> vertexList;
//存储图对应的邻结矩阵
private int[][] edges;
//表示边的数目
private int numOFEdges;
private boolean[] isVisted;
public static void main(String[] args) {
//测试一把图是否创建ok
int n = 8; //结点的个数
//String Vertexs[] = {"A", "B", "C", "D", "E"};
String Vertexs[] = {"1", "2", "3", "4", "5", "6", "7", "8"};
//创建图对象
Graph graph = new Graph(n);
//循环的添加顶点
for (String vertex : Vertexs) {
graph.insertVertex(vertex);
}
//添加边
//A-B A-C B-C B-D B-E
// graph.insertEdge(0, 1, 1); // A-B
// graph.insertEdge(0, 2, 1); //
// graph.insertEdge(1, 2, 1); //
// graph.insertEdge(1, 3, 1); //
// graph.insertEdge(1, 4, 1); //
//更新边的关系
graph.insertEdges(0, 1, 1);
graph.insertEdges(0, 2, 1);
graph.insertEdges(1, 3, 1);
graph.insertEdges(1, 4, 1);
graph.insertEdges(3, 7, 1);
graph.insertEdges(4, 7, 1);
graph.insertEdges(2, 5, 1);
graph.insertEdges(2, 6, 1);
graph.insertEdges(5, 6, 1);
//显示 邻结矩阵
graph.showGarph();
测试深度遍历
System.out.println("深度遍历");
graph.dfs();
System.out.println();
//测试广度优先搜索
//System.out.println("广度遍历");
//graph.bfs();
}
//构造器
public Graph(int n) {
// 初始化矩阵和VertexList
edges = new int[n][n];
vertexList = new ArrayList<String>(n);
numOFEdges = 0;
isVisted = new boolean[n];
}
/**
* @author 冷环渊 Doomwatcher
* @context: 得到第一个邻节点的下标
* @date: 2022/2/22 18:22
* @param index 如果存在就是返回对应的下标 否则返回-1
* @return: int
*/
public int getFirstNeighbor(int index) {
for (int j = 0; j < vertexList.size(); j++) {
if (edges[index][j] > 0) {
return j;
}
}
return -1;
}
public int getNexttNeighbor(int v1, int v2) {
for (int j = v2 + 1; j < vertexList.size(); j++) {
if (edges[v1][j] > 0) {
return j;
}
}
return -1;
}
//深度优先遍历方法
public void dfs(boolean[] isVisted, int i) {
// 首先输出该节点
System.out.print(getValueByindex(i) + "->");
// 将该节点设置为已经访问过
isVisted[i] = true;
//查找节点i 的第一个邻结节点
int w = getFirstNeighbor(i);
while (w != -1) {
if (!isVisted[w]) {
dfs(isVisted, w);
}
// 如果w节点已经被访问过了,那么我
w = getNexttNeighbor(i, w);
}
}
//对dfs进行重载,遍历我们所有的节点并且进行dfs
public void dfs() {
for (int i = 0; i < getNumOFVertex(); i++) {
if (!isVisted[i]) {
dfs(isVisted, i);
}
}
}
//对一个节点进行广度优先搜索遍历
public void bfs(boolean[] isVisted, int i) {
//表示队列的头节点的对应下标
int u;
//邻节点w
int w;
//模拟队列记录节点访问的顺序
LinkedList<Object> queue = new LinkedList<>();
//输出节点信息
System.out.print(getValueByindex(i) + "->");
// 标记为已访问
isVisted[i] = true;
// 将节点加入队列
queue.addLast(i);
//判断只要非空就一直找
while (!queue.isEmpty()) {
// 取出队列头节点下标
u = (Integer) queue.removeFirst();
w = getFirstNeighbor(u);
while (w != -1) {
// 是否访问过
if (!isVisted[w]) {
System.out.print(getValueByindex(w) + "->");
// 标记已经访问
isVisted[w] = true;
// 入队
queue.addLast(w);
}
// 如果访问过 以u 为前驱点 找w后面的第一个节点
w = getNexttNeighbor(u, w);//体现出广度优先
}
}
}
//遍历所有的节点都进行广度优先搜索
public void bfs() {
for (int i = 0; i < getNumOFVertex(); i++) {
if (!isVisted[i]) {
bfs(isVisted, i);
}
}
}
//图中常用的方法
//返回节点的数目
public int getNumOFVertex() {
return vertexList.size();
}
//返回节点i 对应的下标数据
public String getValueByindex(int i) {
return vertexList.get(i);
}
//返回v1和v2的权值
public int getWeight(int v1, int v2) {
return edges[v1][v2];
}
//显示矩阵
public void showGarph() {
for (int[] edge : edges) {
System.out.println(Arrays.toString(edge));
}
}
// 插入顶点
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
/**
* @author 冷环渊 Doomwatcher
* @context: 添加边
* @date: 2022/2/22 18:01
* @param v1 表示点的下标 即使 第几个顶点 a-b a ->0 b->1
* @param v2 和v1同理是第二个顶点的下标
* @param weight 表示矩阵里面用什么来表示他们是关连的 0 表示没有连接 1 表示连接了
* @return: void
*/
public void insertEdges(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOFEdges++;
}
}