DibyasomPuhan - DAA Assignment 2
DibyasomPuhan - DAA Assignment 2
DibyasomPuhan - DAA Assignment 2
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)
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.
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 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
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?
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.
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.
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
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.
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
that: