Unveiling the Mahabharata’s Web: Analyzing Epic Relationships With Neo4j Graph Database (Part 1)

Source: Dall-E

This blog series stems from a weekend tinkering session where I dived into the world of Neo4j. The Mahabharata, an epic brimming with connections, became the ideal testing ground for this graph database.


Have you ever been captivated by a story full of complex relationships? Stories like the Mahabharata, an ancient Indian epic, weave a spell with a multitude of characters and their intricate connections. These connections — familial bonds, alliances, rivalries — form the very foundation of the narrative, driving the plot and shaping the destinies of its heroes and villains.

It all began with a simple conversation. My wife, engrossed in C. Rajagopalachari’s masterful translation of the Mahabharata, sparked my curiosity about the intricate web of relationships within the epic. The idea of translating these connections into a powerful graph model, leveraging the capabilities of Neo4j (the leader in graph databases), became an irresistible challenge.

In this two-part blog series, we’ll embark on a fascinating journey to explore the Mahabharata through the lens of graph theory. In Part -1, we’ll delve into the reasons why relationships hold such significance in this epic saga. We’ll then shed light on the power of graph databases like Neo4j, and how they can be effectively utilized to model and analyze these complex relationships.

Power of Relationships

The Mahabharata, one of the two major Sanskrit epics of ancient India (the other being the Ramayana), is a profound narrative that transcends mere storytelling to weave together philosophy, ethics, and the complexities of dharma (duty/righteousness). Traditionally attributed to the sage Vyasa, it’s considered a central text in Hinduism. The epic is massive, estimated to contain over 100,000 verses with over 1.8 million words. Due to its scale, pinpointing the exact number of characters is difficult. Conservative estimates suggest around 100 central characters and countless secondary figures, each intertwined in a dense web of relationships that range from familial bonds, friendships, and mentorships to rivalries and enmities. But some sources believe it could be many more. These characters and their relationships are not static; they evolve through the narrative, driven by complex motives, desires, and duties.

The importance of relationships in the Mahabharata cannot be overstated. They serve as the epic’s backbone, influencing the characters’ decisions, shaping the plot, and imparting moral lessons.

Mapping these relationships in a graph database like Neo4j can offer unprecedented insights into the epic’s social network, revealing patterns and connections that might not be apparent through traditional reading. Such an analysis can illuminate how characters are positioned within the narrative, the significance of their interactions, and the overarching themes that these relationships represent, providing a fresh perspective on an ancient text that continues to fascinate and instruct millions around the world.

Why Graphs? Why Neo4j?

The Mahabharata, with its vast array of characters and complex interconnections, presents a unique challenge for any data model aiming to accurately represent its intricate web of relationships. Traditional relational databases, while powerful, often struggle with the depth and fluidity of connections inherent in narratives like the Mahabharata.

Graph Databases, however, excel — nodes represent characters, edges depict relationships, perfectly mirroring the narrative’s intricate web. They treat relationships as first-class entities.

This is where graph technology shines, and specifically, why Neo4j, a leading graph database management system, was the natural choice for this project. By leveraging graphs and Neo4j, I can explore the impact of lineage, analyze alliances, and uncover hidden connections, leading to a richer understanding of the epic and its characters.

Graphing the Epic

With the power of graphs and Neo4j at our disposal, the next step was constructing the intricate web of relationships that defines the Mahabharata. This involved several key steps:

  • Data Gathering: The first step was assembling the data. This involved drawing upon various sources — character lists, family trees, and detailed descriptions from the epic itself. This data formed the foundation for our graph. While there is no definite source for this information, I primarily referred to this Quora article (among multiple other sources), which talks about the family lineage in this epic since the time of the creation of the universe.
  • Character Creation: Each character in the Mahabharata became a node in our graph. We populated these nodes with relevant information, such as their name, gender, title, etc.
  • Defining Relationships: The real magic began with defining the relationships between characters. Family bonds, alliances, rivalries — we meticulously mapped these connections using edges in Neo4j. These relationships were:
  • Populating the Graph: With nodes and edges in place, I had to painstakingly populate the graph with all the characters and their connections. This involved meticulous attention to detail, ensuring the accuracy of the relationships depicted in the epic.
CREATE INDEX IF NOT EXISTS FOR (p:Person) ON (p.gender);

CREATE (Brahma:Person {name: 'Brahma', gender: 'Male'})
CREATE (Marichi:Person {name: 'Marichi', gender: 'Male'})
CREATE (Brahma)-[:FATHER_OF]->(Marichi)
CREATE (Marichi)-[:SON_OF]->(Brahma)
CREATE (Kala:Person {name: 'Kala', gender: 'Female'})
CREATE (Marichi)-[:HUSBAND_OF]->(Kala)
CREATE (Kala)-[:WIFE_OF]->(Marichi)
CREATE (Kashyapa:Person {name: 'Kashyapa', gender: 'Male', type: 'sage'})
CREATE (Marichi)-[:FATHER_OF]->(Kashyapa)
CREATE (Kala)-[:MOTHER_OF]->(Kashyapa)
CREATE (Kashyapa)-[:SON_OF]->(Marichi)
CREATE (Kashyapa)-[:SON_OF]->(Kala)

Once the graph is populated with roughly 1000 cypher statements, our database will look something like this: 191 Person nodes and 579 Relationships.

Now, let’s query the database:

MATCH (n:Person where n.name="Vichitravirya") RETURN n;
MATCH (n:Person where n.name=”Vichitravirya”) RETURN n;
Result of the above query

The true power of a graph database like Neo4j shines in scenarios like this. It excels at uncovering hidden relationships within the data, connections that might easily slip through the cracks with traditional databases. Imagine wanting to trace the link between two characters in the Mahabharata. Neo4j allows us to discover the shortest path between them — the most direct chain of relationships connecting them — with remarkable ease.

MATCH (vichitravirya:Person { name:"Vichitravirya" }),
(ghatotakach:Person { name:"Ghatotakach" }),
path= shortestPath((vichitravirya)-[*]-(ghatotakach))

This process of building the Mahabharata graph in Neo4j wasn’t just about data entry. This ability to navigate complex connections unlocks a deeper understanding of the narrative and the characters’ intricate web of interactions. I could see how lineages intertwined, alliances shifted, and rivalries deepened, gaining a deeper appreciation for the complexities of the epic.

Want to replicate this project? Get started with a Neo4j Aura Free Instance and just execute the entire Cypher code available in my Github repo.


The exploration of the Mahabharata through the lens of graphs and Neo4j has only just begun. By building a comprehensive graph of characters and their relationships, we’ve laid the groundwork for exciting discoveries. This intricate web allows us to navigate lineages, analyze alliances, and uncover hidden connections, revealing a deeper understanding of the epic.

But this is just the first chapter in our journey. Part 2 will then unveil an ambitious step forward: a Neo4j-powered chatbot. This innovative tool stems from a desire to make the epic’s complex relationships accessible to everyone, transcending traditional query methods. By integrating Google’s Gemini API for natural language processing, we will be crafting an interactive experience that allows users to explore the Mahabharata in a way that’s as engaging as the narratives that inspired this project.

Get ready to converse with characters, uncover forgotten alliances, and delve deeper into the heart of this timeless epic — all through the power of graphs and chatbots!

Read part 2 here:
Bringing the Mahabharata Epic to Life: A Neo4j-Powered Chatbot using Google Gemini (Part 2)

Unveiling the Mahabharata’s Web: A Graph Journey using Neo4j  — From Epic Relationships to… was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.