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.

Graph

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.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.

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.

Graph

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         Folca  Gandalf
1        Fundin  Gandalf
2       Elessar  Gandalf
3       Saruman  Gandalf
4         Frodo  Gandalf
..          ...      ...
134  Goldilocks  Gandalf
135       Smaug  Gandalf
136       Rúmil  Gandalf
137    Meneldil  Gandalf
138      Maggot  Gandalf

[139 rows x 2 columns]

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

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