Add the section of Graph Traversal.

This commit is contained in:
krahets
2023-02-15 03:22:57 +08:00
parent c74f8293b9
commit dc441928d9
31 changed files with 383 additions and 54 deletions

View File

@ -7,19 +7,13 @@
package chapter_graph;
import java.util.*;
/* 顶点类 */
class Vertex {
int val;
public Vertex(int val) {
this.val = val;
}
}
import include.*;
/* 基于邻接表实现的无向图类 */
class GraphAdjList {
// 请注意vertices 和 adjList 中存储的都是 Vertex 对象
Map<Vertex, Set<Vertex>> adjList; // 邻接表(使用哈希表实现)
// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
// 请注意adjList 中的元素是 Vertex 对象
Map<Vertex, List<Vertex>> adjList;
/* 构造方法 */
public GraphAdjList(Vertex[][] edges) {
@ -59,26 +53,26 @@ class GraphAdjList {
public void addVertex(Vertex vet) {
if (adjList.containsKey(vet))
return;
// 在邻接表中添加一个新链表(即 HashSet
adjList.put(vet, new HashSet<>());
// 在邻接表中添加一个新链表
adjList.put(vet, new ArrayList<>());
}
/* 删除顶点 */
public void removeVertex(Vertex vet) {
if (!adjList.containsKey(vet))
throw new IllegalArgumentException();
// 在邻接表中删除顶点 vet 对应的链表(即 HashSet
// 在邻接表中删除顶点 vet 对应的链表
adjList.remove(vet);
// 遍历其它顶点的链表(即 HashSet,删除所有包含 vet 的边
for (Set<Vertex> set : adjList.values()) {
set.remove(vet);
// 遍历其它顶点的链表,删除所有包含 vet 的边
for (List<Vertex> list : adjList.values()) {
list.remove(vet);
}
}
/* 打印邻接表 */
public void print() {
System.out.println("邻接表 =");
for (Map.Entry<Vertex, Set<Vertex>> entry : adjList.entrySet()) {
for (Map.Entry<Vertex, List<Vertex>> entry : adjList.entrySet()) {
List<Integer> tmp = new ArrayList<>();
for (Vertex vertex : entry.getValue())
tmp.add(vertex.val);
@ -90,25 +84,21 @@ class GraphAdjList {
public class graph_adjacency_list {
public static void main(String[] args) {
/* 初始化无向图 */
Vertex v0 = new Vertex(1),
v1 = new Vertex(3),
v2 = new Vertex(2),
v3 = new Vertex(5),
v4 = new Vertex(4);
Vertex[][] edges = { { v0, v1 }, { v1, v2 }, { v2, v3 }, { v0, v3 }, { v2, v4 }, { v3, v4 } };
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[2], v[3] }, { v[2], v[4] }, { v[3], v[4] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化后图为");
graph.print();
/* 添加边 */
// 顶点 1, 2 即 v0, v2
graph.addEdge(v0, v2);
// 顶点 1, 2 即 v[0], v[2]
graph.addEdge(v[0], v[2]);
System.out.println("\n添加边 1-2 后,图为");
graph.print();
/* 删除边 */
// 顶点 1, 3 即 v0, v1
graph.removeEdge(v0, v1);
// 顶点 1, 3 即 v[0], v[1]
graph.removeEdge(v[0], v[1]);
System.out.println("\n删除边 1-3 后,图为");
graph.print();
@ -119,8 +109,8 @@ public class graph_adjacency_list {
graph.print();
/* 删除顶点 */
// 顶点 3 即 v1
graph.removeVertex(v1);
// 顶点 3 即 v[1]
graph.removeVertex(v[1]);
System.out.println("\n删除顶点 3 后,图为");
graph.print();
}

View File

@ -100,7 +100,7 @@ public class graph_adjacency_matrix {
/* 初始化无向图 */
// 请注意edges 元素代表顶点索引,即对应 vertices 元素索引
int[] vertices = { 1, 3, 2, 5, 4 };
int[][] edges = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 0, 3 }, { 2, 4 }, { 3, 4 } };
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 4 } };
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
System.out.println("\n初始化后图为");
graph.print();

View File

@ -0,0 +1,53 @@
/**
* File: graph_bfs.java
* Created Time: 2023-02-12
* Author: Krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import include.*;
public class graph_bfs {
/* 广度优先遍历 BFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new ArrayList<>();
// 哈希表,用于记录已被访问过的顶点
Set<Vertex> visited = new HashSet<>() {{ add(startVet); }};
// 队列用于实现 BFS
Queue<Vertex> que = new LinkedList<>() {{ offer(startVet); }};
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (!que.isEmpty()) {
Vertex vet = que.poll(); // 队首顶点出队
res.add(vet); // 记录访问顶点
// 遍历该顶点的所有邻接顶点
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // 跳过已被访问过的顶点
que.offer(adjVet); // 只入队未访问的顶点
visited.add(adjVet); // 标记该顶点已被访问
}
}
// 返回顶点遍历序列
return res;
}
public static void main(String[] args) {
/* 初始化无向图 */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
{ v[2], v[5] }, { v[3], v[4] }, { v[3], v[6] }, { v[4], v[5] },
{ v[4], v[7] }, { v[5], v[8] }, { v[6], v[7] }, { v[7], v[8] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化后图为");
graph.print();
/* 广度优先遍历 BFS */
List<Vertex> res = graphBFS(graph, v[0]);
System.out.println("\n广度优先遍历BFS顶点序列为");
System.out.println(Vertex.vetsToVals(res));
}
}

View File

@ -0,0 +1,51 @@
/**
* File: graph_dfs.java
* Created Time: 2023-02-12
* Author: Krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import include.*;
public class graph_dfs {
/* 深度优先遍历 DFS 辅助函数 */
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
res.add(vet); // 记录访问顶点
visited.add(vet); // 标记该顶点已被访问
// 遍历该顶点的所有邻接顶点
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // 跳过已被访问过的顶点
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet);
}
}
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new ArrayList<>();
// 哈希表,用于记录已被访问过的顶点
Set<Vertex> visited = new HashSet<>();
dfs(graph, visited, res, startVet);
return res;
}
public static void main(String[] args) {
/* 初始化无向图 */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化后图为");
graph.print();
/* 深度优先遍历 BFS */
List<Vertex> res = graphDFS(graph, v[0]);
System.out.println("\n深度优先遍历DFS顶点序列为");
System.out.println(Vertex.vetsToVals(res));
}
}