Recommendation algorithm and implementation for finding similar items and users
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A recommendation system usually answers two related questions: which users behave similarly, and which items tend to be liked together. Even simple collaborative-filtering models can answer both questions by representing user behavior as vectors and comparing those vectors with a similarity metric such as cosine similarity.
User-Based And Item-Based Collaborative Filtering
User-based collaborative filtering looks for users whose rating patterns resemble the current user. Item-based collaborative filtering flips the matrix and looks for items that attract similar audiences.
Suppose you have a small ratings matrix where rows are users and columns are items:
Zeros here mean "no rating" for simplicity, though a production system should usually distinguish missing data from an actual zero score.
Computing Similarity Between Users
Cosine similarity is a common first step because it compares direction rather than raw magnitude.
A high value means the two users rated items in a similar pattern. Once you can compute one pair, you can rank all neighbors for a target user.
Recommending Items To A User
A basic user-based recommender can score unseen items using the weighted ratings of similar users.
This is intentionally small and readable rather than industrial-strength. It demonstrates the structure of a recommendation pipeline without requiring extra libraries.
Finding Similar Items
Item-based recommendations often perform better in systems with many users and a stable item catalog. The idea is to compare columns instead of rows.
This gives a ranked list of items consumed by similar users.
Data Sparsity Matters
Real recommendation data is sparse. Most users interact with only a small fraction of the catalog, so dense matrix code becomes wasteful quickly. Production systems often use sparse-matrix representations, approximate nearest-neighbor search, or factorization models. Still, the small vector approach is the right place to learn the mechanics.
Choosing A Similarity Metric
Cosine similarity is popular because it is easy to compute and works reasonably well for many implicit-feedback problems. Pearson correlation can be useful when you care more about rating trends than absolute values. Jaccard similarity is often a better fit when the data is binary, such as whether a user clicked or purchased an item.
The best metric depends on what the numbers mean, not on which formula is most common in tutorials.
Common Pitfalls
The first mistake is treating missing data as a real zero without thinking through the effect on similarity. Another is recommending items the user already consumed because the scoring step forgot to exclude known interactions. Developers also often build a beautiful similarity matrix before asking whether the catalog is too sparse for memory-heavy dense operations. Finally, recommendation quality depends heavily on evaluation, so do not assume a mathematically neat similarity score automatically produces useful product behavior.
Summary
- User-based filtering compares users; item-based filtering compares items.
- Cosine similarity is a good baseline for vector-style recommendation experiments.
- A simple weighted-neighbor score can recommend unseen items.
- Real systems must handle sparse data and evaluation carefully.
- The right similarity metric depends on whether your data represents ratings, clicks, or another signal.
Related reading
- Recommendation Algorithms for tweets in C
- Recommendations for Fast Multipole Method implementation?
- Recommendations for using graphs theory in machine learning?
- Recommendations for using graphs theory in machine learning?
- Recommended Open Source C algorithms data structures libraries
- reconstructing a tree from its preorder and postorder lists
- Reconstructing the list of items from a space optimized 0/1 knapsack implementation
- rectilinear polygon intersection

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.