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
TThe 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:
- Add entities and relationships
- Find connections between entities
- Traverse the graph to discover related information
- Query based on entity types or relationships
For example, to answer "Who worked at Princeton?":
- Find all edges with type "WORKED_AT"
- Filter for target = "Princeton University"
- 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
storeIGraphStore<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
NodeCount
Gets the total number of nodes in the graph.
public int NodeCount { get; }
Property Value
Methods
AddEdge(GraphEdge<T>)
Adds an edge to the graph.
public void AddEdge(GraphEdge<T> edge)
Parameters
edgeGraphEdge<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
nodeGraphNode<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
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
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
Returns
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
nodeIdstringThe 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
nodeIdstringThe 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
nodeIdstringThe 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
labelstringThe 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
nodeIdstringThe source node ID.
Returns
- IEnumerable<GraphEdge<T>>
Collection of outgoing edges.