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 = global 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 static Node[] fillTable(int size, int degree, Random r)
68 Node[] table = global new Node[size];
70 Node prevNode = global new Node(degree, r);
72 for (int i = 1; i < size; i++) {
73 Node curNode = global 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==false); 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 * Allocate the right number of FromNodes for this node. This
122 * step can only happen once we know the right number of from nodes
123 * to allocate. Can be done after unique neighbors are created and known.
125 * It also initializes random coefficients on the edges.
127 public void makeFromNodes()
129 fromNodes = global new Node[fromCount]; // nodes fill be filled in later
130 coeffs = global new double[fromCount];
134 * Fill in the fromNode field in "other" nodes which are pointed to
137 public void updateFromNodes(Random rand)
139 for (int i = 0; i < toNodes.length; i++) {
140 Node otherNode = toNodes[i];
141 int count = otherNode.fromLength++;
142 otherNode.fromNodes[count] = this;
143 otherNode.coeffs[count] = rand.nextDouble();
148 * Get the new value of the current node based on its neighboring
149 * from_nodes and coefficients.
153 public void computeNewValue()
155 for (int i = 0; i < fromCount; i++) {
156 value -= coeffs[i] * fromNodes[i].value;
162 * Override the toString method to return the value of the node.
163 * @return the value of the node.
165 public String toString()
168 returnString = "value " + (long)value + ", from_count " + fromCount;
169 //return "value " + value + ", from_count " + fromCount;