简体   繁体   中英

What is a good design pattern/structure to represent Direct Acyclic Graph in Java?

I need to store a reasonably large Direct Acyclic Graph in Java (order of 100,000 nodes, depth between 7 and 20, irregular shaped, average depth 13).

What would be the best-performing data structure(s) to store it if the predominant operation I need after building the data structure is:

  • 99% operations: Find a full set of accendant paths (from the root down to a given node)
  • 1% operations: Find all children, or more often, all ancestors, of a given node.

As can be obvious, I'd like the first operation to be O(1) if possible, as opposed to O(Average-Depth)

Please note that for the purposes of this question, the data structure is write-once: after I build it from a list of nodes and vertices, the graph topology will never change .


My naive implementation would be to store it as a combination of:

HashMap<Integer, Integer[]> childrenPerParent;
HashMap<Integer, Integer[]> ascendantPaths; 

Eg I store, for each node: a list of children of that node; and separately, a set of paths to the root from that node.

Downside: This seems very wasteful as far as space (we basically store each of the inner graph nodes multiples of multiples of times in the ascendantPaths - eg given size estimates, we would store extra 100,000 * 13 = 1,3Million node copies in ascendantPaths , each of which is an object to be created and stored )

I would recommend using Neo4J . It's a graph database implemented in Java with a lot of low-level optimizations (eg, node and edge attributes are stored in their own blocks so that node identities and their edges can be packed), and it mmaps the on-disk database. Following an edge is independent of the number of nodes in the graph or edges on the origin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM