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
49 toNodes = global new Node[degree];
53 * Create the linked list of E or H nodes. We create a table which is used
54 * later to create links among the nodes.
55 * @param size the no. of nodes to create
56 * @param degree the out degree of each node
57 * @return a table containing all the nodes.
59 public static Node[] fillTable(int size, int degree, Random r)
63 table = global new Node[size];
64 prevNode = global new Node(degree, r);
66 for (int i = 1; i < size; i++) {
67 Node curNode = global new Node(degree, r);
69 prevNode.next = curNode;
76 * Create unique `degree' neighbors from the nodes given in nodeTable.
77 * We do this by selecting a random node from the give nodeTable to
78 * be neighbor. If this neighbor has been previously selected, then
79 * a different random neighbor is chosen.
80 * @param nodeTable the list of nodes to choose from.
82 public void makeUniqueNeighbors(Node[] nodeTable, Random rand)
84 for (int filled = 0; filled < toNodes.length; filled++) {
89 boolean isBreak = false;
90 // generate a random number in the correct range
91 int index = rand.nextInt();
92 if (index < 0) index = -index;
93 index = index % nodeTable.length;
95 // find a node with the random index in the given table
96 otherNode = nodeTable[index];
98 for (k = 0; (k < filled) && (isBreak==false); k++) {
99 if (otherNode == toNodes[filled])
102 } while (k < filled);
104 // other node is definitely unique among "filled" toNodes
105 toNodes[filled] = otherNode;
107 // update fromCount for the other node
108 otherNode.fromCount++;
113 * Allocate the right number of FromNodes for this node. This
114 * step can only happen once we know the right number of from nodes
115 * to allocate. Can be done after unique neighbors are created and known.
117 * It also initializes random coefficients on the edges.
119 public void makeFromNodes()
121 fromNodes = global new Node[fromCount]; // nodes fill be filled in later
122 coeffs = global new double[fromCount];
126 * Fill in the fromNode field in "other" nodes which are pointed to
129 public void updateFromNodes(Random rand)
131 for (int i = 0; i < toNodes.length; i++) {
132 Node otherNode = toNodes[i];
133 int count = otherNode.fromLength++;
134 otherNode.fromNodes[count] = this;
135 otherNode.coeffs[count] = rand.nextDouble();
140 * Get the new value of the current node based on its neighboring
141 * from_nodes and coefficients.
145 public void computeNewValue()
147 for (int i = 0; i < fromCount; i++) {
148 value -= coeffs[i] * fromNodes[i].value;
154 * Override the toString method to return the value of the node.
155 * @return the value of the node.
157 public String toString()
160 returnString = "value " + (long)value + ", from_count " + fromCount;
161 //return "value " + value + ", from_count " + fromCount;