mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Update graph codes
This commit is contained in:
		@ -14,8 +14,9 @@ struct Vertex {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 基于邻接表实现的无向图类 */
 | 
					/* 基于邻接表实现的无向图类 */
 | 
				
			||||||
class GraphAdjList {
 | 
					class GraphAdjList {
 | 
				
			||||||
    // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
					    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
				
			||||||
    unordered_map<Vertex*, unordered_set<Vertex*>> adjList;  // 邻接表(使用哈希表实现)
 | 
					    // 请注意,adjList 中的元素是 Vertex 对象
 | 
				
			||||||
 | 
					    unordered_map<Vertex*, unordered_set<Vertex*>> adjList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    /* 构造方法 */
 | 
					    /* 构造方法 */
 | 
				
			||||||
@ -52,7 +53,7 @@ public:
 | 
				
			|||||||
    /* 添加顶点 */
 | 
					    /* 添加顶点 */
 | 
				
			||||||
    void addVertex(Vertex* vet) {
 | 
					    void addVertex(Vertex* vet) {
 | 
				
			||||||
        if (adjList.count(vet)) return;
 | 
					        if (adjList.count(vet)) return;
 | 
				
			||||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
					        // 在邻接表中添加一个新链表
 | 
				
			||||||
        adjList[vet] = unordered_set<Vertex*>();
 | 
					        adjList[vet] = unordered_set<Vertex*>();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -60,9 +61,9 @@ public:
 | 
				
			|||||||
    void removeVertex(Vertex* vet) {
 | 
					    void removeVertex(Vertex* vet) {
 | 
				
			||||||
        if (!adjList.count(vet))
 | 
					        if (!adjList.count(vet))
 | 
				
			||||||
            throw invalid_argument("不存在顶点");
 | 
					            throw invalid_argument("不存在顶点");
 | 
				
			||||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
					        // 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
        adjList.erase(vet);
 | 
					        adjList.erase(vet);
 | 
				
			||||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
					        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
        for (auto& [key, set_] : adjList) {
 | 
					        for (auto& [key, set_] : adjList) {
 | 
				
			||||||
            set_.erase(vet);
 | 
					            set_.erase(vet);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -24,8 +24,8 @@ func newVertex(val int) vertex {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 基于邻接表实现的无向图类 */
 | 
					/* 基于邻接表实现的无向图类 */
 | 
				
			||||||
type graphAdjList struct {
 | 
					type graphAdjList struct {
 | 
				
			||||||
	// 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
						// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
				
			||||||
	// 邻接表(使用哈希表实现), 使用哈希表模拟集合
 | 
						// 请注意,adjList 中的元素是 Vertex 对象
 | 
				
			||||||
	adjList map[vertex]map[vertex]struct{}
 | 
						adjList map[vertex]map[vertex]struct{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -78,7 +78,7 @@ func (g *graphAdjList) addVertex(vet vertex) {
 | 
				
			|||||||
	if ok {
 | 
						if ok {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// 在邻接表中添加一个新链表(即 set)
 | 
						// 在邻接表中添加一个新链表
 | 
				
			||||||
	g.adjList[vet] = make(map[vertex]struct{})
 | 
						g.adjList[vet] = make(map[vertex]struct{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -90,7 +90,7 @@ func (g *graphAdjList) removeVertex(vet vertex) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// 在邻接表中删除顶点 vet 对应的链表
 | 
						// 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
	delete(g.adjList, vet)
 | 
						delete(g.adjList, vet)
 | 
				
			||||||
	// 遍历其它顶点的链表(即 Set),删除所有包含 vet 的边
 | 
						// 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
	for _, set := range g.adjList {
 | 
						for _, set := range g.adjList {
 | 
				
			||||||
		// 操作
 | 
							// 操作
 | 
				
			||||||
		delete(set, vet)
 | 
							delete(set, vet)
 | 
				
			||||||
 | 
				
			|||||||
@ -1,70 +0,0 @@
 | 
				
			|||||||
/**
 | 
					 | 
				
			||||||
 * File: graph_adjacency_list.java
 | 
					 | 
				
			||||||
 * Created Time: 2023-02-12
 | 
					 | 
				
			||||||
 * Author: Krahets (krahets@163.com)
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
package chapter_graph;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.*;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
public class graph_traversal {
 | 
					 | 
				
			||||||
    /* 以顶点 vet 为起点,对图 graph 执行广度优先遍历 */
 | 
					 | 
				
			||||||
    // 采用 GraphAdjList 表示图,以方便获取指定结点的所有邻接结点
 | 
					 | 
				
			||||||
    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); }};
 | 
					 | 
				
			||||||
        // 循环直至访问完所有顶点
 | 
					 | 
				
			||||||
        while (!que.isEmpty()) {
 | 
					 | 
				
			||||||
            Vertex vet = que.poll(); // 队首顶点出队
 | 
					 | 
				
			||||||
            res.add(vet);            // 访问该顶点
 | 
					 | 
				
			||||||
            // 遍历该顶点的所有邻接顶点
 | 
					 | 
				
			||||||
            for (Vertex adjVet : graph.adjList.get(vet)) {
 | 
					 | 
				
			||||||
                if (!visited.contains(adjVet)) {
 | 
					 | 
				
			||||||
                    que.offer(adjVet);   // 只入队未访问的顶点
 | 
					 | 
				
			||||||
                    visited.add(adjVet); // 标记该顶点已被访问
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // 返回顶点遍历序列
 | 
					 | 
				
			||||||
        return res;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* 输入值列表 vals ,返回顶点列表 vets */
 | 
					 | 
				
			||||||
    static Vertex[] valsToVets(int[] vals) {
 | 
					 | 
				
			||||||
        Vertex[] vets = new Vertex[vals.length];
 | 
					 | 
				
			||||||
        for (int i = 0; i < vals.length; i++) {
 | 
					 | 
				
			||||||
            vets[i] = new Vertex(vals[i]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return vets;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* 输入顶点列表 vets ,返回值列表 vals */
 | 
					 | 
				
			||||||
    static List<Integer> vetsToVals(List<Vertex> vets) {
 | 
					 | 
				
			||||||
        List<Integer> vals = new ArrayList<>();
 | 
					 | 
				
			||||||
        for (Vertex vet : vets) {
 | 
					 | 
				
			||||||
            vals.add(vet.val);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return vals;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static void main(String[] args) {
 | 
					 | 
				
			||||||
        /* 初始化无向图 */
 | 
					 | 
				
			||||||
        Vertex[] v = 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(vetsToVals(res));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -14,7 +14,10 @@ class Vertex {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 基于邻接表实现的无向图类 */
 | 
					/* 基于邻接表实现的无向图类 */
 | 
				
			||||||
class GraphAdjList {
 | 
					class GraphAdjList {
 | 
				
			||||||
 | 
					    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
				
			||||||
 | 
					    // 请注意,adjList 中的元素是 Vertex 对象
 | 
				
			||||||
    adjList;
 | 
					    adjList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 构造方法 */
 | 
					    /* 构造方法 */
 | 
				
			||||||
    constructor(edges) {
 | 
					    constructor(edges) {
 | 
				
			||||||
        this.adjList = new Map();
 | 
					        this.adjList = new Map();
 | 
				
			||||||
@ -54,7 +57,7 @@ class GraphAdjList {
 | 
				
			|||||||
    /* 添加顶点 */
 | 
					    /* 添加顶点 */
 | 
				
			||||||
    addVertex(vet) {
 | 
					    addVertex(vet) {
 | 
				
			||||||
        if (this.adjList.has(vet)) return;
 | 
					        if (this.adjList.has(vet)) return;
 | 
				
			||||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
					        // 在邻接表中添加一个新链表
 | 
				
			||||||
        this.adjList.set(vet, new Set());
 | 
					        this.adjList.set(vet, new Set());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -63,9 +66,9 @@ class GraphAdjList {
 | 
				
			|||||||
        if (!this.adjList.has(vet)) {
 | 
					        if (!this.adjList.has(vet)) {
 | 
				
			||||||
            throw new Error("Illegal Argument Exception");
 | 
					            throw new Error("Illegal Argument Exception");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
					        // 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
        this.adjList.delete(vet);
 | 
					        this.adjList.delete(vet);
 | 
				
			||||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
					        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
        for (let set of this.adjList.values()) {
 | 
					        for (let set of this.adjList.values()) {
 | 
				
			||||||
            set.delete(vet);
 | 
					            set.delete(vet);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -23,9 +23,11 @@ class Vertex: Hashable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 基于邻接表实现的无向图类 */
 | 
					/* 基于邻接表实现的无向图类 */
 | 
				
			||||||
class GraphAdjList {
 | 
					class GraphAdjList {
 | 
				
			||||||
    // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象
 | 
					    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
				
			||||||
    private var adjList: [Vertex: Set<Vertex>] // 邻接表(使用哈希表实现)
 | 
					    // 请注意,adjList 中的元素是 Vertex 对象
 | 
				
			||||||
 | 
					    private var adjList: [Vertex: Set<Vertex>]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 构造方法 */
 | 
				
			||||||
    init(edges: [[Vertex]]) {
 | 
					    init(edges: [[Vertex]]) {
 | 
				
			||||||
        adjList = [:]
 | 
					        adjList = [:]
 | 
				
			||||||
        // 添加所有顶点和边
 | 
					        // 添加所有顶点和边
 | 
				
			||||||
@ -66,7 +68,7 @@ class GraphAdjList {
 | 
				
			|||||||
        if adjList[vet] != nil {
 | 
					        if adjList[vet] != nil {
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
					        // 在邻接表中添加一个新链表
 | 
				
			||||||
        adjList[vet] = []
 | 
					        adjList[vet] = []
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -75,9 +77,9 @@ class GraphAdjList {
 | 
				
			|||||||
        if adjList[vet] == nil {
 | 
					        if adjList[vet] == nil {
 | 
				
			||||||
            fatalError("参数错误")
 | 
					            fatalError("参数错误")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
					        // 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
        adjList.removeValue(forKey: vet)
 | 
					        adjList.removeValue(forKey: vet)
 | 
				
			||||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
					        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
        for key in adjList.keys {
 | 
					        for key in adjList.keys {
 | 
				
			||||||
            adjList[key]?.remove(vet)
 | 
					            adjList[key]?.remove(vet)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,10 @@ class Vertex {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 基于邻接表实现的无向图类 */
 | 
					/* 基于邻接表实现的无向图类 */
 | 
				
			||||||
class GraphAdjList {
 | 
					class GraphAdjList {
 | 
				
			||||||
 | 
					    // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率
 | 
				
			||||||
 | 
					    // 请注意,adjList 中的元素是 Vertex 对象
 | 
				
			||||||
    adjList: Map<Vertex, Set<Vertex>>;
 | 
					    adjList: Map<Vertex, Set<Vertex>>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 构造方法 */
 | 
					    /* 构造方法 */
 | 
				
			||||||
    constructor(edges: Vertex[][]) {
 | 
					    constructor(edges: Vertex[][]) {
 | 
				
			||||||
        this.adjList = new Map();
 | 
					        this.adjList = new Map();
 | 
				
			||||||
@ -54,7 +57,7 @@ class GraphAdjList {
 | 
				
			|||||||
    /* 添加顶点 */
 | 
					    /* 添加顶点 */
 | 
				
			||||||
    addVertex(vet: Vertex): void {
 | 
					    addVertex(vet: Vertex): void {
 | 
				
			||||||
        if (this.adjList.has(vet)) return;
 | 
					        if (this.adjList.has(vet)) return;
 | 
				
			||||||
        // 在邻接表中添加一个新链表(即 HashSet)
 | 
					        // 在邻接表中添加一个新链表
 | 
				
			||||||
        this.adjList.set(vet, new Set());
 | 
					        this.adjList.set(vet, new Set());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -63,9 +66,9 @@ class GraphAdjList {
 | 
				
			|||||||
        if (!this.adjList.has(vet)) {
 | 
					        if (!this.adjList.has(vet)) {
 | 
				
			||||||
            throw new Error("Illegal Argument Exception");
 | 
					            throw new Error("Illegal Argument Exception");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // 在邻接表中删除顶点 vet 对应的链表(即 HashSet)
 | 
					        // 在邻接表中删除顶点 vet 对应的链表
 | 
				
			||||||
        this.adjList.delete(vet);
 | 
					        this.adjList.delete(vet);
 | 
				
			||||||
        // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边
 | 
					        // 遍历其它顶点的链表,删除所有包含 vet 的边
 | 
				
			||||||
        for (let set of this.adjList.values()) {
 | 
					        for (let set of this.adjList.values()) {
 | 
				
			||||||
            set.delete(vet);
 | 
					            set.delete(vet);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user