Subgraph

For some use cases you may only be interested in a subset of nodes within the graph. One solution could be to call g.nodes and filter before continuing your workflow. However, this does not remove anything from future function calls — you would have to constantly recheck these lists.

To handle this, Raphtory provides the subgraph() function which takes a list of nodes of interest. This returns a GraphView where all nodes not in the list are hidden from all future function calls. This also hides any edges linked to hidden nodes to keep the subgraph consistent.

In the example below we demonstrate this by looking at the neighbours of FELIPE in the full graph, compared to a subgraph of FELIPE, LIPS, NEKKE, LOME and BOBO. We also show how subgraph() can be combined with other view functions, in this case a window between the 17th and 18th of June.

Excluding nodes

Sometimes it's easier to specify which nodes you don't want rather than listing all the ones you do. The exclude_nodes() function is the opposite of subgraph() — it removes the specified nodes from the view while keeping everything else.

Helper subgraph functions

Raphtory provides several convenience functions for common subgraph operations, saving you from writing boilerplate code.

Largest connected component

When analysing real-world graphs, it's common to find disconnected components — isolated nodes or small clusters that aren't connected to the main network. The largest_connected_component() function extracts only the largest connected subgraph, which is often the most interesting part of your data.

Subgraph by node types

For heterogeneous graphs with multiple node types (e.g., people, companies, products), you can use subgraph_node_types() to extract only nodes of specific types. This is useful when you want to focus on a particular category of entities.