MATIH Platform is in active MVP development. Documentation reflects current implementation status.
14. Context Graph & Ontology
Search Services
Graph Traversal

Graph Traversal

The GraphTraversalEngine provides advanced graph traversal algorithms for the Context Graph, including depth-first search (DFS), breadth-first search (BFS), shortest path finding, cycle detection, and subgraph extraction. It integrates with the ContextGraphStore for production-ready traversals.


Overview

Graph traversal is the foundation for lineage exploration, impact analysis, and dependency mapping in the Context Graph. The engine supports configurable depth limits, relationship filters, and traversal callbacks.

Source: data-plane/ai-service/src/context_graph/services/graph_traversal.py


Traversal Directions

DirectionDescriptionUse Case
UPSTREAMFollow relationships backward to find sources"Where does this data come from?"
DOWNSTREAMFollow relationships forward to find dependents"What depends on this dataset?"
BOTHBidirectional traversal"Show me the full neighborhood"

Traversal Strategies

StrategyDescriptionPerformance
DFSDepth-first search for deep lineage explorationMemory-efficient, good for deep chains
BFSBreadth-first search for neighbor discoveryFinds closest entities first
SHORTEST_PATHFind shortest path between two entitiesOptimal for connection discovery
ALL_PATHSFind all paths between two entitiesExpensive, use with depth limits

Configuration

from context_graph.services.graph_traversal import (
    TraversalConfig,
    TraversalDirection,
    TraversalStrategy,
)
 
config = TraversalConfig(
    max_depth=10,          # Maximum traversal depth
    max_nodes=1000,        # Maximum nodes to visit
    strategy=TraversalStrategy.BFS,
    direction=TraversalDirection.UPSTREAM,
    include_properties=True,
    relationship_types=["DERIVED_FROM", "CONSUMED_BY"],
)

Basic Traversal

engine = get_traversal_engine()
 
result = await engine.traverse(
    start_urn="urn:matih:model:acme:churn_predictor",
    tenant_id="acme",
    direction=TraversalDirection.UPSTREAM,
    max_depth=5,
)
 
for node in result.nodes:
    print(f"  {node.urn} (depth: {node.depth})")

Path Finding

Find the shortest path between two entities:

path = await engine.find_path(
    source_urn="urn:matih:dataset:acme:raw_events",
    target_urn="urn:matih:dashboard:acme:sales_overview",
    tenant_id="acme",
    max_depth=10,
)

Cycle Detection

Detect circular dependencies in the graph:

cycles = await engine.detect_cycles(
    start_urn="urn:matih:pipeline:acme:etl_main",
    tenant_id="acme",
    max_depth=20,
)
 
for cycle in cycles:
    print(f"Cycle: {' -> '.join(cycle.path)}")

Subgraph Extraction

Extract an isolated subgraph around an entity for visualization or analysis:

subgraph = await engine.extract_subgraph(
    center_urn="urn:matih:dataset:acme:sales_data",
    tenant_id="acme",
    radius=3,
)

REST API Endpoints

EndpointMethodDescription
/api/v1/context-graph/traversePOSTExecute a graph traversal
/api/v1/context-graph/traverse/pathsPOSTFind paths between entities
/api/v1/context-graph/traverse/cyclesPOSTDetect circular dependencies

Performance Limits

ParameterDefaultMaximum
max_depth1050
max_nodes100010000
Timeout30 seconds120 seconds