From 73c8920c6bd19e4a526ff65f4589c6a3c062eebe Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 23 Feb 2023 20:23:49 +0800 Subject: [PATCH] Add python code of Heap and Graph to docs. --- .../chapter_graph/graph_adjacency_list.py | 2 +- .../chapter_graph/graph_adjacency_matrix.py | 3 +- codes/python/chapter_heap/my_heap.py | 28 +++++------ docs/chapter_graph/graph_traversal.md | 4 +- docs/chapter_heap/heap.md | 48 +++++++++++++++++-- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/codes/python/chapter_graph/graph_adjacency_list.py b/codes/python/chapter_graph/graph_adjacency_list.py index eed5f98d8..5a82ce7b1 100644 --- a/codes/python/chapter_graph/graph_adjacency_list.py +++ b/codes/python/chapter_graph/graph_adjacency_list.py @@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * -""" 基于邻接表实现的无向图类 """ +""" 基于邻接表实现的无向图类 """ class GraphAdjList: # 邻接表,key: 顶点,value:该顶点的所有邻接结点 adj_list = {} diff --git a/codes/python/chapter_graph/graph_adjacency_matrix.py b/codes/python/chapter_graph/graph_adjacency_matrix.py index 00af6455b..d55a21a27 100644 --- a/codes/python/chapter_graph/graph_adjacency_matrix.py +++ b/codes/python/chapter_graph/graph_adjacency_matrix.py @@ -8,6 +8,7 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * + """ 基于邻接矩阵实现的无向图类 """ class GraphAdjMat: # 顶点列表,元素代表“顶点值”,索引代表“顶点索引” @@ -75,7 +76,7 @@ class GraphAdjMat: self.adj_mat[i][j] = 0 self.adj_mat[j][i] = 0 - # 打印邻接矩阵 + """ 打印邻接矩阵 """ def print(self): print("顶点列表 =", self.vertices) print("邻接矩阵 =") diff --git a/codes/python/chapter_heap/my_heap.py b/codes/python/chapter_heap/my_heap.py index f7adc508d..777e7fcf6 100644 --- a/codes/python/chapter_heap/my_heap.py +++ b/codes/python/chapter_heap/my_heap.py @@ -8,9 +8,9 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * -# 大顶堆 +""" 大顶堆 """ class MaxHeap: - # 使用列表而非数组,这样无需考虑扩容问题 + """ 构造方法 """ def __init__(self, nums: List[int]): # 将列表元素原封不动添加进堆 self.max_heap = nums @@ -18,43 +18,43 @@ class MaxHeap: for i in range(self.parent(self.size() - 1), -1, -1): self.sift_down(i) - # 获取左子结点索引 + """ 获取左子结点索引 """ def left(self, i: int) -> int: return 2 * i + 1 - # 获取右子结点索引 + """ 获取右子结点索引 """ def right(self, i: int) -> int: return 2 * i + 2 - # 获取父结点索引 + """ 获取父结点索引 """ def parent(self, i: int) -> int: return (i - 1) // 2 # 向下整除 - # 交换元素 + """ 交换元素 """ def swap(self, i: int, j: int): a, b = self.max_heap[i], self.max_heap[j] self.max_heap[i], self.max_heap[j] = b, a - # 获取堆大小 + """ 获取堆大小 """ def size(self) -> int: return len(self.max_heap) - # 判断堆是否为空 + """ 判断堆是否为空 """ def is_empty(self) -> bool: return self.size() == 0 - # 访问堆顶元素 + """ 访问堆顶元素 """ def peek(self) -> int: return self.max_heap[0] - # 元素入堆 + """ 元素入堆 """ def push(self, val: int): # 添加结点 self.max_heap.append(val) # 从底至顶堆化 self.sift_up(self.size() - 1) - # 从结点 i 开始,从底至顶堆化 + """ 从结点 i 开始,从底至顶堆化 """ def sift_up(self, i: int): while True: # 获取结点 i 的父结点 @@ -67,7 +67,7 @@ class MaxHeap: # 循环向上堆化 i = p - # 元素出堆 + """ 元素出堆 """ def poll(self) -> int: # 判空处理 assert not self.is_empty() @@ -80,7 +80,7 @@ class MaxHeap: # 返回堆顶元素 return val - # 从结点 i 开始,从顶至底堆化 + """ 从结点 i 开始,从顶至底堆化 """ def sift_down(self, i: int): while True: # 判断结点 i, l, r 中值最大的结点,记为 ma @@ -97,7 +97,7 @@ class MaxHeap: # 循环向下堆化 i = ma - # 打印堆(二叉树) + """ 打印堆(二叉树) """ def print(self): print_heap(self.max_heap) diff --git a/docs/chapter_graph/graph_traversal.md b/docs/chapter_graph/graph_traversal.md index ec7c5e4c3..815b6676b 100644 --- a/docs/chapter_graph/graph_traversal.md +++ b/docs/chapter_graph/graph_traversal.md @@ -43,7 +43,7 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质, === "Python" ```python title="graph_bfs.py" - + [class]{}-[func]{graph_bfs} ``` === "Go" @@ -160,7 +160,9 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质, === "Python" ```python title="graph_dfs.py" + [class]{}-[func]{dfs} + [class]{}-[func]{graph_dfs} ``` === "Go" diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 76ffe15e2..09f9a15a9 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -123,7 +123,41 @@ comments: true === "Python" ```python title="heap.py" - + # 初始化小顶堆 + min_heap, flag = [], 1 + # 初始化大顶堆 + max_heap, flag = [], -1 + + # Python 的 heapq 模块默认实现小顶堆 + # 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆 + # 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆 + """ 元素入堆 """ + heapq.heappush(max_heap, flag * 1) + heapq.heappush(max_heap, flag * 3) + heapq.heappush(max_heap, flag * 2) + heapq.heappush(max_heap, flag * 5) + heapq.heappush(max_heap, flag * 4) + + """ 获取堆顶元素 """ + peek = flag * max_heap[0] # 5 + + """ 堆顶元素出堆 """ + # 出堆元素会形成一个从大到小的序列 + val = flag * heapq.heappop(max_heap) # 5 + val = flag * heapq.heappop(max_heap) # 4 + val = flag * heapq.heappop(max_heap) # 3 + val = flag * heapq.heappop(max_heap) # 2 + val = flag * heapq.heappop(max_heap) # 1 + + """ 获取堆大小 """ + size = len(max_heap) + + """ 判断堆是否为空 """ + is_empty = not max_heap + + """ 输入列表并建堆 """ + min_heap = [1, 3, 2, 5, 4] + heapq.heapify(min_heap) ``` === "Go" @@ -311,7 +345,11 @@ comments: true === "Python" ```python title="my_heap.py" + [class]{MaxHeap}-[func]{left} + [class]{MaxHeap}-[func]{right} + + [class]{MaxHeap}-[func]{parent} ``` === "Go" @@ -403,7 +441,7 @@ comments: true === "Python" ```python title="my_heap.py" - + [class]{MaxHeap}-[func]{peek} ``` === "Go" @@ -493,7 +531,9 @@ comments: true === "Python" ```python title="my_heap.py" + [class]{MaxHeap}-[func]{push} + [class]{MaxHeap}-[func]{sift_up} ``` === "Go" @@ -613,7 +653,9 @@ comments: true === "Python" ```python title="my_heap.py" + [class]{MaxHeap}-[func]{poll} + [class]{MaxHeap}-[func]{sift_down} ``` === "Go" @@ -693,7 +735,7 @@ comments: true === "Python" ```python title="my_heap.py" - + [class]{MaxHeap}-[func]{__init__} ``` === "Go"