2 * This class implements nodes (both E- and H-nodes) of the EM graph. Sets
3 * up random neighbors and propagates field values among neighbors.
7 * The value of the node.
11 * The next node in the list.
15 * Array of nodes to which we send our value.
19 * Array of nodes from which we receive values.
23 * Coefficients on the fromNodes edges
27 * The number of fromNodes edges
31 * Used to create the fromEdges - keeps track of the number of edges that have
37 * Constructor for a node with given `degree'. The value of the
38 * node is initialized to a random value.
40 public Node(int degree, Random r)
42 value = r.nextDouble();
43 // create empty array for holding toNodes
44 toNodes = global new Node[degree];
48 * Create the linked list of E or H nodes. We create a table which is used
49 * later to create links among the nodes.
50 * @param size the no. of nodes to create
51 * @param degree the out degree of each node
52 * @return a table containing all the nodes.
54 public static Node[] fillTable(int size, int degree, Random r)
58 table = global new Node[size];
59 prevNode = global new Node(degree, r);
61 for (int i = 1; i < size; i++) {
62 Node curNode = global new Node(degree, r);
64 prevNode.next = curNode;
71 * Create unique `degree' neighbors from the nodes given in nodeTable.
72 * We do this by selecting a random node from the give nodeTable to
73 * be neighbor. If this neighbor has been previously selected, then
74 * a different random neighbor is chosen.
75 * @param nodeTable the list of nodes to choose from.
77 public void makeUniqueNeighbors(Node[] nodeTable, Random rand)
79 for (int filled = 0; filled < toNodes.length; filled++) {
84 boolean isBreak = false;
85 // generate a random number in the correct range
86 int index = rand.nextInt();
87 if (index < 0) index = -index;
88 index = index % nodeTable.length;
90 // find a node with the random index in the given table
91 otherNode = nodeTable[index];
93 for (k = 0; (k < filled) && (isBreak==false); k++) {
94 if (otherNode == toNodes[filled])
99 // other node is definitely unique among "filled" toNodes
100 toNodes[filled] = otherNode;
102 // update fromCount for the other node
103 otherNode.fromCount++;
108 * Allocate the right number of FromNodes for this node. This
109 * step can only happen once we know the right number of from nodes
110 * to allocate. Can be done after unique neighbors are created and known.
112 * It also initializes random coefficients on the edges.
114 public void makeFromNodes()
116 fromNodes = global new Node[fromCount]; // nodes fill be filled in later
117 coeffs = global new double[fromCount];
121 * Fill in the fromNode field in "other" nodes which are pointed to
124 public void updateFromNodes(Random rand)
126 for (int i = 0; i < toNodes.length; i++) {
127 Node otherNode = toNodes[i];
128 int count = otherNode.fromLength++;
129 otherNode.fromNodes[count] = this;
130 otherNode.coeffs[count] = rand.nextDouble();
135 * Override the toString method to return the value of the node.
136 * @return the value of the node.
138 public String toString()
141 returnString = "value " + (long)value + ", from_count " + fromCount;