fall 2007, final exam, data structures and algorithms name...
Post on 04-Jun-2020
2 Views
Preview:
TRANSCRIPT
Fall 2007, Final Exam, Data Structures and Algorithms
Name:Section:Email id:
12th December, 2007
This is an open book, one crib sheet (2 sides), closed notebook exam.
Answer all twelve questions. Each Question is worth 10 points. You have 180 minutes tocomplete the exam.
Happy Holidays and Have a Great New Leap Year. The formulas you need are:
1 Formulas
i=n(TL+1)/2
n(n+ 1)(2n+ 1)/6
a=(l—a’’)/(1—a), wherea 1
1
1. Graphs/DFS/Path
The 49 postal codes in USA (48 continental states and Washington DC) are AL, AR,
AZ, CA, CO, cr, DC, DE, FL, GA. IA, ID, IL, IN, KS, KY, LA, MA, MD, ME,
MI, MN, MO, MS, MT, NC, ND, NE, NH, NJ, NM, NV, NY, OH, OK, OR, PA, RI,
SC, SD, TN, TX, UT, VA, VT, WA, WI, WV, WY,
in alphabetical order. Suppose we consider two states be adjacent if their postal codes
agree in one place (Constructing an undirected graph with 49 states as.vertices/nodes
and the edges are defined as given above- However, you need not construct the
graph to answer the questions below.).[1O points]
(a) What is the degree of vertex/node NY?
(b) Write a path of length 2 from CA to NY.
(c) Write the paths (any length is acceptable) from AZ to ME, WA to SC and WA to
FL.
(d) Is the graph Connected?
,
\/ !.
b) CA
A- MAM
- tAFt..
WA— i...A
c
1ø It-?‘1’0ie
1-4 e’ A
2
Shortest Path Algorithms/Spanning Tree Here is a map of United States. as an
undirected graph with adjacent states connected by an edge.’ {1O Pointsj
what is the shortest path from ND to SC (using Dijkstra’s algorithm for unweightedgraphs)
(b) Give two different paths which are edge disjoint from NY to CA. W
(c) Draw DFS Spanning tree starting at NY
(d) Draw a BFS Spanning tree starting at NY.
3
3. Eulerian Cycles, Topological Sort
(a) Draw a simple connected directed graph with 8 vertices and 16 edges such that the
in-degree and out-degree of each vertex is 2. Show that there is an Eulerian Cycle
by listing the cycle. (5 Pointsl
5 --
4•LS—’ -7(
(b) Mr. Kay loves foreign languages and wants to plan his course schedule to take the fol
lowing nine language courses LA15, LA16, LA22, LS31, LA32,LA1267LA127,LA141
and LA169 The course prerequisites are
LA15: (none)LA16: LAI5LA22: (none)LA31: LA15LA32: LA16,LA31
Find a sequence of courses that allows Bob to satisfy all the prerequisites. [5 Points]
L
9 L
LAI4I
-7 4
LA126: LA22,LA32LA127: LA16LA141: LA22,LA16LA169: LA32
L.z-Z.
LAL
(0
LPrf2.7 4
cit
9
AC
4. Network Flows Consider the following network (the numbers are edge capacities).[1O
Pointsj
(a) Find the maximum flow, f, from S to T
(b) Draw the residual graph Gf (along with edge capacities). In this residual network,
mark the vertices reachable from S and the vertices from which T is reachable.
(c) An edge in a network is a bottleneck edge if increasing its capacity reults in an
increase in the maximum flow. List II bottleneck edges in the above network.
(d) Give a very simple example (containing at most 4 nodes/vertices) of a network which
has no bottleneck edges.
4c, 2,c
7
:4?
4
S T
BD
0
;-
C
T
c)
d)
-r
5. Dynamic Programming/Dijkstra Yuckdonald’s is considering opening a series of
restaurants along Judson Valley Highway (JVH). The n possible location are along a
straight line, and the distances of these locations from the start of JVH are, in miles, and
in increasing order m1,rn2, , rn...The constraints are as follows:
• At each location, Yuckdonald’s may open at most one restaurant. The expected
profit from opening a restaurant at location i is p1, where p.O, for i 1,2, ,n.
• Any two restaurants should be at least k miles apart, where k is a positive integer.
Give an efficient algorithm to compute the maximum expected total profit, subject to the
given constraints. 1O Pointsj
°
S.,
, O4A ,.
&L
- J
8
9. Selection, Median Professor Oiler is consulting for an oil company, which is planninga large pipeline running east to west through an oil field of ii wells. For each well, a spurpipeline is to be connected directly to the main pipeline along a shortest path (either northor south) as shown in the following figure. Given x, y co-ordinates of the wells, how shouldthe professor pick the optimal location of the main pipeline (the one that minimizes thetotal lengths of the spurs)? Show that the optimal solution can be determined in linear
time. The main pipeline Is a line parallel to X-axis. For inefficient algorithm willget 5 points. [10 Points
/ --
-‘
-4Y
L
A
8. Recursion, Search
How many’ lines does the following program print for the function call f(16). Gener
a1ie this for f(n), n = 2c.[5 Points]
void f (jut ii)
{if (a > 1) {
cout << ‘StIll Going << end).;
f(nj2);f(n/2);
}}
( L
n1
(a) Given a sorted array of distinct integers A[1,...n], you want to find an index i which
A[iJ = i. (If no such index exists, you may report it too) Give an algorithm that runs
in O(log n) time.[5 Points]
ALJ?
11
6. Huffman
(a) A long string consists of Characters A, C, G, T.; they appear with frequency 31%.20%,9%
arid 40% respectively. What is the Huffrnan encoding of these four characters? [5
Pointsj
40/
T
i
A
(b) We use Huffma&s algorithm to obtain the encoding of alphabet {c1, b, c} with frequen
cies f, ft,, f. In each of the cases, either give an example of frequencies fa, fb, f that
would yield the specified code, or explain why the code cannot be possibly obtained
(no matter what the frequencies are) [5 Points]
i. Code:{O,10,11}
ii. Code: {0,1O0}
iii. Code: {iO,oi,00}
*
I
:L.Lb:.Q
0
7. Analysis, Greedy
(a) Give a linear time algorithm that takes input as a tree T and determines whether it
has a perfect matching (i.e., a subset of edges of the given tree T) that touches each
vertex only once) edges 0(n), where n is the number Of vertices in a tree. 5 Pointsj
51- a.. ‘U °I-. 4%s.AQ’°’-’
Dp c ‘2, F - 4w içp.A r..La.
.
W,d.k ‘r’ Sd 1
/I-sr--. I 4
/ -
I , t(414..
(b) Solveand T(l)1.
[5 PointsJ
0
T(’
10.
10. Heap Sort
(a) Ts the sequence 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 a max heap? If not convert into amax heap4d Points]
/ \Li lt
4’, 23I
11
I \1 toIc,
Cz.
(b) Where in a max heap of size 12, might the smallest element reside assuming all
elements are distinct.[3 Points]
- crA A 1
C5, 4 I L y Cu ,.-
)( £73
(c) What are the minimum and maximum number of elements in a heap of height h? [3Points]
A.A 1’.
a
(V1’i(jflU€A.-
13
11. Hash Tib1e, Binary Serach Tree
(a) Write a recursive algorithm for inserting a key in a Binary Search ‘Three. (5 Points]
0
(b) Demonstrate the insertion of the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a Hash
Table of size 9 with collissions resolved by chaining. Let the hash function be h(k) =
k mod 9. [5 Points]
ne’- .cr
<
ic.tr-, Ie-)’’)
i’nsev4- bGT (k TIecI-)
ycL.) ,
)( I.ç$- .nu.1\;
x-,,. ço
-V.. -eçk- XF
—
14
12. Sorting
Give an algorithm (sequences of comparisons) to sort 5 elements with 7 comparisons.
(Tf you take more than 7 comparisons, you will get half the credit.) [5 Points]
I’
15
L.JtL-l
/qJ
c3
L4A.LLtq5
a,,_,)S
• ,/ I
a.
1 i•’
r/
‘( et
j—7_4—qc
(a) Sort 3, 1, 4, 1, 5 9, 2, 6 using merge sort (Show your steps.) [5 Points]
31
3’
$3
2’
2G
1L4
fj3L.
top related