cs502 collection of old papers
DESCRIPTION
CS502 COLLECTION OF OLD PAPERSTRANSCRIPT
(b) An arbitrary graph with G (V, E), with |E| = |V|-1 edges is a tree.
(a) Kruskals algorithm for minimum weight spanning trees is an example of dynamic programming algorithm.
Answer yes or no and give a brief explanation for your choice.
Question No. 1 Marks : 10
Total Marks: 80 Total Questions: 9
**WARNING: Please note that Virtual University takes serious note of unfair means. Anyone found involved in cheating will get an `F` grade in this course.
b. On the other hand, if you believe that all of the choices provided for a particular question are wrong then select the one that appears to you as being the least wrong.
a. If you believe that two (or more) of the choices are correct for a particular question, choose the best one.
4. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
b. If you believe that some essential piece of information is missing, make an appropriate assumption and use it to solve the problem.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding.
3. Do not ask any questions about the contents of this examination from anyone. 2. This examination is closed book, closed notes, closed neighbors. 1. The duration of this examination is 150 Mins.
Please read the following instructions carefully before attempting any of the questions:
Instructions
Time Allowed: 150 Minutes Final Term Examination - February 2005
CS502 Fundamentals of Algorithms
www.vujannat.ning.com
(a) Estimate how many seconds it will take (in the worst case) to solve a problem of size N = 1,000? Full credit if you are within 1% of the exact answer.
} return 0; // none found if (a[i] + a[j] + a[k] == 0) return 1; for (k = 0; k < N; k++) for (j = 0; j < N; j++) for (i = 0; i < N; i++) int i, j, k; { int brute(int a[], int N)
Given a list of N integers, the 3-sum problem is to determine whether there exists 3 integers in the list (not necessarily distinct) such that x + y + z = 0. Suppose that you are executing the brute force algorithm below on a computer that executes 1 billion operations (addition, increment, or comparison) per second.
Question No. 4 Marks : 5
4 O(n2)
3 O(n2 log n)
2 O(n3)
1 O(n) What order is the execution of this code? } Do_something_constant(); { for(l=5; l<n; l++) for (k=1; k<15;k++) for (j=1; j<n;j++) Consider the following code:
Question No. 3 Marks : 5
3 Unknown 2 False 1 True
If a problem is not in P, it must be NP-complete.
Question No. 2 Marks : 5
(e) If problem A reduces to problem B and B is in P, then A is in P. (d) Every problem in NP is NP-complete.
(c) If problem A reduces (is polynomial-time reducible) to problem B and B is NP-complete then A is NP-complete.
Consider the following undirected network with edge weights as show:
Question No. 8 Marks : 20
T(n) = T(n) = 2+T(n - 1) for n ≥ 2 T(1) = 5
Consider the following recurrence equation:
Question No. 7 Marks : 5
Where (i-j, w) means that the edge that connects node i to node j has weight w. give the parent-link representation of the shortest-paths tree from node 0 that is computed by Dijkstra's algorithm for the graph using the standard adjacency-lists representation.
(1-6, 3) (2-3, 2) (2-5, 3) (3-4, 1) (4-5, 1) (5-6, 3) (0-1, 1) (0-4, 4) (0-6, 1) (0-5, 2) (1-2, 1) (1-4, 2)
Consider the following undirected graph
Question No. 6 Marks : 10
5 O(2n) 4 O(n2)
3 O(nlogn) 2 O(n) 1 O(logn)
What is the solution to the recurrence T(n) = T(n/2)+n, T(1) = 1
Question No. 5 Marks : 5
(b) Of size N = 10,000?
3 Unknown 2 False 1 True
Kruskals algorithm (choose best non-cycle edge) is better than Prim's (choose best tree edge) when the graph has relatively few edges.
Question No. 9 Marks : 5
(b) List the edges in the MST in the order that Kruskal's algorithm selects them.
(a) List the edges in the MST in the order that Prim's algorithm chooses them. Start Prim's algorithm from vertex 1.
Page 1 of 2
www.vujannat.ning.com
CS502 Fundamentals of Algorithms Final Term Examination - August 2004
Time Allowed: 150 Minutes
Instructions Please read the following instructions carefully before attempting any of the questions:
1. Attempt all questions. Marks of every question are shown adjacent to it. 2. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding. b. If you believe that some essential piece of information is missing, make an appropriate assumption and use it to solve the problem.
**WARNING: Please note that Virtual University takes serious note of unfair means. Anyone found involved in cheating will get an `F` grade in this course.
Very Important: Some results you may need:
( 1)21
n n ni +=∑
=i ,
3 22
1
2 36
n
i
n n ni=
+ +=∑
,
( 1) 111
ni
n xxxi
+ −=∑
−= So please copy this in the word file for later use.
Total Marks: 120 Total Questions: 12 Question No. 1 Marks : 5
It is impossible to design a sorting algorithm based on comparison of keys worst-case run time is in O(n).
o True o False
Question No. 2 Marks : 15
Consider the following problem: You are given a sequence of n integers that contains log2 n different integers.For example, n = 8, log(8) = 2: {3,5,3,5,3,5,5,5}; the list of 8 numbers is made up of only two distinct integers 3 and 5. Design an algorithm to sort this sequence using O(nlog logn) element comparisons (which is better than O(nlogn)). Prove that your algorithm is indeed O(nlog logn) [Hint: use variation of heap sort.]
Question No. 3 Marks : 20
In the following graph, edges are labeled with upper case letters. Edge weights are given as numbers next to edges.
Recall that Kruskal's algorithm greedily adds edges in a way that avoids cycles. For the graph shown above, list the edges in the order chosen by Kruskal's algorithm.
Question No. 4 Marks : 5
Greedy algorithms are called "greedy" because they often take a lot of time.
o True o False
Question No. 5 Marks : 5
Kruskal's algorithm (choose best non-cycle edge) is better than Prim's (choose best tree edge) when the graph has relatively few edges.
o True o False
Question No. 6 Marks : 5
Quick sort has a good average run time and a poor worst-case run time.
o True o False
Page 2 of 2 Question No. 7 Marks : 15
In the following algorithm, ". . ." stands for some simple calculations that take constant time, i.e., Θ(1). procedure(n) for k from 1 to n do ... /* produces a number j */ if k divides j, then mergesort an n-long list ... end for ... end Note: Think of j as a random integer, so the probability that "k divides j" is "1/k". (a) Suppose the sorting were free (which it is not). What is the complexity class for the average running time of this algorithm. You MUST give a reason for your answer. (The class should be of the form Θ( f (n)) where f (n) is a simple function.) (b) Suppose that the basic operation is a comparison in merge sort. What is the complexity class for the average running time of this algorithm. (You may give your answer in the form Θ(∑ f (k)) where f (k) is a simple function and the sum runs from 1 to n.) You MUST give a reason for your answer. (c) Use (a) and (b) to find the complexity class for the average running time of this algorithm. You MUST give a reason for your answer.
Question No. 8 Marks : 5
There is a good greedy algorithm for the 0-1 Knapsack Problem.
o True o False
Question No. 9 Marks : 10
Consider the following two problems. In P1 we are given as input a set of n squares (specified by their corner points), and a number k. The problem is to determine whether there is any point in the plane that is covered by k or more squares. In P2 we are given as input an n–vertex graph, and a number k; the problem is to determine whether there is a set of k mutually adjacent vertices. (E.g. for k = 3 we are just looking for a triangle in the graph.). Obviously, the problems are both in NP. There exists a simple translation from P1 to P2: just make a graph vertex for each square, and add an edge between a pair of vertices if the corresponding two squares overlap. (a) If P1 is NP-complete, would this translation imply that P2 is NP-complete? (b) If P2 is NP-complete, would this translation imply that P1 is NP-complete?
(Give your Answer in Yes or NO only) Question No. 10 Marks : 5
Dynamic programming uses a top-down approach.
o True o False
Question No. 11 Marks : 10
Solve the following recurrence relation with full history:
n-1
i=1( ) T(i), (1) 1T n T= =∑
Hint: subtract T(n) from T(n+1).
Question No. 12 Marks : 20
Consider the directed graph G below:
(a) Run DFS on this graph and write down the values of d[v] and f [v] for all vertices v. Assume that in outer for-loop of the DFS, vertices of G are processed in numeric order of their labels. For example, DFS will choose v1 initially. Also assume that in DFS-Visit, vertices adjacent to each vertex u are processed in increasing numeric label. For example, if v10, v5, v8 are adjacent to some vertex, they will be processed in the order v5, v8, v10. (b) Identify tree edges, back edges, forward edges and cross edges for the DFS.
= −=∑
−( 1) 111
ni
n xxxi
+
,
+=∑ + n3 2
2
1
2 36
n
i
n ni= ,
( 1)21
n n nii
+=∑
=
Some results you may need:
**WARNING: Please note that Virtual University takes serious action against unfair means. Anyone found involved in cheating will get an `F` grade in this course.
b. On the other hand, if you believe that all of the choices provided for a particular question are wrong then select the one that appears to you as being the least wrong.
a. If you believe that two (or more) of the choices are correct for a particular question, choose the best one.
4. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
b. If you believe that some essential piece of information is missing, make an appropriate assumption and use it to solve the problem.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding.
3. Do not ask any questions about the contents of this examination from anyone.
2. This examination is closed book, closed notes, closed neighbors. 1. The duration of this examination is 150 Mins.
Please read the following instructions carefully before attempting any of the questions:
Time Allowed: 150 Minutes
Final Term Examination – Spring 2005
CS502 Fundamentals of Algorithms
www.vujannat.ning.com
Question No. 5 Marks : 25
o 4 Bellman Ford algorithm. o 3 Floyd Warshall algorithm
1 Dijkstra's algorithm oo 2 Prims algorithm
You are given the task of laying down new railway lines which will connect all n cities. Thus for any pair of cities, you will end up with track connecting them. Note that two routes may share the same track; track laid between Lahore and Islamabad can be used to travel in both directions. your goal is to use the minimum amount of track. How would you achieve the goal now? (Note : consider the scenario carefully and name only the best suited algorithm)
Question No. 4 Marks : 05
o (e) none of the above o (d) all of the above
(b) O(n2) oo (c) O(2 + 4 + 3)
o (a) O(log2 n)
If an algorithm has a complexity of 2n2 + 4n + 3 for some model of computation (some
set of assumptions) and some complexity measures (such as number of comparison operations) we could say that it has complexity
Question No. 3 Marks : 05
o No o Yes
Can an adjacency matrix for a directed graph ever not be square in shape?
Question No. 2 Marks : 05
o False o True
Best and worst case times of an algorithm may be same.
Question No. 1 Marks : 05
Total Marks: 95 Total Questions: 08
Question No. 7 Marks : 25 T(1) = 1 T(n) = n + 2T(n/2) Solve the following recurrence using iteration method ( show intermediate steps )[10 pts]
Question No. 6 Marks : 10
You land at Heathrow airport for a vacation in London. Your hotel located in Essex, a suburb of London. You will need to take the train to get to your hotel. You have a map that shows the train lines and the fares between various points in London. You would like to minimize the fare you need to pay to travel from the airport to your hotel. To make matters complicated, the airline you traveled on gave you a discount coupon that can be used on one train journey. The coupon can be used only once. The coupon will get you 50% discount on the train fare. The coupon also lists which train lines the coupon can be used on to get the 50% discount. Solve the problem on the following sample train lines map (node A is the airport, node B is Essex where the hotel is). The notation 40,y means the fare is 40 and discount is available. If the coupon is used on this line, the fare would be 20. Describe the algorithm you have used to determine the train route that minimizes the total fare. [25 pts]
[25 pts]
(b) For the above example, list the items to take (i.e., list their indices).
(a) Describe this last part of the algorithm: how, in general, it determines the items to be taken.(Note : in maximum three lines.)
The last part of the algorithm uses this table to determine which items the thief should take to achieve the maximum value, 22.
From these inputs the following c table is computed by the algorithm:
In the following example the inputs are n = 6,W = 8, with values vi and weights wi:
The algorithm based on this recurrence takes as inputs the maximum weight W, the number of items n, and the two sequences v = hv1,v2, . . . ,vni and w = hw1,w2, . . . ,wni. It stores the c[i, j] values in a table c[0..n,0..W]. At the end of the computation, c[n,W] contains the maximum value the thief can take.
>:
Recall that a dynamic programming solution to the 0-1 knapsack problem can be derived from the following recurrence formula for c[i,w], the value of the solution for items 1, . . . , i and maximum weight w.
[15 pts]
Run DFS sweep and topological sort on the directed graph defined by the following adjacency matrix.
Question No. 8 Marks : 15
www.vujannat.ning.comConnecting VU Students
CS502-Fundamentals of Algorithms
Final Term Examination – spring 2006 Time Allowed: 150 Minutes
Question No. 1 Marks : 15 Compute all-pairs shortest paths using Floyd-Warshall algorithm on the following graph
Question No. 2 Marks : 15 Use Prim’s Algorithm starting with vertex “V1”to find the minimal spanning tree for the graph
Question No. 3 Marks : 10 Consider the following digraph
(a) Find the adjacency matrix of this graph (b)List the strongly connected components of this digraph Question No. 4 Marks : 1 If a graph has v vertices and e edges. Then to obtain a spanning tree we have to delete
• v edges • v – e + 5 edges • v + e edges • None of these
Question No. 5 Marks : 1 Which of the following is not true about Dijkstra’s algorithm? I. The length of the shortest path to the start vertex is always zero II.It takes time polynomial to the number of vertices III. It will work on any weighted graph with positive weights IV. It can be implemented to run in O(V ) time
• Only II • Only IV • Both II and IV • None of these
Question No. 6 Marks : 1 If a problem is not in P, it must be NP-complete
• Yes • No • Unknown
Question No. 7 Marks : 15 You are managing 300 million dollars. From this money you can sponsor 4 projects P1, P2, P3, and P4 with total cost no more than 300 million dollars. The project P1 has cost 60 million and generates a profit 20 millions ,the, project P2 has cost 240 million and generates a profit 500
millions, the project P3 has cost 180 million and generates a profit 300 millions ,the project P4 has cost 120 million and generates a profit 210 millions Find out which projects to sponsor so that you get the largest profit. Hint: This is the classic 0-1 knapsack problem Question No. 8 Marks : 1 Consider the following adjacency list:
Which of the following graph(s) describe(s) the above adjacency list?
1.
2.
3.
4.
5. Question No. 9 Marks : 1 Which statement is true (I) The running time of Bellman-Ford algorithm is Θ(VE) (II) Both Dijkstra’s algorithm and Bellman-Ford are based on performing repeated relaxations (III) The 0-1 knapsack problem is hard to solve
• Only I • Only III • Both I and III • All of these
Question No. 10 Marks : 1 The recurrence relation of Tower of Hanoi is given below
1 1
( )2 ( 1) 1 1
if nT n
T n if n=⎧
=⎨ − + >⎩
In order to move a tower of 4 rings from one peg to another, how many ring moves are required
• 15 • 7 • 12 • None of these
Question No. 11 Marks : 1 Which sequence is not a valid topological order of the directed graph shown below?
• ABDCEF • ADBCEF • ABCDFE • ADBCFE • ABDFEC
Question No. 12 Marks : 1 Suppose we have two problems A and B .Problem A is polynomial-time reducible and problem B is NP-complete. If we reduce problem A into B then problem A becomes NP-complete
• Yes • No
Question No. 13 Marks : 1
The total degrees of the following graph G
are
• 4 • 5 • 7 • 8
Question No. 14 Marks : 1 Which of the following arrays represent descending (max) heaps? I. [10,7,7,2,4,6] II. [10,7,6,2,4,7] III. [10,6,7,2,4,6] IV. [6,6,7,2,4,10]
• Only II • Only IV • Both II and IV • Both I and III
Question No. 15 Marks : 1 Which of the following statement(s) is/are correct? (a) O(n log n + n2) = O(n2). (b) O(n log n + n2) = O(n2 log 2n) (c) O(c n2) = O(n2) where c is a constant. (d) O(c n2) = O(c) where c is a constant. (e) O(c) = O(1) where c is a constant.
• Only (a) • Only (c) • Both (a) and (e) • Both (c) and (e)
• (a) ,(c),(e) Question No. 16 Marks : 1 The adjacency matrix say A= [aij] is defined as
•
,1 { }
0i j
ij
if v v is anedgeof Ga
otherwise⎧
= ⎨⎩
•
,0 { }
1i j
ij
if v v is anedgeof Ga
otherwise⎧
= ⎨⎩
•
,1 { }
0i j
ij
if v v is anedgeof Ga
otherwise
−⎧= ⎨⎩
•
,0 { }
1i j
ij
if v v is anedgeof Ga
otherwise⎧
= ⎨−⎩
Question No. 17 Marks : 1 Consider the following Huffman Tree
The binary code for the string “TEA” is
• 10 00 010 • 011 00 010 • 10 00 110 • None of these
Question No. 18 Marks : 1 Which of the shortest path algorithms would be most appropriate for finding paths in the graph with negative edge weights and cycles? I.Dijkstra’s Algorithm II. Bellman-Ford Algorithm III. Floyd Warshall Algorithm
• Only II • Only III • Both II and III
Question No. 19 Marks : 1 Which of the following orders is not a possible order in which Depth First Search can visit the vertices of the directed graph shown below?
• ABCEFD • ACEBFD • ADFEBC • ADFBCE • ABFECD
WWW.vujannat.ning.comhttp://vujannat.ning.com
Largest Online Community of VU Students FINALTERM EXAMINATION
SPRING 2007
CS502 - FUNDAMENTALS OF ALGORITHMS (Session - 5 )
Marks: 50
Time: 150min
StudentID/LoginID: ______________________________
Student Name: ______________________________
Center Name/Code: ______________________________
Exam Date: Thursday, July 05, 2007
INSTRUCTIONS
Please read the following instructions carefully before attempting any of the questions:
1. Attempt all questions. Marks are written adjacent to each question 2. Paste the bitmap image for the tables, diagrams etc while solving your
questions 3. This examination is closed book, closed notes, closed neighbors. 4. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions,attempt it to the best of your understanding.
b. If you believe that some essential piece of information is missing, make anappropriate assumption and use it to solve the problem.
5. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
6. Write all steps, missing steps may lead to deduction of marks 7. Use of cell phone during the examination is strictly prohibited, otherwise strict
disciplinary action will be taken as per university rules
**WARNING: Please note that Virtual University takes serious action against unfair means. Anyone found involved in cheating will get an `F` grade in this course.
For Teacher's use only Question 1 2 3 4 5 6 7 8 9 10 Total
Marks Question No: 1 ( Marks: 1 ) - Please choose one
The recurrence relation of Tower of Hanoi is given below
1 1if n=⎧( )
2 ( 1) 1 1T n
T n if n=⎨ − + >⎩
In order to move a tower of 6 rings from one peg to another, how many moves are required? ►
15
►
7
►
63
►
32
Question No: 2 ( Marks: 1 ) - Please choose one Edge (u, v) is a forward edge if ►
u is a proper descendant of v in the tree
►
v is a proper descendant of u in the tree
►
None of these
Question No: 3 ( Marks: 1 ) - Please choose one Is 22n = O 2n⎛
⎝ ⎜ ⎞
⎠ ⎟ ?
►
Yes it is possible
►
No it is not possible
►
None of these
Question No: 4 ( Marks: 1 ) - Please choose one If, in a DFS forest of digraph G = (V, E), f[u] ≤ f[v] for an edge (u, v) Є E then the edge is called ►
Back edge
►
Forward edge
►
Cross Edge
►
Tree Edge
►
None of these
Question No: 5 ( Marks: 1 ) - Please choose one
How can the number of strongly connected components of a graph change if a new edge is added? ►
The number of strongly connected components can be increased.
►
The number of strongly connected components can be reduced.
►
No change will occur.
►
None of these.
Question No: 6 ( Marks: 10 ) Complete the following instance of the optimal matrix multiplication ordering problem. Also give the optimal multiplication order.
p[0]=5 p[1]=4 p[2]=3 p[3]=4 p[4]=5 p[5]=6
1 K 2 K 3 K 4 K 5 k 1 0 0 60 1 120 2 195 2 ? ? 2 0 0 48 2 120 2 222 2 3 0 0 60 3 150 4 4 0 0 120 4 5 0 0
Question No: 7 ( Marks: 10 ) For the following given comparison, what are the tradeoffs between adjacency lists and adjacency matrices? Comparison Winner Faster to test if (x, y) exists. ? Faster to find vertex degree? ? Less memory on small graphs? ? Less memory on big graphs? ? Edge insertion or deletion? ? Faster to traverse the graph? ? Better for most problems? ? Question No: 8 ( Marks: 10 )
Run Dijkstra's algorithm on the following graph, starting at node A. Show all the intermediate values.
Question No: 9 ( Marks: 10 ) Apply Bellman-Ford Algorithm on the following graph. Show values after each iterationin a tabular form. S is the starting node.
Question No: 10 ( Marks: 5 )
Show the linear ordering of vertices produced by TOPOLOGICAL-SORT when it is run on the following DAG
WWW.vujannat.ning.COMConnecting VU Students
FINALTERM EXAMINATION SEMESTER FALL 2004
CS502-FUNDAMENTALS OF ALGORITHM-3
Total Marks: 75 Duration: 120min
StudentID/LoginID
Name
PVC Name/Code
Date
Maximum Time Allowed: (2 Hour)
Please read the following instructions carefully before attempting any of the questions:
1. Attempt all questions. All Questions carry Equal Marks. 2. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding. b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
**WARNING: Please note that Virtual University takes serious note of unfair means. Anyone found involved in cheating will get an `F` grade in this course.
For Teacher’s use only Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total Marks
Some results you may need:
∑=
+=
n
i
nni1 2
)1( , ∑=
++=
n
i
nnni1
232
632 , ∑
= −−
=+n
i xxx
ni
1 11)1(
Question No: 1 Marks: 20
Indicate whether true or false. Beware of guessing: correct answer +5pts, no answer 0pts
(a) In Prim’s algorithm, the additional information maintained by the algorithm is the length of the shortest edge from vertex v to points already in the tree.
A) TRUE B) FALSE C) UNKNOWN
(b) Although it requires more complicated data structures, Prim's algorithm for a minimum spanning tree is better than Kruskal's when the graph has a large number of vertices.
A) TRUE. B) FALSE C: UNKNOWN
(c) If a problem is NP-complete, it must also be in NP.
A) TRUE. B) FALSE C) UNKNOWN
(d) What is the worst-case runtime complexity of the following C function
int function(int n){ int i, j, k; k = n; for(i=-100; i<10*log(n);i++){ k = k/2; } for(j=i; j> n; j--){ k=j/2; } return k; }
What order is the execution of this code
a) O(log n) b) O(n) c) O(n log n) d) O(n2) e) O(n2 log n)
Question No: 4 Marks: 5
Suppose Prim’s algorithm is applied to the weighted graph shown here starting at vertex a to find a minimum cost spanning tree. State the edges that would be added to the tree in the order that they are added. Name an edge using its endpoints, in alphabetical order. For example, the edge joining vertices p and q should be called {p, q}, not {q, p}. Given a choice of edges at any point, choose the one that would be first alphabetically. For example, given the possibility of choosing either {x, z} or {w, y}, you should choose {w, y} (because w proceeds x).
Question No: 5 Marks: 10
Consider the digraph on five nodes, labeled 0 through 4, with six directed edges
0→1, 1→4, 4→0, 0→2, 2→3, 3→2
List the strongly connected components of this digraph.
Question No: 2 Marks: 5
What is the solution to the recurrence T(n) = 2T(n/2)+1, T(1) = 1
T(n) =
Question No: 3 Marks: 5
The first diagram shown below is a tree which is a heap. The items in the heap are now to be sorted using heapsort. The following diagram shows the tree after all items have been sorted. Show the contents of the tree as it would appear after each of the first six passes of heapsort.
Question No: 6 Marks: 10
Consider the following 21 character message that consists of 3 a’s, 7 c’s, 6 t’s, and 5 g’s: a a c c c c a c t t g g g t t t t c c g g Are the following 43 bits a possible Huffman encoding of the message above? 0000001111000101010010010010101010111001001 Justify your answer as concisely and rigorously as possible. Question No: 7 Marks: 20
In the following graph, edges are labeled with upper case letters. Edge weights are given as numbers next to edges.
Recall that Dijkstra’s algorithm finds the shortest path from v1 to all other vertices by adding edges that make shortest paths. For the graph shown above, each edge is bidirectional; that is, you can travel on either direction on it for the same cost. List the edges in the order chosen by Dijkstra’s algorithm.
T1(n) = 6000000 ·n
910A PC like computer can perform operations per second. Consider that we have five different algorithms for a specific problem. For each algorithm i, we know the number of operations Ti(n) it will perform on a problem of size n:
Question No. 1 Marks : 15
Total Marks: 70 Total Questions: 7
**WARNING: Please note that Virtual University takes serious note of unfair means. Anyone found involved in cheating will get an `F` grade in this course.
b. On the other hand, if you believe that all of the choices provided for a particular question are wrong then select the one that appears to you as being the least wrong.
a. If you believe that two (or more) of the choices are correct for a particular question, choose the best one.
4. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
b. If you believe that some essential piece of information is missing, make an appropriate assumption and use it to solve the problem.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding.
3. Do not ask any questions about the contents of this examination from anyone. 2. This examination is closed book, closed notes, closed neighbors. 1. The duration of this examination is 90 Mins.
Please read the following instructions carefully before attempting any of the questions:
Instructions
Time Allowed: 90 Minutes Mid Term Examination - November 2004
CS502 Fundamentals of Algorithms
www.vujannat.ning.com
2. (3 points) C = B = 1. (3 points)
• After each of the first three iterations of the fourth loop (line 7); • (2 points) after the third for-loop (line 5); C = • (4 points) after the second for-loop (line 3); C =
the arrays B and C (a) For the input array [6,3,7,5,4,3,3,2,8,9,1,6], and k = 9, specify the contents of
9: C[A[i]] = C[A[i]] - 1 8: B[C[A[i]]] = A[i] 7: for i = n downto 1 6: C[i] = C[i] + C[i-1] 5: for i = 2 to k 4: C[A[i]] = C[A[i]] + 1 3: for i = 1 to n 2: C[i] = 0 1: for i = 1 to k
5 C is the intermediate array of size k 4 B is the final sorted output array 3 A is the input array, of size n 2 each element is an integer in the range 1 to k 1 n is the number of elements to be sorted
Recall the counting sort algorithm, where
Question No. 3 Marks : 20
Describe an efficient algorithm to find the median of a set of 106 integers; it is known that there are fewer than 100 distinct integers in the set.
Question No. 2 Marks : 5
For each algorithm compute the size nmax of the largest problem the respective algorithm can solve within 1 second, 1 minute and 1 hour. Enter the maximal problem sizes into the following
1810−T5(n) = ·2n
610−T4(n) = ·n3
T3(n) = 0.003 ·n2
T2(n) = 60000 ·nlogn
cout << "hello world\n"; for (k = 1; k <= j; k++)
for (j = 1; j <= n/2; j++) for (i = 1; i <= n; i++)
justify your answer. Using big-oh notation, give the running time of the following piece of code. Briefly
Question No. 7 Marks : 10
( 1, ) 1( , 1) 1
E(i, j) = min ( 1, 1) 1 [ ] [ ]( 1, 1) [ ] [ ]
E i jE i jE i j ifA i B jE i j ifA i B j
− +⎛ ⎞⎜ ⎟− +⎜ ⎟⎜ ⎟− − + ≠⎜ ⎟
− − =⎝ ⎠
Compute the edit distance and edit scripts for the strings "HEAP" and "CHEAT". Recall that the edit distance recurrence is
Question No. 6 Marks : 10
T(n) =
Insertion sort can be expressed as a recursive procedure as follows: In order to sort array A[1..n], we recursively sort array A[1..n-1] and then insert A[n] into the sorted array A[1..n-1]. Give an equation that describes the overall running time of this algorithm on an input array of size n, in terms of the running time on smaller input.
Question No. 5 Marks : 5
When using merge sort to sort an array, do the recursive calls to merge sort depend on the number of elements in the array, the values of the elements or both? Briefly explain.
Question No. 4 Marks : 5
Modification to the algorithm and the output? Will the algorithm still sort the input? If no, explain why not. If yes, what is the effect of this
7: for i = 1 to n (b) (5 points) Suppose that the for-loop on line 7 above, is modified as follows:
C = B = 3. (3 points) C = B =
WWW.vujannat.ning.COM Connecting VU Students
CS502-ALGORITHMS
MID –FALL2007
Q#1Total time for heapify is: Ο (log2 n) Ο (n log n) Ο (n2 log n) Ο (log n) Q#2 Solve the recurrence using iteration method and also find time complexity (Θ notation) T (n) = C + O (1) + T (n-1) T (1) =1 and C is a constant. Q#3 Suggest the criteria for measuring algorithms. Also discuss the issues need to be discussed in the algorithm design. Q#4 If an algorithm has a complexity of log 2 n + nlog 2 n + n. we could say that it has complexity O(n) O( n log2 n) O(3) O( log2 ( log2 n )) O ( log2 n) Q#5 Let the set P = {(1, 13), (2, 9), (3, 15), (4, 12), (5, 14), (6, 6), (7, 3), (8, 10), (9, 2), (10, 8), (11, 9), (13, 6), (15, 3), (18, 5)}. You are required to give the final state of stack after the execution of sweep line algorithm for 2d-maxima. No intermediate steps or graphics to be shown. Q#6 Suppose we have hardware capable of executing 106 instructions per second. How long would it take to execute an algorithm whose complexity function is T (n) = 2n2 on an input of size n = 108? Q#7 In RAM model instructions are executed One after another Parallel
Concurrent Random Q#8 In selection algorithm, because we eliminate a constant fraction of the array with each phase, we get the Convergent geometric series Divergent geometric series None of these Q#9 Due to left-complete nature of binary tree, heaps can be stored in Link list Structure Array None of above
o O(1) The worst-case search time for a sorted singly-linked list of n items is
Question No. 2 Marks : 5
Develop an O(nlogn)-time algorithm that solves the following problem: Given an unsorted array of n real numbers and a real number x, decide whether there are two numbers y and z in this array such that x = y+z. The output of your algorithm should be "No" if no such numbers y and z exist; otherwise, it should report y and z. (Note that there may be more than one such pair of numbers. It is sufficient to report one of them.) Argue briefly why your algorithm takes O(nlogn) time.
Question No. 1 Marks : 10
Total Marks: 55 Total Questions: 5
∑= −
−=
+n
i xxx
ni
1 11)1(
∑=
++=
n
i
nnni1
232
632
∑=
+=
n
i
nni1 2
)1(
Some results you may need:
Please do copy the expressions written down, in your word document, these will be used in the solution of your paper
Very Important Note:
b. On the other hand, if you believe that all of the choices provided for a particular question are the wrong ones, select the one that appears to you as being the least wrong.
a. If you believe that two (or more) of the choices are the correct ones for a particular question, choose the best one.
4. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
b. If you believe that some essential piece of information is missing, make an appropriate assumption and use it to solve the problem.
a. If you think that there is something wrong with any of the questions, attempt it to the best of your understanding.
3. Do not ask any questions about the contents of this examination from anyone.
2. This examination is closed book, closed notes, closed neighbors; any one found cheating will get no grade.
1. The duration of this examination is 60 Mins.
Please read the following instructions carefully before attempting any of the questions:
Instructions
Time Allowed: 90 Minutes Mid Term Examination – Spring 2005
CS502 Fundamentals of Algorithms
www.vujannat.ning.com
Page 1 of 3
{ for(l = 5; l < n; l++ ) for(k = 1; k < 15; k++ ) for(j =1; j < n; j++) Consider the following code:
Question No. 5 Marks : 5
Note: show all necessary steps to get maximum marks. T(n) = 1+2T(n-1), T(1) = 1
Solve the recurrence using Iteration method and also find time complexity (Theta notation)
Question No. 4 Marks : 10
E(i-1, j-1) if A[i] = B[ j]
B[ j] ≠E(i, j) = min E(i-1, j-1)+1 if A[i] E(i, j-1)+1 E(i-1, j)+1 "PLOUGH". Recall that the edit distance recurrence is Compute the edit distance and edit scripts for the strings "THROUGH" and
Question No. 3 Marks : 15
o O(n2) o O(nlogn) o O(n) o O(logn)
Page 2 of 3
o O(nlogn) o O(n2) o O(n2 logn) o O(n3) o O(n)
What order is the execution time of this code? } do_something_constant(); // takes O(1) time
Page 3 of 3
www.vujannat.ning.com
, for integer k and n. Determine closed expression T(n) for running time. Assuming that arithmetic operations take unit time.
nThe following function calculate k Question No. 1 Marks : 15
**WARNING: Please note that Virtual University takes serious action against unfair means. Anyone found involved in cheating will get an `F` grade in this course.
7. Write all steps, missing steps may lead to deduction of marks
6. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
assumption and use it to solve the problem.
b. If you believe that some essential piece of information is missing, make an appropriate
best of your understanding.
a. If you think that there is something wrong with any of the questions, attempt it to the
5. Do not ask any questions about the contents of this examination from anyone. 4. This examination is closed book, closed notes, closed neighbors.
3. The duration of this examination is 90 Minutes. 2. Paste the bitmap image for the tables, diagrams etc while solving your questions 1. Attempt all questions. Marks are written adjacent to each question
Please read the following instructions carefully before attempting any of the questions:
Time Allowed: 90 Minutes
Mid Term Examination – Spring 2006 (session I) CS502 Fundamentals of Algorithms
2 O(N ) O(N)
is best described as being } cout << A[Idx] << endl; { for (Idx = 0; Idx < N; Idx++) int Idx; Execution of the following code fragment Question No. 3 Marks : 1
None of the above Both I and III Only II Only I
Which of the pairs of functions f and g are asymptotic?
2 +17x +3)2 +7) g(x) = (x + log(3x III f(x) = x 84log(x) g(x) = x32 II f(x) = x
+ 10 + 3x+7 g(x) = x I . f(x) = x 22Consider the following pairs of functions Question No. 2 Marks : 1 T(n) = ? } return(power) } power := newpower } newpower := newpower + power; for j := 1 to k do{ newpower := 0; for i = 1 to n do{ power := 1; { function exp1(k, n)
n n n n ……..n (n times)
……………
……………
3 3 3
2 2
1
That is
Determine the complexity of an algorithm that measures the number of print statements in an algorithm that takes a positive integer n and prints 1 one time, 2 two times, 3 three times , ... , n n times.
Question No. 6 Marks : 5
Both iii and iv Both i and ii Only ii Only i
(iv) Counting Sort. (iii) Heap sort, (ii) Quick sort, (i) Merge sort, Which of the following sorting algorithms is stable? Question No. 5 Marks : 1
None of others B is asymptotically greater A is asymptotically greater Both have same asymptotic time complexity
, then 2 + 2n + 3 and algorithm B has running time 2n2If algorithm A has running time 7n
Question No. 4 Marks : 1
O(N log N) O(log N)
{ compute(N)
for a given number N. The following subroutine computes 22
N
Question No. 11 Marks : 1
2 θ(n log n) 2 θ(n ) n θ(2 )
θ(n)
+ 97n + 1000 is 2The appropriate big θ classification of the given function. f(n) = 4n Question No. 10 Marks : 1
None of the above 12 72 36
Then T(2) is
2 0( )
6 ( 1)if n
T nT n otherwise
=⎧=⎨ −⎩
Consider the following recurrence relation Question No. 9 Marks : 1 Compute the edit distance and edit scripts for the strings “PROPERTY” and “POCKET”. Question No. 8 Marks : 10
Solve the following recurrence 1 1
( )2 ( 2)+ 1
if nT n
T n n if n=⎧
=⎨ − >⎩
Question No. 7 Marks : 10 Note:There is no need to write Algorithm(Pseudo code)
Consider the following polynomial Question No. 14 Marks : 1
0.09 seconds 0.9 seconds 9 seconds 90 seconds
operations for an input of size N. Let us say that a computer takes 1 microsecond (1/1000000 second) to carry out one operation. How long does the algorithm run for an input of size 3000?
2Let us say we have an algorithm that carries out N Question No. 13 Marks : 1
C(N) = 2(N - 1) N 2 C(N) = log
C(N) = (N - 1)2 C(N) = N – 1
Which of the following functions C(N) describes the runtime of the algorithm?
20 1
( )( 1) 2
if NC N
C N if N=⎧
= ⎨ − + >⎩
Assume that a given algorithm has a runtime C that depends on the size N of its input according to the following two formulas:
Question No. 12 Marks : 1
Dynamic Programming Greedy algorithm Divide-and-conquer Search and traversal
What category of algorithmic solutions best characterizes the approach taken in this subroutine (algorithm)?
} return compute(N - 1) * compute(N - 1) else return 2 If (N==1)
None of the above ) k+1 O(n
) k O(n) n O(k
What is the Big –O representation of the above polynomial?
0 . +………….ak-1nk-1+aknk a
www.vujannat.ning.com
+ 1) g(x) = log( x)2 V f(x) = log(x log(x) g(x) = x IV f(x) = x 3 2
2 +17x +3)2 +7) g(x) = (x + log(3x III f(x) = x 84+ x + 1)32 + x3 +x4 g(x) = (x4 + x + 1)2 + x3 II f(x) = (x
+ 10 2 + 3x+7 g(x) = x I . f(x) = x2Consider the following pairs of functions Question No. 1 Marks : 1
7. Write all steps, missing steps may lead to deduction of marks
6. Some of the examination consists of multiple-choice questions. Choose only one choice as your answer.
assumption and use it to solve the problem.
b. If you believe that some essential piece of information is missing, make an appropriate
best of your understanding.
a. If you think that there is something wrong with any of the questions, attempt it to the
5. Do not ask any questions about the contents of this examination from anyone.
4. This examination is closed book, closed notes, closed neighbors. 3. The duration of this examination is 60 Minutes.
2. Paste the bitmap image for the tables, diagrams etc while solving your questions
1. Attempt all questions. Marks are written adjacent to each question
Please read the following instructions carefully before attempting any of the questions:
Time Allowed: 90 Minutes
Mid Term Examination – Spring 2006 (session II) CS502 Fundamentals of Algorithms
3+ x + 1)2 + x3 +x4 g(x) = (x4 + x + 1)2 + x3 I . f(x) = (xConsider the following pairs of functions Question No. 4 Marks : 1
Write a single recursive function in C++ that computes Ackerman’s function. Do not use other functions or loops. Assume parameters m and n passed are always greater or equal to zero
≠⎩ ≠
+1 if = 0( , ) ( -1, 1) if 0, = 0
( -1, ( , n-1)) if 0, 0
n ma m n a m m n
a m a m m n
⎧⎪= ≠⎨⎪
Ackerman’s function is defined recursively on nonnegative integer’s m and n as follows Question No. 3 Marks : 10
. 2 n log n . n log n . n.
The running time of the above algorithm is } Cout<< “The sum for sub-array is<< sum; Sum+=a[j]; for (j = 1,sum=a[0]; j < = i; j++) for ( i=0 ; i < n; i++) { Consider the C++ program segment given below: Question No. 2 Marks : 1
None of above I , II and III II and V I and IV
Question No. 8 Marks : 10
None of the above 79 75 25
Then T(5) is
2
4 1( )
( /5) 3 5if n
T nT n n if n is divisible by
=⎧=⎨
+⎩
Consider the following recurrence relation Question No. 7 Marks : 1
Exact four At least four At most four
The edit distance between FOOD and MONEY is Question No. 6 Marks : 1
False True
Dynamic programming uses a top-down approach. Question No. 5 Marks : 1
Both III and IV Both I and II Only III Only I
Which of the pairs of functions f and g are asymptotic?
+ 1) g(x) = log( x)2 IV f(x) = log(xx + 7 g(x) = 2 III f(x) = 2x + 3
g(x) = II f(x) = x2
2x22
False True
It is impossible to design a sorting algorithm based on comparison of keys whose worst-case run time is in O(n).
Question No. 11 Marks : 1
O(N log N) O(log N)
2 O(N ) O(N)
is best described as being } i--; cout << Sum << end; Sum++; for (j = 0; j < N; j++) int j; int Sum = 0; { while (i > 0) int i = N; Execution of the following code fragment Question No. 10 Marks : 1 } swap(A[j-1], A[j]) if( A[j-1] > A[j] ) for j = n downto i do for i = 2 to n do { sort( A[1..n] ) Calculate the θ complexity of the following sort procedure Question No. 9 Marks : 10 Compute the edit distance and edit scripts for the strings “CRICKET” and “HOCKEY”.
Let X[1..n] and Y[1..n] be two arrays, each containing n numbers already in sorted order. Give an O(logn)-time algorithm to find the median of all 2n elements in arrays X and Y.
Question No. 14 Marks : 10
(iii),(i),(ii) (iii),(ii),(i) (ii),(iii),(i) (i),(ii),(iii)
Which is the correct order of instructions in merge sort algorithm?
(iii) If the number of items to sort is 0 or 1, return. (ii) Merge the two-sorted halves into a sorted group.
halves separately nd and 2st(i) Recursively sort the 1The merge sort algorithm involves the following steps. Question No. 13 Marks : 1
None of the above 13 29 61
Then T(8) is
5 1( )
2 ( / 2) 3if n
T nT n if n is even
=⎧=⎨ +⎩
Consider the following recurrence relation Question No. 12 Marks : 1