
Weighted and Temporal Graphs: Context in 4D
Inject reality into your Knowledge Graph. Learn to use edge weights for confidence and ranking, and temporal nodes for time-aware retrieval and historical reasoning.
Weighted and Temporal Graphs: Context in 4D
Your basic graph is a skeleton. It has bones (Nodes) and joints (Edges). But to build a "Smart" agent, your graph needs Muscle (Weights) and Memory (Time). In the real world, a connection isn't just "there" or "not there"—it has a strength and a timestamp.
In this lesson, we will explore Weighted Graphs, where edges represent "Strength" or "Confidence," and Temporal Graphs, where edges have an expiration date or a sequence. We will learn how these dimensions allow your Graph RAG system to answer complex questions like "Which supplier is the MOST RELIABLE?" or "What was the state of the project BEFORE the March update?"
1. Weighted Graphs: The "Strength" of a Link
Definition: A graph where each edge is assigned a numerical value (the "Weight") that represents a property like cost, distance, capacity, or—most importantly for RAG—Confidence.
Why Weights Matter for AI:
When an LLM extracts a relationship from text, it is sometimes only 70% sure.
- Fact 1:
(Sudeep)-[LEADS]->(Engineering) {weight: 0.95}(Extracted from HR DB). - Fact 2:
(Sudeep)-[LEADS]->(Marketing) {weight: 0.20}(Gleaned from a vague Slack joke).
The Logic: When your agent "Walks" the graph to find the team leader, it can choose to follow the path with the Highest Weight. This reduces the impact of extraction errors.
2. Temporal Graphs: Navigating the Timeline
Definition: A graph where nodes or edges have time-related properties, or where the graph itself evolves over a series of "Snapshots."
The "Stale Data" Problem:
If your graph says "(Alice)-[:MANAGER_OF]->(Bob)" but Alice left the company 2 months ago, your AI agent will give an incorrect answer (A hallucination of state).
Solutions:
- Valid-Time Properties: Every edge has a
startDateandendDate. - Temporal Nodes: Instead of connecting
Alicedirectly toBob, you connect them via a2024_Rolenode.
Example Implementation:
(:Person {name: 'Alice'}) -[:HELD_ROLE {year: 2024}]-> (:Role {title: 'Manager'})
graph LR
P[Alice] -- MANAGER_OF {from: 2020, to: 2023} --> B[Bob]
P -- CONSULTANT_TO {from: 2024} --> B
style P fill:#4285F4,color:#fff
style B fill:#34A853,color:#fff
3. Path Ranking with "Shortest Path" Algorithms
In a weighted graph, the "Best" path isn't necessarily the one with the fewest hops. It's the one with the Best Total Weight.
- Example: An agent looking for a technical solution.
- Path A:
Solution X(1 hop, but weight 0.3 - very unreliable). - Path B:
Solution Y->Verified By->Senior Arch(2 hops, but total weight 0.9 - very reliable).
Graph RAG Decision: The system should present Path B to the user, even though it is longer. This is Trust-Aware Retrieval.
4. Implementation: Dijkstra's Algorithm in Python
Let's use networkx to find the "Most Reliable" path between two nodes using weights.
import networkx as nx
G = nx.Graph()
# Nodes
G.add_edge("User_Query", "Concept_A", weight=0.1) # Fast but weak link
G.add_edge("User_Query", "Concept_B", weight=0.5)
G.add_edge("Concept_B", "The_Answer", weight=0.5)
# Task: Find the path with the HIGHEST weight (Reliability)
# Note: nx.shortest_path finds the LOWEST weight, so we invert weights
# or use specialized logic. Here we just show the structure.
path = nx.shortest_path(G, source="User_Query", target="The_Answer", weight="weight")
print(f"Optimal Path for AI to follow: {path}")
# In Graph RAG, the weights are often the 'LLM Confidence Scores'
# from the extraction phase.
5. Summary and Exercises
Weights and Time turn a generic graph into a Decision Engine.
- Weights represent the "Certainty" or "Priority" of a relationship.
- Temporal markers allow the agent to distinguish between "What was" and "What is."
- Pathfinding based on weight ensures the agent presents the most reliable evidence.
Exercises
- Confidence Mapping: You are extracting entities from a 20-year-old archive. The text is blurry (OCR errors). How would you weight the relationships found in these documents compared to relationships found in a clean digital JSON file?
- Timeline Challenge: Draw a graph of your work history. Use edges with
STARTandENDdates. Now, "Retrieve" the node representing where you worked on July 1st, 2019. This is the Point-in-Time Query. - The "Strongest Link": If Node A has 5 incoming edges of weight 0.2 and 1 incoming edge of weight 0.9, which "Context" is more likely to be the correct one for the LLM?
In the next lesson, we will look at how the agent actually moves through these bones and muscles: Graph Traversal Concepts.