Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:58:32+00:00 2026-06-04T04:58:32+00:00

My collection contains: { user_id : 1, product_id : 1 }, { user_id :

  • 0

My collection contains:

{ user_id : 1, product_id : 1 },
{ user_id : 1, product_id : 2 },
{ user_id : 1, product_id : 3 },
{ user_id : 2, product_id : 2 },
{ user_id : 2, product_id : 3 },
{ user_id : 3, product_id : 2 },

My collection track product viewed by a user where user_id is ID of user and product_id is ID of product.
I want to compute similarity between two users, e.g. number of product they both viewed.
For example from collection above, similarity between users will be

{ user_id1 : 1, user_id2 : 2, similarity : 2 },
{ user_id1 : 1, user_id2 : 3, similarity : 1 },
{ user_id1 : 2, user_id2 : 3, similarity : 1 },

Edited

I’ve done it without map-reduce

def self.build_similarity_weight
  users_id = ProductView.all.distinct(:user_id).to_a
  users_id.each do |user_id|
    this_user_products = ProductView.all.where(user_id: user_id).distinct(:product_id).to_a

    other_users = users_id.map { |e| e } 
    other_users.delete_if { |x| x == user_id }

    other_users.each do |other_uid|
      other_user_products = ProductView.all.where(user_id: other_uid).distinct(:product_id).to_a
      user_sim = (other_user_products & this_user_products).length
      usw = UserSimilarityWeight.new(user_id1: user_id, user_id2: other_uid, weight: user_sim)
      usw.save
    end
  end
end

The problem is my code is not efficient, O(n2), where n is number of users.
How can I make my code more efficient using map-reduce?

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:58:35+00:00Added an answer on June 4, 2026 at 4:58 am

    First, you do 2 mapreduces.

      • map: Omit product_id as key and user_id as value
      • reduce: iterate with loop within a loop the value list (list of user ids for each product) and omit as key pair of user ids (where the smallest user id is the first one) and value 1
    1. (working on the result of the first map reduce)

      • map: just pass the pair of users as key and the value of 1 as value
      • reduce: sum the value for each pair.

    Second, you can’t be more efficient than O(n2) because your result is of order of O(n2).
    Meaning, even if in some magically way, you will get the pairs and the similarity, you still need to write n^2 of pairs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control that contains a collection of controls to be reused
I have a collection that contains all the items that I want to keep
Example class User has_many :tickets end I want to create association which contains logic
I need to select User Name from the collection of Users. I do it
I have a collection that contains user objects, each with a unique ID and
I have collection of documents Users User { Status: ACTIVE, Login: { UserName: login,
How to combine 2 collections in such a way that the resultant collection contains
I have an entity class Foo foo that contains Collection<Bar> bars . I've tried
Suppose I have a solution inside a TFS collection that contains 3 projects. Ho
I have a REST service that returns a collection that contains non- normalized data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.