python - Implementing a 'category' system with attributes -
i have around 1000 'categories' cover edges. i'm specifiying these categories using attributes:
g.es[0]["$cat1"]=true
one edge can belong multiple categories.
the problem is, causes of other edges, if have nothing category 1 attribute $cat1=none
.
so if edge part of single category, have 999 other attributes such $catn=none
.
i need ability fetch each category (including it's member nodes , edges) separate sub-graph. right traverse on edges, seeing $catn = true
, , edges , nodes new graph.
- does method whole scalable? seems bit messy, since there might million nodes , ten thousand categories. mean each edge store 2-3
$catn = true
thousands of redundant$catn = none
. - if not, have suggestions implement 'category' system better?
- if best can done, suggestions retrieving particular category? traversing edges seems waste. guess can maintain separate data structure edge nos corresponding each category. pain maintain though.
is necessary store category of each edge edge attribute? if graph not mutated (i.e. not delete edges graph), can use external python dict maps category ids edge ids - can fetch members of each category single dictionary lookup. if need tell category edge belongs to, need reverse mapping, maybe it's best create separate "bidirectional mapping" class , maintain 2 sides of mapping separately:
from collections import defaultdict class categorymapping(object): def __init__(self): self.category_to_members = defaultdict(set) self.member_to_categories = defaultdict(set) def add(self, category, member): self.category_to_members[category].add(member) self.member_to_categories[member].add(category) def remove(self, category, member): self.category_to_members[category].discard(member) self.member_to_categories[member].discard(category) def categories_of(self, member): return self.member_to_categories[member] def members_of(self, category): return self.category_to_member[category]
edit: if edges removed graph, can assign unique id each edge in id
edge attribute , use ids in categorymapping
. problem edge lookups attribute o(n) operation n number of edges. mitigate this, can create edge id-to-index mapping class well. class have edges_removed()
method must call old ids of removed edges whenever remove edges graph, , should update internal id-to-index mapping accordingly. (sadly, igraph not have special id-like attribute edges, though treats name
attribute of vertex objects way can achieve o(1) lookup of vertex name). can make use of fact index of edge i become i-k after removal iff there k removed edges index smaller i in original graph.
Comments
Post a Comment