I was wondering what would be the best way to implement undirected graphs (and hence graph traversal) on Google App Engine. I’m currently storing edges in the database as a list, i.e.
class Relation(db.Model):
connect = db.ListProperty(str, required=True)
but this is notoriously inefficient.
I’m aware of the directed graph question as posed here, but I find that it would not be so suitable for an undirected graph.
I would store the graph as a directed graph, which allows you to use queries more effectively. Obviously you need to have the constraint that all directed edges must have a partnering edge going in the opposite direction.
You can then pull out all of the edges relating to a specific vertex with a simple query:
Nice and simple, taking advantage of the fact you’re using appengine so you can let indexing do a lot of the work for you 😉
If you want to make sure that directed constraint remains true, obviously use a method to create edges like so:
Addressing roffles concerns about storing all the edges twice, you could try something like this:
This approach basically saves you storage space (every edge is only stored once) at the cost of slightly higher query times and much messier code. In my opinion that’s not a very good tradeoff, since you’re saving yourself about 64 bytes storage per edge.