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;
24 * This class is an adaptor making any Graph an undirected graph
25 * by making its edges bidirectional. The graph to be made undirected
26 * is passed to the constructor. Only the reference is stored so
27 * if the directed graph changes later, the undirected version will
28 * follow that change. However, {@link #getNeighbours} has O(n) time complexity
29 * (in other words, too slow for large graphs).
30 * @see ConstUndirGraph
32 public class UndirectedGraph implements Graph {
35 // ====================== private fileds ========================
36 // ==============================================================
39 private final Graph g;
42 // ====================== public constructors ===================
43 // ==============================================================
46 public UndirectedGraph( Graph g ) {
52 // ======================= Graph implementations ================
53 // ==============================================================
56 public boolean isEdge(int i, int j) {
58 return g.isEdge(i,j) || g.isEdge(j,i);
61 // ---------------------------------------------------------------
64 * Uses sets as collection so does not support multiple edges now, even if
65 * the underlying directed graph does.
67 public Collection<Integer> getNeighbours(int i) {
69 Set<Integer> result = new HashSet<Integer>();
70 result.addAll(g.getNeighbours(i));
71 final int max = g.size();
72 for(int j=0; j<max; ++j)
74 if( g.isEdge(j,i) ) result.add(j);
77 return Collections.unmodifiableCollection(result);
80 // ---------------------------------------------------------------
82 public Object getNode(int i) { return g.getNode(i); }
84 // ---------------------------------------------------------------
87 * If there is an (i,j) edge, returns that, otherwise if there is a (j,i)
88 * edge, returns that, otherwise returns null.
90 public Object getEdge(int i, int j) {
92 if( g.isEdge(i,j) ) return g.getEdge(i,j);
93 if( g.isEdge(j,i) ) return g.getEdge(j,i);
97 // ---------------------------------------------------------------
99 public int size() { return g.size(); }
101 // --------------------------------------------------------------------
103 public boolean directed() { return false; }
105 // --------------------------------------------------------------------
108 public boolean setEdge( int i, int j ) {
110 throw new UnsupportedOperationException();
113 // ---------------------------------------------------------------
116 public boolean clearEdge( int i, int j ) {
118 throw new UnsupportedOperationException();
121 // --------------------------------------------------------------------
123 public int degree(int i) {
125 return getNeighbours(i).size();
128 // --------------------------------------------------------------------
130 public static void main( String[] args ) {
134 UndirectedGraph ug = new UndirectedGraph(net);
135 for(int i=0; i<net.size(); ++i)
136 System.err.println(i+" "+net.getNeighbours(i));
137 System.err.println("============");
138 for(int i=0; i<ug.size(); ++i)
139 System.err.println(i+" "+ug.getNeighbours(i));
140 for(int i=0; i<ug.size(); ++i)
142 for(int j=0; j<ug.size(); ++j)
143 System.err.print(ug.isEdge(i,j)?"W ":"- ");
144 System.err.println();
147 GraphIO.writeGML(net,System.out);