ahmedakef
ahmedakef

Reputation: 301

how to simulate graph in database

i have a website that simulate courses dependencies like this : courses dependencies

i found that network model is the best model or my case but i haven't found any implementation to it in RDBMS.

so how can i simulate graph in database ?

Upvotes: 1

Views: 466

Answers (1)

Lukisn
Lukisn

Reputation: 190

A naive Django model could look like this:

from django.db import models

class Graph(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

class Node(models.Model):
    name = models.CharField(max_length=100)
    graph = models.ForeignKey(Graph, on_delete=models.CASCADE)

class Edge(models.Model):
    name = models.CharField(max_length=100)
    graph = models.ForeignKey(Graph, on_delete=models.CASCADE)
    from_node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name="from_node")
    to_node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name="to_node")

This lets you store arbitrary graph information. In your case the nodes would be the courses and the edges would be the dependencies. For working with this information a relational database is not an ideal tool. Maybe you could minimize queries by querying a whole graph and process this data with graph related tools like e.g. NetworkX.

Another issue of this model I would like to point out is that it is generally possible to store edges that go from one to another graph. Also other graph related properties like duplicate edges or loops have to be considered.

Upvotes: 3

Related Questions