Add a edge to direct acyclic graph with other restrictions

為{幸葍}努か 提交于 2019-12-03 16:36:48

Your question comes down to "Can inserting edges in a DAG be done faster than O(v+e)?" as per requirement (1). Requirement (2) is a more local constraint that doesn't require checking the entire graph.

I think the answer is no: you can't do better than O(v+e) in the worst case (where v is the number of nodes/vertices and e is the number of edges).

No doubt there are tricks to improve expected performance, depending on the properties of your DAG and how it changes over time. This appears to be an active research topic. For example, I imagine for some graphs it might be beneficial to cluster nodes. Inserting edges within a cluster then only requires checks within the cluster sub-DAG. But then you'd need a proper clustering strategy with support for cheaply updating the clustering when adding nodes, etc.

Don't know about your implmentation, but recommend you add indexing. Each row index must store pairs metaparent-metachild

metaparent - is parent in link: parent, parent-of-parent,...

metachid - is child in link: child, child-of-child, ...

so for graph A->B->C exists following indexes: A-B, B-C, A-C Adding explicit edge between B->C causes an assertion since such entry already exist. So complexity of algorithm reduces from n^2 to ln(n)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!