Recall the recursive program (discussed in the class) that computes the n-th Fibonacci number. Compute the exact number of additions used by the program.
令
故
Given a tree T and k subtrees of T such that each pair of subtrees has at least one vertex in common, prove that there is at least one vertex in common to all the subtrees.
Helly property : A family
Theorem : A family of subtrees of a tree satisfies the Helly property 一棵樹的所有子樹皆滿足 Helly property
Proof:
令 A 收集的是樹 T 的所有子樹,
若有一棵子樹
我們對子樹的節點數進行歸納法。假設樹 T 有 n 個節點,每個子樹最多有 n 個節點,且滿足 Helly property。
當 T 為 n + 1 個節點的樹時,
令
令
(
根據歸納假設,
由 (1)(2) 根據歸納假設,因為
The lattice points in the plane are the points with integer coordinate. Consider a polygon that all of its vertices are lattice points. Let p be the number of lattice points that are on the boundary of the polygon, and let q be the number of lattice points that are inside the polygon. Prove that the area of the polygon is p/2+q-1.
定義小三角形為內部沒有任何 Lattice Points 的三角形。
欲計算任意簡單多邊形面積,可先將一多邊形分割成許多個小三角形,每個小三角形的面積為
令 r 為平面圖上的區域數,則多邊形內部有 r - 1 個區域,會減去 1 是代表扣掉多邊形外面的區域。
又
Given a positive integer n, find a way to partition n into one or more positive integers j1, j2, … , jk (i.e. j1 + j2 + … + jk = n) such that the product of these integers is maximized. Prove the correctness of your formula.
如果一個數拆成兩個數字相加,要使這兩個數字的乘積最大,則這兩個數字一定相等。同理,將數字 n 要拆成多個等份的 x 相加,即 x + x + … + x = n,則乘積最大值為
令
另外,6 = 3 + 3 = 2 + 2 + 2,但 3 * 3 > 2 + 2 + 2,所以每拆出三個 2 可以用二個 3 去替換。因此在分割 n 時,
首先,將 n mod 3 找出餘數是多少 :
若餘數為 0,則最大值為
若餘數為 1,將 1 和最後一個 3 組合成 4,並把 4 拆成 2, 2,因為
若餘數為 2,則最大值為
Let a1, a2, … , an be positive real numbers such that a1 x a2 x … x an = 1. Prove, without using the arithmetic versus geometric inequality, that
(1+a1)(1+a2) x … x (1+an) ≥ 2^n.
當 n = 1,
假設 n = k 時命題成立 : 若
當 n = k + 1 時,若
因此由數學歸納法可得證。
Given n ≥ 1 numbers x1, x2, …, xn, show that the function f(x) = Σ |x - xi| for i = 1 to n takes its minimum value at the median of these n number.
解法一 :
想像
假設 n = 7,你現在要開始往右邊旅行,去拜訪每個
你繼續往右走抵達
同理,抵達
抵達
抵達
抵達
抵達
抵達
由上述可觀察出,當你從最左端往右前進,距離總和 f(x) 會一直遞減,直到遇到中位數
解法二 :
令
考慮
可看出前兩項都會大於等於零,且最後一項等於零 (因為
考慮
可看出前兩項都會大於等於零,故當
同理,當
Given a set of n +1 numbers out of the first 2n natural numbers 1, 2, …, 2n, prove by mathematic induction that here are two numbers in the set, one of which divides the other.
當 n = 1 時,取 1 和 2,1 | 2,命題成立
假設當
當
因此由數學歸納法可得證。
Let d1, d2, …, dn, n ≥ 2, be positive integers. Prove that if d1 + d2 + … + dn= 2n - 2, then there exists a tree with n vertices of degrees exactly d1, d2, …, dn. Based on your proof, design an efficient algorithm to construct such a tree.
當 n = 2 時,若
假設
現在考慮此序列
考慮加入一點
由數學歸納法可得證。
Algo
定義 adj[n][n] 為儲存樹的 n * n Adjacency matrix。
Input : d1, d2, ..., dn
Output : construct a tree if there exists a tree with the given degree sequence
if d1 + d2 + … + dn = 2n - 2
for i = 1 to n
for j = i + 1 to n
if(di == 0) break // 邊數已經建完後面節點就不用管了
if(dj == 0) continue // 點 j 不能再加入邊了
adj[i][j] = adj[j][i] = 1
di = di - 1
dj = dj - 1
return adj[][]
else return "There is no such a tree"
Time :
Let G=(V, E) be a directed graph (not necessarily acyclic). Design an efficient algorithm to label the vertices of the graph with distinct labels from 1 to |V| such that the label of each vertex v is greater than the label of at least one of v’s predecessors, or determine that no such labeling is possible. (A predecessor of v is a vertex w such that wv in E.)
想法 : 找到 indegree = 0 的點,並從該點當作起點,利用 DFS 走訪整張圖,每拜訪一個點就為該點設編號。拜訪第一個點設 1 號,拜訪第二個點設 2 號,依此類推。當發現此圖有迴圈時,則不存在此種編號方式。DFS 走訪完後,檢查每個點是否都有拜訪過,如果有點沒被拜訪過代表此圖不連通,亦不存在此種編號方式。
時間 :
Give a linear-time algorithm that takes as input a tree and determines whether it has a perfect matching: a set of edges that touches each node exactly once.
想法 : 給定一樹 T,從樹葉節點 L 開始配對,因為樹葉節點的分支度為 1,所以若要配對一定得和其父節點 P 組成一對。組好一對 (L, P) 後,將樹葉節點 L 和其父節點 P 從樹中刪除。接著再繼續找下一個樹葉節點進行上述動作,直到樹 T 為空,代表此樹有 Perfect Matching。如果過程中發現樹 T 有度數為 0 的節點代表該點已經孤立了,則此樹就不會有 Perfect Matching。
演算法 :
定義一邊集合 M = ∅,收集每組配對 (i, j) 的邊
while |T|≠ ∅
若 T 有一節點 L 的度數為 0,回傳 "No matching"
否則,找一樹葉節點 L
令 p 為 L 的鄰點
將邊 (p, L) 加入邊集合 M
從樹 T 中刪除節點 p 和 L (兩點的鄰邊也一同刪除)
回傳 M
時間分析 : 一個一個刪掉樹葉節點和其鄰點,基本上就是遍歷一整棵樹,所以時間為線性時間。
Consider a variation of the towers of Hanoi. We no longer assume that all the disks are initially on one peg. They may be arbitrarily distributed among the three pegs, as long as they are ordered in decreasing sizes on each peg. The purpose remains to move all disks to one specified peg, under the same constraints as the original problem, with as few moves as possible. Design an algorithm to compute the minimal number of moves. How about the reverse of the problem?
想法 : 從最大的盤子開始思考要怎麼移動到目標柱。
令三個柱子分別為 A, B, C,總共有 n 個盤子。假設第 n 個盤子位於柱子 X,目標柱為 C。
定義 f(n, target) 為變化版河內塔,將 n 個散落於三個柱子的盤子全部移動到目標柱 target 上。
定義 g(n, P, target) 為原始版河內塔,將 n 個盤子全部移動到目標柱 target 上,過程中透過柱子 P 輔助。
Let T be an undirected tree. The diameter of T is the maximal distance over all pairs of vertices. Design an algorithm to find the diameter of the given tree.
(回學校再補)
UVa 146 ID Codes
想法 : 從最低位逐一往最高位檢查順序,要找下一個排列需盡可能增加越少越好。如果目前排序為遞增就沒辦法讓此字串變大;如果遇到某數 x 開始遞減,就從最低位開始和某數 x 進行比大小,找到比 x 大的數字 y,接著交換 x 和 y 的位置,最後將最低位到 x 的前一位由小到大擺放,則此數即為下一個排列。若一個排列從最低到最高位為遞增代表此數沒有下一個排列了。
Consider the iterative method for solving the towers of Hanoi problem described in Unit 2. Prove the correctness of the algorithm
while (1) {
將最小的盤子移動到下一個柱子 (順時鐘順序)
若所有盤子都堆在同一根柱子,即完成
最小盤不動,移動唯一可以移動的盤子
}
將上述迴圈版的演算法對於偶數的盤子成立,但奇數個盤子不成立,問題出在於將最小盤移動到下一個柱子的方式,奇數盤應該要移動到下二個柱子。所以將此演算法更精確地描述如下 :
令起始柱為 A,目標柱為 C,輔助柱為 B。
假設目前位於柱子 i,
當有奇數個盤子,下一個柱子會跳到 (i + 2) mod 3 的位置;
當有偶數個盤子,下一個柱子會跳到 (i + 1) mod 3 的位置;
當 n 為奇數,
當 n 為偶數,