Directed vs Undirected Graphs: The Flow of Context

Directed vs Undirected Graphs: The Flow of Context

Understand the critical role of edge direction in Graph RAG. Learn how directed relationships define hierarchy and causality, and when to use undirected edges for mutual associations.

Directed vs Undirected Graphs: The Flow of Context

In graph theory, as in real life, context often has a specific Direction. If I tell you that "A implies B," that is very different from "B implies A." To build an AI agent that understands Causality, Hierarchy, and Dependency, you must understand the difference between Directed and Undirected relationships.

In this lesson, we will explore the "Vector" (direction) of information flow. We will learn how to model hierarchies that go "Top-Down" and social connections that go "Both Ways," and why choosing the wrong direction can lead to an AI that retrieves the "Effect" when it was looking for the "Cause."


1. Undirected Graphs: The "Mutual" Connection

Definition: A graph where the edges have no orientation. If there is an edge between $A$ and $B$, you can travel in both directions equally.

  • Analogy: A local road between two towns. You can drive from Town A to Town B, and back, on the same path.
  • Example: "Sudeep and Jane are colleagues." This is a mutual relationship.
  • Graph RAG Context: Useful for "Similarity Clusters" or "Co-occurrence." If two entities appear together in a document, they are simply "Related."

2. Directed Graphs (DiGraphs): The "Asymmetric" Truth

Definition: A graph where the edges have a specific direction (from a "Source" to a "Target").

  • Analogy: A one-way street.
  • Example: "Sudeep manages Jane." This is asymmetric.
  • Graph RAG Context: Essential for Dependencies and Lineage.
    • (File_A) --[:DERIVED_FROM]--> (Raw_Data)
    • (Server_A) --[:DEPENDS_ON]--> (Database_B)

If your agent is troubleshooting a server outage, it needs to follow the [:DEPENDS_ON] arrows forward to find the root cause, or backward to find the affected systems.

graph LR
    subgraph "Undirected: Partnership"
    A((Partner A)) --- B((Partner B))
    end
    
    subgraph "Directed: Hierarchy"
    C((CEO)) --> D((VP))
    D --> E((Manager))
    end
    
    style C fill:#4285F4,color:#fff
    style D fill:#34A853,color:#fff

3. The "In-Degree" and "Out-Degree" of Intelligence

In a Directed Graph, nodes have two "Degrees" (number of connections):

  1. Out-Degree: How many edges point outward from the node. A node with a high out-degree is often a Source of Knowledge or a Hub.
  2. In-Degree: How many edges point inward to the node. A node with a high in-degree is often a Target of Interest or a Bottleneck.

AI Reasoning: If your agent is asked "What is the most critical system in our stack?", it can search for the node with the highest In-Degree of [:DEPENDS_ON] edges.


4. When Directionality Fails: The "Cyclic" Trap

One danger in directed graphs is the Cycle: A -> B -> C -> A. If your AI agent starts "Walking" this path to find a root cause, it could get stuck in an infinite loop. This is why many Graph RAG systems use DAGs (Directed Acyclic Graphs) for workflows and logic—they ensure that the reasoning always progresses toward a conclusion.


5. Implementation: Detecting Hierarchy in Python

Let's use Python to check the "Direction" of a management chain.

import networkx as nx

# Create a Directed Graph
G = nx.DiGraph()

# Define the Hierarchy
G.add_edge("CEO", "VP_Product")
G.add_edge("VP_Product", "Lead_Engineer")
G.add_edge("Lead_Engineer", "Junior_Dev")

# Task: Is there a path from CEO to Junior_Dev?
has_path = nx.has_path(G, "CEO", "Junior_Dev")
print(f"CEO can reach Junior Dev: {has_path}")

# Task: Is there a reverse path? (Can Junior_Dev reach CEO?)
reverse_path = nx.has_path(G, "Junior_Dev", "CEO")
print(f"Junior Dev can reach CEO: {reverse_path} (FALSE)")

# In Graph RAG, this prevents the assistant from 
# thinking the Junior Dev manages the CEO.

6. Summary and Exercises

The direction of an edge is the "Logic Gate" of your knowledge.

  • Undirected graphs represent mutual relationships (Friendship, Association).
  • Directed graphs represent asymmetric relationships (Management, Dependency, Time).
  • In-Degree/Out-Degree are metrics that can help your agent identify "Important" nodes automatically.

Exercises

  1. Directional Map: Think of a recipe. Is the relationship between "Flour" and "Cake" directed or undirected? Why?
  2. Degree Audit: If you have a Knowledge Graph of 1,000 documents, and 900 of them refer to "The Security Manual," what does that tell you about the "In-Degree" of the Security Manual node?
  3. Cyclic Logic: Can you think of a real-world scenario where a directed cycle makes sense? (e.g., A circular economy where Waste -> Raw Material -> Product -> Waste). How would an AI agent handle "Retrieving" from a circle?

In the next lesson, we will add two more dimensions to our building blocks: Weighted and Temporal Graphs.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn