TrustGraphGet Started

Knowledge Graph

A structured representation of knowledge as a network of entities (nodes) and their relationships (edges), enabling machines to understand and reason over interconnected information.

Core Concepts

A Knowledge Graph is a structured representation of knowledge that models real-world entities and their interrelationships as a graph, enabling advanced reasoning, querying, and knowledge discovery.

Structure

// Basic Knowledge Graph structure
{
  nodes: [
    {
      id: "person_1",
      type: "Person",
      properties: {
        name: "Jane Smith",
        age: 35,
        email: "jane@example.com"
      }
    },
    {
      id: "company_1",
      type: "Company",
      properties: {
        name: "TechCorp",
        industry: "Software",
        founded: 2010
      }
    }
  ],
  edges: [
    {
      from: "person_1",
      to: "company_1",
      type: "works_at",
      properties: {
        since: 2020,
        role: "CEO"
      }
    }
  ]
}

Key Components

1. Entities (Nodes)

Objects or concepts in the real world:

// Examples of entities
(person:Person {name: "John Smith", age: 30})
(company:Company {name: "Acme Corp", founded: 2015})
(product:Product {name: "Widget X", price: 99.99})
(city:Location {name: "San Francisco", population: 873965})

2. Relationships (Edges)

Connections between entities:

// Typed relationships with properties
(john:Person)-[:WORKS_AT {since: 2020, role: "Engineer"}]->(acme:Company)
(acme:Company)-[:LOCATED_IN]->(sf:Location)
(acme:Company)-[:PRODUCES]->(widget:Product)
(john:Person)-[:LIVES_IN]->(sf:Location)

3. Properties (Attributes)

Key-value pairs describing entities and relationships:

// Entity properties
{
  id: "person_123",
  type: "Person",
  properties: {
    name: "Jane Smith",
    birthDate: "1990-05-15",
    email: "jane@example.com",
    skills: ["Python", "Machine Learning", "Data Science"]
  }
}

// Relationship properties
{
  type: "WORKS_AT",
  properties: {
    startDate: "2020-01-15",
    position: "Senior Engineer",
    salary: 150000,
    department: "Engineering"
  }
}

Types of Knowledge Graphs

1. Enterprise Knowledge Graphs

Internal company knowledge:

// Corporate knowledge
(employee:Person)-[:WORKS_IN]->(department:Department)
(department)-[:PART_OF]->(division:Division)
(employee)-[:REPORTS_TO]->(manager:Person)
(employee)-[:WORKED_ON]->(project:Project)
(project)-[:USES]->(technology:Tech)

2. Open Knowledge Graphs

Public, crowd-sourced knowledge:

  • DBpedia: Structured data from Wikipedia
  • Wikidata: Collaborative knowledge base
  • YAGO: Academic knowledge graph
# Querying DBpedia
SELECT ?person ?birthPlace WHERE {
  ?person rdf:type dbo:Scientist .
  ?person dbo:birthPlace ?birthPlace .
  ?person dbo:field dbr:Artificial_Intelligence .
}

3. Domain-Specific Knowledge Graphs

Specialized knowledge:

// Medical Knowledge Graph
(disease:Disease {name: "Type 2 Diabetes"})
(symptom:Symptom {name: "Increased thirst"})
(drug:Drug {name: "Metformin"})

(disease)-[:HAS_SYMPTOM]->(symptom)
(drug)-[:TREATS]->(disease)
(drug)-[:HAS_SIDE_EFFECT]->(:Symptom {name: "Nausea"})

Building Knowledge Graphs

Automatic Extraction

// TrustGraph automatic extraction
await trustgraph.ingest({
  sources: ["documents/", "databases/", "apis/"],
  graphConfig: {
    extractEntities: true,
    entityTypes: ["Person", "Company", "Product", "Location"],
    extractRelationships: true,
    linkingStrategy: "semantic",
    deduplication: true
  }
});

// Extracts:
// Entities: Named entities from text
// Relationships: Verb phrases, co-occurrences, semantic links
// Properties: Attributes and metadata

Manual Construction

// Programmatic graph construction
await trustgraph.graph.createNode({
  type: "Person",
  properties: {
    name: "Jane Smith",
    role: "CEO"
  }
});

await trustgraph.graph.createNode({
  type: "Company",
  properties: {
    name: "TechCorp",
    industry: "Software"
  }
});

await trustgraph.graph.createRelationship({
  from: "person_jane",
  to: "company_techcorp",
  type: "leads",
  properties: {
    since: "2020-01-01"
  }
});

Schema-Driven Construction

// Define ontology first
await trustgraph.schema.define({
  ontology: `
    @prefix ex: <http://example.com/ontology#> .

    ex:Person a rdfs:Class .
    ex:Company a rdfs:Class .

    ex:worksAt a rdf:Property ;
      rdfs:domain ex:Person ;
      rdfs:range ex:Company .
  `
});

// Graph validates against schema
await trustgraph.ingest({
  sources: ["data.json"],
  validateSchema: true  // Ensures conformance
});

Querying Knowledge Graphs

Graph Traversal

// Find all paths between two entities
MATCH path = (start:Person {name: "Jane"})-[*1..4]-(end:Company {name: "Acme"})
RETURN path, length(path) as hops
ORDER BY hops

// Find friends of friends
MATCH (me:Person {name: "Jane"})-[:KNOWS]->(friend)-[:KNOWS]->(fof)
WHERE NOT (me)-[:KNOWS]->(fof)
RETURN DISTINCT fof.name

// Pattern matching
MATCH (company:Company)-[:EMPLOYS]->(employee:Person)-[:HAS_SKILL]->(skill:Skill)
WHERE company.industry = "Technology"
RETURN company.name, count(DISTINCT employee) as headcount,
       collect(DISTINCT skill.name) as skills

Multi-Hop Reasoning

// TrustGraph reasoning
const analysis = await trustgraph.reason({
  query: "How does Company A affect Market B?",
  startEntity: "company_a",
  targetEntity: "market_b",
  maxDepth: 4,
  relationshipTypes: ["invests_in", "competes_with", "operates_in"]
});

// Returns reasoning paths:
// Company A → invests_in → Company C
// Company C → operates_in → Market B
// Therefore: Company A has indirect presence in Market B

Benefits

1. Flexible Schema

// Easy to add new entity types
await graph.createNode({
  type: "NewEntityType",  // No schema migration needed
  properties: {...}
});

// Easy to add new relationship types
await graph.createRelationship({
  type: "new_relationship",  // Just start using it
  from: "entity_1",
  to: "entity_2"
});

2. Multi-Hop Queries

// Complex traversals are natural
MATCH path = (start)-[*1..5]-(end)
WHERE start.name = "Jane" AND end.type = "Technology"
RETURN path

// Traditional databases require multiple joins
SELECT ...
FROM persons p
JOIN works_at wa ON p.id = wa.person_id
JOIN companies c ON wa.company_id = c.id
JOIN uses u ON c.id = u.company_id
JOIN technologies t ON u.tech_id = t.id
WHERE p.name = "Jane"

3. Relationship-First Modeling

// Relationships are first-class
MATCH (a)-[r]->(b)
WHERE r.weight > 0.8  // Query on relationship properties
RETURN a, type(r), b

// Not just foreign keys - rich, typed connections

4. Discovery and Insights

// Find hidden patterns
MATCH (p1:Person)-[:INVESTED_IN]->(c:Company)<-[:INVESTED_IN]-(p2:Person)
WHERE NOT (p1)-[:KNOWS]-(p2)
RETURN p1, p2, c
// Discover: People who invested in same company but don't know each other

// Community detection
CALL gds.louvain.stream('myGraph')
YIELD nodeId, communityId
RETURN gds.util.asNode(nodeId).name, communityId

Use Cases

1. Recommendation Systems

// Collaborative filtering
MATCH (me:Person {id: 'user123'})-[:PURCHASED]->(p:Product)<-[:PURCHASED]-(other:Person)
MATCH (other)-[:PURCHASED]->(recommendation:Product)
WHERE NOT (me)-[:PURCHASED]->(recommendation)
RETURN recommendation.name, count(*) as score
ORDER BY score DESC
LIMIT 10

2. Fraud Detection

// Find suspicious patterns
MATCH (account:Account)-[:TRANSFERRED_TO*2..4]->(suspicious:Account)
WHERE suspicious.flagged = true
  AND all(r IN relationships(p) WHERE r.amount > 10000)
RETURN account, suspicious

3. Knowledge Discovery

// Drug repurposing
MATCH (drug:Drug)-[:TREATS]->(disease1:Disease)
MATCH (disease1)-[:SHARES_PATHWAY]->(disease2:Disease)
WHERE NOT (drug)-[:TREATS]->(disease2)
RETURN drug.name, disease2.name as potential_treatment

Best Practices

1. Define Clear Entity Types

// Good: Well-defined types
{
  type: "Person",
  properties: { name, email, birthDate }
}

// Avoid: Vague or overlapping types
{
  type: "Thing",
  properties: { stuff, data, info }
}

2. Use Meaningful Relationships

// Good: Semantic relationships
(person)-[:WORKS_AT]->(company)
(person)-[:MANAGES]->(team)
(company)-[:LOCATED_IN]->(city)

// Avoid: Generic relationships
(person)-[:RELATED_TO]->(company)
(person)-[:CONNECTED_TO]->(team)

3. Include Provenance

{
  type: "EMPLOYED_BY",
  properties: {
    since: "2020-01-01",
    source: "HR Database",
    confidence: 1.0,
    lastVerified: "2024-12-01"
  }
}

See Also

Examples

  • Google's Knowledge Graph connecting entities like 'Apple Inc.' → founded_by → 'Steve Jobs'
  • Enterprise knowledge graph linking customers, products, and transactions
  • Medical knowledge graph connecting diseases, symptoms, treatments, and drugs

Related Terms

Learn More