0% found this document useful (0 votes)
60 views2 pages

CS207A: Data Structures and Algorithms (Module #3) Assignment #1

This document outlines 3 programming assignments for a data structures and algorithms course. Assignment 1 involves (a) implementing a coin selection algorithm and (b) solving the knapsack problem. Assignment 2 extends the optimal binary search tree problem to allow for search misses. Assignment 3 involves using dynamic programming to find the largest square of zeros in a binary matrix. Input and output formats are specified for automated grading. The total marks are 55, with 10 marks for part 1, 20 marks for part 2, and 15 marks for part 3.

Uploaded by

Ankush Bhaal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

CS207A: Data Structures and Algorithms (Module #3) Assignment #1

This document outlines 3 programming assignments for a data structures and algorithms course. Assignment 1 involves (a) implementing a coin selection algorithm and (b) solving the knapsack problem. Assignment 2 extends the optimal binary search tree problem to allow for search misses. Assignment 3 involves using dynamic programming to find the largest square of zeros in a binary matrix. Input and output formats are specified for automated grading. The total marks are 55, with 10 marks for part 1, 20 marks for part 2, and 15 marks for part 3.

Uploaded by

Ankush Bhaal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS207A: Data Structures and Algorithms (Module #3)

Assignment #1

Max marks: 55
Due on/before:17.00, 2-July-2017 23-June-2017

Note: All questions will be graded using an automated judge so please ensure that your programs follow
the input-output instructions exactly.

1. (a) For the first coin game discussed in class implement a function that prints out the coins that are
chosen. Your program should read in input from a file called INPUT which has the following
structure.
1st line: n - the number of coins in the sequence.
2nd line: c1 c2 . . . cn
It should write its output in a file called OUTPUT where the chosen coins ci are printed separated
by a single space and terminated at the end by a newline.
(b) For the Simple Knapsack Problem write a program that finds the set of items that goes into the
knapsack for which the value is a maximum. The program should read its input from a file called
INPUT which has the following structure:
1st line: n - the number of items.
2nd line: c1 v1 c2 v2 . . . cn vn
3rd line: C - capacity of the knapsack.

It should write the output into a file called OUTPUT with the following format:
ci1 ci2 . . . cim
where ci1 ≤ ci2 ≤ . . . ≤ cim
[10,10=20]

2. For the optimal binary search tree (BST) we assumed that the search key was always present in the
tree. In general this is not true. The search key may not be present in the tree. We can model this
situation as follows:
For each i, i = 1..(n − 1), assume we have probability qi which gives the probability that a search key,
say k lies between ki and ki+1 , that is ki < k < ki+1 - remember the keys k1 , . . . , kn are in increasing
order.In addition, let q0 be the probability that a search key k < k1 and qn be the probability that
k > kn . So, in addition to probabilities p1P, . . . , pn forPa hit in the tree we have probabilties q0 , . . . , qn
for a miss in the tree. Of course, we have ni=1 pi + j=0n qj = 1.
Implement the optimal BST algorithm to find the optimal BST and the minimum expected value of
the number of comparisons. The file INPUT in which all input is given should have the following
structure:
1st line: n - number of keys.
2nd line: p1 p2 . . . pn
3rd line: q0 q1 . . . qn

The output should be in file OUTPUT with the following format:


1st line: x - the expected value for the number of comparisons in the optimal binary tree.
2nd line: BST - written as a nested linear structure using round brackets. The tree should be written
recursively as follows: (root(left-BST)(right-BST)).
[20]

3. Given an m × n binary array (or matrix) using dynamic programming to find the largest contiguous
square containing only zeroes in the matrix/array. This is useful in locating large free area on a
computer screen or land for construction. The input will be in file INPUT and structured as follows:
1st line: m n x - where m, n is row size and column size, x is the number of 1s in the matrix/array.
2nd line: i1 j1 i2 j2 . . . ix jx - locations of the x ones in the array or matrix.
The output should be in file OUTPUT and should give (i, j) coordinates of the top left and bottom
right 0 entries without brackets. For example, if the square is (2, 3) till (7, 8) the output should be:
2 3 7 8. [15]

Page 2

You might also like