Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could implement this both in a breadth-first, and depth-first. I am gonna post a depth-first algorithm:</p> <pre><code>public class Node { private List&lt;Node&gt; parents = new ArrayList&lt;Node&gt;(); private List&lt;Node&gt; children = new ArrayList&lt;Node&gt;(); private Map&lt;Node, String&gt; receivedMessages = new HashMap&lt;Node, String&gt;(); private String id = ""; public Node(String id) { this.id = id; } void processMessage(Node sender, String message) { if (parents.contains(sender)) receivedMessages.put(sender, message); // if all the parents sent the message if (receivedMessages.size() == parents.size()) { String newMessage = composeNewMessage(receivedMessages); if (children.size() == 0) // if end node or "leaf" ouputResult(this, newMessage); else { for (Node child : children) { child.processMessage(this, newMessage); } } } } public void addParent(Node parent) { if (parent != null) parents.add(parent); parent.children.add(this); } public void addChild(Node child) { if (child != null) children.add(child); child.parents.add(this); } private void ouputResult(Node node, String newMessage) { // TODO: implement } private String composeNewMessage(Map&lt;Node, String&gt; receivedMessages2) { // TODO: implement return ""; } public static void main(String[] args) { Node A = new Node("A"); Node B = new Node("B"); Node C = new Node("C"); Node D = new Node("D"); Node start = new Node("start"); Node end = new Node("end"); A.addParent(start); B.addParent(A); C.addParent(A); D.addParent(B); D.addParent(C); end.addParent(D); A.processMessage(start, "Message"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload