Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Directed Acyclic Graph (DAG) data type in Java, and should I use it?

I am modeling a power subsystem in Java. A simple SQLite database contains a set of Line Replaceable Units (LRUs) and the connections between them. I am writing a Power Model API to simplify queries of the data store, using DDD patterns and repositories.

I am seeking an appropriate Java collection to model the query results. There are some special cases in a LRU connection stream that have to be modeled:

  1. Initially, there's a Power Distribution Unit (PDU) with multiple ports (<=16) that feeds power to downstream LRUs.
  2. Typical connections in a power stream involve a single source LRU where power originates and a single Sink LRU where power is drained.
  3. However, downstream there could be a single source LRU connected to multiple sink LRUs.
  4. There are no cycles in a power stream.

The inclusion of #3 above has led me to think about returning query results from the API as a tree. But the only tree I've found in java.util is a TreeMap key-value paired red-black tree, which doesn't seem appropriate (or I can't think of an appropriate abstraction for modeling power streams with it.) I've also been considering a LinkedHashSet, but I'm not convinced it is appropriate either. It's not clear to me how a node in this structure would point to downstream nodes.

I'm not concerned about efficiency in time or space at this point. My API just has to work by supplying power connection information to external clients (i.e., the Presentation Tier of a Java-based Power Monitoring & Control app.) There are also no restrictions on the use of open source data types/libraries.

In general computer science parlance, what I'm really seeking is a Directed-Acyclic-Graph (DAG).

Is there an implementation of that for Java? Am I correct that a DAG is appropriate for my scenario?

like image 379
retrodrone Avatar asked Jun 06 '11 15:06

retrodrone


3 Answers

I don't know if it can help but take a look at JGraphT.

like image 169
patapizza Avatar answered Oct 17 '22 18:10

patapizza


As you can see if regarding the "related" questions, similar questions were already asked. And no, Java does not have a general-purpose graph/tree/DAG data type (if we don't count Swing's TreeModel).

Make your own one.


Here is an example (readonly) interface:

interface Node<N extends Node<N,E>, E extends Edge<N,E>> {
    public Set<E> outgoingEdges();
    public Set<E> ingoingEdges();
}

interface Edge<N extends Node<N,E>, E extends Edge<N,E>> {
    public E source();
    public E sink();
}

You would then have

interface LRU implements Node<LRU, Line> { ... }
interface Line implements Edge<LRU, Line> { ... }

(or classes instead).

like image 43
Paŭlo Ebermann Avatar answered Oct 17 '22 17:10

Paŭlo Ebermann


For this particular problem. I've decided to use a LinkedListMultimap from Guava.

like image 30
retrodrone Avatar answered Oct 17 '22 17:10

retrodrone