Understand how Warshall’s Algorithm helps answer the question: “Can I get from one node to another, even through other nodes?”
You’re building a simplified social network like Facebook.
❓ Goal: Figure out who each person may indirectly know ("friend-of-a-friend") using Warshall’s Algorithm.
Fill this matrix with 1
(yes) or 0
(no):
Alice | Bob | Carol | Dave | |
---|---|---|---|---|
Alice | 0 | 1 | 1 | 0 |
Bob | 1 | 0 | 0 | 1 |
Carol | 0 | 0 | 0 | 1 |
Dave | 0 | 1 | 0 | 0 |
Apply the formula:
R[i][j] = R[i][j] OR (R[i][k] AND R[k][j])
Do this for k = 0 to 3 (each user).
Use extra sheets or whiteboards to update the matrix each time k
changes.
After applying Warshall’s Algorithm, complete the matrix:
Alice | Bob | Carol | Dave | |
---|---|---|---|---|
Alice | ||||
Bob | ||||
Carol | ||||
Dave |
Use the matrix to write friend suggestions:
Though Warshall’s algorithm is typically taught for foundational graph concepts, large-scale social networks rely on multi-layered recommendation engines that combine graph-based heuristics (e.g., mutual friends) with sophisticated machine learning. They use advanced embedding techniques (like node2vec, DeepWalk, or GNNs) to capture user similarity in a high-dimensional space. Finally, they employ distributed systems and approximate nearest neighbor searches to handle billions of users and provide real-time friend suggestions.