Package: dicn
Direct-Indirect Common Neighbors: Python ImplementationI'm sharing a Python implementation of the link prediction algorithm from "Similarity-based link prediction in social networks using latent relationships between the users" by Ahmad Zareie & Rizos Sakellariou. I coded this as a small part of a school project and figured I would share since I was unable to find an existing implemention online and since the results were relatively strong.About the AlgorithmThis algorithm goes beyond traditional first-order neighbor measures and digs deeper into the structural similarities by considering latent relationships, thanks to the clever use of second-order neighbors.In essence, it:Builds Neighborhood Vectors: Each node gets its own vector, showcasing its immediate and extended neighborhood.Finds Union Neighborhood Sets: For every pair of nodes, it considers their overlapping neighborhoods.Measures Similarity: Using the Pearson correlation coefficient, the algorithm examines how similar the neighborhoods of two nodes are.Calculates DICN Scores: Direct-Indirect Common Neighbours (DICN) scores are derived, blending direct and indirect connections to predict potential links.I found this approach compelling because it mirrors how we often form connections in real life - not just with those we know directly, but also through extended networks.PerformanceThis algorithm was tested on citation network datasets Cora, CiteSeer, and PubMed, all pulled fromPyTorch Geometric. 50%-90% of randomly selected edges were removed, prediction scores were generated for these edges, and then prediction scores were generated for an equal number of randomly selected negative samples. AUC score and time were recorded for each test run, and then averaged across the five runs at each edge removal level and for each algorithm.AUC and time (s) for each dataset are displayed below, and an example notebook has been provided for reproducibility.CoraNumber of nodes = 2708, Number of edges = 5278Edge SplitAUC Mean - DICNAUC Mean - Resource AllocationAUC Mean - JaccardAUC Mean - Adamic AdarAUC Mean - Preferential AttachmentAUC Mean - Common Neighbor CentralityTime Mean (s) - DICNTime Mean (s) - Resource AllocationTime Mean (s) - JaccardTime Mean (s) - Adamic AdarTime Mean (s) - Preferential AttachmentTime Mean (s) - Common Neighbor Centrality50%68.4%59.8%59.7%60.0%60.3%67.7%0.740.010.020.0100.7660%74.4%63.6%62.9%62.9%62.0%71.9%0.720.010.020.0100.9770%79.8%66.6%66.6%66.8%62.1%76.1%0.680.010.010.0101.1980%83.6%70.6%69.7%70.1%64.0%79.0%0.670.010.010.0101.3190%87.1%73.4%73.0%73.2%64.7%83.4%0.6300.01001.46CiteSeerNumber of nodes = 3327, Number of edges = 4552Edge SplitAUC Mean - DICNAUC Mean - Resource AllocationAUC Mean - JaccardAUC Mean - Adamic AdarAUC Mean - Preferential AttachmentAUC Mean - Common Neighbor CentralityTime Mean (s) - DICNTime Mean (s) - Resource AllocationTime Mean (s) - JaccardTime Mean (s) - Adamic AdarTime Mean (s) - Preferential AttachmentTime Mean (s) - Common Neighbor Centrality50%64.1%57.7%57.6%57.8%59.4%67.0%1.190.010.010.0100.2260%70.1%60.5%60.2%60.3%59.4%69.9%1.130.010.010.0100.470%76.0%62.9%62.7%62.6%58.4%71.8%0.940.010.010.0100.5880%81.6%65.6%65.2%65.3%58.3%74.7%0.980.010.010.0100.7690%85.8%67.9%67.8%67.9%58.0%75.5%1.0400000.94PubMedNumber of nodes = 19717, Number of edges = 44324Edge SplitAUC Mean - DICNAUC Mean - Resource AllocationAUC Mean - JaccardAUC Mean - Adamic AdarAUC Mean - Preferential AttachmentAUC Mean - Common Neighbor CentralityTime Mean (s) - DICNTime Mean (s) - Resource AllocationTime Mean (s) - JaccardTime Mean (s) - Adamic AdarTime Mean (s) - Preferential AttachmentTime Mean (s) - Common Neighbor Centrality50%61.5%57.5%57.4%57.3%71.3%72.4%117.160.160.210.160.0349.6460%69.2%59.6%59.5%59.5%71.5%73.6%117.860.150.190.140.0362.2270%76.4%61.7%61.5%61.5%71.6%74.2%116.610.120.160.120.0274.2580%83.0%63.5%63.5%63.6%71.4%74.9%117.860.090.110.090.0286.4890%88.6%65.0%65.4%65.3%72.1%75.4%117.730.050.060.050.0197.88As can be seen, AUC tends to be very competitve (against other NetworkX link prediction algorithms) while time efficiency sometimes suffers, especially on larger graphs.Getting StartedPrerequisitesPython 3.9 or higherNetworkX 3.1 or higherNumpy 1.1 or higherInstallationpipinstalldicnUsageTo use the DICN algorithm in your Python code, first install the package and then follow this example:fromdicnimportdicnimportnetworkxasnxG=nx.erdos_renyi_graph(n=10,p=0.5)print(f"True Edges:{G.edges()}")output=dicn(G)foru,v,scoreinoutput:print(f"({u},{v}):{score}")This will output something like the following (note that actual output will vary due to the random nature of the graph generation):True Edges: [(0, 1), (0, 4), (0, 8), (1, 3), (1, 4), ..., (6, 9)]

(0, 2): 2.0355727751898023
(0, 3): 6.561239813665759
(0, 5): 3.8620436566990364
(0, 6): 3.7799442608229374
...
(8, 9): 4.419480670222152ReferencesZareie, A., Sakellariou, R. (2020). Similarity-based link prediction 
in social networks using latent relationships between the users. 
Scientific Reports, 10, 20137. DOI:10.1038/s41598-020-76799-4