avg_outdegree()#

relationalai.std.graphs.Compute
#avg_outdegree() -> Expression

Compute the average outdegree of all nodes in a graph. In a directed graph, the outdegree of a node is the number of edges that point away from the node. For an undirected graph, .avg_outdegree() is an alias of .avg_degree(). Must be called in a rule or query context.

Supported Graph Types#

Graph TypeSupportedNotes
DirectedYes
UndirectedYes
WeightedYesWeights are ignored.
UnweightedYes

Returns#

Returns an Expression object that produces the average outdegree of the graph as a floating-point value.

Example#

Use .avg_outdegree() to compute the average outdegree of a node in a graph. You access the .avg_outdegree() 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")
    carol = Person.add(name="Carol")
    alice.friends.extend([bob, carol])
    bob.friends.add(alice)
    carol.friends.add(alice)

# Create an undirected graph with Person nodes and edges between friends.
# This graph has two edges: one between Alice and Bob and one between Alice and Carol.
graph = Graph(model, undirected=True)
graph.Node.extend(Person)
graph.Edge.extend(Person.friends)

# Compute the average outdegree of the graph.
with model.query() as select:
    avg_outdegree = graph.compute.avg_outdegree()
    response = select(avg_outdegree)

print(response.results)
# Output:
#    v
# 0  1

In undirected graphs, .avg_outdegree() is the same as .avg_degree().

See Also#