Node centric algorithms
The second category of algorithms are node centric
and return a value for each node in the graph. These results are stored within a AlgorithmResult object which has functions for sorting, grouping, top_k and conversion to dataframes. To demonstrate these functions below we have run PageRank and Weakly Connected Components.
Continuous Results (PageRank)
PageRank
is an centrality metric developed by Google's founders to rank web pages in search engine results based on their importance and relevance. This has since become a standard ranking algorithm for a whole host of other usecases.
Raphtory's implementation returns the score for each node - these are continuous values, meaning we can discover the most important characters in our Lord of the Rings dataset via top_k()
.
In the code below we first get the result of an individual character (Gandalf), followed by the values of the top 5 most important characters.
from raphtory import algorithms as rp
results = rp.pagerank(lotr_graph)
# Getting the results for an individual character (Gandalf)
gandalf_rank = results.get("Gandalf")
print(f"Gandalf's ranking is {gandalf_rank}\n")
# Getting the top 5 most important characters and printing out their scores
top_5 = results.top_k(5)
for rank, (node, score) in enumerate(top_5, 1):
print(f"Rank {rank}: {node.name} with a score of {score:.5f}")
Output
Gandalf's ranking is 0.015810830531114203
Rank 1: Aragorn with a score of 0.09526
Rank 2: Faramir with a score of 0.06148
Rank 3: Elrond with a score of 0.04042
Rank 4: Boromir with a score of 0.03468
Rank 5: Legolas with a score of 0.03323
Discrete Results (Connected Components)
Weakly connected components
in a directed graph are subgraphs
where every node is reachable from every other node (if edge direction is ignored).
This algorithm returns the id of the component each node is a member of - these are discrete values, meaning we can use group_by
to find additional insights like the size of the largest connected component.
In the code below we first run the algorithm and turn the results into a dataframe so we can see what they look like.
Info
The component ID (value)
is generated from the lowest node ID
in the component, hence why they look like a random numbers in the print out.
Next we take the results and group the nodes by these IDs and calculate the size of the largest component.
Info
Almost all nodes are within this component (134 of the 139), as is typical for social networks.
from raphtory import algorithms as rp
results = rp.weakly_connected_components(lotr_graph)
# Convert the results to a dataframe so we can have a look at them
df = results.to_df()
print(f"{df}\n")
# Group the components together
components = results.group_by()
print(f"{components}\n")
# Get the size of each component
component_sizes = {key: len(value) for key, value in components.items()}
# Get the key for the largest component
largest_component = max(component_sizes, key=component_sizes.get)
# Print the size of the largest component
print(
f"The largest component contains {component_sizes[largest_component]} of the {lotr_graph.count_nodes()} nodes in the graph."
)
Output
Node Value
0 Barahir Gandalf
1 Saruman Gandalf
2 Angbor Gandalf
3 Thrór Gandalf
4 Beren Gandalf
.. ... ...
134 Glóin Gandalf
135 Legolas Gandalf
136 Celeborn Gandalf
137 Elessar Gandalf
138 Barliman Gandalf
[139 rows x 2 columns]
{'Bard': ['Bard', 'Bain', 'Brand'], 'Gandalf': ['Gandalf', 'Frodo', 'Thorin', 'Gollum', 'Bilbo', 'Peregrin', 'Boromir', 'Arwen', 'Barahir', 'Meriadoc', 'Galadriel', 'Hamfast', 'Odo', 'Legolas', 'Sam', 'Gimli', 'Merry', 'Pippin', 'Elrond', 'Imrahil', 'Nazgûl', 'Mablung', 'Isildur', 'Aragorn', 'Elendil', 'Balin', 'Samwise', 'Gil-galad', 'Sméagol', 'Anborn', 'Éomer', 'Saruman', 'Amroth', 'Beregond', 'Denethor', 'Shelob', 'Ori', 'Gorbag', 'Faramir', 'Lobelia', 'Ungoliant', 'Orophin', 'Haldir', 'Maggot', 'Celeborn', 'Meneldor', 'Théoden', 'Treebeard', 'Tom', 'Forlong', 'Éowyn', 'Shadowfax', 'Valandil', 'Butterbur', 'Húrin', 'Halbarad', 'Nob', 'Elessar', 'Eärnur', 'Nimloth', 'Bob', 'Arvedui', 'Glorfindel', 'Erestor', 'Sauron', 'Helm', 'Lúthien', 'Beren', 'Dúnhere', 'Thingol', 'Elwing', 'Eärendil', 'Fréa', 'Fengel', 'Brego', 'Hirgon', 'Baldor', 'Thengel', 'Déor', 'Goldwine', 'Beorn', 'Gamling', 'Folcwine', 'Dwalin', 'Aldor', 'Erkenbrand', 'Walda', 'Bombur', 'Folca', 'Glóin', 'Smaug', 'Grimbold', 'Galdor', 'Wormtongue', 'Óin', 'Fastred', 'Horn', 'Robin', 'Meneldil', 'Celebrimbor', 'Túrin', 'Damrod', 'Hador', 'Angbor', 'Findegil', 'Fredegar', 'Halfast', 'Déagol', 'Bill', 'Fundin', 'Fëanor', 'Lotho', 'Daeron', 'Shagrat', 'Ecthelion', 'Rúmil', 'Goldberry', 'Bergil', 'Barliman', 'Celebrían', 'Anárion', 'Dior', 'Grimbeorn', 'Círdan', 'Ohtar', 'Bofur', 'Ghân-buri-Ghân', 'Thranduil', 'Harding', 'Radagast', 'Thrór', 'Gram', 'Finduilas', 'Goldilocks'], 'Blanco': ['Blanco', 'Marcho']}
The largest component contains 134 of the 139 nodes in the graph.