![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
文章平均质量分 60
萤火虫之暮
这个作者很懒,什么都没留下…
展开
-
哈夫曼编码的非树节点形式实现
哈夫曼编码的非树节点形式实现楔子思考过程于是想自己写一个headq构建二叉树实在太久了,完全不让看文档,不敢不相信在有限的时间里可以调试成功,于是就想了使用非树的实现方式,就是把手动画的二叉树,从树叶往上补充哈夫曼编码总结楔子今日心血来潮参加了某厂家的机试,牛客网上机试,一看只有一题且时间90分钟200分,允许使用本地IDE,就知道肯定几分钟出不来,看题目可喜的是秒懂哈夫曼编码,可悲的是一年半...原创 2020-04-18 20:50:34 · 386 阅读 · 0 评论 -
list(map(lambda x:x[:-1], g.readlines())) 理解
首先,理解map,#map(函数,可以迭代的对象,让元素统一操作)ret = map(lambda x: x + 100 if x%2==1 else x, [11, 22, 33, 44])print ret[111, 22, 133, 44]遍历逗号后面的list 然后理解:list(map(lambda x:x[:-1], g.readlines()))...原创 2018-09-17 12:48:27 · 2028 阅读 · 2 评论 -
Tensorflow保存模型,恢复模型
tensorflow从已经训练好的模型中,恢复(指定)权重(构建新变量、网络)并继续训练(finetuning)https://blog.csdn.net/ying86615791/article/details/76215363 Tensorflow保存模型,恢复模型,使用训练好的模型进行预测和提取中间输出(特征)https://blog.csdn.net/ying8661579...转载 2018-10-13 09:52:41 · 141 阅读 · 0 评论 -
ValueError: Dimensions must be equal
Error code:File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl raise ValueError(err.message)ValueError: Dimensions m...原创 2018-09-29 10:17:43 · 5869 阅读 · 0 评论 -
Iterative Quick Sort
Iterative Quick SortFollowing is an iterative implementation of the above recursive code from geekforgeek:# Python program for implementation of Quicksort # This function is same in both iterative ...原创 2018-10-28 11:21:47 · 170 阅读 · 0 评论 -
Morris traversal for Preorder
Morris traversal for PreorderMorris traversal for PreorderFollowing is the implementation of the above algorithm.Limitations:Using Morris Traversal, we can traverse the tree without using stack and r...转载 2018-10-25 15:56:21 · 240 阅读 · 0 评论 -
Morris traversal for Inorder
Inorder Tree Traversal without recursion and without stack!Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary...转载 2018-10-25 16:10:16 · 338 阅读 · 0 评论 -
Iterative Preorder Traversal
Iterative Preorder TraversalGiven a Binary Tree, write an iterative function to print Preorder traversal of the given binary tree.Refer this for recursive preorder traversal of Binary Tree. To conve...转载 2018-10-25 16:19:13 · 292 阅读 · 0 评论 -
Tree Traversals (Inorder, Preorder and Postorder) In Recursive way
Tree Traversals (Inorder, Preorder and Postorder)Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in di...转载 2018-10-25 16:34:49 · 358 阅读 · 1 评论 -
Inorder Tree Traversal without Recursion
Inorder Tree Traversal without RecursionUsing Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step ex...转载 2018-10-25 16:52:44 · 210 阅读 · 0 评论 -
Merge Sort with Recursion and Iteration
Merge SortFollowing is a typical recursive implementation of Merge Sort: def mergeSorting(self,arry): def two_part_sort(left,right): result = [] i,j = 0,0...原创 2018-10-28 23:09:08 · 241 阅读 · 0 评论 -
Binary Tree Traverasl with OO and Stack
Binary Tree Traverasl with OOB and Stack1、一切都是对象,对象就是关系和操作;每个节点,都有两种操作,print和visit。2、放在stack里面可以使操作,可以是函数,递归也是面向对象的体现;Python代码实现如下:class guide(object): def __init__(self, opt,node): s...原创 2018-10-25 22:51:10 · 114 阅读 · 0 评论 -
Insertion Sort
Insertion SortInsertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.Algorithm// Sort an arr[] of size ninsertionSort(arr, n)Loop from i = 1 to n-1.……...转载 2018-10-26 11:19:54 · 421 阅读 · 0 评论 -
Selection Sort
Selection SortThe selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm mai...转载 2018-10-26 11:51:24 · 142 阅读 · 0 评论 -
Bubble Sort
Bubble SortBubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.Example:First Pass:( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), He...转载 2018-10-26 12:20:44 · 707 阅读 · 0 评论 -
QuickSort
QuickSortLike Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickS...转载 2018-10-26 15:26:02 · 438 阅读 · 0 评论 -
ShellSort
ShellSortShellSort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The id...转载 2018-10-29 18:06:32 · 372 阅读 · 0 评论 -
(python的坑,坑的我头晕,下行循环写后根遍历)
总是提示越界错误IndexError: list index out of range### if s and s[-1].left == t:需要先判断s是否为空,假如:if s[-1].left == t and s就会出现越界。## 边界条件的非常非常重要!!!!## 代码如下:```pythonclass Solution(object): def posto...原创 2018-10-24 18:23:16 · 230 阅读 · 0 评论 -
HeapSort
HeapSortHeap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at...转载 2018-10-30 13:23:09 · 446 阅读 · 0 评论 -
图的遍历
图的遍历图的遍历是图的操作算法中最基本也是最重要的方法,与树的遍历相似,这里也分为深度优先遍历和宽度优先遍历,通过深度优先遍历得到的顶点序列称为该图的深度优先序列(Depth-First Search,DFS序列),通过宽度优先遍历得到的顶点序列称为该图的宽度优先序列(Breadth-First Search,BFS序列)Breadth-First Searchdef BFS(graph,s...原创 2018-11-06 13:37:39 · 137 阅读 · 1 评论 -
裘宗燕-数据结构与算法python描述-ppt及源代码
免费共享https://pan.baidu.com/s/1__aqzzGDNBGgPIhuErv-8w原创 2018-11-06 13:44:57 · 3721 阅读 · 2 评论 -
Backpropagation 总结
参照代码理解,比较直观: # These are the weights between the input layer and the hidden layer. self.weights_0_1 = np.zeros((self.input_nodes,self.hidden_nodes)) # These are the weight...原创 2018-11-02 13:30:41 · 340 阅读 · 1 评论 -
Dijkstra的理解和实现
Dijkstra理解:1、单点按照距离递增辐射出去;2、辐射到的点会被放进优先级队列,距离远的点可能会先放到队列里;3、距离近的,可能会后放进队列,但是肯定会先出去;4、每一个放进优先级队列的点,需要记录:自己,自己的前继节点,自己到起点的距离,会被用到的。容易的误解不是简单的每次走最近的点,是逐步(按照已确定的点不断传染辐射方式,保证最近的点一定在候选区)添加点到候选区,选出距离起点...原创 2018-11-07 12:28:23 · 187 阅读 · 0 评论 -
动态规划:斐波那契数列里面的东西?
斐波那契数列我想每个人都会写斐波那契数列吧!!斐波那契数列的定义f(0) = 1,f(1) = 1,f(n) = f(n-1) + f(n-2)基于递归的方式实现,讲到递归都会用到:def fib_sequence(n): def fib_rec(n): if n == 0 or n ==1: return n return...原创 2018-11-10 14:32:27 · 135 阅读 · 0 评论 -
图的实现基本总结
1、采取邻接矩阵方式实现:infinity = float("inf")class AdjGraphError(TypeError): passclass Graph: # basic graph class, using adjacent matrix def __init__(self, mat, unconn = 0): vnum1 = len(ma...原创 2018-11-05 15:28:16 · 223 阅读 · 0 评论 -
01背包问题从简单到复杂
题目:有 N 件物品和一个容量为 V 的背包。放入第 i 件物品耗费的费用是 C i 1 ,得到的价值是 W i 。求解将哪些物品装入背包可使价值总和最大。状态转移方程:F [i, v] = max {F [i − 1, v], F [i − 1, v − C i ] + W i }基于递归的实现:def pack_0_1_Rec(N,V,C,W): if N == 0 or ...原创 2018-11-15 17:13:36 · 275 阅读 · 0 评论 -
动态规划,从例子到理解
如何设计动态规划?1、问题建模:得到优化的目标函数,约束条件?2、划分子问题:明确问题的规模在哪里?哪些维度的规模,这样才能才会划分子问题,注意边界。3、得到递推方程:可以宏观的考虑,也可以借助于微观的归纳演绎。4、然后检测是否满足最优子结构:检查子问题对其子问题的始、终点也是最优的序列。5、最小问题的界定,确定初值。如何编码动态规划?一般得到了递推方程,就可以写出基于递归的代码实...原创 2018-11-12 16:27:52 · 351 阅读 · 0 评论 -
floyd算法和动态规划
楔子long long ago就已经知道了Floyd算法,关键代码就4行,也容易记住,上上周又看到了Floyd,都说是动态规划,所以特意去学了一圈动态规划,今天终于又回到了它状态方程:d[k][i][j]定义:“只能使用第1号到第k号点作为中间媒介时,点i到点j之间的最短路径长度。”在动态规划算法中,处于首要位置、且也是核心理念之一的就是状态的定义这个大家喜欢把它叫做“松弛操作”,也就是...原创 2018-11-20 16:52:15 · 1075 阅读 · 0 评论 -
完全背包问题从简单到复杂
题目有 N 种物品和一个容量为 V 的背包,每种物品都有无限件可用。放入第 i 种物品的费用是 C i ,价值是 W i 。求解:将哪些物品装入背包,可使这些物品的耗费的费用总和不超过背包容量,且价值总和最大。第一种思路,基于投资问题模型从每种物品的角度考虑,与它相关的策略已并非取或不取两种,而是有取 0 件、取 1 件、取 2 件…直至取 ⌊V /C i ⌋ 件等许多种。状态方程为:F...原创 2018-11-16 12:25:52 · 525 阅读 · 0 评论 -
Bellman-Ford 算法 和 动态规划
Floyd算法:状态:d[k][i][j]定义:“只能使用第1号到第k号点作为中间媒介时,点i到点j之间的最短路径长度。”动态转移方程:d[k][i][j] = min(d[k-1][i][j], d[k-1][i][k]+d[k-1][k][j])(k,i,j∈[1,n])Bellman-Ford 算法:状态:d[k][u]定义:从源点v出发最多经过不构成负权值回路的k条边到达终点...原创 2018-11-21 14:06:22 · 2085 阅读 · 0 评论 -
Dijkstra和动态规划
有人说Dijkstra也是动态规划。它不是贪心吗?怎么变成动态规划了,是动态规划的话,那么就有状态,有状态方程。将图中的顶点分成2个部分,已知最短路径的顶点集合U,不知最短路径的集合V-U问题规模:就是U里面顶点个数状态:已知最短路径长度:状态方程如下:如果v 在 U中:cdis[v] = dis[v]如果v和U中某点u直连:cdis[v] =min(dis(u) + w(u,v))...原创 2018-11-21 16:38:12 · 5400 阅读 · 0 评论 -
回溯法解决01背包问题
回溯法问题的关键在于如何定义问题的解空间,转化成树(即解空间树)。只要是解可以描述成向量方式的,就可以使用回溯法,不断的扩张部分向量,向下深入的过程就是,从部分解到可行解的过程。解必须满足多米诺性质:可行解满足约束–>部分解满足约束逆否为:部分解不满足约束–>可行解不满足约束这样可以用来pruning。最简单的回溯,解空间为二叉树,又叫子集树递归方式实现的框架如下:v...原创 2018-11-22 17:09:33 · 4573 阅读 · 0 评论 -
多重背包从简单到复杂
题目有 N 种物品和一个容量为 V 的背包。第 i 种物品最多有 M i 件可用,每件耗费的空间是 C i ,价值是 W i 。求解将哪些物品装入背包可使这些物品的耗费的空间总和不超过背包容量,且价值总和最大。状态转移方程:F [i , v] = max {F [i − 1, v − k ∗ C i ] + k ∗ W i | 0 ≤ k ≤ M i }复杂度是 O(V ΣM i ) 。...原创 2018-11-17 12:40:45 · 165 阅读 · 0 评论 -
混合背包问题,和多重背包问题一样的处理
问题如果将三种背包问题混合起来。也就是说,有的物品只可以取一次(01背包),有的物品可以取无限次(完全背包),有的物品可以取的次数有一个上限(多重背包)。应该怎么求解呢?转化为01背包问题,对的就是和多重背包一模一样,min(V // C[i],M[i])包含了3中情况:def change_multiple_to_01(N,V,C,W,M): C_ =[] W_ =[] ...原创 2018-11-17 12:51:00 · 147 阅读 · 0 评论 -
背包问题追踪解
混合背包问题通用处理如下,我们以通用状态方式处理解空间的追踪:def pack_01_and_complete_and_multiple_Bottom_up(N,V,C,W,M): list = np.zeros((N+1,V+1),dtype=int) for i in range(1,N+1): for j in range(0,V+1): ...原创 2018-11-17 15:39:45 · 1150 阅读 · 0 评论 -
背包问题求第K优解
以01背包为例:首先看01背包求最优解的状态转移方程: F [i, v] = max {F [i − 1, v], F [i −1, v − C i ] + W i } 。如果要求第 K 优解,那么状态 F [i, v] 就应该是一个大小为 K 的队列 F [i, v, 1 . . . K] 。其中 F [i, v, k] 表示前 i 个物品中,背包大小为 v 时,第 k 优解的值。这里也可以...原创 2018-11-17 17:28:12 · 661 阅读 · 2 评论 -
最长公共子序列和追踪解
题目:LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.实现起来比较简单,状态方程:LCS F[i,v] = (max{F[i-1,v],F[i,v-1]},F[i-1,...原创 2018-11-18 14:52:43 · 296 阅读 · 0 评论 -
01背包问题:回溯法和限界分支、递归和迭代方式
01背包问题递归方式模板:void backtrack(int t){ if(t > n) output(x); else{ for(int i = f(n,t); i <= g(n,t);i++){ x[t] = h(i); if(constraint(t) && bound(t...原创 2018-11-25 16:19:36 · 2447 阅读 · 1 评论 -
限界分支法:01背包问题,优先级队列(包含解的追踪)
前面提到:不知道大家注意到没有?上述实现方式没有使用单位体积价值的排序,和之前提到01背包回溯法基于单位体积价值实现不一样(先装单位体积价值高的)。我们网上经常看到都是基于以上实现的,到底这个用有什么好处了?实际上基于排序的单位体积价值是一个非常精确的限界函数基于优先级队列的实现方式,就需要用到以上的结构:优先级队列方式需要数据预处理,为什么需要预处理数据了,这里使用的是优先级队列,这个优...原创 2018-12-02 17:03:04 · 1832 阅读 · 0 评论 -
回溯法:批量作业调度
问题描述给定 n 个作业的集合 j = {j1, j2, …, jn}。每一个作业 j[i] 都有两项任务分别在两台机器上完成。每一个作业必须先由机器1 处理,然后由机器2处理。作业 j[i] 需要机器 j 的处理时间为 t[j][i] ,其中i = 1, 2, …, n, j = 1, 2。对于一个确定的作业调度,设F[j][i]是作业 i 在机器 j 上的完成处理的时间。所有作业在机器2上完...原创 2018-11-27 16:53:02 · 1567 阅读 · 0 评论