Professional Documents
Culture Documents
Spring 2010
Ans:
In undirected, connected graph whose edges have numeric weights are
G = (V, E)
Here’s applying generic greedy algorithm operates in minimum spanning tree is simple.
we maintain a subset of edges E of the graph .
∪ This subset is A.
Initially, A is empty. We will add edges one at a time until A equals the MST.
A subset A ⊆ E is viable if A is a subset of edges of some MST.
An edge (u, v) ∈ E − A is safe if
A ∪ {(u, v)} is viable.
In other words, the choice (u, v) is a safe choice to add so that A can still be extended to
form a MST. if A is viable, it cannot contain a cycle. A generic greedy algorithm operates
by repeatedly adding any safe edge to the current spanning tree.
Ans:
(k)
The two cases for computing d ij assuming we already have the previous matrix d ( k −1)
using Floyed-Warshall algorithm are:
1. Don’t go through vertex k at all.
2. Do go through vertex k
Ans:
A problem is called a decision problem if its output is a simple
• yes or no
• true/false,
• 0/1,
• Accept/reject.)
For example:
The MST decision problem would be a decision problem, Given a weighted graph G and
an integer k, does G have a spanning tree whose weight is at most k?
Ans:
Proof:
it should be sured that no vertex is marked more than once. The algorithm marks s. Let
v = s be a vertex and
let s → · · · → u → v be a path from s to v with the minimum number of edges.
Since the graph is connected, such a path always exists. If the algorithm marks u, then it
must put (u, v) into the bag, so it must take (u, v) out of the bag at which point v must be
marked. Thus, by induction on the shortest path distance from s, the algorithm marks
every vertex in the graph.
Call an edge (v, parent(v)) with parent(v) = ∅, a parent edge. For any node v, the path of
parent edges v → parent(v) → parent(parent(v)) → . . . eventually leads back to s. So the
set of parent edges form a connected graph.
both end points of every parent edge are marked, and the number of edges is exactly one
less than the number of vertices. Thus, the parent edges form a spanning tree.
Ans:
Proof:
For the non-tree forward and back edges the proof follows directly from the parenthesis
lemma.
For example,
for a forward edge (u, v), v is a descendent of u and so v’s start-finish interval is
contained within u’s implying that v has an earlier finish time.
For a cross edge (u, v) the two time intervals are disjoint. When we were processing u, v
was not white (otherwise (u, v) would be a tree edge), implying that v was started before
u. Because the intervals are disjoint, v must have also finished before u.
Ans:
DFS procedure can be written
1. recursively or
2. non-recursively.
RECURSIVEDFS(v)
if (v is unmarked )
then mark v
for each edge (v,w)
do RECURSIVEDFS(w)
ITERATIVEDFS(s)
PUSH(s)
while stack not empty
do v ← POP()
if v is unmarked
then mark v
for each edge (v,w)
do PUSH(w)
26 MCQ’s
Using ASCII standard the string abacdaacac will be encoded with __________ bits.
80
160
320
100
Using ASCII standard the string abacdaacac will be encoded with 160 bits.
True
False
Using ASCII standard the string abacdaacac will be encoded with 320 bits.
True
False
Using ASCII standard the string abacdaacac will be encoded with 100 bits.
True
False
10 00 010
011 00 010
10 00 110
11 10 110
Huffman algorithm uses a greedy approach to generate a postfix code T that minimizes
the expected length B (T) of the encoded string.
► True
► False
The difference between Prim’s algorithm and Dijkstra’s algorithm is that Dijkstra’s
algorithm uses a different key.
► True
► False
4 Questions of 2 Marks
1. Difference between Prim’s algorithm and Dijikstra’s algorithm.
Ans:
The difference between Prim’s algorithm and Dijkstra’s algorithm is that Dijkstra’s
algorithm uses a different key.
3 Questions of 3 Marks
1. following is the list of adjacency vertex, Identify that is any vertex have
isolated property.
a-------b--c
b------d--e
e------f--d
c-----b---f
f----b
g
3 Questions of 5 Marks.
1. What is the reduction, Explain with example. (From Last chapter of handsout)
• log (v)
• v.v
• e.e
• log(e)
3: Kruskal's algorithm (choose best non-cycle edge) is better than Prim's (choose best tree
edge) when the graph has relatively few edges.
• True
• False
4: You have an adjacency list for G, what is the time complexity to compute Graph
transpose G^T ?
• V+e
• v.e
• v
• e
5: What general property of the list indicates that the graph has an isolated vertex?
• There is Null pointer at the end of list.
• The Isolated vertex is not handled in list.
• Only one value is entered in the list.
• There is at least one null list.
• Breadth first search is shortest path algorithm that works on un-weighted graphs.
• Depth first search is shortest path algorithm that works on un-weighted graphs.
• Both of above are true.
• None of above are true.
• Lists require less space than matrices but take longer to find the weight of an edge
(v1,v2)
• Lists require less space than matrices and they are faster to find the weight of an
edge (v1, v2)
• Lists require more space than matrices and they take longer to find the weight of
an edge (v1, v2).
9: Suppose that a graph G = (V,E) is implemented using adjacency lists. What is the
complexity of a breadth-first traversal of G?
• O(|V |^2)
• O(|V | |E|)
• O(|V |^2|E|)
• O(|V | + |E|)
10: The relationship between number of back edges and number of cycles in DFS is,
11: Using ASCII standard the string “abacdaacacwe” will be encoded with __________
bits
• 64
• 128
• 96
• 120
12: What is the time complexity to extract a vertex from the priority queue in Prim’s
algorithm?
• log (V)
• v.v
• e.e
• log
13: the analysis of selection algorithm shows the total running time is indeed------------in
n.
• arithmetic
• geometric
• linear
• orthogonal
(1) 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
(2) 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
• Only I
• Only III
• Both I and III
• All of these
5) 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
6. 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.
7. 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 & III
8. 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
• Yes
• No
10. 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.
• Yes it is possible
• No it is not possible
• None of these
14. 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
15. 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.
16. Best and worst case times of an algorithm may be same.
• True
• False
17. Can an adjacency matrix for a directed graph ever not be square in shape?
• Yes
• No
18. 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
(a) O(log n)
2
(b) O(n2)
(c) O(2 + 4 + 3)
(d) all of the above
(e) none of the above
1. In which order we can sort?
• increasing order only
• decreasing order only
• increasing order or decreasing order
• both at the same time
5. One of the clever aspects of heaps is that they can be stored in arrays without using any
_______________.
• pointers
• constants
• variables
• functions
6. the analysis of Selection algorithm, we eliminate a constant fraction of the array with
each phase; we get the convergent _______________ series in the analysis
• linear
• arithmetic
• geometric
• exponent
7:. Sieve Technique applies to problems where we are interested in finding a single item
from a larger set of _____________
• n items
• phases
• pointers
• constant
8. The sieve technique works in ___________ as follows
• phases
• numbers
• integers
• routines
9. For the heap sort, access to nodes involves simple _______________ operations.
• arithmetic
• binary
• algebraic
• logarithmic
10. The analysis of Selection algorithm shows the total running time is indeed
________in n,
• arithmetic
• geometric
• linear
• orthogonal
• recursively
• mathematically
• precisely
• accurately
25: The reason for introducing Sieve Technique algorithm is that it illustrates a very
important special case of,
• divide-and-conquer
• decrease and conquer
• greedy nature
• 2-dimension Maxima
• true
• false
• T(n)
• T(n / 2)
• log n
• n/2+n/4
28: Divide-and-conquer as breaking the problem into a small number of
• pivot
• Sieve
• smaller sub problems
• Selection
• T(n^2)
• T(n)
• T( log n)
• T(n log n)
31: One of the clever aspects of heaps is that they can be stored in arrays without using
any _______________.
• pointers
• constants
• variables
• functions
32: Sorting is one of the few problems where provable ________ bonds exits on how fast
we can sort,
• upper
• lower
• average
• log n
• mathematically
• precisely
• accurately
• recursively
34: Sieve Technique can be applied to selection problem?
• True
• False
35: How much time merge sort takes for an array of numbers?
• (n^2)
• T(n)
• T( log n)
• T(n log n)
• T(nk)
• T(n / 3)
• n^2
• n/3
37: Heaps can be stored in arrays without using any pointers; this is due to the
____________ nature of the binary tree,
• left-complete
• right-complete
• tree nodes
• tree leaves
38: How many elements do we eliminate in each time for the Analysis of Selection
algorithm?
• n / 2 elements
• (n / 2) + n elements
• n / 4 elements
• 2 n elements
41: : In the analysis of Selection algorithm, we make a number of passes, in fact it could
be as many as,
• T(n)
• T(n / 2)
• log n
• n/2+n/4
42: The sieve technique is a special case, where the number of sub problems is
just
• 5
• Many
• 1
• few
► O(logn)
► O(n)
► O(nlogn)
► O(n2)
}
Recurrence for the following algorithm is:
► T(n) = T(n-1) +1
► T(n) = nT(n-1) +1
► T(n)= T(n-1) +n
► T(n)=T(n(n-1)) +1
> asymptotic
>. flowchart
C.A.R. Hoare
5. function is given like 4n^4+ 5n^3+n what is the run time of this
• Θ (n^4)
• Θ (n^3)
• Θ (4n^4+ 5n^3)
• Θ (4n^4+ 5n^3)
Divide---->conquer---------->combine
answer is 79
>True
>false
• ∑ notation
Question No: 2 ( Marks: 1 ) - Please choose one
FINALTERM EXAMINATION
Fall 2008
CS502- Fundamentals of Algorithms (Session - 1)
Marks: 75
Question No: 1 ( Marks: 1 ) - Please choose one
_______________ is a graphical representation of an algorithm
notation
Flowchart
Asymptotic notation
notation
The greedy part of the Huffman encoding algorithm is to first find two nodes with
character frequency
True
False
True
False
hb
left-complete
right-complete
tree nodes
tree leaves
Sieve Technique can be applied to selection problem?
Select correct option:
True
False
heap
binary tree
binary search tree
array
pivot
Sieve
smaller sub problems
Selection
Question # 7 of 10 ( Start time: 06:22:40 PM ) Total Marks: 1
In Sieve Technique we do not know which item is of interest
Select correct option:
True
False
16
10
32
31 (yeh just tukkahai)
linear
arithmetic
geometric (yeh b gup hi lugtihai)
exponent
arithmetic
binary
algebraic
logarithmic (bongihai...)
Ans:
A graph is connected if every vertex can reach
Every other vertex. and is isolated if any node has not connected to other vertex
,here’s “g” is the node that is not connected with any other vertex. This general
property of the list indicates that the graph has an isolated vertex.
Algorithm-CS502
QUIZ # 1
Mc090400760
03-11-2010
2^(h+1) 1
2 * (h+1) 1
2 * (h+1)
((h+1) ^ 2) 1
heap order
(log n) order
nT(n-1)+1
2T(n-1)+1
T(n-1)+cn
T(n-1)+1
T(n^2)
T(n)
T( log n)
T(n log n)
True
False
divide-and-conquer
decrease and conquer
greedy nature
2-dimension Maxima
arithmetic
geometric
linear
orthogonal
T(nk)
T(n / 3)
n^2
n/3
n / 2 elements
(n / 2) + n elements
n / 4 elements
2 n elements
left-complete
right-complete
tree nodes
tree leaves
True
False
heap
binary tree
binary search tree
array
pivot
Sieve
smaller sub problems
Selection
MC090406557 : Nadia Parveen
True
False
16
10
32
31 (yeh just tukka hai)
MC090406557 : Nadia Parveen
linear
arithmetic
geometric (yeh b gup hi lugti hai)
exponent
arithmetic
binary
algebraic
logarithmic (bongi hai...)
In the analysis of Selection algorithm, we eliminate a constant fraction of the array with
each phase; we get the convergent _______________ series in the analysis,
Select correct option:
linear
arithmetic
geometric
exponent
The sieve technique is a special case, where the number of sub problems is just
Select correct option:
5
many
1
few
The recurrence relation of Tower of Hanoi is given below T(n)={1 if n=1 and 2T(n-1) if
n >1 In order to move a tower of 5 rings from one peg to another, how many ring moves
are required?
Select correct option:
16
10
32
31
pivot
Sieve
smaller sub problems
Selection
The analysis of Selection algorithm shows the total running time is indeed ________in n,
Select correct option:
arithmetic
geometric
linear
orthogonal
How many elements do we eliminate in each time for the Analysis of Selection
algorithm?
Select correct option:
n / 2 elements
(n / 2) + n elements
n / 4 elements
2 n elements
True
level-order traversal
in-order traversal
pre-order traversal
post-order traversal
1-One of the clever aspects of heaps is that they can be stored in arrays without
using any _______________.
pointers **
constants
variables
functions
level-order traversal**
in-order traversal
pre-order traversal
post-order traversal
phases
numbers
routines
linear
arithmetic
geometric **
exponent
5- We do sorting to,
T(n)
T(n / 2)***
log n
n/2+n/4
False
recursively**
mathematically
precisely
pivot
Sieve
Selection
n / 2 elements
(n / 2) + n elements
n / 4 elements
2 n elements
16
10
32
31
MC090406505 : 85
Time Left
sec(s)
2^(h+1) 1
2 * (h+1) 1
2 * (h+1)
((h+1) ^ 2) 1
MC090406505 87
Time Left
sec(s)
pointers
constants
variables
functions
MC090406505 : 87
Time Left
sec(s)
True
False
Click here to Save Answ er & Move to Next Question
MC090406505 : 83
Time Left
sec(s)
nT(n-1)+1
2T(n-1)+1
T(n-1)+cn
T(n-1)+1
MC090406505 : 85
Time Left
sec(s)
T(n)
T(n / 2)
log n
n/2+n/ 4
MC090406505 : 84
Time Left
sec(s)
divide-and-conquer
greedy nature
2-dimension Maxima
divide-and-conquer
greedy nature
2-dimension Maxima
MC090406505 : 80
Time Left
sec(s)
1. n^2 the3.following
17 - Consider O(n^2) Algorithm:
accurately
2. n^n/2
Factorial 4. precisely
(n){ if (n=1) O(n^2logn)
3. n Correct Choice : 1 3 From Lectuer # 10 4
www.vugujranwala.com
4.1n^8
return else return (n *
11 - For
24 Whichthesorting
Sieve Technique
algorithm iswe faster
take time
Correct Choice : 1 From Lectuer # 3
Factorial(n-1))
www.vugujranwala.com
58 - Merge
4. n / 2sort
+ nis/ 4stable
1. TimeCorrectsort,vary
will Choice
but according
not an: 2 in-place
Fromto theLectuer
algorithm
nature#of15input data
1. arithmetic www.vugujranwala.com
Correct 2. It can
Choice not be
: 3 ofFrompredicted
Lectuer # 11
64 - One example
2. binary in place butwww.vugujranwala.com
not stable algorithm is
1. TRUE 3. It will take O (log n)
46 - In which order 3. algebraic
we can sort?
2. FALSE 4. None of the Given
4. Merger
1. logarithmic Sort
3. Correct Choice : 3 From Lectuer #
1. increasing 2. orderCorrect
Quickonly Sort Choice : 1 From Lectuerwww.vugujranwala.com # 1211
4. www.vugujranwala.com
2. decreasing 3. Continuation
order only Sort
Correct Choice : 1 From Lectuer # 15 www.vugujranwala.com
3. increasing 4. order
Bubble orSort
decreasing orderwww.vugujranwala.com www.vugujranwala.com
53 - After Correct
partitioning Choice array in Quick sort, pivot # is15placed in a position such that
59 - In4. both at thesort,
counting same oncetimewe know :the 2 From
ranks, Lectuer
we simply _________ numbers to their final
www.vugujranwala.com
positions in Correct
an-output
37 We65Choice
do array.
- One : 3the
sorting
of From
to,clever Lectuer # 11
1. Values smaller than aspects areofonheaps
pivotwww.vugujranwala.com
left andis that
larger theythancanpivot
be stored
are oninright
arrays without using
Correct Choice : 3 From Lectuer # 10
47 - One ofany the _______________.
2. Values
clever larger
aspects of than
heaps pivot are
is that on left
they canand
www.vugujranwala.com besmaller
stored in than pivotwithout
arrays are on using
right any
1. Delete
29 - The reason3.
_______________. 1.for
keep
Pivot elements
is the firstSieve
introducing in
element
random ofpositions
Technique array algorithm is that it illustrates a very
2. copy
important special1. 2.case
4. keepof,
Pivot
pointers the
is thealgorithm
last elementrun inoflinear
arrayorder
3. Mark
1. pointers 2. 3. constants
keep the algorithm run
Correct Choice : 2 From Lectuer in (log n) order# 13
4. arrange www.vugujranwala.com
1. constants
2. divide-and-conquer
4. variables
3. keep elements in increasing or decreasing order
Correct
54 - TheChoicerunning
Correct : 2
time
From of quick
Lectuersort # depends
15
Lectuer # 11 on the selection of
heavily
2. variables
3. decrease4.and conquerChoice : 1 From
functions www.vugujranwala.com
60 - An3. functions
4. greedy
in placenature
sorting Correct
algorithmChoice is one: 1that uses ___ From arraysLectuer
www.vugujranwala.com
for storage # 15
1. No of inputs www.vugujranwala.com
4. 2-dimension
Correct Maxima
Choice : 1 From Lectuer # 11
66 - Quick sort is
2. Arrangement in array www.vugujranwala.com
of elementswww.vugujranwala.com
1. Two dimensional
Correct Choicearrays : 1 From Lectuer # 10
48 - One of the clever 3. Sizeaspects
o elements of heaps is that they canwww.vugujranwala.com
www.vugujranwala.com be stored in arrays without using any
2. More than one array
4. Stable
1.
30 - The sieve technique
_______________. Pivot element
& in place
works in ___________ as follows
3. No Additional Array www.vugujranwala.com
2. Not stable Choice
Correct but in place : 4 From Lectuer # 13
4. None of the above www.vugujranwala.com
1. pointers
phases 3. Stable but not in place www.vugujranwala.com
Correct
55 - In Quick
Choice
42 - For the4.heap Sort
: 3 Constants
From
sort Lectuer
westable hidden
store the # 15
in T(n
tree nodes log n) are
in place
2. constants
numbers Some time &www.vugujranwala.com
some times in
3. variables
integers
61 - Continuation/counting Correct sortChoice
is suitable: 3 From
to sortLectuer
the elements # 15 in range 1 to k
1. Large www.vugujranwala.com
4. functions
routines 1. level-order traversal
67 - Quick
2. Medium sort is
1. K Correct
is Large 2. Choice
in-order:traversal
1 From Lectuer # 11 10
3. Small www.vugujranwala.com
2. K is not known
3. pre-order traversal
31 - Slow
49 The sievesorting 4.algorithms
1. Not Known
Stable
technique & in
worksrunplace
in
in,___________ as follows
3. K may be4.small post-order
or large traversal
2. Not stable but in place
Correct
4. K is small Correct ChoiceChoice :: 3 1 From
From Lectuer #
# 14
Lectuerwww.vugujranwala.com
11
1. O(n^2)
phases 3. Stable but not in place
www.vugujr
anwala.com
79 - A p x q matrix callsA Correct
can
andbe multiplied
looking
Choicethem: with a q xLectuer
3upFrom
again ifr matrix
we need #B. The result
21them later will be a p x r matrix
www.vugujranwala.com
C. There are (p .3.r) To totalmake
entries
the inprocess
C and accurate
each takes _________ to compute.
85 - Huffman algorithm uses a greedy approach to generate a postfix code T that
4. None of the above
minimizesCorrect the expected
Choice length B (T) Lectuer
: 2 From of the encoded
# 16 string.
1. O (q)
www.vugujranwala.com
2. O (1)
1. TRUE
73 - Dynamic programming algorithms need to store the results of intermediate
3. O (n^2)
2. FALSE
sub-problems.
4. O (n^3)
3.
Correct Choice : 1 From Lectuer # 19
1. TRUE
4. www.vugujranwala.com
1. Only i 4. 2. FALSE
www.vugujranwala.com
2. Only ii 3.
82 - A p x q matrix A Correct Choice : with
can be multiplied 2 Froma q xLectuer
r matrix #B.22The result will be a p x r matrix
www.vugujranwala.com
3. Both i and 4. ii
C. There are 87(p- .Ther) total entries
greedy partin of
C and each takes
the Huffman _________
encoding to compute.
algorithm is to first find two nodes with
4. Both iii and ivCorrect Choice : 2 From Lectuer # 17
larger frequency.
Correct Choice : 1 From Lectuer # 15
www.vugujranwala.com
70 - Which
2. O (1) of the1.following
TRUE sorting algorithms is stable?
(i) Merge
3. O sort,
(n^2) 2. 1. FALSE
At most four
(ii) Quick
4. Osort,
(n^3) 3. 2. At least four
Correct
(iii) Heap sort, 3. Choice
4. Exact four: 1 From Lectuer # 19
www.vugujranwala.com
(iv) Counting Sort. 4. Wrong
Correctbelongs
Choiceto: 2the From Lectuer # 22
83 - The Knapsack problem domain of _______________
www.vugujranwala.com problems.
Correct Choice : 1 From Lectuer # 17
88 - An optimization problem is one in which you want to find,
www.vugujranwala.com
1. Only i
1. Optimization
76 - The edit distance between FOOD and MONEY is
2. Only ii
2. NP Complete 1. Not a solution
3. Both i and ii
3. Linear Solution
1. An
2. At most
algorithm
four
4. Both iii and iv
4. Sorting 3. 2. Good
At leastsolution
four
Correct Choice : 1 From Lectuer # 15
Correct 3. Choice
4. Exactbest
The : 1solution
four From Lectuer # 21
www.vugujranwala.com
84 - Suppose we have three items as shown in the following table, and suppose the
Correct Choice : 1 From Lectuer # 17
capacity of
1. TRUE the knapsack is 50 i.e. W = 50. Item Value Weight 1 60 10 2 100
www.vugujranwala.com
20 3 120 3077The
2. FALSE
- If optimal
there are O (n^2)
solution is entries
to pick in edit distance matrix then the total running time is
3.
1. O (1)
4.
2. O (n^2)
Correct Choice : 1 From Lectuer # 16
3. O (n) www.vugujranwala.com
72 - Memorization
1. Items 1 and 4. is?
O2(n log n)
2. Items 1 and 3Correct Choice : 2 From Lectuer # 18
www.vugujranwala.com
1. Items
3. To store 2 andprevious
3 results for future use
2. None
4. To avoid of these
this unnecessary repetitions by writing down the results of recursive
www.vugujranwala.com
Correct Choice : 4 From Lectuer # 22