Skip to content

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}: {} with a score of {score:.5f}")


Gandalf's ranking is 0.015810830531114206

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.


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.


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()

# Group the components together
components = results.group_by()

# 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
    f"The largest component contains {component_sizes[largest_component]} of the {lotr_graph.count_nodes()} nodes in the graph."


     Key              Value
0     99  45256802920602448
1     20  45256802920602448
2     46  45256802920602448
3     64  45256802920602448
4    116  45256802920602448
..   ...                ...
134   44  45256802920602448
135   15  45256802920602448
136  106  45256802920602448
137  111  45256802920602448
138   49  45256802920602448

[139 rows x 2 columns]

{45256802920602448: ['Gandalf', 'Elrond', 'Frodo', 'Bilbo', 'Thorin', 'Gollum', 'Samwise', 'Peregrin', 'Elessar', 'Arwen', 'Aragorn', 'Barahir', 'Faramir', 'Findegil', 'Meriadoc', 'Elendil', 'Galadriel', 'Celeborn', 'Hamfast', 'Merry', 'Odo', 'Pippin', 'Fredegar', 'Sam', 'Halfast', 'Saruman', 'Isildur', 'Gil-galad', 'Sauron', 'Sméagol', 'Déagol', 'Lobelia', 'Lotho', 'Maggot', 'Goldberry', 'Tom', 'Butterbur', 'Barliman', 'Nob', 'Bob', 'Bill', 'Lúthien', 'Beren', 'Thingol', 'Elwing', 'Eärendil', 'Dior', 'Glorfindel', 'Glóin', 'Beorn', 'Grimbeorn', 'Dwalin', 'Bombur', 'Bofur', 'Ori', 'Balin', 'Óin', 'Smaug', 'Gimli', 'Galdor', 'Erestor', 'Círdan', 'Legolas', 'Thranduil', 'Boromir', 'Anárion', 'Valandil', 'Ohtar', 'Meneldil', 'Denethor', 'Celebrimbor', 'Radagast', 'Thrór', 'Túrin', 'Hador', 'Húrin', 'Fundin', 'Fëanor', 'Daeron', 'Orophin', 'Rúmil', 'Haldir', 'Amroth', 'Celebrían', 'Thengel', 'Shadowfax', 'Treebeard', 'Wormtongue', 'Helm', 'Erkenbrand', 'Gamling', 'Grimbold', 'Mablung', 'Damrod', 'Anborn', 'Shelob', 'Ungoliant', 'Shagrat', 'Gorbag', 'Imrahil', 'Ecthelion', 'Théoden', 'Beregond', 'Bergil', 'Forlong', 'Halbarad', 'Éomer', 'Arvedui', 'Éowyn', 'Dúnhere', 'Brego', 'Baldor', 'Hirgon', 'Nazgûl', 'Ghân-buri-Ghân', 'Fastred', 'Horn', 'Harding', 'Angbor', 'Meneldor', 'Finduilas', 'Eärnur', 'Nimloth', 'Fréa', 'Fengel', 'Déor', 'Goldwine', 'Folcwine', 'Aldor', 'Walda', 'Folca', 'Gram', 'Robin', 'Goldilocks'], 10447362470584615464: ['Blanco', 'Marcho'], 4954526713244181378: ['Bard', 'Bain', 'Brand']}

The largest component contains 134 of the 139 nodes in the graph.