cookbook/ Social & Marketing Patterns
Last Updated: October 20, 2018Graph patterns for community detection, influencer analysis, and recommendation engines.
Social Analytics Cookbook
Understand your users and drive engagement with graph algorithms.
1. Influence & Connectivity
PageRank for Influencers
gsqlterminal// Calculate the relative importance of users based on their follower network CREATE QUERY user_pagerank(DOUBLE max_change, INT max_iter, DOUBLE damping) FOR GRAPH SocialGraph { MaxAccum<DOUBLE> @@max_diff; SumAccum<DOUBLE> @received_score; SumAccum<DOUBLE> @score = 1.0; V = {User.*}; WHILE @@max_diff > max_change LIMIT max_iter DO @@max_diff = 0; Result = SELECT s FROM V:s -(FOLLOWS:e)-> User:t ACCUM t.@received_score += s.@score / s.outdegree("FOLLOWS"); V = SELECT s FROM V:s POST_ACCUM @@max_diff += abs(s.@score - (1 - damping) + damping * s.@received_score), s.@score = (1 - damping) + damping * s.@received_score, s.@received_score = 0; END; PRINT V[V.name, V.@score] ORDER BY V.@score DESC LIMIT 100; }
Community Detection (LPA)
gsqlterminal// Group users into communities based on label propagation CREATE QUERY detect_communities(INT max_iter) FOR GRAPH SocialGraph { MapAccum<INT, SumAccum<INT>> @neighbor_labels; SumAccum<INT> @label; V = {User.*}; V = SELECT s FROM V:s ACCUM s.@label = getvid(s); // Initialize with unique ID WHILE i < max_iter DO V = SELECT s FROM V:s -(FOLLOWS:e)- User:t ACCUM s.@neighbor_labels += (t.@label -> 1) POST_ACCUM s.@label = s.@neighbor_labels.argmax().key, s.@neighbor_labels = 0; i = i + 1; END; PRINT V.group_by(V.@label, count()) AS community_sizes; }
2. Recommendation Engines
Collaborative Filtering (Users Like You)
gsqlterminal// Recommend products that similar users have purchased CREATE QUERY recommend_products(VERTEX<User> u, INT k) FOR GRAPH SocialGraph { SumAccum<INT> @common_purchases; SumAccum<INT> @reco_score; Start = {u}; // Find users who bought the same items MyItems = SELECT t FROM Start:s -(PURCHASED:e)-> Product:t; SimilarUsers = SELECT t FROM MyItems:s -(REVERSE_PURCHASED:e)- User:t WHERE t != u ACCUM t.@common_purchases += 1; // Recommend items bought by similar users but not by 'u' Recommendations = SELECT t FROM SimilarUsers:s -(PURCHASED:e)-> Product:t WHERE NOT MyItems.contains(t) ACCUM t.@reco_score += s.@common_purchases ORDER BY t.@reco_score DESC LIMIT k; PRINT Recommendations; }
3. Marketing Attribution
Multi-Touch Attribution Path
gsqlterminal// Trace the marketing channels a user interacted with before a conversion CREATE QUERY attribution_path(VERTEX<User> u) FOR GRAPH MarketingGraph { ListAccum<STRING> @@channel_path; Start = {u}; Interactions = SELECT t FROM Start:s -(INTERACTED_WITH:e)- Campaign:t ORDER BY e.timestamp ASC ACCUM @@channel_path += t.channel; PRINT @@channel_path; }
4. Viral Growth
K-Core Decomposition
gsqlterminal// Find the "core" group of users who are all connected to at least K others CREATE QUERY k_core(INT k) FOR GRAPH SocialGraph { SumAccum<INT> @degree; SetAccum<VERTEX<User>> @@k_core_set; V = {User.*}; V = SELECT s FROM V:s ACCUM s.@degree = s.outdegree(); WHILE V.size() > 0 AND (V.min("@degree") < k) DO Removed = SELECT s FROM V:s WHERE s.@degree < k; V = V MINUS Removed; V = SELECT s FROM V:s -(FOLLOWS:e)- User:t WHERE Removed.contains(t) ACCUM s.@degree += -1; END; PRINT V; }
[!TIP] Recommendation Engines in TigerGraph are real-time. Unlike traditional SQL-based recommendations that require pre-computation, TigerGraph can generate "Users Like You" results on-the-fly during a user's session.