Final 2016
Final 2016
Instruction: Each problem is worth 10 points. Please strategically manage your time to
maximize the total number of points you can collect (you can collect at most 110 points).
Show how to find the minimum value of a given array of N numbers using N-1 comparisons; then show
how to find the second minimum of the same array using no more than log2N -1 comparisons.
6. (Topological sorting)
Apply topological sorting to the graph shown in Problem 3 (you can ignore the weights).
Note: for programming problems 7-10, please feel free to write either C/C++ or Java codes
7p. (Array programming) Pairwise swap adjacent elements in an array without using extra space.
e.g., {1,2,3,4,5,6,7,8} becomes {2,1,4,3,6,5,8,7}.
8p. (Linked list programming) You are given a function as follows. What will the output look
like for an input of 0→1→2→3→4→5→6→7→8→9
Given a BST, print out its elements in increasing order (a.k.a. inorder traversal). Please define
the syntax yourself.
10p. (Binary tree diameter finding) Given a binary tree, find out its diameter (the number of
nodes on the longest path between any two leaves in the tree). Please define the syntax yourself.
The following two problems are more challenging. Please try them if you have spare time.
Given an array of integers, an element is a peak if it is not smaller than its two adjacent elements. Devise
a sublinear-time algorithm to find such a peak element. For instance, for a given input of
{10,20,15,2,23,90,67}, your algorithm should return either 20 or 90.
An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,….vn. Two nodes vi , vj
are connected if and only if 0 < |i – j| <= 2. Each edge (vi, vj ) is assigned a weight i + j. Show
that the cost of the minimum spanning tree for such a graph with n nodes is n2 -n+1.
Hint: all odd-weighted edges except the first one cannot belong to MST.