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
41 * Constructor for a node with given `degree'. The value of the
42 * node is initialized to a random value.
44 public Node(int degree, Random r)
46 value = r.nextDouble();
47 // create empty array for holding toNodes
48 toNodes = new Node[degree];
51 for (int i = 0; i<fromCount; i++) {
60 * Create the linked list of E or H nodes. We create a table which is used
61 * later to create links among the nodes.
62 * @param size the no. of nodes to create
63 * @param degree the out degree of each node
64 * @return a table containing all the nodes.
66 public Node[] fillTable(int size, int degree, Random r)
68 Node[] table = new Node[size];
70 Node prevNode = new Node(degree, r);
72 for (int i = 1; i < size; i++) {
73 Node curNode = new Node(degree, r);
75 prevNode.next = curNode;
82 * Create unique `degree' neighbors from the nodes given in nodeTable.
83 * We do this by selecting a random node from the give nodeTable to
84 * be neighbor. If this neighbor has been previously selected, then
85 * a different random neighbor is chosen.
86 * @param nodeTable the list of nodes to choose from.
88 public void makeUniqueNeighbors(Node[] nodeTable, Random rand)
90 for (int filled = 0; filled < toNodes.length; filled++) {
97 // generate a random number in the correct range
98 int index = rand.nextInt();
99 if (index < 0) index = -index;
100 index = index % nodeTable.length;
102 // find a node with the random index in the given table
103 otherNode = nodeTable[index];
105 for (k = 0; (k < filled) && (!isBreak); k++) {
106 if (otherNode == toNodes[filled])
110 } while (k < filled);
112 // other node is definitely unique among "filled" toNodes
113 toNodes[filled] = otherNode;
115 // update fromCount for the other node
116 otherNode.fromCount++;
121 public void makeUniqueNeighborsThread(Node[] nodeTable, Random rand, int l, int u)
123 for (int filled = 0; filled < toNodes.length; filled++) {
130 // generate a random number in the correct range
131 int index = rand.nextInt();
132 if (index < 0) index = -index;
133 index = index % nodeTable.length;
135 // find a node with the random index in the given table
136 otherNode = nodeTable[index];
138 for (k = 0; (k < filled) && (!isBreak); k++) {
139 if (otherNode == toNodes[filled])
143 } while (k < filled);
145 // other node is definitely unique among "filled" toNodes
146 toNodes[filled] = otherNode;
148 // update fromCount for the other node
149 otherNode.fromCount++;
156 * Allocate the right number of FromNodes for this node. This
157 * step can only happen once we know the right number of from nodes
158 * to allocate. Can be done after unique neighbors are created and known.
160 * It also initializes random coefficients on the edges.
162 public void makeFromNodes()
164 fromNodes = new Node[fromCount]; // nodes fill be filled in later
165 coeffs = new double[fromCount];
169 * Fill in the fromNode field in "other" nodes which are pointed to
172 public void updateFromNodes(Random rand)
174 for (int i = 0; i < toNodes.length; i++) {
175 Node otherNode = toNodes[i];
176 int count = otherNode.fromLength++;
177 otherNode.fromNodes[count] = this;
178 otherNode.coeffs[count] = rand.nextDouble();
183 * Get the new value of the current node based on its neighboring
184 * from_nodes and coefficients.
191 for (int i = 0; i < n.fromCount; i++) {
192 n.value -= n.coeffs[i] * n.fromNodes[i].value;
198 public void computeNewValue()
200 for (int i = 0; i < fromCount; i++) {
201 value -= coeffs[i] * fromNodes[i].value;
207 * Override the toString method to return the value of the node.
208 * @return the value of the node.
210 public String toString()
213 returnString = "value " + (long)value + ", from_count " + fromCount;
214 //return "value " + value + ", from_count " + fromCount;