Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package utils;
00025
00026 import java.util.*;
00027 import java.io.*;
00028
00029
00030
00031
00032
00033 public class GraphMatrix {
00034
00035 protected int numNodes;
00036
00037 protected int numEdges;
00038
00039 protected BitSet[] matrix;
00040
00041 protected Object[][] tags;
00042
00043
00044 public class Edge {
00045
00046 public int from;
00047 public int to;
00048
00049 public Edge(int from, int to) {
00050 this.from = from;
00051 this.to = to;
00052 }
00053 };
00054
00055 public GraphMatrix(int numNodes) {
00056 this.numNodes = numNodes;
00057 numEdges = 0;
00058 matrix = new BitSet[numNodes];
00059
00060 for (int i = 0; i < numNodes; ++i) {
00061 matrix[i] = new BitSet(numNodes);
00062 }
00063 }
00064
00065 public int getNumNodes() {
00066 return numNodes;
00067 }
00068
00069 public int getNumEdges() {
00070 return numEdges;
00071 }
00072
00073 public void addEdge(int fromNode, int toNode) {
00074 assert((fromNode < numNodes) && (toNode < numNodes));
00075 matrix[fromNode].set(toNode);
00076 }
00077
00078
00079
00080
00081 public boolean hasEdge(int fromNode, int toNode) {
00082 assert((fromNode < numNodes) && (toNode < numNodes));
00083 return matrix[fromNode].get(toNode);
00084 }
00085
00086 protected void initTags() {
00087 tags = new Object[numNodes][numNodes];
00088 }
00089
00090
00091 public void setTag(int fromNode, int toNode, Object tag) {
00092 assert(hasEdge(fromNode, toNode));
00093
00094 if (tags == null) {
00095 initTags();
00096 }
00097 tags[fromNode][toNode] = tag;
00098 }
00099
00100 public Object getTag(int fromNode, int toNode) {
00101 assert(hasEdge(fromNode, toNode) && (tags != null));
00102
00103 return tags[fromNode][toNode];
00104 }
00105
00106
00107
00108
00109 public Iterator getEdgeIterator() {
00110 return new EdgeIterator();
00111 }
00112
00113
00114
00115
00116
00117
00118 public Iterator getAncestorIterator(int node) {
00119 return new AncestorIterator(node);
00120 }
00121
00122
00123
00124
00125
00126
00127 public Iterator getDescendantIterator(int node) {
00128 return new DescendantIterator(node);
00129 }
00130
00131
00132
00133
00134
00135
00136 protected class EdgeIterator implements Iterator {
00137
00138 int from = 0;
00139 int to = -1;
00140
00141 GraphMatrix.Edge next;
00142
00143 public EdgeIterator() {
00144 moveToNext();
00145 }
00146
00147 public boolean hasNext() {
00148 return (next != null);
00149 }
00150
00151 public Object next() {
00152 if (next == null) throw new NoSuchElementException();
00153 GraphMatrix.Edge result = next;
00154 moveToNext();
00155
00156 assert(hasEdge(result.from, result.to));
00157 return result;
00158 }
00159
00160 public void remove() {
00161 throw new UnsupportedOperationException();
00162 }
00163
00164 protected void moveToNext() {
00165
00166 ++to;
00167
00168 while (from < numNodes) {
00169 while (to < numNodes) {
00170 if (matrix[from].get(to)) {
00171 next = new GraphMatrix.Edge(from, to);
00172 return;
00173 }
00174 ++to;
00175 }
00176
00177 ++from;
00178 to = 0;
00179 }
00180
00181 next = null;
00182
00183 }
00184
00185 }
00186
00187 protected class AncestorIterator implements Iterator {
00188
00189 int desc;
00190
00191 int nextAnc = -1;
00192
00193
00194 public AncestorIterator(int desc) {
00195 this.desc = desc;
00196 moveToNext();
00197 }
00198
00199 public Object next() {
00200 assert(hasNext());
00201
00202 Integer result = new Integer(nextAnc);
00203 moveToNext();
00204 return result;
00205 }
00206
00207 protected void moveToNext() {
00208 boolean nextAncFound = false;
00209
00210 while (!nextAncFound && (nextAnc + 1 < numNodes)) {
00211 ++nextAnc;
00212 nextAncFound = matrix[nextAnc].get(desc);
00213 }
00214
00215 if (!nextAncFound) ++nextAnc;
00216
00217
00218 assert(!nextAncFound || hasEdge(nextAnc, desc));
00219 }
00220
00221 public boolean hasNext() {
00222 return (nextAnc < numNodes);
00223 }
00224
00225 public void remove() {
00226 throw new UnsupportedOperationException();
00227 }
00228
00229 };
00230
00231
00232 protected class DescendantIterator implements Iterator {
00233
00234 BitSet descs;
00235
00236 int nextDesc = -1;
00237
00238
00239 public DescendantIterator(int anc) {
00240 descs = matrix[anc];
00241 moveToNext();
00242 }
00243
00244 public Object next() {
00245 assert(hasNext());
00246
00247 Integer result = new Integer(nextDesc);
00248 moveToNext();
00249 return result;
00250 }
00251
00252 protected void moveToNext() {
00253 ++nextDesc;
00254 nextDesc = descs.nextSetBit(nextDesc);
00255 }
00256
00257 public boolean hasNext() {
00258 return (nextDesc != -1);
00259 }
00260
00261 public void remove() {
00262 throw new UnsupportedOperationException();
00263 }
00264
00265 };
00266
00267 };
00268
00269
00270
00271
00272
00273