这个分类用来放个人对于Introduction to Algorithms, 2ed 的答案
算法导论(CLRS, 2nd) 个人答案 Ch34.1
摘要:34.1-1:Assumption: the graph is unweightedLONGEST-PATH-LENGTH ∈P=> LONGEST-PATH∈P:Suppose there's a problem solves LHS in polynomial time, gives result k; then for the problem of RHS, it returns true if the given k' <= k.LONGEST-PATH∈P:=> LONGEST-PATH-LENGTH ∈P:Guess the k to the po
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch2 end of chapter
摘要:2-1:2-2:a) loop invariant,initial condition, termination condition.b) A[j] is thesmallest element in A[j .. length[A]]. initialization: at the start of the loop, j = length[A].It's the smallest element in A[length[A] ~ length[A]] (because it'sthe only element). maintenance: if j = k is the s
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch2.3
摘要:2.3-1:2.3-2:function MERGE-IMPROVED(A,p,q,r){ create array B[0 ... r - p]; int i = p, j = q+1, t =0; while(i<=q && j<=r){ if(A[i] >= A[j]){ B[t++] = A[j++]; } else{ B[t++] = A[i++]; } } if(i>q){ // i pile exhaustedwhile(j<=r) B[t++] = A...
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch2.2
摘要:2.2-1:O(n^3)2.2-2:Selection-Sort(A): for( i=0 ; i<A.size()-1 ; i++){ max = A[i]; index = 0; for(j=i+1; j<A.size(); j++){ if(max < A[j]){ max = A[j]; index = j; } } // then swap the next max and the current element A[j] = A[i]; A[i] = max; }2.2-3:Say we're sorting nelementsWorst Case: nB
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch2.1
摘要:2.1-1:2.1-2:NON-INCREASING-INSERTION-SORT(A) forj: 1~ length(A)-1: if(A[j]< A[j-1]): key= A[j]; t = j-1; while(A[t]< key && t>=0): A[t+1]= A[t]; t--; A[t]= key; returnA;2.1-3:// Assumption: Ifthere're duplicated elements, only return// the index of the first element.LINEAR-SEARC
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch1
摘要:1.1-1Sorting: rank thestudent's according to the scoreDetermining thebest order for multiplying matrices: When doingprobability calculation, one may require lots of matrices multiplication for the cumulative probabilityresultsFinding the convexhull: 1.1-2Space1.1-3Linked list:Strength: can beuse
阅读全文
算法导论(CLRS 2ed) 个人答案 11.2
摘要:题目总体上还是不太难……只是,一个晚上都卡在第四题,做Java实现去了……up date: 最后Java实现了一个上午,终于把第四题做好了。基本思想和Solution Manual是一样的,只是我允许相同key的情况——因为我认为,即使是带着相同的key,data也有可能不同。11.2-1:11.2-2:11.2-3:For everynode, it's a linked list; therefore, by keeping the list sorted, the runningtime would change like this:OperationOriginalNew: k
阅读全文
算法导论(CLRS, 2nd) 个人答案 3.2
摘要:感觉挺难的。尤其是3.2-4, 我花了近2h做出来……还是参考了别人的解法的情况下。3.2-1:3.2-2:3.2-3:3.2-4:3.2-5:3.2-6:3.2-7:Reference:Stirling's Approximation, in Wolfman World, retrieved on 2011年3月5日14:35:08, from:http://mathworld.wolfram.com/StirlingsApproximation.htmlCLRS 2e: Exercise 3.2-4, in My Answers. BlogSpot, retrieved at 20
阅读全文
算法导论(CLRS, 2nd) 个人答案 3.1
摘要:3.1-1:3.1-2:3.1-3:It's useless because the best case don'tnormally happen.3.1-4:3.1-5:3.1-6: Same as 3.1-5;3.1-7:3.1-8:
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch11.1
摘要:11.1-1:Perform a linear search on T, worst case is O(m)11.1-2:0 stands for no elements in the corresponding key, while 1 means the key correspond to some data.11.1-3:Assumption: besides of the key, the satellite data can also have data to be mapped to a secondary key.Thus we can consider a case of 2
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch5.1
摘要:5.1-1:Since we can compare between any two elements, we know the total order on the ranks of the candidates.5.1-2:RANDOM(a,b)%range = b - a;%d = the number of binary bits %range hasdofor(%i = 1 to %d):%i th bit of integer %res = rand(0,1);while(%res > %range);return (a + %res);Each time of while
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch6.1
摘要:6.1 - 1:Define n to be the number of elements in a heap of height h, then:6.1-2:6.1-3:For any of the sub-tree of one max-heap, it maintains the basic heap property, i.e., A[parent(i)] >= A[i]. and the root of one sub-tree is the ancestor of all its nodes, therefore, the root is the largest by rec
阅读全文
算法导论 个人答案 12.2 (未完)
摘要:(今天主要解决了昨天遗留的问题。在上课的论坛上讨论还是很有效的。12.2未结)12.2-1:[代码]12.2-5:If one BST node has two children, then its predecessor is the maximum of its leftsub-tree, and its successor is the minimum of its right sub-tree. The smallestelement has no left child, and the largest one has no right child.12.2-6:Think from
阅读全文
算法导论(CLRS, 2nd) 个人答案 Ch12.1
摘要:我打算发一下自己对于算法导论书里面的个人答案。欢迎大家指正。因为读的是英文版,这里也用英文来发吧:)12.1 What is a binary search tree12.1-1:12.1-2:BSP Property: Let x be a node in BST, if y is a node in the left subtree of x, then:key[x]>= key[y]; if y is a node on the right subtree of x, then key[x]<=key[y].Min-Heap Property: for any node x,
阅读全文
|