Question
Your team is working on an online shopping website that has been receiving increased traffic over the last few months due to the introduction of
Your team is working on an online shopping website that has been receiving increased traffic over the last few months due to the introduction of new products and an increase in popularity. The growth in the number of visitors has resulted in the website becoming slower with each passing day. This is mainly because the algorithms used to design the initial website have a high runtime complexity, which worked well when the numbers of customers were low, but with the growth of the business, the website needs well-developed algorithms with a low runtime complexity to handle the dramatic increase in workload.
This is the code I already have
CODE:
import java.lang.Math;
public class SimilarCustomer { // rename as SimilarCustomer
/**
* This methods finds the most similar customer to the given customer using
* Euclidean distance
*
* @param customerBehaviorMatrix data from online store
* @param currentCustomerIndex current customer visiting the online store
* @return index of the most similar customer wrt given customer index
*/
public int findMostSimilarCustomer(double[][] customerBehaviorMatrix, int currentCustomerIndex) {
customerBehaviorMatrix = calculateNormalizedCustomerBehaviorMatrix(customerBehaviorMatrix);
double similarity[] = new double[customerBehaviorMatrix.length];
int min = 0;
for(int i=0;i similarity[i] = calculateEuclideanDistance(customerBehaviorMatrix[i],customerBehaviorMatrix[currentCustomerIndex]); // System.out.print(similarity[i]);System.out.print(" "); if( similarity[min] > similarity[i] && i!=currentCustomerIndex || min == currentCustomerIndex)min = i; } return min; } /** * This method gets a customer index and normalized customer behavior data then * returns the most similar customer to given one * @param normalizedCustomerBehaviorMatrix normalized customer behavior data * @param currentCustomerIndex index of the customer which will be the base of search * @return index of most similar other customer */ public int findMostSimilarCustomerIndex(double[][] normalizedCustomerBehaviorMatrix, int currentCustomerIndex) { double similarity[] = new double[normalizedCustomerBehaviorMatrix.length]; int min = 0; for(int i=0;i similarity[i] = calculateEuclideanDistance(normalizedCustomerBehaviorMatrix[i],normalizedCustomerBehaviorMatrix[currentCustomerIndex]); // System.out.print(similarity[i]);System.out.print(" "); if( similarity[min] > similarity[i] && i!=currentCustomerIndex || min == currentCustomerIndex)min = i; } return min; } /** * This method normalizes given customer behavior matrix * @param customerBehaviorMatrix customer Behavior data * @return normalized customer behavior matrix */ public double[][] calculateNormalizedCustomerBehaviorMatrix( double[][] customerBehaviorMatrix) { //define a new return matrix double normalizedCustomerBehaviorMatrix[][] = new double[customerBehaviorMatrix.length][customerBehaviorMatrix[0].length]; double minmax[][] = new double[2][customerBehaviorMatrix[0].length]; minmax= findMinMaxValuesOfEachColumn(customerBehaviorMatrix); // for each entry normalize value and store in return matrix for(int i=0;i for(int j=0;j normalizedCustomerBehaviorMatrix[i][j] = customerBehaviorMatrix[i][j]/minmax[1][j]; //Assign the normalized value return normalizedCustomerBehaviorMatrix; } /** * This method finds min and max values of the customer behavior matrix * and stores them in a matrix * * @param costumerBehaviourMatrix data from online store * @return 2 row matrix with first row showing min of each column and * second row showing max of each */ public double[][] findMinMaxValuesOfEachColumn(double[][] customerBehaviorMatrix) { //define a new return matrix double minmax[][] = new double[2][customerBehaviorMatrix[0].length]; //assign min and max values to return matrix //for each column find min and max //System.out.println(customerBehaviorMatrix[0].length); //System.out.println(customerBehaviorMatrix.length); for(int i=0;i int min = 0,max = 0; for(int j=0;j if(customerBehaviorMatrix[j][i] else if(customerBehaviorMatrix[j][i] > customerBehaviorMatrix[max][i]) max = j; } minmax[0][i] = customerBehaviorMatrix[min][i]; minmax[1][i] = customerBehaviorMatrix[max][i]; } return minmax; } /** * This methods calculates Euclidean distance between two equally size vector * * @param vectorOne First vector * @param vectorTwo Second Vector * @return distance (double) between given two vectors */ public double calculateEuclideanDistance(double[] vectorOne, double[] vectorTwo) { //check if vectors have same length if(vectorOne.length != vectorTwo.length)return -1; double distance = 0; for(int i=0;i return Math.sqrt(distance); } public static void main(String[] args) { /* * This main method is a stub. * It does nothing. * Feel free to write your own code to test your implementation. * In this case, we have nothing actionable in here, just this comment block, so the JVM should rapidly lose interest and move on to the rest of your code. */ SimilarCustomer m = new SimilarCustomer(); double data[][] = { {1,2,3,4}, {4,5,6,7}, {2,3,4,5} }; System.out.println(m.findMostSimilarCustomer(data,0)); } } I am having trouble with the following check parts
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started