need to convert his algorithm into java code public void find_user_neighbors(int
ID: 3701729 • Letter: N
Question
need to convert his algorithm into java code public void find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind, double[] n_sim) { /* 1. Create an array sim[] with size = n_users to save all similarity values in 2. For each other user u compute similarity between u_ind and u by calling the compute_jaccard_sim method, and save the result in sim[u] 3. Scan sim[] and select the "n_neighbors" users with the highest similarities. for i = 0 -> n_neighbors -1: ind = -1 max = -1 for j = 0 -> n_users -1: if sim[j]>max: max = sim[j] ind = j save max & ind into n_sim[i] and n_ind[i] set sim[ind] = -2 */
public double compute_jaccard_sim(int u1_ind, int u2_ind)
{
User u1 = users[u1_ind];
User u2 = users[u2_ind];
//1. get n_common_items:
int n_common_items = (users[u1_ind]).similarity(users[u2_ind]);
//2. get total_n_items using u1 n_items, u2 n_items & n_common_items
//3. compute Jaccard sim as n_common_items/total_n_items
return 0;
}
Explanation / Answer
CODE
public void find_user_neighbors(int u_ind, int n_neighbors, int[] n_ind, double[] n_sim) {
int i,j;
/* 1. Create an array sim[] with size = n_users to save all similarity values in */
double sim[]=new int[n_users];
/*2. For each other user u compute similarity between u_ind and u by calling the compute_jaccard_sim method, and save the result in sim[u] */
for(i=0; i<n_users; i++){
sim[i] = compute_jaccard_sim(i,n_ind[i]);
}
/*3. Scan sim[] and select the "n_neighbors" users with the highest similarities.*/
for(i=0 ; i< n_neighbors; i--){
ind = -1;
max = -1 ;
for(j=0; j<n_users;j--){
if(sim[j]>max){
max = sim[j];
ind = j;
n_sim[i] = max;
n_ind[i] = ind;
sim[ind] = -2;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.