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.
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:
In this graph: Circles are entities (Diseases, Drugs, Symptoms). Arrows are relationships describing how entities connect.
Why Are Knowledge Graphs Useful?
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:
- Clinical decision support (what tests to order, what treatments are recommended)
- Drug interaction checking (is this drug safe with the patient's other medications?)
- Patient risk stratification (who is at high risk for complications?)
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:
- How to query a knowledge graph (SPARQL, Cypher)
- Knowledge graph embeddings (representing entities as vectors)
- Inference and reasoning over a knowledge graph
- Integrating knowledge graphs with neural networks