Graph Theory Exercises
Through these exercises, you will practice computing similarity metrics, centrality measures, network properties, and graph algorithms 🧙
Exercise 1 : Similarity Metrics
This exercise focuses on measuring similarity between entities in networks, which is fundamental for recommendation systems and collaborative filtering.
You're analyzing a social network where people are connected based on shared interests.
Question 1.1
Given two users with the following interests:
- User A: \(\{Music, Sports, Reading, Gaming\}\)
- User B: \(\{Music, Reading, Cooking, Travel\}\)
Calculate step by step:
- Jaccard Similarity between A and B
- Overlap Coefficient between A and B
Question 1.2
In a recommendation system, you have 5 users and their movie preferences:
- User 1: \(\{Action, Comedy, Drama\}\)
- User 2: \(\{Action, Horror, Sci\text{-}Fi\}\)
- User 3: \(\{Comedy, Drama, Romance\}\)
- User 4: \(\{Action, Comedy, Horror\}\)
- User 5: \(\{Drama, Romance, Thriller\}\)
Calculate step by step:
- Create a similarity matrix using Jaccard Similarity for all user pairs
- Which pair of users is most similar?
- Which user would you recommend User 1 to connect with based on highest similarity?
Question 1.3
You're analyzing a bipartite graph of customers and products with ratings:
-
Customer A: \(\{(Phone, 5), (Laptop, 4), (Headphones, 3), (Case, 4)\}\)
-
Customer B: \(\{(Phone, 4), (Tablet, 5), (Headphones, 4), (Charger, 3), (Case, 3)\}\)
-
Customer C: \(\{(Laptop, 5), (Monitor, 4), (Keyboard, 4), (Mouse, 3)\}\)
Calculate step by step:
- Weighted Jaccard similarity using rating-adjusted intersection
- Predict Customer A's rating for Monitor using collaborative filtering
- Calculate recommendation precision if similarity threshold is \(\theta = 0.3\)
Exercise 2 : Centrality Measures
Let's explores various centrality measures used to identify important nodes in a network, such as key employees in an organization or influential users in social media.
You are analyzing an organizational network with 5 employees.
Question 2.1
Given adjacency matrix for directed network:
Calculate step by step:
- In-degree centrality: \(C_{in}(v) = \sum_{u} A_{u,v}\)
- Out-degree centrality: \(C_{out}(v) = \sum_{u} A_{v,u}\)
- Which node has highest total degree centrality?
Question 2.2
Calculate betweenness centrality for node B using:
Calculate step by step:
- Find all shortest paths between every pair of nodes
- Count paths passing through B: \(\sigma_{st}(B)\)
- Apply the betweenness formula
Question 2.3
Calculate PageRank centrality with damping factor \(d = 0.85\): \(\(PR(v) = \frac{1-d}{N} + d \sum_{u \in M(v)} \frac{PR(u)}{L(u)}\)\)
Calculate step by step:
- Initialize: \(PR_0(v) = \frac{1}{N} = 0.2\) for all nodes
- Perform 3 iterations
- Show convergence pattern
Exercise 3 : Complex Networks & Degree Distribution
This exercise examines the structural properties of different network models, comparing random networks with scale-free networks that exhibit power-law degree distributions.
Question 3.1
Degree sequence: \([1, 2, 2, 3, 3, 3, 4, 5, 6]\)
Calculate step by step:
- Total edges using handshaking lemma: \(|E| = \frac{1}{2}\sum_{v} deg(v)\)
- Average degree: \(\langle k \rangle = \frac{1}{n}\sum_{v} deg(v)\)
- Validate sequence
Question 3.2
Compare network models:
- Random: \(n=100\), edge probability \(p=0.05\)
- Scale-free: Power law \(P(k) \propto k^{-\gamma}\) with \(\gamma = 2.5\)
Calculate step by step:
- Expected degree in random network: \(\langle k \rangle = (n-1)p\)
- Probability of exactly 3 connections: \(P(k=3) = \binom{n-1}{k}p^k(1-p)^{n-1-k}\)
- Scale-free node count ratio
Question 3.3
Network resilience analysis with clustering coefficient calculation.
Calculate step by step:
- Random network clustering: \(C_{random} = p\)
- Scale-free clustering with hub concentration
- Compare failure resilience: \(R = 1 - \frac{S_{after}}{S_{before}}\)
Exercise 4: Bipartite Graphs & Projections
This exercise demonstrates how to work with bipartite graphs and how to create meaningful projections for analysis.
Let's dive into a simple movie recommendation system analysis.
Question 4.1
Bipartite graph \(G = (U \cup M, E)\) where \(U = \{U_1, U_2, U_3\}\), \(M = \{M_1, M_2, M_3, M_4\}\):
- \(U_1\) watched: \(\{M_1, M_2\}\)
- \(U_2\) watched: \(\{M_2, M_3\}\)
- \(U_3\) watched: \(\{M_1, M_3, M_4\}\)
Calculate step by step:
- Create user projection with weights \(w(u_i, u_j) = |N(u_i) \cap N(u_j)|\)
- Calculate edge weights
- Identify most similar users
Question 4.2
Extended bipartite network:
- \(U_1: \{M_1, M_2, M_3\}\), \(U_2: \{M_2, M_4, M_5\}\), \(U_3: \{M_1, M_3, M_6\}\), \(U_4: \{M_4, M_5, M_6\}\)
Calculate step by step:
- Movie-movie projection with Jaccard weights
- Calculate \(J(M_1, M_3)\) and \(J(M_2, M_4)\)
- Recommendation algorithm: Should \(M_6\) be recommended to \(U_1\)?
Question 4.3
Collaborative filtering with matrix factorization approach.
Calculate step by step:
Implement user-based CF:
Calculate prediction accuracy using RMSE:
Optimize similarity threshold \(\theta\) for maximum precision
Exercise 5: Network Properties & Complex Analysis
In this exercise we will covers advanced network properties including reciprocity, transitivity, clustering coefficients, and multi-layer network analysis.
Let's take a look at a basic communication network with edges: \(E = \{(A,B), (B,A), (B,C), (C,D), (D,A)\}\)
Question 5.1
Calculate step by step:
- Reciprocity: \(\rho = \frac{L^{\leftrightarrow}}{L}\) where \(L^{\leftrightarrow}\) is bidirectional edges
- Transitivity: Check triangles \(\triangle = \{(i,j), (j,k), (k,i)\}\)
- Local clustering for node B: \(C_i = \frac{2e_i}{k_i(k_i-1)}\)
Question 5.2
Self-referencing network with loops: \(E = \{(A,B), (B,C), (C,A), (A,A), (B,B), (C,D), (D,B)\}\)
Calculate step by step:
- Global clustering coefficient: \(\langle C \rangle = \frac{1}{n}\sum_i C_i\)
- Transitivity with self-loops: \(T = \frac{3 \times \text{triangles}}{\text{connected triplets}}\)
- Impact analysis: How do self-loops affect centrality rankings?
Question 5.3
Multi-layer network analysis comparing formal hierarchy vs. informal connections.
Calculate step by step:
- Structural holes using Burt's constraint: \(C_i = \sum_j \left(p_{ij} + \sum_{q} p_{iq}p_{qj}\right)^2\)
- Bridge detection between layers
- Network efficiency: \(E = \frac{1}{n(n-1)}\sum_{i \neq j} d_{ij}^{-1}\)
Exercise 6: Advanced Applications 🚀[BONUS]
This bonus exercise challenges you to combine multiple graph theory concepts and optimization techniques to design an advanced recommendation algorithms 🥷
Question 6.1
Design a recommendation algorithm combining multiple similarity measures:
Where \(\alpha + \beta + \gamma = 1\) and:
- \(J(u,v)\) is Jaccard similarity
- \(\cos(u,v) = \frac{u \cdot v}{||u|| \cdot ||v||}\) is cosine similarity
- \(corr(u,v)\) is Pearson correlation
Calculate optimal weights \((\alpha, \beta, \gamma)\) to maximize recommendation precision.
Question 6.2 (Implementation)
Code the following algorithms to solve your problem :
- Greedy optimisation basic
- Gradient descent
- Modularity optimization: \(Q = \frac{1}{2m}\sum_{ij}\left[A_{ij} - \frac{k_i k_j}{2m}\right]\delta(c_i, c_j)\)