一、Data Structures (50%)
1. (15%)

We define common friends between node A and node B are the common neighbors of node A and node B. For example, in the above graph the common friends between node 7 and node 10 are node 2 and node 3. Assuming the neighbor list of each node is given, please write a function NumCommonFriend(node A, node B) to return the number of common friends between node A and B in the given graph. Note that you should optimize your code by reducing the number of operations.
參考解答:
int NumCommonFriend(node A, node B)
{
int arr[13], counter = 0;
for (int i = 0; i < 13; i++)
arr[i] = 0;
for each element in A.neighborList:
arr[element - 1] = 1;
for each element in B.neighborList:
if (arr[element - 1])
counter++;
return counter;
}
2. (15%) Insert a sequence of values 100, 99, 98, 97, 96, 70, 90, 72, 89 into an empty AVL tree. You will experience x times LL, y times LR, z times RL, and w times RR. Please find (x, y, z, w).
參考解答:(3, 1, 0, 1)
3. (20%) Symmetric Min-Max Heap (SMMH) is defined as a complete binary tree, which supports the implementation of double-ended priority queue. The insert, delete-min, and delete-max are three major operations with complexity It satisfies the following properties: (1) Root contains no data. (2) Left sibling data Right sibling data (3) If node A has grandparent X, the data in the left child of the node X is equal to or smaller than the data in X (4) If node A has grandparent X, the data in the right child of the node X is equal to or larger than the data in X. Build an SMMH by inserting elements 13, 25, 18, 30, 40, 50, 22, 16, 19, 96 in order. If SMMH is implemented with the array data structure T[] starting with index 1, which represents the root node. Please show T[3] in the final SMMH (5%). If we apply delete-min, please show T[4] in the resulted SMMH (5%). Instead, if we apply delete-max to the original SMMH, please show T[5] in the final SMMH (5%). Please present another two data structures which also support the implementation of double-ended priority queue. (5%)
參考解答:(1) 96 (2) 18 (3) 25 (4) Min-Max Heap, Double-Ended Heap
二、Algorithms (50%)
1. (10%) What are the minimum and maximum numbers of elements in a heap of height
參考解答:If root is in level 1:
2. (15%) The matrix-chain multiplication problem can be stated as follows: Given a chain of matrices, where for matrix has dimension fully parenthesize the product in a way that minimizes the number of scalar multiplications. Suppose that you have 6 matrices: has dimension has dimension has dimension has dimension has dimension has dimension Please calculate the minimum number of scalar multiplications.
參考解答:17900
3. (10%) Give a asymptotic tight bound for Assume that is constant for sufficiently small
參考解答:
4. (15%) We are given a directed graph on which each edge has an associated value which is a real number in the range that represents the reliability of a communication channel from vertex to vertex We interpret as the probability that the channel from to will not fail, and we assume that these probabilities are independent. Given an efficient algorithm to find the most reliability path between two given vertices.
參考解答:將原本之edges取 則還是會 並且新值越小代表越reliable, 所以再用single source shortest path即可求出。
試題(pdf):連結
有任何問題,或想要完整解釋的,歡迎在下方留言唷!
發佈留言