CHAPTER 2

📊 Knowledge Graphs Basics

Understanding the Foundation of Structured Knowledge

What is a Knowledge Graph?

A Knowledge Graph (KG) is a way of organizing information so that computers can understand relationships between things. Instead of storing facts in text form, a knowledge graph represents information as a network of connected entities and relationships—similar to how your brain organizes concepts.

Think of it this way: If you wanted to tell someone about a disease, you could write a paragraph describing symptoms, treatments, and causes. Or, you could draw a diagram showing the disease node connected to symptom nodes, treatment nodes, and cause nodes. A knowledge graph is exactly that—a visual and logical way to connect concepts.

Core Idea: A knowledge graph represents real-world knowledge as a network of entities (nodes) and relationships (edges) that machines can reason over, query, and extend.

The Three Components of a Knowledge Graph

1. Entities (Nodes)

Entities are the "things" or "concepts" in your knowledge graph. They represent real-world objects, people, places, or abstract concepts.

Examples of Entities:

  • In Medicine: Diseases (Diabetes, Hypertension), Drugs (Metformin, Lisinopril), Symptoms (Chest Pain, Fatigue)
  • In Law: Court Cases, Legal Documents, Statutes, Judges, Plaintiffs, Defendants
  • In Insurance: Customers, Claims, Risk Factors, Incidents
  • In General: People, Companies, Places, Events, Concepts

2. Relationships (Edges)

Relationships describe how entities are connected. They define the nature of the connection between two entities.

Examples of Relationships:

  • "treats": Drug → treats → Disease
  • "causes": Virus → causes → Disease
  • "hasSymptom": Disease → hasSymptom → Symptom
  • "prescribedFor": Doctor → prescribedFor → Patient
  • "basedOn": Case → basedOn → Statute
  • "detects": Test → detects → Disease

3. Attributes (Properties)

Attributes are properties or characteristics of entities. They describe qualities or measurements associated with an entity.

Examples of Attributes:

  • Disease Attributes: prevalence, mortality_rate, ICD_code, severity_level
  • Drug Attributes: dosage, side_effects, cost, chemical_formula
  • Patient Attributes: age, gender, risk_score, medical_history

Visual Example: A Simple Healthcare Knowledge Graph

Here's a simple example of how a knowledge graph looks in a medical context:

Diabetes Metformin Fatigue Hypertension Chest Pain hasSymptom treatedBy hasSymptom comorbidity

In this graph: Circles are entities (Diseases, Drugs, Symptoms). Arrows are relationships describing how entities connect.

Why Are Knowledge Graphs Useful?

1. Structured Reasoning: A KG allows computers to answer complex questions by traversing relationships. Example: "What symptoms are associated with Diabetes?" or "What drug treats Hypertension and is safe for patients with kidney disease?"
2. Transparency: Unlike a neural network (a "black box"), a knowledge graph's logic is explicit and interpretable. You can see exactly why a system made a recommendation.
3. Safety & Control: In healthcare, you can enforce hard rules using a KG. Example: "Never recommend Drug X if patient has Condition Y" is a rule that's always enforced.
4. Easy Updates: Adding new knowledge is straightforward—just add new entities and relationships. No need to retrain a neural network.
5. Scalability: Knowledge graphs can represent billions of facts and still allow fast querying and reasoning.

Building a Knowledge Graph: Step-by-Step

Step 1: Identify Entities

List all the important concepts or objects in your domain:

Healthcare Example:

Diseases, Drugs, Symptoms, Doctors, Patients, Lab Tests, Treatments, Insurance Plans

Step 2: Define Relationships

Specify how entities connect:

Healthcare Example:

hasSymptom, treatedBy, prescribedFor, causedBy, contraindication, sideEffect, detectedBy

Step 3: Extract or Create Data

Populate your graph with actual data:

{
  "entities": [
    {"id": "D001", "name": "Diabetes", "type": "Disease"},
    {"id": "S001", "name": "Fatigue", "type": "Symptom"},
    {"id": "M001", "name": "Metformin", "type": "Drug"}
  ],
  "relationships": [
    {"source": "D001", "target": "S001", "type": "hasSymptom"},
    {"source": "D001", "target": "M001", "type": "treatedBy"}
  ]
}

Step 4: Implement the Graph

Store it in a graph database like Neo4j or networkx:

from networkx import DiGraph

# Create graph
kg = DiGraph()

# Add entities
kg.add_node("Diabetes", type="Disease")
kg.add_node("Fatigue", type="Symptom")
kg.add_node("Metformin", type="Drug")

# Add relationships
kg.add_edge("Diabetes", "Fatigue", relation="hasSymptom")
kg.add_edge("Diabetes", "Metformin", relation="treatedBy")

# Query: What symptoms does Diabetes have?
symptoms = [target for source, target, data in kg.out_edges(data=True)
            if data['relation'] == 'hasSymptom']

Knowledge Graphs vs. Other Data Structures

Feature Relational DB (SQL) Document DB (JSON) Knowledge Graph
Best For Structured tables, transactions Nested, flexible data Complex relationships, reasoning
Query Relationships Via joins (slow for deep relationships) Flatten data (expensive) Direct traversal (fast)
Reasoning Limited; requires application logic Limited; requires application logic Native; built-in inference engines
Explainability High (SQL queries are explicit) Medium (schema is flexible) Very High (relationships are transparent)
Scalability Scales well for simple queries Flexible but can be slow Optimized for graph queries

Real-World Applications of Knowledge Graphs

Healthcare

Medical knowledge graphs connect diseases, symptoms, drugs, treatments, and patient records. They enable:

Google Search

Google's Knowledge Graph powers search results. It connects entities like people, places, movies, and organizations so that search can understand intent. Example: When you search "Einstein," Google shows you his biography, related people, his theories, and more—all from the knowledge graph.

Social Networks

Facebook and LinkedIn use knowledge graphs to represent people, relationships, companies, and interests. This enables friend recommendations, job suggestions, and targeted advertising.

E-Commerce

Amazon and other retailers use knowledge graphs to connect products, categories, attributes, reviews, and customer preferences. This powers recommendations: "Customers who bought X also bought Y."

Knowledge Graph Sizes: From Small to Massive

Scale Entities Relationships Use Case
Small 100s–1,000s 1,000s–10,000s Single hospital's patient records, small clinical trial
Medium 100,000s–millions Millions–billions National drug database, disease classification system
Large Billions+ Billions+ Google Knowledge Graph, DBpedia, Wikidata

Key Concepts to Remember

✓ Essential Takeaways:

  • Entities are the "nouns" (diseases, drugs, people, places)
  • Relationships are the "verbs" (treats, causes, contains, manages)
  • Attributes are the "adjectives" (severity, cost, efficacy)
  • KGs are explicit and interpretable—you can trace reasoning
  • KGs are safe and predictable—perfect for high-consequence domains like healthcare
  • KGs scale from thousands to billions of facts
  • KGs can be queried to answer complex questions about relationships

What's Next?

Now that you understand the basics of knowledge graphs, the next chapter will dive deeper into:

🎯 Ready for Assessment?

10 Questions
~10 mins
70% to Pass
Take Quiz Now →