Garvey
Garvey

Reputation: 1309

Build a Knowledge Graph in python

I want to build a knowledge graph to store instance and link weights. Example like:

graph={
'a':{'b':3,'c':4},
'b':{'a':3,'c':2},
'c':{'a':4,'b':2}
}

This is an undirected graph. The link weight need to be updated frequently, and I don't know whether dict of dict is recommended to do this. I don't know how to start with this. Any code example or existing python API recommended is appreciated.

Upvotes: 1

Views: 950

Answers (1)

blhsing
blhsing

Reputation: 106946

Using a dict of dicts would require that the same weights be stored twice for both directions of every edge, resulting in redundancy and possible sync issues.

Instead, you can use a dict of weight values indexed by frozensets of vertices:

graph = {
    frozenset({'a', 'b'}): 3,
    frozenset({'b', 'c'}): 2,
    frozenset({'a', 'c'}): 4
}

Upvotes: 4

Related Questions