Compute.indegree()#
relationalai.std.graphs
#Compute.indegree(node: Producer) -> Expression
Compute the indegree of a node.
In a directed graph, the indegree of a node is the number of edges that point to the node.
For an undirected graph, .indegree() is an alias of .degree().
Must be called in a rule or query context.
Supported Graph Types#
| Graph Type | Supported | Notes |
|---|---|---|
| Directed | Yes | |
| Undirected | Yes | |
| Weighted | Yes | Weights are ignored. |
| Unweighted | Yes |
Parameters#
| Name | Type | Description |
|---|---|---|
| node | Producer | A node in the graph. |
Returns#
Returns an Expression object that produces the indegree of the node as an integer value.
Example#
Use .indegree() to compute the indegree of a node in a graph.
You access the .indegree() method from a Graph object’s
.compute attribute:
#import relationalai as rai
from relationalai.std.graphs import Graph
# Create a model named "socialNetwork" with a Person type
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add some people to the graph and connect them with a 'friends' property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
alice.friends.add(bob)
bob.friends.add(alice)
# Create a directed graph with Person nodes and edges between friends.
# Note that graphs are directed by default.
# This graphs has two edges: one from Alice to Bob and one from Bob to Alice.
graph = Graph(model)
graph.Node.extend(Person)
graph.Edge.extend(Person.friends)
# Get the number of nodes in the graph.
with model.query() as select:
# Get all Person objects.
person = Person()
# Compute the indegree of each person.
indegree = graph.compute.indegree(person)
# Select the name of each person and their indegree.
response = select(person.name, indegree)
print(response.results)
# Output:
# name v
# 0 Alice 1
# 1 Bob 1
For an undirected graph, .indegree() is same as .degree().