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.
20 * Created on Jan 30, 2005 by Spyros Voulgaris
23 package peersim.graph;
25 import java.util.ArrayList;
26 import java.util.BitSet;
29 * Speeds up {@link ConstUndirGraph#isEdge} by storing the links in an
30 * adjacency matrix (in fact in a triangle).
31 * Its memory consumption is huge but it's much faster if the isEdge method
32 * of the original underlying graph is slow.
34 public class FastUndirGraph extends ConstUndirGraph
37 private BitSet[] triangle;
40 // ======================= initializarion ==========================
41 // =================================================================
44 /** Calls super constructor */
45 public FastUndirGraph(Graph graph)
50 // -----------------------------------------------------------------
52 protected void initGraph()
54 final int max = g.size();
55 triangle = new BitSet[max];
56 for (int i=0; i<max; ++i)
58 in[i] = new ArrayList<Integer>();
59 triangle[i] = new BitSet(i);
62 for(int i=0; i<max; ++i)
64 for(Integer out:g.getNeighbours(i))
69 // But always add the link to the triangle
70 if (i>j) // make sure i>j
79 // ============================ Graph functions ====================
80 // =================================================================
83 public boolean isEdge(int i, int j)
92 return triangle[i].get(j);