Functions for querying the degree of nodes and retrieving their connected edges.
getDegree
Returns the total degree of a node (inDegree + outDegree). For undirected graphs, each edge is counted once.
function getDegree(graph: Graph, nodeId: string): number
ID of the node to get degree for
Total number of edges connected to the node
import { createGraph, getDegree } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
edges: [
{ id: 'e1', sourceId: 'a', targetId: 'b' },
{ id: 'e2', sourceId: 'c', targetId: 'b' },
],
});
getDegree(graph, 'b'); // => 2
getDegree(graph, 'a'); // => 1
getInDegree
Returns the in-degree of a node (number of incoming edges).
function getInDegree(graph: Graph, nodeId: string): number
ID of the node to get in-degree for
import { createGraph, getInDegree } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }],
edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
});
getInDegree(graph, 'b'); // => 1
getInDegree(graph, 'a'); // => 0
getOutDegree
Returns the out-degree of a node (number of outgoing edges).
function getOutDegree(graph: Graph, nodeId: string): number
ID of the node to get out-degree for
import { createGraph, getOutDegree } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }],
edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
});
getOutDegree(graph, 'a'); // => 1
getOutDegree(graph, 'b'); // => 0
getEdgesOf
Returns all edges (incoming + outgoing) connected to a node.
function getEdgesOf<E>(
graph: Graph<any, E>,
nodeId: string
): GraphEdge<E>[]
ID of the node to get edges for
Array of all edges connected to the node
import { createGraph, getEdgesOf } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
edges: [
{ id: 'e1', sourceId: 'a', targetId: 'b' },
{ id: 'e2', sourceId: 'c', targetId: 'b' },
],
});
const edges = getEdgesOf(graph, 'b');
// => [edge e1, edge e2]
getInEdges
Returns incoming edges to a node.
function getInEdges<E>(
graph: Graph<any, E>,
nodeId: string
): GraphEdge<E>[]
ID of the node to get incoming edges for
import { createGraph, getInEdges } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }],
edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
});
const inEdges = getInEdges(graph, 'b');
// => [edge e1]
const noInEdges = getInEdges(graph, 'a');
// => []
getOutEdges
Returns outgoing edges from a node.
function getOutEdges<E>(
graph: Graph<any, E>,
nodeId: string
): GraphEdge<E>[]
ID of the node to get outgoing edges for
import { createGraph, getOutEdges } from '@statelyai/graph';
const graph = createGraph({
nodes: [{ id: 'a' }, { id: 'b' }],
edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
});
const outEdges = getOutEdges(graph, 'a');
// => [edge e1]
const noOutEdges = getOutEdges(graph, 'b');
// => []