2-1 圖的分類
圖是一個用線 或 邊連接在一起的頂點的集合,可以說,圖是有限 頂點V 和 邊E 的有序對。頂點(Vertex),邊(Edge)
圖a中的邊沒有方向,稱爲無向圖。圖b中邊存在方向稱爲有向圖。
上圖a中:所示的無向網可表示爲 G1(V, E),其中頂點集合 V(G1) = { 1, 2, 3, 4, 5, 6, 7 };
所以依據圖的有無方向和權值可以分爲4類:
1.無向無權圖
2.有向無權圖
3.無向有權圖
4.有向有權圖
2-2 圖的基本概念
頂點的度(degree):對於無向圖來說,一個頂點的度就是這個頂點的相鄰的邊的數量。如第一張圖a中點1的度就是 2 。
簡單圖:沒有自環邊,沒有平行邊
子圖:例如,圖 1.8(a)、(b)所示的無向圖都是圖 1.1(a)所示的無向圖 G1的子圖
聯通圖和非聯通圖:
樹是一種無環圖,任意結點都可以看做是根節點。聯通的無環圖是樹。
2-3 圖的基本表示:鄰接矩陣
練習的是簡單圖,不包含自環邊和平行邊
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
//只是處理簡單的圖
public class AdjMatrix {
private int V; // 圖的頂點的數量
private int E; // 圖的邊的數量
private int[][] adj; // 鄰接矩陣
public AdjMatrix(String filename) {
File file = new File(filename);
try {
// 讀取文件
Scanner scanner = new Scanner(file);
V = scanner.nextInt();
// 判斷頂點數量是否有誤
if (V < 0) throw new IllegalArgumentException("V 必須是個不爲負數的數值");
adj = new int[V][V]; // 創建二維矩陣
E = scanner.nextInt();
if (V < 0) throw new IllegalArgumentException("E 必須是個不爲負數的數值");
for (int i = 0; i< E; i++) {
int a = scanner.nextInt();
validateVertex(a);
int b = scanner.nextInt();
validateVertex(b);
// 判斷是否是自環邊
if (a == b) throw new IllegalArgumentException("不允許存在自環邊");
if (adj[a][b] == 1) throw new IllegalArgumentException("不允許存在平行邊");
adj[a][b] = 1;
adj[b][a] = 1;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void validateVertex(int v) {
if (v < 0 || v > V) {
throw new IllegalArgumentException("輸入的數值" + v +"不合法");
}
}
// 獲取指定結點相鄰的結點
public ArrayList<Integer> adj(int v){
validateVertex(v);
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < V; i++) { // 頂點的數量
if (adj[v][i] == 1) {
res.add(i);
}
}
return res;
}
// 獲取指定結點的度,即相鄰的結點的數量
public int degree(int v) {
return adj(v).size();
}
public int V() {
return V;
}
public int E() {
return E;
}
public boolean hasEdge(int x, int y) { // 依據兩個頂點判斷邊是否存在
validateVertex(x);
validateVertex(y);
return adj[x][y] == 1;
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("V = %d, E = %d \n", V, E));
// 打印出矩陣
for (int i =0; i< V; i++) {
for (int j = 0; j < V; j++) {
stringBuilder.append(String.format("%d ", adj[i][j]));
}
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
public static void main(String[] args) {
AdjMatrix adjMatrix = new AdjMatrix("g.txt");
System.out.println(adjMatrix);
// V = 7, E = 9
// 0 1 0 1 0 0 0
// 1 0 1 0 0 0 1
// 0 1 0 1 0 1 0
// 1 0 1 0 1 0 0
// 0 0 0 1 0 1 0
// 0 0 1 0 1 0 1
// 0 1 0 0 0 1 0
System.out.println(adjMatrix.adj(2).toString());
System.out.println(adjMatrix.degree(2));
}
}
2-4 圖的基本表示:鄰接表
2-6 鄰接表的實現
2-7 鄰接表的問題和改進
2-8 實現鄰接表的改進
2-9 圖的基本表示的比較
待更新。。。。。。。。。。。。。