Table of Contents

Class KnowledgeGraph<T>

Namespace
AiDotNet.RetrievalAugmentedGeneration.Graph
Assembly
AiDotNet.dll

Knowledge graph for storing and querying entity relationships using a pluggable storage backend.

public class KnowledgeGraph<T>

Type Parameters

T

The numeric type used for vector operations.

Inheritance
KnowledgeGraph<T>
Inherited Members

Remarks

A knowledge graph stores entities (nodes) and their relationships (edges) to enable structured information retrieval. This implementation delegates storage operations to an IGraphStore<T> implementation, allowing you to swap between in-memory, file-based, or database-backed storage.

For Beginners: A knowledge graph is like a map of how information connects together.

Imagine Wikipedia as a graph:

  • Each article is a node (Albert Einstein, Physics, Germany, etc.)
  • Links between articles are edges (Einstein STUDIED Physics, Einstein BORN_IN Germany)
  • You can traverse the graph to find related information

This class lets you:

  1. Add entities and relationships
  2. Find connections between entities
  3. Traverse the graph to discover related information
  4. Query based on entity types or relationships

For example, to answer "Who worked at Princeton?":

  1. Find all edges with type "WORKED_AT"
  2. Filter for target = "Princeton University"
  3. Return the source entities (people who worked there)

Storage backends you can use:

  • MemoryGraphStore: Fast, in-memory (default)
  • FileGraphStore: Persistent, disk-based
  • Neo4jGraphStore: Professional graph database (future)

Constructors

KnowledgeGraph()

Initializes a new instance of the KnowledgeGraph<T> class with default in-memory storage.

public KnowledgeGraph()

KnowledgeGraph(IGraphStore<T>)

Initializes a new instance of the KnowledgeGraph<T> class with a custom graph store.

public KnowledgeGraph(IGraphStore<T> store)

Parameters

store IGraphStore<T>

The graph store implementation to use for storage.

Properties

EdgeCount

Gets the total number of edges in the graph.

public int EdgeCount { get; }

Property Value

int

NodeCount

Gets the total number of nodes in the graph.

public int NodeCount { get; }

Property Value

int

Methods

AddEdge(GraphEdge<T>)

Adds an edge to the graph.

public void AddEdge(GraphEdge<T> edge)

Parameters

edge GraphEdge<T>

The edge to add.

Exceptions

InvalidOperationException

Thrown when source or target nodes don't exist.

AddNode(GraphNode<T>)

Adds a node to the graph or updates it if it already exists.

public void AddNode(GraphNode<T> node)

Parameters

node GraphNode<T>

The node to add.

BreadthFirstTraversal(string, int)

Performs breadth-first search traversal starting from a node.

public IEnumerable<GraphNode<T>> BreadthFirstTraversal(string startNodeId, int maxDepth = 2147483647)

Parameters

startNodeId string

The starting node ID.

maxDepth int

Maximum traversal depth (default: unlimited).

Returns

IEnumerable<GraphNode<T>>

Collection of nodes in BFS order.

Clear()

Clears all nodes and edges from the graph.

public void Clear()

FindRelatedNodes(string, int)

Finds nodes related to a query by entity name or property matching.

public IEnumerable<GraphNode<T>> FindRelatedNodes(string query, int topK = 10)

Parameters

query string

The search query.

topK int

Maximum number of results to return.

Returns

IEnumerable<GraphNode<T>>

Collection of matching nodes.

FindShortestPath(string, string)

Finds the shortest path between two nodes using BFS.

public List<string> FindShortestPath(string startNodeId, string endNodeId)

Parameters

startNodeId string

The starting node ID.

endNodeId string

The target node ID.

Returns

List<string>

List of node IDs representing the path, or empty if no path exists.

GetAllEdges()

Gets all edges in the graph.

public IEnumerable<GraphEdge<T>> GetAllEdges()

Returns

IEnumerable<GraphEdge<T>>

Collection of all edges.

GetAllNodes()

Gets all nodes in the graph.

public IEnumerable<GraphNode<T>> GetAllNodes()

Returns

IEnumerable<GraphNode<T>>

Collection of all nodes.

GetIncomingEdges(string)

Gets all incoming edges to a node.

public IEnumerable<GraphEdge<T>> GetIncomingEdges(string nodeId)

Parameters

nodeId string

The target node ID.

Returns

IEnumerable<GraphEdge<T>>

Collection of incoming edges.

GetNeighbors(string)

Gets all neighbors of a node (nodes connected by outgoing edges).

public IEnumerable<GraphNode<T>> GetNeighbors(string nodeId)

Parameters

nodeId string

The node ID.

Returns

IEnumerable<GraphNode<T>>

Collection of neighbor nodes.

GetNode(string)

Gets a node by its ID.

public GraphNode<T>? GetNode(string nodeId)

Parameters

nodeId string

The node ID.

Returns

GraphNode<T>

The node, or null if not found.

GetNodesByLabel(string)

Gets all nodes with a specific label.

public IEnumerable<GraphNode<T>> GetNodesByLabel(string label)

Parameters

label string

The node label to filter by.

Returns

IEnumerable<GraphNode<T>>

Collection of nodes with the specified label.

GetOutgoingEdges(string)

Gets all outgoing edges from a node.

public IEnumerable<GraphEdge<T>> GetOutgoingEdges(string nodeId)

Parameters

nodeId string

The source node ID.

Returns

IEnumerable<GraphEdge<T>>

Collection of outgoing edges.