Chapter 7

Social Networks and Recommendation Systems

Social networks and recommendation systems are quintessential graph database applications. This chapter explores how to model, query, and analyze social graphs, and build sophisticated recommendation engines using graph algorithms.

Social Network Modeling

Core Social Graph Schema

// Users and profiles
CREATE (alice:User:Person {
  id: 'user_001',
  username: 'alice',
  name: 'Alice Johnson',
  email: 'alice@example.com',
  joinedDate: date('2020-01-15'),
  bio: 'Software engineer and coffee enthusiast'
})

// Social relationships
CREATE (alice)-[:FOLLOWS {since: date('2020-02-01')}]->(bob:User)
CREATE (alice)-[:FRIENDS_WITH {since: date('2020-01-20'), confirmed: true}]->(carol:User)

// Content
CREATE (post:Post {
  id: 'post_001',
  content: 'Just learned about graph databases!',
  timestamp: datetime('2024-01-15T10:30:00'),
  likes: 0,
  shares: 0
})
CREATE (alice)-[:POSTED]->(post)

// Interactions
CREATE (bob)-[:LIKED {timestamp: datetime('2024-01-15T11:00:00')}]->(post)
CREATE (carol)-[:COMMENTED {
  text: 'Great topic!',
  timestamp: datetime('2024-01-15T11:15:00')
}]->(post)

Advanced Social Features

Groups and Communities

CREATE (group:Group {
  id: 'group_001',
  name: 'Graph Database Enthusiasts',
  description: 'Learning and sharing about graph databases',
  createdDate: date('2020-03-01'),
  memberCount: 0
})
CREATE (alice)-[:MEMBER_OF {role: 'admin', joinedDate: date('2020-03-01')}]->(group)

Privacy and Visibility

// Privacy settings on posts
CREATE (post:Post {
  content: 'Private thought',
  visibility: 'friends_only',
  allowComments: true
})

// Query respecting privacy
MATCH (viewer:User {id: $viewerId}), (post:Post)
WHERE post.visibility = 'public'
   OR (post.visibility = 'friends_only' 
       AND (viewer)-[:FRIENDS_WITH]-(:User)-[:POSTED]->(post))
   OR (viewer)-[:POSTED]->(post)
RETURN post

Common Social Network Queries

Friend Recommendations

// Suggest friends: friends of friends
MATCH (user:User {id: $userId})-[:FRIENDS_WITH]->()-[:FRIENDS_WITH]->(suggestion:User)
WHERE NOT (user)-[:FRIENDS_WITH]-(suggestion)
  AND user <> suggestion
WITH suggestion, count(*) AS mutualFriends
ORDER BY mutualFriends DESC
LIMIT 10
MATCH (suggestion)<-[:FRIENDS_WITH]-(mutual)-[:FRIENDS_WITH]->(user)
RETURN suggestion.name, 
       suggestion.bio,
       mutualFriends,
       collect(mutual.name) AS mutualFriendsList

Trending Content

// Find trending posts (last 24 hours)
MATCH (post:Post)
WHERE post.timestamp > datetime() - duration({hours: 24})
OPTIONAL MATCH (post)<-[:LIKED]-(liker)
OPTIONAL MATCH (post)<-[:SHARED]-(sharer)
WITH post, count(DISTINCT liker) AS likes, count(DISTINCT sharer) AS shares
WITH post, likes, shares, (likes * 1.0 + shares * 2.0) AS trendScore
RETURN post.content, 
       likes, 
       shares, 
       trendScore
ORDER BY trendScore DESC
LIMIT 20

User Feed Generation

// Personalized feed
MATCH (user:User {id: $userId})
MATCH (user)-[:FOLLOWS]->(followed:User)-[:POSTED]->(post:Post)
WHERE post.timestamp > datetime() - duration({days: 7})
OPTIONAL MATCH (post)<-[interaction:LIKED|COMMENTED]-(interactor)
  WHERE (user)-[:FOLLOWS]->(interactor)
WITH post, followed, count(interaction) AS relevanceScore
RETURN post.content,
       followed.name AS author,
       post.timestamp,
       relevanceScore
ORDER BY relevanceScore DESC, post.timestamp DESC
LIMIT 50

Building Recommendation Systems

Types of Recommendations

1. Collaborative Filtering

Recommend based on similar users' preferences:

// User-based collaborative filtering
MATCH (user:User {id: $userId})-[:PURCHASED]->(p:Product)
      <-[:PURCHASED]-(similar:User)
MATCH (similar)-[:PURCHASED]->(rec:Product)
WHERE NOT (user)-[:PURCHASED]->(rec)
WITH rec, count(DISTINCT similar) AS commonUsers
RETURN rec.name, 
       rec.description,
       commonUsers AS score
ORDER BY score DESC
LIMIT 10

2. Content-Based Filtering

Recommend similar items:

// Products similar to user's purchases
MATCH (user:User {id: $userId})-[:PURCHASED]->(p:Product)
      -[:IN_CATEGORY]->(cat:Category)<-[:IN_CATEGORY]-(similar:Product)
WHERE NOT (user)-[:PURCHASED]->(similar)
  AND p <> similar
WITH similar, count(DISTINCT cat) AS categoryOverlap
RETURN similar.name, 
       similar.price,
       categoryOverlap
ORDER BY categoryOverlap DESC
LIMIT 10

3. Hybrid Approaches

// Combine multiple signals
MATCH (user:User {id: $userId})

// Collaborative signal
OPTIONAL MATCH (user)-[:PURCHASED]->()<-[:PURCHASED]-(similar:User)
                      -[:PURCHASED]->(collab:Product)
WHERE NOT (user)-[:PURCHASED]->(collab)
WITH user, collab, count(DISTINCT similar) AS collabScore

// Content signal
OPTIONAL MATCH (user)-[:PURCHASED]->(p:Product)
                     -[:IN_CATEGORY]->(cat:Category)
                     <-[:IN_CATEGORY]-(collab)
WITH user, collab, collabScore, count(DISTINCT cat) AS contentScore

// Social signal
OPTIONAL MATCH (user)-[:FOLLOWS]->(friend:User)-[:LIKED]->(collab)
WITH collab, collabScore, contentScore, count(DISTINCT friend) AS socialScore

// Weighted combination
WITH collab, 
     (collabScore * 1.0 + contentScore * 0.5 + socialScore * 0.8) AS finalScore
RETURN collab.name, 
       finalScore
ORDER BY finalScore DESC
LIMIT 10

Advanced Recommendation Techniques

Time-Decay Weighting

// Recent interactions weighted more
MATCH (user:User {id: $userId})-[interaction:VIEWED|PURCHASED]->(item)
WITH item, interaction.timestamp AS timestamp
WITH item, 
     sum(exp(-duration.between(timestamp, datetime()).days / 30.0)) AS decayedScore
RETURN item.name, decayedScore
ORDER BY decayedScore DESC

Diversity and Serendipity

// Diverse recommendations across categories
MATCH (user:User {id: $userId})-[:PURCHASED]->(p:Product)
      -[:IN_CATEGORY]->(userCat:Category)
WITH user, collect(DISTINCT userCat.id) AS userCategories

MATCH (rec:Product)-[:IN_CATEGORY]->(cat:Category)
WHERE NOT rec.category IN userCategories
  AND NOT (user)-[:PURCHASED]->(rec)
WITH rec, cat, 
     CASE WHEN cat.id IN userCategories THEN 0 ELSE 1 END AS diversityBonus
RETURN rec.name, cat.name, diversityBonus
ORDER BY diversityBonus DESC, rand()
LIMIT 10

Session-Based Recommendations

// Recommend based on current session
MATCH (user:User {id: $userId})-[v:VIEWED]->(item:Product)
WHERE v.sessionId = $sessionId
WITH collect(item) AS sessionItems

MATCH (session_item)<-[:VIEWED]-(other:User)-[:VIEWED]->(rec:Product)
WHERE session_item IN sessionItems
  AND NOT rec IN sessionItems
WITH rec, count(*) AS coOccurrence
RETURN rec.name, coOccurrence
ORDER BY coOccurrence DESC
LIMIT 5

Evaluation Metrics

Metric Formula Interpretation
Precision@K Relevant in top K / K Accuracy of recommendations
Recall@K Relevant in top K / Total Relevant Coverage of relevant items
NDCG Normalized DCG Ranking quality
Diversity Unique categories in top K Recommendation variety

Real-Time Personalization

Stream Processing Integration

// Update recommendations as user browses
MATCH (user:User {id: $userId})
MERGE (session:Session {id: $sessionId, userId: $userId})
ON CREATE SET session.startTime = datetime()
ON MATCH SET session.lastActivity = datetime()

WITH user, session
MATCH (product:Product {id: $productId})
MERGE (session)-[v:VIEWED]->(product)
ON CREATE SET v.timestamp = datetime(), v.count = 1
ON MATCH SET v.timestamp = datetime(), v.count = v.count + 1

// Trigger recommendation update
WITH user, session
CALL recommendations.update(user, session)
YIELD recommendations
RETURN recommendations

A/B Testing Recommendations

// Track recommendation variants
CREATE (experiment:Experiment {
  id: 'exp_001',
  name: 'Collaborative vs Content',
  startDate: date(),
  variants: ['collaborative', 'content-based']
})

// Assign user to variant
MATCH (user:User {id: $userId}), (exp:Experiment {id: 'exp_001'})
MERGE (user)-[p:PARTICIPATES_IN]->(exp)
ON CREATE SET p.variant = exp.variants[toInteger(rand() * size(exp.variants))]
RETURN p.variant

// Track outcomes
MATCH (user)-[p:PARTICIPATES_IN]->(exp),
      (user)-[purchase:PURCHASED]->(product:Product)
WHERE purchase.timestamp > exp.startDate
CREATE (outcome:Outcome {
  userId: user.id,
  experimentId: exp.id,
  variant: p.variant,
  productId: product.id,
  timestamp: purchase.timestamp,
  revenue: purchase.amount
})

7.7 Simulator ENUM and Recommendation Algorithm Mapping

This section reconciles the recommendation ENUM tokens of the Graph Database Standard simulator with the procedures discussed in §7.1 through §7.6. The simulator exposes five recommendation tokens — COLLABORATIVE_FILTERING, CONTENT_BASED, MATRIX_FACTORIZATION, DEEP_LEARNING_RECOMMENDER, and GRAPH_NEURAL_NETWORK — reflecting the historical evolution of the field1. From the algorithm class, PAGERANK, BFS, LOUVAIN, NODE2VEC, and GRAPHSAGE are auxiliary tokens commonly applied directly on recommendation graphs2, and among the database engine ENUMs, NEO4J, NEPTUNE, and TIGERGRAPH are the most frequently deployed candidates for recommendation workloads.

Table 7.7.1. Simulator Recommendation ENUM and Primary Source Mapping
ENUM Token Era Representative Datasets Representative Metric Primary Source
COLLABORATIVE_FILTERING1992–MovieLens, Netflix PrizeRMSE 0.85–0.95Goldberg 19923
CONTENT_BASED2000–News, MusicF1 0.40–0.65Pazzani 2007
MATRIX_FACTORIZATION2009–Netflix PrizeRMSE 0.86Koren KDD 20094
DEEP_LEARNING_RECOMMENDER2017–Amazon, YelpHR@10 0.60+He WWW 20175
GRAPH_NEURAL_NETWORK2018–Pinterest, RedditNDCG@10 0.45+LightGCN 20206

The historical trajectory shown in Table 7.7.1 illustrates that recommendation systems have evolved through five paradigm shifts: collaborative filtering (1992), content-based methods (2000), matrix factorization (2009), deep learning (2017), and graph neural networks (2018)7. Importantly, no paradigm replaces its predecessors entirely; real-world industrial systems instead compose ensembles of multiple algorithms with dynamically tuned user-specific weights. Netflix, YouTube, and Amazon all operate matrix factorization and GNN models concurrently, with the relative weighting adjusted per session.

7.8 Graph Neural Network-Based Recommendation

Graph neural network (GNN) based recommendation operates over the bipartite user-item graph, learning embeddings through message-passing iterations6. Landmark models include NGCF (2019), LightGCN (2020), UltraGCN (2021), HCCF (2022), and SimGCL (2022). LightGCN in particular demonstrated that removing nonlinear activation and feature transformation from the standard GCN paradigm improved recommendation accuracy, an unexpected finding that has guided subsequent simplification efforts.

The principal value propositions of GNN-based recommendation are threefold. First, high-order connectivity is explicitly leveraged, enabling meaningful recommendations even between users and items without direct interaction history. Second, the cold-start problem is mitigated by integrating social graphs and content graphs into a unified embedding space. Third, explainability is naturally enabled through path-based reasoning, which is particularly valuable in regulated domains such as healthcare and financial services8.

Table 7.8.1. Performance Comparison of GNN Recommendation Models (MovieLens-1M)
Model Year Recall@20 NDCG@20 Reference
NGCF20190.18150.1623Wang SIGIR 20199
LightGCN20200.18300.1637He SIGIR 20206
UltraGCN20210.18640.1697Mao CIKM 2021
HCCF20220.18810.1710Xia SIGIR 2022
SimGCL20220.19160.1736Yu SIGIR 202210

In "LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation," He et al. demonstrate that removing feature transformation and nonlinear activation from a GCN improves rather than degrades recommendation accuracy.

— Xiangnan He et al., SIGIR 2020, DOI: 10.1145/3397271.3401063

7.9 Sequential Recommendation and Social Graph Integration

Sequential recommendation models such as SASRec (Kang and McAuley, 2018), BERT4Rec (Sun et al., 2019), and GraphRec (Fan et al., 2019) combine temporal user-item interaction sequences with graph structure for richer modeling11. BERT4Rec adapts the bidirectional transformer to user behavior sequences, treating each item as a token and masking strategy as the training objective.

Global social graph assets, most notably the Facebook Open Graph and the LinkedIn Economic Graph, represent two pillars of large-scale industrial recommendation infrastructure12. Each maintains user networks on the order of three billion and 900 million respectively, with relationship types including friendship, professional connection, employment, education, and skill endorsement.

The Netflix Prize Dataset (2006–2009), although now historical, remains a touchstone benchmark for collaborative filtering research, with over 100 million ratings spanning 480,000 users and 17,770 movies. The Pinterest dataset, used in many GNN papers, contains user-image interaction data appropriate for testing visual recommendation algorithms with graph structure.

Endnotes

  1. WIA Standards Committee, WIA-DATA-015: Graph Database Standard, §7 Social Networks and Recommendation, WIA Standards Revision Committee, 2026.
  2. Charu C. Aggarwal, Recommender Systems: The Textbook, Springer, 2016, ISBN 978-3-319-29659-3.
  3. David Goldberg et al., "Using Collaborative Filtering to Weave an Information Tapestry," Communications of the ACM, vol. 35, no. 12, 1992, pp. 61–70, DOI: 10.1145/138859.138867.
  4. Yehuda Koren et al., "Matrix Factorization Techniques for Recommender Systems," IEEE Computer, vol. 42, no. 8, 2009, pp. 30–37, DOI: 10.1109/MC.2009.263.
  5. Xiangnan He et al., "Neural Collaborative Filtering," Proc. WWW 2017, ACM, 2017, pp. 173–182, DOI: 10.1145/3038912.3052569.
  6. Xiangnan He et al., "LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation," Proc. SIGIR 2020, ACM, 2020, pp. 639–648, DOI: 10.1145/3397271.3401063.
  7. Shuai Zhang et al., "Deep Learning Based Recommender System: A Survey and New Perspectives," ACM Computing Surveys, vol. 52, no. 1, 2019, DOI: 10.1145/3285029.
  8. Yongfeng Zhang and Xu Chen, "Explainable Recommendation: A Survey and New Perspectives," Foundations and Trends in Information Retrieval, vol. 14, no. 1, 2020, DOI: 10.1561/1500000066.
  9. Xiang Wang et al., "Neural Graph Collaborative Filtering," Proc. SIGIR 2019, ACM, 2019, pp. 165–174, DOI: 10.1145/3331184.3331267.
  10. Junliang Yu et al., "Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for Recommendation," Proc. SIGIR 2022, ACM, 2022, pp. 1294–1303, DOI: 10.1145/3477495.3531937.
  11. Fei Sun et al., "BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer," Proc. CIKM 2019, ACM, 2019, pp. 1441–1450, DOI: 10.1145/3357384.3357895.
  12. LinkedIn Corporation, The LinkedIn Economic Graph, LinkedIn Engineering Blog, 2024, https://engineering.linkedin.com/blog.

Chapter 7 — Notes & References

  1. WIA Standards Public Repository (graph-database folder), MIT License, GitHub: WIA-Official/wia-standards-public/tree/main/graph-database — open standard initiative providing source code for simulator, spec, API, and ebook assets cited throughout this volume; serves as the canonical verification record for all primary-source citations made by the WIA standard committee in this chapter. Canonical ENUM tokens used in this volume include NEO4J, NEPTUNE, DGRAPH, ARANGODB, JANUSGRAPH, TIGERGRAPH, MEMGRAPH, NEBULA_GRAPH, CYPHER, GREMLIN, SPARQL, GQL, PAGERANK, LOUVAIN, NODE2VEC, GRAPHSAGE, ISO_GQL_2024, W3C_RDF, W3C_SPARQL, LDBC_SNB, WIKIDATA, DBPEDIA, YAGO, CONCEPTNET.