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];
52 * Create the linked list of E or H nodes. We create a table which is used
53 * later to create links among the nodes.
54 * @param size the no. of nodes to create
55 * @param degree the out degree of each node
56 * @return a table containing all the nodes.
58 public static Node[] fillTable(int size, int degree, Random r)
62 table = global new Node[size];
63 prevNode = global new Node(degree, r);
65 for (int i = 1; i < size; i++) {
66 Node curNode = global new Node(degree, r);
68 prevNode.next = curNode;
75 * Create unique `degree' neighbors from the nodes given in nodeTable.
76 * We do this by selecting a random node from the give nodeTable to
77 * be neighbor. If this neighbor has been previously selected, then
78 * a different random neighbor is chosen.
79 * @param nodeTable the list of nodes to choose from.
81 public void makeUniqueNeighbors(Node[] nodeTable, Random rand)
83 for (int filled = 0; filled < toNodes.length; filled++) {
88 boolean isBreak = false;
89 // generate a random number in the correct range
90 int index = rand.nextInt();
91 if (index < 0) index = -index;
92 index = index % nodeTable.length;
94 // find a node with the random index in the given table
95 otherNode = nodeTable[index];
97 for (k = 0; (k < filled) && (isBreak==false); k++) {
98 if (otherNode == toNodes[filled])
101 } while (k < filled);
103 // other node is definitely unique among "filled" toNodes
104 toNodes[filled] = otherNode;
106 // update fromCount for the other node
107 otherNode.fromCount++;
112 * Allocate the right number of FromNodes for this node. This
113 * step can only happen once we know the right number of from nodes
114 * to allocate. Can be done after unique neighbors are created and known.
116 * It also initializes random coefficients on the edges.
118 public void makeFromNodes()
120 fromNodes = global new Node[fromCount]; // nodes fill be filled in later
121 coeffs = global new double[fromCount];
125 * Fill in the fromNode field in "other" nodes which are pointed to
128 public void updateFromNodes(Random rand)
130 for (int i = 0; i < toNodes.length; i++) {
131 Node otherNode = toNodes[i];
132 int count = otherNode.fromLength++;
133 otherNode.fromNodes[count] = this;
134 otherNode.coeffs[count] = rand.nextDouble();
139 * Override the toString method to return the value of the node.
140 * @return the value of the node.
142 public String toString()
145 returnString = "value " + (long)value + ", from_count " + fromCount;