文章目錄
前言
一、爲什麼要有圖
- 前面我們學了線性表和樹
- 線性表侷限於一個直接前驅和一個直接後繼的關係
- 樹也只能有一個直接前驅也就是父節點
- 當我們需要表示多對多的關係時, 這裏我們就用到了圖
二、圖的舉例說明
圖是一種數據結構,其中結點可以具有零個或多個相鄰元素。兩個結點之間的連接稱爲邊。 結點也可以稱爲頂點。如圖:
三、圖的常用概念
-
頂點(vertex)
-
邊(edge)
-
路徑
-
無向圖(右圖)
-
有向圖
-
帶權圖
四、圖的表示方式
圖的表示方式有兩種:二維數組表示(鄰接矩陣);鏈表表示(鄰接表)。
4.1 鄰接矩陣
鄰接矩陣是表示圖形中頂點之間相鄰關係的矩陣,對於n個頂點的圖而言,矩陣是的row和col表示的是1…n個點。
4.2 鄰接表
鄰接矩陣需要爲每個頂點都分配n個邊的空間,其實有很多邊都是不存在,會造成空間的一定損失.
鄰接表的實現只關心存在的邊,不關心不存在的邊。因此沒有空間浪費,鄰接表由數組+鏈表組成
五、圖的快速入門案例
- 要求: 代碼實現如下圖結構.
- 思路分析 (1) 存儲頂點String 使用 ArrayList (2) 保存矩陣 int[][] edges
- 代碼實現
核心代碼,彙總在後面
// 插入 結點
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
// 添加邊
/**
* @param v1 表示點的下標 即第幾個定點 “A”-“B” "A"->0 "B"->1
* @param v2 第二個頂點對應的下標
* @param weight 表示關聯 , 0或者1
*/
public void insertEdge(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight; // 反向也可以
numOfEdges++;
}
六、圖的深度優先遍歷介紹
6.1 圖遍歷介紹
所謂圖的遍歷,即是對結點的訪問。一個圖有那麼多個結點,如何遍歷這些結點,需要特定策略,一般有兩種訪問策略: (1)深度優先遍歷 (2)廣度優先遍歷
6.2 深度優先遍歷基本思想
圖的深度優先搜索(Depth First Search) 。
-
深度優先遍歷,從初始訪問結點出發,初始訪問結點可能有多個鄰接結點,深度優先遍歷的策略就是首先訪問第一個鄰接結點,然後再以這個被訪問的鄰接結點作爲初始結點,訪問它的第一個鄰接結點, 可以這樣理解:每次都在訪問完當前結點後首先訪問當前結點的第一個鄰接結點。
-
我們可以看到,這樣的訪問策略是優先往縱向挖掘深入,而不是對一個結點的所有鄰接結點進行橫向訪問。
-
顯然,深度優先搜索是一個遞歸的過程
6.3 深度優先遍歷算法步驟
- 訪問初始結點v,並標記結點v爲已訪問。
- 查找結點v的第一個鄰接結點w。
- 若w存在,則繼續執行4,如果w不存在,則回到第1步,將從v的下一個結點繼續。
- 若w未被訪問,對w進行深度優先遍歷遞歸(即把w當做另一個v,然後進行步驟123)。
- 查找結點v的w鄰接結點的下一個鄰接結點,轉到步驟3。
看一個具體案例分析:
6.4 核心代碼
// 深度優先遍歷算法
// i 第一次就是 0
public void dfs(boolean isVisited[], int i) {
// 首先我們訪問該結點,輸出
System.out.print(getValueByIndex(i) + "->");
// 將結點設置爲已經訪問
isVisited[i] = true;
// 查找結點 i 的第一個鄰接結點 w (w就是下標) (i, w) 就是直接關係,前提 w 不爲 -1
int w = getFirstNeighbor(i);
while (w != -1) { // 說明 有鄰接結點
if (!isVisited[w]) { // 爲 true時,說明未被訪問。
dfs(isVisited, w); // 遞歸
}
// 如果 w 結點被訪問, 則根據前一個鄰接結點的下標 來 獲取下一個鄰接結點
w = getNextNeighbor(i, w);
}
}
// 對 dfs 進行一個重載,遍歷所有結點,並進行 dfs
public void depthFirstSearch() {
isVisited = new boolean[vertexList.size()];
// 遍歷所有結點,進行dfs(回溯)
for (int i = 0; i < getNumOfVertex(); i++) { // getNumOfVertex() 結點個數
if (!isVisited[i]) {
dfs(isVisited, i);
}
}
}
七、圖的廣度優先遍歷
7.1 廣度優先遍歷基本思想
圖的廣度優先搜索(Broad First Search) 。
類似於一個分層搜索的過程,廣度優先遍歷需要使用一個隊列以保持訪問過的結點的順序,以便按這個順序來訪問這些結點的鄰接結點
7.2廣度優先遍歷算法步驟
- 訪問初始結點v並標記結點v爲已訪問。
- 結點v入隊列
- 當隊列非空時,繼續執行,否則算法結束。
- 出隊列,取得隊頭結點u。
- 查找結點u的第一個鄰接結點w。
- 若結點u的鄰接結點w不存在,則轉到步驟3;否則循環執行以下三個步驟:
6.1 若結點w尚未被訪問,則訪問結點w並標記爲已訪問。
6.2 結點w入隊列
6.3 查找結點u的繼w鄰接結點後的下一個鄰接結點w,轉到步驟6。
廣度優先舉例說明
7.3 核心代碼
/*
* 廣度 優先遍歷的方法, 沒用遞歸
* */
// 對一個節點進行廣度優先遍歷的方法
private void bfs(boolean[] isVisited, int i) {
int u; // 表示隊列的頭結點對應下標
int w; // 鄰接結點 w
//隊列,記錄結點訪問的順序
LinkedList queue = new LinkedList();
//訪問結點,輸出結點信息
System.out.print(getValueByIndex(i) + "=>");
//標記爲已訪問
isVisited[i] = true;
//將結點加入隊列
queue.addLast(i);
while (!queue.isEmpty()) {
//取出隊列的頭結點下標
u = (Integer) queue.removeFirst();
//得到第一個鄰接結點的下標 w
w = getFirstNeighbor(u);
while (w != -1) {//找到
//是否訪問過
if (!isVisited[w]) {
System.out.print(getValueByIndex(w) + "=>");
//標記已經訪問
isVisited[w] = true;
//入隊
queue.addLast(w);
}
//以u爲前驅點(起始點),找w後面的下一個鄰結點
w = getNextNeighbor(u, w); //體現出我們的廣度優先 // 以 u 爲一行的 下一個 w 的結點
}
}
}
//遍歷所有的結點,都進行廣度優先搜索
public void broadFirstSearch() {
isVisited = new boolean[vertexList.size()];
for (int i = 0; i < getNumOfVertex(); i++) {
if (!isVisited[i]) {
bfs(isVisited, i);
}
}
}
八、圖的代碼彙總
8.1 Graph圖類
package com.feng.ch17_graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
public class Graph {
private ArrayList<String> vertexList; // 儲存定點集合
private int[][] edges; // 存儲圖對應的鄰接矩陣
private int numOfEdges;// 表示邊的數目
//定義給數組boolean[], 記錄某個結點是否被訪問
private boolean[] isVisited;
// 構造器
public Graph(int n) {
// 初始化矩陣和 vertexList
this.vertexList = new ArrayList<String>(n);
this.edges = new int[n][n];
this.numOfEdges = 0;
}
/*
* 深度 優先遍歷的方法
* */
// 得到 第一個鄰接結點 的下標 w
/**
* @param index 傳入的下標的行
* @return 如果存在就返回對應的下標,否則返回 -1
*/
public int getFirstNeighbor(int index) {
for (int j = 0; j < vertexList.size(); j++) {
if (edges[index][j] > 0) {
return j;
}
}
return -1;
}
// 根據前一個鄰接結點的下標 來 獲取下一個鄰接結點
public int getNextNeighbor(int v1, int v2) {
for (int j = v2 + 1; j < vertexList.size(); j++) {
if (edges[v1][j] > 0) {
return j;
}
}
return -1;
}
// 深度優先遍歷算法
// i 第一次就是 0
public void dfs(boolean isVisited[], int i) {
// 首先我們訪問該結點,輸出
System.out.print(getValueByIndex(i) + "->");
// 將結點設置爲已經訪問
isVisited[i] = true;
// 查找結點 i 的第一個鄰接結點 w (w就是下標) (i, w) 就是直接關係,前提 w 不爲 -1
int w = getFirstNeighbor(i);
while (w != -1) { // 說明 有鄰接結點
if (!isVisited[w]) { // 爲 true時,說明未被訪問。
dfs(isVisited, w); // 遞歸
}
// 如果 w 結點被訪問, 則根據前一個鄰接結點的下標 來 獲取下一個鄰接結點
w = getNextNeighbor(i, w);
}
}
// 對 dfs 進行一個重載,遍歷所有結點,並進行 dfs
public void depthFirstSearch() {
isVisited = new boolean[vertexList.size()];
// 遍歷所有結點,進行dfs(回溯)
for (int i = 0; i < getNumOfVertex(); i++) { // getNumOfVertex() 結點個數
if (!isVisited[i]) {
dfs(isVisited, i);
}
}
}
/*
* 廣度 優先遍歷的方法, 沒用遞歸
* */
// 對一個節點進行廣度優先遍歷的方法
private void bfs(boolean[] isVisited, int i) {
int u; // 表示隊列的頭結點對應下標
int w; // 鄰接結點 w
//隊列,記錄結點訪問的順序
LinkedList queue = new LinkedList();
//訪問結點,輸出結點信息
System.out.print(getValueByIndex(i) + "=>");
//標記爲已訪問
isVisited[i] = true;
//將結點加入隊列
queue.addLast(i);
while (!queue.isEmpty()) {
//取出隊列的頭結點下標
u = (Integer) queue.removeFirst();
//得到第一個鄰接結點的下標 w
w = getFirstNeighbor(u);
while (w != -1) {//找到
//是否訪問過
if (!isVisited[w]) {
System.out.print(getValueByIndex(w) + "=>");
//標記已經訪問
isVisited[w] = true;
//入隊
queue.addLast(w);
}
//以u爲前驅點(起始點),找w後面的下一個鄰結點
w = getNextNeighbor(u, w); //體現出我們的廣度優先 // 以 u 爲一行的 下一個 w 的結點
}
}
}
//遍歷所有的結點,都進行廣度優先搜索
public void broadFirstSearch() {
isVisited = new boolean[vertexList.size()];
for (int i = 0; i < getNumOfVertex(); i++) {
if (!isVisited[i]) {
bfs(isVisited, i);
}
}
}
/*
* 圖中常用方法
* */
// 返回結點的個數
public int getNumOfVertex() {
return vertexList.size();
}
// 得到的邊數目
// getNumOfEdges() 方法
// 返回結點 i (下標) 對應的數據 0->"A" 1->"B" 2->"C"
public String getValueByIndex(int i) {
return vertexList.get(i);
}
// 返回v1 和 v2 的權值
public int getWeight(int v1, int v2) {
return edges[v1][v2];
}
// 顯示圖對應的矩陣
public void show() {
for (int[] link : edges) {
System.out.println(Arrays.toString(link));
}
}
// 插入 結點
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
// 添加邊
/**
* @param v1 表示點的下標 即第幾個定點 “A”-“B” "A"->0 "B"->1
* @param v2 第二個頂點對應的下標
* @param weight 表示關聯 , 0或者1
*/
public void insertEdge(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight; // 反向也可以
numOfEdges++;
}
public ArrayList<String> getVertexList() {
return vertexList;
}
public void setVertexList(ArrayList<String> vertexList) {
this.vertexList = vertexList;
}
public int[][] getEdges() {
return edges;
}
public void setEdges(int[][] edges) {
this.edges = edges;
}
public int getNumOfEdges() {
return numOfEdges;
}
public void setNumOfEdges(int numOfEdges) {
this.numOfEdges = numOfEdges;
}
}
8.2 GraphMain測試類
package com.feng.ch17_graph;
public class GraphMain {
public static void main(String[] args) {
// 測試 圖是否創建
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);
}
// 添加邊
// ab ac bc bd be
// graph.insertEdge(0, 1, 1); // ab
// graph.insertEdge(0, 2, 1);
// graph.insertEdge(1, 2, 1);
// graph.insertEdge(1, 3, 1);
// graph.insertEdge(1, 4, 1);
graph.insertEdge(0, 1, 1);
graph.insertEdge(0, 2, 1);
graph.insertEdge(1, 3, 1);
graph.insertEdge(1, 4, 1);
graph.insertEdge(3, 7, 1);
graph.insertEdge(4, 7, 1);
graph.insertEdge(2, 5, 1);
graph.insertEdge(2, 6, 1);
graph.insertEdge(5, 6, 1);
// 顯示鄰接矩陣
graph.show();
System.out.println();
// 深度遍歷
System.out.println("深度優先遍歷");
graph.depthFirstSearch(); // A->B->C->D->E-> 1->2->4->8->5->3->6->7->
System.out.println();
System.out.println();
// 廣度遍歷
System.out.println("廣度優先遍歷");
graph.broadFirstSearch(); // A=>B=>C=>D=>E=> 1=>2=>3=>4=>5=>6=>7=>8=>
}
}