You are on page 1of 34

BITS WILP Data Structures and Algorithms Design Assignment

2017-H1
Birla Institute of Technology & Science, Pilani
Work-Integrated Learning Programmes Division
Second Semester 2016-2017
EC-1 Lab Component
Course Title : Data Structures & Algorithms Design Due Date : 02/05/2017
Course No : SS ZG 519 Total : 10 marks

Given adirected, weighted graph in the format described below, your task is to nd the
shortest distance between two vertices A and B in the graph. If there are multiple shortest
paths between A and B, you have to nd the path with the least number of edges.
Input Format
The vertices are represented by numbers starting from one. The rst line will include two
numbers N and M, the number of vertices in the graph and the number of edges in the
graph respectively. The following M lines will have three numbers each, u, v, w which
represents adirected edge between u and v with a weight w. The nal line will have the
values of A and B, the nodes between which you will have to nd the shortest path.
Output Format
Print two numbers separated by a space: C E, where C is the cost from A to B and E is the
number of edges from A to B. For unreachable nodes, print -1 -1, denoting no valid distance
and no valid number of edges, i.e. C = -1 and E = -1.
Example 1
Consider the following graph. It has ve vertices and ve edges. We have to nd the shortest
part from vertex 1 to vertex 5. There are two shortest paths, based on the weight of the
path, from vertex 1 to vertex 5: path 1,2,5 and path 1,3,4,5. But we want to nd the path
with minimum number of edges. So we pick the path with total weight 3 and number of
edges 2.

1
Input for Example 1
55
121
131
252
341
451
15
Output for Example 1
32

Constraints
Consider the fact the input graph can be quite large. You should come up with e cient
code to avoid the timeout error. However, there is a limit on the number of vertices and
number of vertices as follows.
1 <= M <= 104
1 <= N <= 104

2
BITS WILP Data Structures and Algorithms Design Quiz-1
2017-H1
Question 1
Consider the ArrayFind algorithm.
input : an element x, n, an array A of n integers
Output : The index i such that A[i]=x or -1 if no element in A is equal to x
i←0
while i <n do
   if x = A[i] then
        return i
   else
   i ←i+1
return -1
 The number of primitive operations for the best case is

Select one:

a. 5n+3

b. 5n-2

c. None of the above

d. 5n

e. 5n+1
Feedback
The correct answer is: 5n+1
Question 2
T(n) = b if n=1
          2T(n-1) +b otherwise
Then T(n) is

Select one:

a. O(2^n)

b. O(n)

c. O(n^b)

d. None of the above

e. O(n2)
Feedback
The correct answer is: O(2^n)

3
Question 3
1+3+9+27+... +3^n

Select one:

a. O(n^3)

b. O(2^n)

c. O(n)

d. O(4^n)

e. None of the above


Feedback
The correct answer is: O(4^n)

Question 4
Algorithm Loop(n)
p←1
for i← 1 to n do
       p ← p.i
return p
What is the value of Loop(6)?

Select one:

a. 720

b. 5

c. 1

d. None of the above

e. 120
Feedback
The correct answer is: 720
Question 5
Which of these is the correct big-O expression for 1+4+9+...+(n+2)²?

Select one:

a. O(log n)

b. O(n log n)

c. O(n²)

4
d. None of the above

e. O(n³)
Feedback
The correct answer is: O(n³)
Question 6
Consider the ArrayFind algorithm.
input : an element x, n, an array A of n integers
Output : The index i such that A[i]=x or -1 if no element in A is equal to x
i←0
while i <n do
   if x = A[i] then
       return i
   else
   i ←i+1
return -1
The number of primitive operations for the best case is

Select one:

a. 5

b. n/2

c. None of the above

d. 4

e. n
Feedback
The correct answer is: 5
Question 7
Consider the following recurrence relation.
T(n) = 5 if n <= 2
           T(n-1)+ n otherwise
Closed form solution for T(n) is

Select one:

a. n(n+1)/2 +2

b. n(n+1)/2 +7

c. n(n-1)/2

d. n(n+1)/2

e. None of the above

5
Feedback
The correct answer is: n(n+1)/2 +2
Question 8
Which of the following statements are true? 
i. If f(n) is O(n^3), then f(n)+10n^3 is O(n^3)
ii. If f(n) is O(n^3), then f(n) is Θ(n^3)
iii. If f(n)+10n^3 is O(n^3), then f(n) is O(n^3)
Select one:

a. None of the above

b. All of them are true

c. iii only

d. i only

e. i and iii only


Feedback
The correct answer is: i and iii only
Question 9
If C =1, what would be the appropriate value of n0 to show that 2n² +9 is O( n²)?
Select one:

a. 4

b. None of the above

c. 100

d. 5

e. 10
Feedback
The correct answer is: None of the above
Question 10
Consider the array implementation of Stack ADT with array size N. 
Which of the following statements are true? 
i. We can store at max N-1 elements in the stack
ii. Stack ADT supports inserting elements anywhere

Select one:

a. Both of them are true

b. i only

c. ii only

6
d. None of them are true
Feedback
The correct answer is: None of them are true
Question 11
Which of the following statements are true?
i) n! is O(n)
ii) n! is O(n^n)
iii) n! is O(2^n)

Select one:

a. i only

b. None of the above

c. ii and iii only

d. i and iii only

e. ii only
Feedback
The correct answer is: ii only
Question 12
Algorithm Loop(n)
p←1
for i← 1 to n do
    p ← p.i
return p
Give a big-Oh characterization for the above algorithm. 
Select one:

a. O(1)

b. O(√n)

c. O(log n)

d. O(n)

e. None of the above


Feedback
The correct answer is: O(n)
Question 13
Which of the following statements are true.
i) (2^(n+1)) is O(2^(n/2))

7
ii) (2^(2n)) is O(2^n)

Select one:

a. i only

b. None of them true

c. Both of them are true

d. None of the above

e. ii only
Feedback
The correct answer is: None of them true
Question 14
Consider the array implementation of Stack ADT S and we modify the Pop operation as follows.
Algorithm pop()
if isEmpty() then
     return Error
dummy ← ???
t ← t-1
return dummy

What should ??? be replaced with?

Select one:

a. S[t+1]

b. S[t]

c. None of the above

d. S[t-1]

e. S[0]
Feedback
The correct answer is: S[t]
Question 15
Algorithm A uses 5nlog n operations and algorithm B uses n√
operations. Determine the value n0 such that A is better than B for n > n0

Select one:

a. 2048

b. 1024

8
c. None of the above

d. 512

e. 4096
Feedback
The correct answer is: 512
Question 16
Consider the following stack operations. new(), push(a), push(b), pop(), push (c), pop(), push(5).
what is the index of top element in the array implementation?

Select one:

a. 0

b. -1

c. 2

d. 1

e. None of the above


Feedback
The correct answer is: 1
Question 17
Which of the following formulas in big-O notation best represent the expression 10n²+5nlogn?
Select one:

a. None of the above

b. O(n)

c. O(n³)

d. O(42)

e. O(n²)
Feedback
The correct answer is: O(n²)
Question 18
Which of the following expressions is not sublinear? 

Select one:

a. O(logn)

b. O(n)

c. None of the above

9
d. O(n√)

e. O(log log n)
Feedback
The correct answer is: O(n)
Question 19
Log^2 (n/2) is
Select one:

a. log^2 (n)−2logn+1

b. log^2 (n) -1

c. None of the above

d. log log (n/2)

e. log(n2)

Feedback
The correct answer is: log^2 (n)−2logn+1

Question 20
Consider the Array implementation for Stack ADT. Stack is empty when 
Select one:

a. t=1

b. t=0

c. None of the above

d. t= -1
Feedback
The correct answer is: t= -1

Question 1
The following letter sequence was generated by using a postorder traversal of a perfect binary
tree T.  What is the root of this tree?

debfgch
Select one:

a. h

10
b. f

c. d

d. None of the above


Feedback
The correct answer is: h
Question 2
The following letter sequence was generated by using a postorder traversal of a perfect binary
tree T. What is the preorder traversal?

 d e b f g c h
Select one:

a. d b f g h c e

b. d e b f g c h

c. d b e h f c g

d. h b d e c f g

e. None of the above


Feedback
The correct answer is: h b d e c f g
Question 3
The following letter sequence was generated by using a postorder traversal of a perfect binary
tree T.  What is the inorder traversal?

debfgch
Select one:

a. h b d e c f g

b. d b f g h c e

c. d b e h f c g

d. None of the above

e. d e b f g c h
Feedback
The correct answer is: d b e h f c g
Question 4
The following letter sequence was generated by using a postorder traversal of a perfect binary
tree T. 

d e b f g c h 
Which of the following is true?
11
i. f and e are siblings 
ii. f and g are siblings 
iii. c is decendent of h
Select one:

a. All of them are false

b. i is true and ii, iii are false

c. ii and iii are true and i is false

d. All of them are true

e. None of the above


Feedback
The correct answer is: ii and iii are true and i is false
Question 5
The following letter sequence was generated by using a postorder traversal of a perfect binary
tree T. What is the depth of node c?

d e b f g c h 
Select one:

a. 0

b. 1

c. None of the above

d. 2
Feedback
The correct answer is: 1
Question 6
Let T be a binary tree with n nodes. The time required to traverse the tree using in-order traversal
is
Select one:

a. O(n)

b. O(n2)

c. none of the above

d. O(nlogn)
Feedback
The correct answer is: O(n)
Question 7
Suppose T is a proper binary tree with 11 internal nodes. What is the size of T?
Select one:

12
a. 23

b. 12

c. None of the above

d. 22

e. 21
Feedback
The correct answer is: 23
Question 8
Suppose T is a binary tree with 10 nodes. What is the maximum possible height of T?
Select one:

a. 4

b. None of the above

c. 10

d. 5
Feedback
The correct answer is: 10
Question 9
A binary search tree has n internal nodes. The number of external nodes is at most
Select one:

a. None of the above

b. n-1

c. log n

d. 2n

e. n
Feedback
The correct answer is: 2n
Question 10
The worst case running time to find a maximum element in a binary search tree with n items is
Select one:

a. O(n)

b. O(logn)

c. None of the above

13
d. O(n2)

e. O(1)
Feedback
The correct answer is: O(n)
Question 11
Which of the following have O(1) running time for insert operation?
Select one:

a. Log File, Hash Table

b. Hash Table, Binary Search Tree

c. Look-up Table, Hash Table

d. None of the above

e. Binary Search Tree, Look-up Table


Feedback
The correct answer is: Log File, Hash Table
Question 12
Which of the following has more than O(n) space requirement where n is the number of items
stored.
Select one:

a. Log File

b. None of the above

c. Look-up Table

d. Binary Search Tree

e. Direct Address Table


Feedback
The correct answer is: Direct Address Table
Question 13
Keys "63, 73, 43, 98, 110" are stored in a direct address table. What is the minimum size of the
table?
Select one:

a. None of the above

b. 4

c. 111

d. 109

14
e. 5
Feedback
The correct answer is: 111
Question 14
Keys "63, 73, 43, 98, 110" are stored in a direct address table. "43" will be stored in the slot
____________
Select one:

a. 43

b. 3

c. 0

d. None of the above

e. 44
Feedback
The correct answer is: 43
Question 15
Keys { 200,205,210,...,600} are stored in a chained hash table. Suppose h(k) = k mod 100 is
used, which slot will have maximum number of keys?
Select one:

a. 200

b. 0

c. 99

d. None of the above

e. 100
Feedback
The correct answer is: 0
Question 16
Keys { 200,205,210,...,600} are stored in a chained hash table. Let h(k) = k mod 101, alpha be
the load factor, and v be the maximum number of keys stored in a single slot. Which of the
following is true?
Select one:

a. alpha >v

b. None of the above

c. alpha < v

d. alpha =v

15
e. alpha > 1
Feedback
The correct answer is: alpha < v

Question 17
When using linear probing policy, what is  the probability of an empty slot gets filled if it is
preceded by i full slots?
Select one:

a. (i+1)/n

b. 1/n

c. (i+1)/m

d. 1/m

e. None of the above


Feedback
The correct answer is: (i+1)/m
Question 18
Assume the keys are inserted in the following order. 1055, 1492, 1776, 1812, 1918, 1945. Find
the the total number of key comparisons if linear probing policy is used and h(k) = 5*x mod 8 is
the auxiliary hash function.
Select one:

a. 9

b. None of the above

c. 7

d. 6

e. 4
Feedback
The correct answer is: 9
Question 19
Assume the keys are inserted in the following order. 1055, 1492, 1776, 1812, 1918, 1945. 1945
is stored in the slot ________ if linear probing policy is used and h(k) =5*x mod 8 is the
auxiliary hash function.
Select one:

a. 6

b. 0

c. 7

16
d. None of the above

e. 5
Feedback
The correct answer is: 7
Question 20
Assume the keys are inserted in the following order. 1055, 1492, 1776, 1812, 1918, 1945. What
is the minimum number of nodes in a complete binary tree with height 4?
Select one:

a. None of the above

b. 8

c. 4

d. 3

e. 11
Feedback
The correct answer is: 8
Question 21
Consider the following statements.
i) External nodes of heap does not store any keys or elements
 ii) Insertion and deletion in heap can be done in O(logn) time
 iii) Minimum of a heap can be found in constant time.
Select one:

a. All of them are true

b. i is true and ii, iii are false

c. None of the above

d. i, ii are true and iii is false

e. All of them are false


Feedback
The correct answer is: All of them are true
Question 22
Queue is empty in the circular array implementation when
Select one:

a. None of the above

b. f=r

c. f=0

17
d. r=0
Feedback
The correct answer is: f=r
Question 23
Queue is full in the circular array implementation when
Select one:

a. f-r=0

b. f-r=1

c. r-f=1

d. None of the above


Feedback
The correct answer is: f-r=1
Question 24
Consider an exeperiment of rolling a die and tossing a coin. The size of the sample space is
Select one:

a. None of the above

b. 2

c. 12
O(n^2)

d. 36

e. 6
Feedback
The correct answer is: 12
Question 25
We toss a coin until a head appears. What is the expected number of coin tosses?
Select one:

a. 2

b. None of the above

c. 4

d. 0.5

e. 1
Feedback
The correct answer is: 2

18
BITS WILP Data Structures and Algorithms Design Quiz-3
2017-H1

Question 1
Consider the experiment of tossing a coin until 10 heads appear. Then expected
number of tosses is

Select one:

a. 2

b. 20

c. None of the above

d. 11

e. 10
Feedback
The correct answer is: 20
Question 2
Total number of comparisons needed for merge(L1,L2) where L1 is 2,4,6,8 and
L2 is 10, 12, 13,15, 17,19
Select one:

a. 6

b. 10

c. 9

19
d. None of the above

e. 4
Feedback
The correct answer is: 4
Question 3
Consider the problem of sorting a sequence in ascending order. If the input is
already in ascending order, which of the following sorting procedure is most
efficient.

Select one:

a. Merge Sort

b. Quick Sort

c. Heap  Sort

d. None of the above

e. Insertion Sort
Feedback
The correct answer is: Insertion Sort
Question 4
Suppose the input to Quick sort is 1,2,...17. What would be the best pivot
element during the first invocation?
Select one:

a. 9

b. 1

c. None of the above

d. 2

e. 17
Feedback
The correct answer is: 9
Question 5
Let G be a simple undirected graph with n vertices. Then number of edges in G
is
Select one:

a. at least n(n-1)/2

b. at least n

c. at most n

20
d. None of the above

e. at most n(n-1)/2
Feedback
The correct answer is: at most n(n-1)/2
Question 6
Let T be a tree with m edges. Then the number of vertices in T is
Select one:

a. None of the above

b. exactly m+1

c. exactly m

d. exactly m-1

e. at most m
Feedback
The correct answer is: exactly m+1
Question 7
Let A be an adjacency matrix of an undirected graph in G. Then sum of all
entries in the matrix is equal to

Select one:

a. Number of edges in G

b. Number of  vertices in G

c. Twice the number of edges in G

d. None of the above

e. Twice the number of vertices in G


Feedback
The correct answer is: Twice the number of edges in G
Question 8
Worst case running time for quick sort is
Select one:

a. O(nlogn)

b. O(n^2)

c. None of the above

d. O(n)

21
e. O(log n)
Feedback
The correct answer is: O(n^2)

Question 9
Suppose an undirected graph, which has n vertices and d maximum degree, is
represented using adjacency list. The running time to find the degree of a given
vertex is
Select one:

a. O(d)

b. O(1)

c. O(logn)

d. O(n)

e. None of the above


Feedback
The correct answer is: O(d)
Question 10
Consider the experiment of  tossing a coin until a head appears. The number of
elements in the sample space is
Select one:

a. None of the above

b. 0

c. 4

d. 2

e. 1
Feedback
The correct answer is: None of the above
Question 11
Suppose a directed graph is represented using adjacency list. The running time
to calculate indegree of a vertex is
Select one:

a. O(log n)

b. None of the above

c. O(m+n)

22
d. O(n)

e. O(d)
Feedback
The correct answer is: O(n)
Question 12
Which of the following statements is correct.
i) Any comparison based algorithm must perform Ω(nlogn) comparisons to sort n
elements in the worst case
ii. Any comparison based algorithm must perform Ω(nlogn) comparisons to sort n
elements in the best case
Select one:

a. Both of them is true

b. None of them is true

c. ii only true

d. i only true

e. None of the above


Feedback
The correct answer is: i only true
Question 13
The space complexity to represent a graph with n vertices and m edges using
adjacency matrix is
Select one:

a. None of the above

b. O(m+n)

c. O(n^2)

d. O(m)

e. O(n)
Feedback
The correct answer is: O(n^2)
Question 14
Average case running time for quick sort is
Select one:

a. O(n)

b. O(n^2)

23
c. None of the above

d. O(nlogn)
Feedback
The correct answer is: O(nlogn)
Question 15
Worst case running time for merge sort is
Select one:

a. O(n)

b. O(n2)

c. O(nlog logn)

d. O(nlog n)

e. None of the above


Feedback
The correct answer is: O(nlog n)
Question 16
Assume the keys are inserted in the following order. 1055, 1492, 1776, 1812,
1918, 1945.
1812 is stored in the slot ___________ if double hashing policy is used with
h_1(k) = 5*x mod 8 and h_2(x) = 1+ (k mod 7).
Select one:

a. 7

b. None of the above

c. 4

d. 3

e. 1
Feedback
The correct answer is: None of the above
Question 17
Suppose a simple uniform hashing function is used with chaining. The expected
number of key comparisons in successful search is at most __________
Select one:

a. α

b. 1+α

c. 1+α/2-α/2n

24
d. 1

e. None of the above


Feedback
The correct answer is: 1+α/2-α/2n
Question 18
Which of the following statements are true?
i. In linear probing method, there are only m different probe sequences are
possible.
ii. In quadratic probing method,  there are m^2 different probe sequences are
possible
iii. In double hashing, there are only m different probe sequences are possible.
Select one:

a. i only true

b. None of the above

c. All of them are false

d. All of them are true

e. i and ii are true and iii is false


Feedback
The correct answer is: All of them are true
Question 19
1812 is stored in the slot ___________ if double hashing policy is used with h_1(k) = 5*x mod

8 and h_2(x) = 1+ (k mod 7).


Select one:

a. 1

b. None of the above

c. 7

d. 4

e. 3
Feedback
The correct answer is: 3
Question 20
Let T be a tree with a maximum degree d. Then the number of leaf vertices is
Select one:

a. at most d

25
b. 1

c. None of the above

d. exactly d

e. at least d
Feedback
The correct answer is: at least d

26
27
28
BITS WILP Data Structures and Algorithms Design Mid-Sem
Exam (Regular) 2016-H2

3. What is the average case running time for binary search algorithm on a sorted array
of n distinct integers? Justify your answer with a precise argument. (4Marks)

4. (a) Write a pseudocode for all stack operations (push, pop, size, top, isEmpty) when
stacks are implemented using arrays. (5Marks)
(b) Write a pseudocode for deleting a node from a doubly linked list. (2Marks)

5. (a) A certain Professor Amongus claims that if a binary tree which stores elements
in its internal nodes has the following property, then it is a binary search tree.
For every node v, the element stored at the left child of v is less than or equal to
the element stored at v and the element stored in the right child of v is greater
than or equal to the element stored at v.
Is the Professor making a correct claim? (3Marks)
(b) Write a pseudocode for finding a successor of an element k in a binary search
tree. (3Marks)

SS ZG519 (EC-2 Regular Mid-Sem Test) First Semester 2016-2017 

29
BITS WILP Data Structures and Algorithms Design End-Sem
Exam (Regular) 2016-H2
1. (a) Write a program to list out all the monotonic increasing subsequence of an array of
inte-
gers. For example, for the input is 1; 4; 2; 7; 9, output will be 1; 1; 4; 1; 2; 1; 7; 1; 4; 7;
1; 2; 7;
1; 9; 1; 4; 9; 1; 2; 9; 11; 7; 9; 1; 4; 7; 9; 1; 2; 7; 9; 4; 4; 7; 4; 9; 4; 7; 9; 2; 2; 7; 2; 9;
2; 7; 9; 7; 7; 9; 9;
(4Marks)
(b) What is the running time of your program and justify your answer. (3Marks)
(c) Prove that af(n) + bg(n) is O(max{f(n); g(n)}) where a and b are some constants.
(3Marks)

2. The goal of n-queens problem is to place n queens on a nn chessboard such that no


queen
attacks any other queen (A queen attacks any queen if it is in the same row, or column or
diagonal). Following is a

30
gure shows an attempted solution that fails (two queens on the
same diagonal) for 8-queens problem.

(a) Formulate the problem so that we can use a greedy algorithm: That is, describe the
states, initial state, successor states for each state. Which data structure will you use
to represent the state? (4Marks)
(b) Write an algorithm to generate all successor states of a given state? (3Marks)
(c) Write an algorithm to check whether no queens attack each other in a given state.
(3Marks)
(d) Provide a strategy to pick up the best state from the set of successor states of a given
state and justify why you think it is a best strategy. (2Marks)
3. Draw the hash table of size 11 resulting from hashing the keys 45, 93, 97, 58, 53, 105,
26,
41, 31.
(a) Using the hash function h(i) = (i - 5) mod 11) and assuming collisions are handled
by chaining. (3Marks)
SS ZG519 (EC-3 Regular Compre) First Semester 2016-2017 Page 1 of 2
(b) Using the same hash function but collisions are handled by linear probing. (2Marks)
(c) Using the same hash function but collisions are handled by quadratic probing. (2Marks)
(d) Using the same hash function but collisions are handled by double hashing with
secondary hash function h' where h'(k) is denoted as the least significant digit in k. For
example h'(45) = 5. (2Marks)
4. (a) Provide a best case instance for the heap sort algorithm. We are not assuming
anything
about the input and the best case running time is O(n). (5Marks)
(b) Modify insertion sort so that the output will be in decreasing order. (2Marks)
(c) We used decision trees to model comparison based algorithms for instances of size n.
Draw a decision tree for your algorithm for the input size 4. (4Marks)
5. (a) Construct the adjacency matrix and adjacency list for the following graph.

31
(4Marks)
(b) Construct a simple, connected, weighted graph with 7 vertices and 12 edges and each
with unique edge weights. Identify one vertex as a start vertex and illustrate a running
on Dijkstra's algorithm on this graph. (4Marks)

SS ZG519 (EC-3 Regular Compre) First Semester 2016-2017 

32
Birla Institute of Technology & Science, Pilani
Work-Integrated Learning Programmes Division
Second Semester 2015-2016

Comprehensive Examination
(EC-3 Regular)

Course No.                  : SS ZG519 


Course Title                 : DATA STRUCTURES AND ALGORITHM DESIGN 
Nature of Exam           : Open Book
Weightage                    : 50%
Duration                      : 3 Hours 
Date of Exam              : 09/04/2016    (AN)
No of pages: 2
No of questions: 6
Note:
1.       Please follow all the Instructions to Candidates given on the cover page of the answer
book.
2.       All parts of a question should be answered consecutively. Each answer should start from a
fresh page. 
3.       Assumptions made if any, should be stated clearly at the beginning of your answer.

Q.1.        Let A[1…n] be an array of n distinct numbers. If i < j and A[i]  > A[j], then the pair (i,
j) is called an inversion of A.
(a)             Which array with elements from the set {1,2,…n} has the most inversions? How many
does it have?
(b)             Describe an algorithm that determines the number of inversions in any permutation
on n elements in Q(n logn) worst case time.                                                  [4 + 4 = 8]

Q.2.        A certain Professor Amongus claims that the order in which a fixed set of elements is
inserted into an AVL tree does not matter - the same tree results every time. Give a
example that proves Professor Amongus
wrong.                                                               [8]                                   

Q.3.        Let G be a graph whose vertices are the integers 1 through 8, and let the adjacent vertices
of each vertex be given by the table below:

Vertex Adjacent vertices


1 2, 3, 4
2 1, 3, 4
3 1, 2, 4
4 1, 2, 3, 6
5 6, 7, 8
6 4, 5, 7
7 5, 6, 8
8 5, 7

Assume that, in a traversal of G, the adjacent vertices of a given vertex are visited in the
same order as they are listed in the above table.

(a)             Draw G.

33
(b)             Show how depth-first search works on the graph G starting from vertex 1. Also identify
discovery and back edges.                                                                  [3 + 5 = 8]

SS ZG519 (EC-3 Regular)                 Second Semester 2015-


2016                                      Page 2

Q.4.        Does the strategy of choosing items in increasing order of weight yield an optimal solution
for the fractional knapsack problem? Justify your
answer.                                         [8]        

Q.5.        Find the minimum cost spanning tree for the following graph using Kruskal’s
algorithm?  Show all the intermediate steps.        [8]                                                    

 
   
Q.6.        Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car’s
gas tank, when full, holds enough gas to travel n miles, and his map gives the distances
between gas stations on his route. The professor wishes to make as few gas stops as
possible along the way. Give an efficient method by which Professor Midas can determine at
which gas stations he should stop, and prove that your strategy yields an optimal
solution.                                                                                                                          
  [10]

34

You might also like