2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.graph;
25 * Implements static methods to load and write graphs.
27 public class GraphIO {
31 // ================== public static methods =========================
32 // ==================================================================
36 * Prints graph in edge list format. Each line contains exactly two
37 * node IDs separated by whitespace.
39 public static void writeEdgeList( Graph g, PrintStream out ) {
41 for(int i=0; i<g.size(); ++i)
43 Iterator it=g.getNeighbours(i).iterator();
46 out.println(i+" "+it.next());
51 // ------------------------------------------------------------------
54 * Prints graph in neighbor list format. Each line starts with the
55 * id of a node followed by the ids of its neighbors separated by space.
57 public static void writeNeighborList( Graph g, PrintStream out ) {
59 out.println("# "+g.size());
61 for(int i=0; i<g.size(); ++i)
64 Iterator it=g.getNeighbours(i).iterator();
67 out.print(it.next()+" ");
73 // ------------------------------------------------------------------
76 * Saves the given graph to
77 * the given stream in DOT format. Good for the graphviz package.
79 public static void writeDOT( Graph g, PrintStream out ) {
81 out.println((g.directed()?"digraph":"graph")+" {");
83 for(int i=0; i<g.size(); ++i)
85 Iterator<Integer> it=g.getNeighbours(i).iterator();
88 final int j = it.next();
90 out.println(i+" -> "+j+";");
92 out.println(i+" -- "+j+";");
99 // ------------------------------------------------------------------
102 * Saves the given graph to
103 * the given stream in GML format.
105 public static void writeGML( Graph g, PrintStream out ) {
107 out.println("graph [ directed "+(g.directed()?"1":"0"));
109 for(int i=0; i<g.size(); ++i)
110 out.println("node [ id "+i+" ]");
112 for(int i=0; i<g.size(); ++i)
114 Iterator it=g.getNeighbours(i).iterator();
118 "edge [ source "+i+" target "+it.next()+" ]");
125 // --------------------------------------------------------------------
128 * Saves the given graph to
129 * the given stream to be read by NETMETER. It should be ok also for Pajek.
131 public static void writeNetmeter( Graph g, PrintStream out ) {
133 out.println("*Vertices "+g.size());
134 for(int i=0; i<g.size(); ++i)
135 out.println((i+1)+" \""+(i+1)+"\"");
137 out.println("*Arcs");
138 for(int i=0; i<g.size(); ++i)
140 Iterator it=g.getNeighbours(i).iterator();
143 out.println((i+1)+" "+
144 (((Integer)it.next()).intValue()+1)+" 1");
147 out.println("*Edges");
150 // --------------------------------------------------------------------
153 * Saves the given graph to
154 * the given stream in UCINET DL nodelist format.
156 public static void writeUCINET_DL( Graph g, PrintStream out ) {
158 out.println("DL\nN="+g.size()+"\nFORMAT=NODELIST\nDATA:");
160 for(int i=0; i<g.size(); ++i)
162 out.print(" " + (i+1));
163 Iterator it=g.getNeighbours(i).iterator();
166 out.print(" "+(((Integer)it.next()).intValue()+1));
173 // --------------------------------------------------------------------
176 * Saves the given graph to
177 * the given stream in UCINET DL matrix format.
179 public static void writeUCINET_DLMatrix( Graph g, PrintStream out ) {
181 out.println("DL\nN="+g.size()+"\nDATA:");
183 for(int i=0; i<g.size(); ++i)
185 BitSet bs = new BitSet(g.size());
186 Iterator it=g.getNeighbours(i).iterator();
189 bs.set( ((Integer)it.next()).intValue() );
191 for(int j=0; j<g.size(); ++j)
193 out.print(bs.get(j)?" 1":" 0");
200 // --------------------------------------------------------------------
203 * Saves the given graph to
204 * the given stream in Chaco format. We need to output the number of edges
205 * so they have to be counted first which might not be very efficient.
206 * Note that this format is designed for undirected graphs only.
208 public static void writeChaco( Graph g, PrintStream out ) {
210 if( g.directed() ) System.err.println(
211 "warning: you're saving a directed graph in Chaco format");
214 for(int i=0; i<g.size(); ++i) edges += g.getNeighbours(i).size();
216 out.println( g.size() + " " + edges/2 );
218 for(int i=0; i<g.size(); ++i)
220 Iterator it=g.getNeighbours(i).iterator();
223 out.print((((Integer)it.next()).intValue()+1)+" ");
231 // -------------------------------------------------------------------
234 * Read a graph in newscast graph format.
235 * The format depends on mode, the parameter.
236 * The file begins with the three byte latin 1 coded "NCG" string followed
237 * by the int MODE which is the
238 * given parameter. The formats are the following as a function of mode:
240 * <li> 1: Begins with cacheSize in binary format (int), followed by the
241 * numberOfNodes (int), and then a continuous series of exactly
242 * numberOfNodes records, where a record describes a node's
243 * neighbours and their timestamps.
244 * A record is a series of exactly cacheSize (int,long) pairs where
245 * the int is the node id, and the long is the timestamp.
246 * Node id-s start from 1. Node id 0 means no node and used if the parent
247 * node has less that cacheSize nodes.</li>
249 * @param file Filename to read
250 * @param direction If 0, the original directionality is preserved, if 1,
251 * than each edge is reversed, if 2 then directionality is dropped and the
252 * returned graph will be undirected.
254 public static Graph readNewscastGraph( String file, int direction )
257 NeighbourListGraph gr = new NeighbourListGraph( direction != 2 );
258 FileInputStream fis = new FileInputStream(file);
259 DataInputStream dis = new DataInputStream(fis);
265 final int MODE = dis.readInt();
266 if( MODE != 1 ) throw new IOException("Unknown mode "+MODE);
268 final int CACHESIZE = dis.readInt();
269 final int GRAPHSIZE = dis.readInt();
271 //System.out.println("header: "+MODE+" "+CACHESIZE+" "+GRAPHSIZE);
273 for(int i=1; i<=GRAPHSIZE; ++i)
275 int iind = gr.addNode(i);
277 for(int j=0; j<CACHESIZE; ++j)
279 int a = dis.readInt();
282 int agentIndex = gr.addNode(a);
283 if( direction == 0 ) gr.setEdge(iind,agentIndex);
284 else gr.setEdge(agentIndex,iind);