GraphMatrix.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00024 package utils;
00025 
00026 import java.util.*;
00027 import java.io.*;
00028 
00029 /*
00030  * A graph, where the nodes are denoted by integers, and the edges are represented by 
00031  * a NxN matrix of bits (N...# of nodes). 
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      * Complexity: O(const). 
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     // there must already be an edge between these nodes!
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      * Returns an iterator which iterates through all edges and which next() method returns Edge instances.
00108      */
00109     public Iterator getEdgeIterator() {
00110         return new EdgeIterator();
00111     }
00112 
00113     /*
00114      * This iterator iterates through all ancestors of node; the Iterator.next() method returns Integer instances.
00115      *
00116      * Note: the complexity of each Iterator.next() method is O(N).
00117      */
00118     public Iterator getAncestorIterator(int node) {
00119         return new AncestorIterator(node);
00120     }
00121 
00122     /*
00123      * This iterator iterates through all descendants of node; the next() method returns Integer instances.
00124      *
00125      * Note: the complexity of each Iterator.next() method is O(N).
00126      */
00127     public Iterator getDescendantIterator(int node) {
00128         return new DescendantIterator(node);
00129     }
00130 
00131 
00132     // ------------------------- iterator classes ----------------------------------
00133     
00134 
00135     // probably untested..
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     }  // class GraphMatrix.EdgeIterator
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             // postcondition 
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     };  // class GraphMatrix.AncestorIterator
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     };  // class GraphMatrix.DescendantIterator
00266 
00267 };  // class GraphMatrix
00268     
00269 
00270 
00271 
00272 
00273 


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16