Pergunta

I've read this post Name of data structure that's tree-like with multiple root nodes. What I'm asking for is not a forest.

I would give you a simple example that easily depicts my case.

You have a usual power source from EB and an inverter (alternate power source) in your house. Consider these as the root nodes. And the next level are the monitoring meters at every room. And at the next level you have your equipment like Fan, AC, Fridge etc.

In this tree the first level are the list of power sources and the second-to-nth level will have the energy flow till the consumption equipment.

Is there any suitable data structure to depict this tree which has multiple root nodes?

Edit:

Image of the Required Structure

enter image description here

Foi útil?

Solução

You are looking for a kind of Directed Acyclic Graph (DAG). These graphs do not have a root node. However, the nodes have a partial order. I.e. when we look at two nodes we can sometimes tell which node is “higher”. In your example, we could say that power sources are higher than meters which are higher than consumers.

Trees are a special kind of DAG where each node has exactly one parent node (except for the root which has no parents).

A Multitree is a DAG where there is only one unambiguous path between two nodes. I.e. all nodes that are reachable from any (root) node form a tree.

Outras dicas

There are datastructures called multitree or polytree, direcected acyclic graphs with multiple root nodes.

From your example regarding energy flow, you may what to check the max-flow min-cut theorem as well (a.k.a. s-t flow theorem).

Licenciado em: CC-BY-SA com atribuição
scroll top