Question: The difference between two sequences of the small length {a1, a2, a3, ..., an} and {b1,b2,b3,...bn} can be defined as the sum of the absolute
The difference between two sequences of the small length {a1, a2, a3, ..., an} and {b1,b2,b3,...bn} can be defined as the sum of the absolute differences between their respective elemenets:
diff(a,b)={ |a1-b1|, |a2-b2|, |a3-b3|, ..., |an-bn| }
For the given sequences a and b {not neccessarily having the same lengths), find the subsequence a' or a and sequence b' of b such that diff{a',b') is minimal. Return the diffrence.
Example:
A ={1,2,6}, b={0,1,2,3,4,5}, the output should be smallestDifference(a,b) = 2, where the best subsequence will b' = {1,3,5}
The goal of this assignment is to use dynamic programming techniques to write a recursive solution to the problem, convert that solution to an amortized solution, and then to identify a way to convert that solution to a loop-based solution.
I need help implementing a recursive version of the smallestDifference function in the smallestDifference_recursive_utility function. There is no time restriction on this solution (it will take a while to run larger tests) I need to implement an amortized version of the smallestDifference function in the smallestDifference)amortized_utility function. The maximum time for this function to identify the solution is 500ms (regardless of input size) I need to implement a loop-based version of the smallestDifference function in the smallestDifference_optimized function. The maximum time for this funcion to identify the solution is 500ms (regardless of input size) The length of a will be in the range [3,1000] The length of b will be in the range [a.length, 1000] The values in each array will be in the range [-1000,1000] Only smallest_difference.cpp get changed the rest stay untouched.
main.cpp
#include
#include "smallest_difference.h"
typedef int (*differenceFunction)(const std::vector
void printSet(const std::vector
void findDifference(const std::vector
int main(int argc, char** argv) { if (argc != 3) { std::cout << "Usage: " << argv[0] << " a-sequence-file b-sequence-file" << std::endl; return 0; }
std::ifstream aIn(argv[1]); std::ifstream bIn(argv[2]);
std::vector
std::copy(std::istream_iterator
std::cout << "Looking for the smallest difference between the following sets:" << std::endl; printSet(a, "a"); printSet(b, "b");
std::cout << "Using Recursive function: "; findDifference(a, b, smallestDifference_recursive); std::cout << std::endl; std::cout << "Using Amortized function: "; findDifference(a, b, smallestDifference_amortized); std::cout << std::endl;
std::cout << "Using Optimized function: "; findDifference(a, b, smallestDifference_optimized); std::cout << std::endl;
return 0; }
smallest_difference.cpp
#include
/*********************************************************************\ Smallest Difference using Recursive techniques \*********************************************************************/ int smallestDifference_recursive_utility(const std::vector
}
int smallestDifference_recursive(const std::vector
/*********************************************************************\ Smallest Difference using Recursive techniques with Amortization \*********************************************************************/
int smallestDifference_amortized_utility(const std::vector
int smallestDifference_amortized(const std::vector
/*********************************************************************\ Smallest Difference using non-Recursive techniques \*********************************************************************/ int smallestDifference_optimized(const std::vector
smallest_difference.h
#pragma once #ifndef __SMALLEST_DIFFERENCE_H__ #define __SMALLEST_DIFFERENCE_H__
#include
int smallestDifference_recursive(const std::vector
#endif // __SMALLEST_DIFFERENCE_H__
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
