Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

DAA Assignment-2

Greedy Algorithms are hungry?

Dibyasom Puhan
06/11/2020
1

Introduction
I’m Dibyasom Puhan, of AIML-B3. SAP-id: 500076104.
This assignment contains my algorithms and code to solve
questions given in assignment-2.

Q1/
a)

Show that every comparison based (i.e.,


decision tree) algorithm that sorts 5 elements, makes at least 7 comparisons
in the worst-case.

Hence proved, comparison-based sorting algorithm makes at least 7-comparisons to sort an array of
5-elements. (Worst Case)
2

b) Give a comparison based (i.e., decision tree) algorithm that sorts 5 elements
using at most 7 comparisons in the worst case.

Merge-Sort is a typical comparison-based sorting algorithm.


It will make utmost 7-comparisons to sort an array of 5-elements in the worst-case sequence.

Worst-Case sequence for merge sort can occur when the corresponding elements in left and
right subarray are actually the alternate elements of the sorted array or when all the elements
of the array are the same, because it would have to compare every element.

Proof by Brute-Force.

To prove, merge sort makes utmost 7-comparisons in the worst case.

To prove this via Brute-Force I made an algo, which generates arrays of size ‘5’ filled with
random values in every iteration, and are in worst-case sequence (All elements are same, in my
case) and counts the number of comparisons made by merge sort. I’ve. Code used
Comparison-Sort-Simulator

Output >
3

Number of comparisons made by merge-sort to sort 1500 random generated arrays.

We can see the result, 100% of


the times, merge sort
makes at most 7-
comparisons to sort th
array.(Which is in wors
case).

Since, for any arbitrary value fil


array, merge-sort make
comparisons when the
array is in worst-case
sequence, I conclude th
for the given-question
merge sort is the answ

And the number of compariso


is 7 only in worst-case,
because for any other
sequence no. of
comparisons also change, Output of the code >
4

a) Given a Binary Search Tree (BST) holding n keys, give an efficient algorithm to print
those keys in sorted order. What is the running time of the algorithm?

Algorithm and Asymptotic analysis,

Implementing this algorithm into code(BST-flatten-inorder), and here’s a snapshot of the output.
5

a) Within the decision tree model derive a lower bound on the BST construction

problem, i.e., given a set of n keys in no particular order, construct a BST that holds those n
keys.

Algorithm and theory >


6

Implementing the above mentioned algorithm into code (BST-construction), and here’s the output of
generating a BST from a non-sorted array.
7

Q3/ Coin Change making: For each of the following coin denomination systems
either argue that the greedy algorithm always yields an optimum solution for
any given amount, or give a counter-example:
(a) Coins c0 , c1 , c2 , …, cn-1 , where c is an integer > 1.
(b) Coins 1, 7, 13, 19, 61.
(c) Coins 1, 7, 14, 20, 61.
A3/ Greedy algorithm to find the minimum number of coins that would suffice the value.

Implemented the above algorithm in code, but the question says to verify the correctness of the
algo in specific cases.
8

To verify the correctness of greedy algorithm in given specific test-cases, I’ve developed a
Brute-Force based Test-Bench that uses a Dynamic Programming based algorithm (Which is
believed to solve all edge cases) and compares the result to that of greedy algorithm for
randomly generated arrays 10000 time, and in-case both the outputs don’t match, means Greedy
Algorithm has failed, because DP is a complete solution for this problem statement.

Code Implemented > Coin-Exchange-Simulator.

A/ Test-Case-1 >> Coins [c^0 , c^1 , c^2 , …, c^(n-1)] where c is an integer > 1.

Result from Brute-Force Testbench for 10000 iterations, every iteration having random value of C,
N, and TotalValue to be expressed as coins. Since for every 10000 iteration the result of
Greedy Algo matches that of the DP approach, for any arbitrary “TotalValue to be exchanged”,
Greedy Algo yields an optimum solution for this test-case.
9

B/ Test-Case-1 >> Coins [1, 7, 13, 19, 61 ].

Result from Brute-Force Testbench for 10000 iterations, every iteration having random value of
TotalValue to be expressed as coins. Since for every 10000 iteration the result of Greedy Algo
matches that of the DP approach, for any arbitrary “TotalValue to be exchanged”, Greedy Algo
yields an optimum solution for this test-case.

C/ Test-Case-1 >> Coins [1, 7, 14, 20, 61].

Result from Brute-Force Testbench for 10000 iterations, every iteration having random value of C,
N, and TotalValue to be expressed as coins. During the second iteration, loop breaks as result
from DP and greedy don’t match.
10

Random Value generator puts ‘28’ as the TotalValue_toBeExchanged_into_coins and Greedy


Approach fails this test-case.

Greedy Approach picks [20,


7, 1] as the solution which
requires 3-coins. But it’s
wrong since 2 coins of
denomination 14 would do
suffice here. Some other
counterexamples are 42,
43, etc..

Contd. In next page >>


11

Q4/ Balls and Boxes:

We have 𝑛 balls, each with at most weight 1. More specifically, the

input is an array of weights 𝑊 [1. . 𝑛] , where 𝑊[𝑖] is the weight of the

𝑖th ball, 0 ≤ 𝑊[𝑖] ≤ 1, 𝑖 = 1. . 𝑛.

The problem is to put these balls in a minimum number of boxes so

that:

i. each box contains no more than two balls, and

ii. the total weight of the balls placed in each box is ≤ 1.


A4/ Algorithm and time complexity analysis.

b/Designing an efficient greedy algorithm for this problem.


12

Code implemented based on above mentioned algorithm, [Uploaded here]


a) Show an optimum solution for the following instance:
W = [0.36, 0.45, 0.91, 0.62, 0.53, 0.05, 0.82, 0.35].
Output of code, for this test-case.

Output with different test-case.


13

You might also like