KnowledgeGraph<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.Graph

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

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)

How It Works

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

Constructors

Constructor Summary
KnowledgeGraph() Initializes a new instance of the KnowledgeGraph class with default in-memory storage.
KnowledgeGraph(IGraphStore<T> store) Initializes a new instance of the KnowledgeGraph class with a custom graph store.

Properties

Property Summary
int EdgeCount { get; } Gets the total number of edges in the graph.
int NodeCount { get; } Gets the total number of nodes in the graph.

Methods

Method Summary
GraphNode<T> GetNode(string nodeId) Gets a node by its ID.
IEnumerable<GraphEdge<T>> GetAllEdges() Gets all edges in the graph.
IEnumerable<GraphEdge<T>> GetEdgesAt(DateTime timestamp) Gets all edges valid at a specific point in time.
IEnumerable<GraphEdge<T>> GetIncomingEdges(string nodeId) Gets all incoming edges to a node.
IEnumerable<GraphEdge<T>> GetIncomingEdgesAt(string nodeId, DateTime timestamp) Gets incoming edges to a node that are valid at a specific point in time.
IEnumerable<GraphEdge<T>> GetOutgoingEdges(string nodeId) Gets all outgoing edges from a node.
IEnumerable<GraphEdge<T>> GetOutgoingEdgesAt(string nodeId, DateTime timestamp) Gets outgoing edges from a node that are valid at a specific point in time.
IEnumerable<GraphNode<T>> BreadthFirstTraversal(string startNodeId, int maxDepth) Performs breadth-first search traversal starting from a node.
IEnumerable<GraphNode<T>> FindRelatedNodes(string query, int topK) Finds nodes related to a query by entity name or property matching.
IEnumerable<GraphNode<T>> GetAllNodes() Gets all nodes in the graph.
IEnumerable<GraphNode<T>> GetNeighbors(string nodeId) Gets all neighbors of a node (nodes connected by outgoing edges).
IEnumerable<GraphNode<T>> GetNeighborsAt(string nodeId, DateTime timestamp) Gets neighbors of a node considering only edges valid at a specific point in time.
IEnumerable<GraphNode<T>> GetNodesByLabel(string label) Gets all nodes with a specific label.
List<string> FindShortestPath(string startNodeId, string endNodeId) Finds the shortest path between two nodes using BFS.
void AddEdge(GraphEdge<T> edge) Adds an edge to the graph.
void AddNode(GraphNode<T> node) Adds a node to the graph or updates it if it already exists.
void Clear() Clears all nodes and edges from the graph.