X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ff6cb26262ba25fefdf1265628265a75d790ebd6..200986a368bbbbb5df459d43cbc7f5ef3d7678db:/contrib/psg/src/peersim/graph/GraphFactory.java diff --git a/contrib/psg/src/peersim/graph/GraphFactory.java b/contrib/psg/src/peersim/graph/GraphFactory.java new file mode 100644 index 0000000000..81857cc0e0 --- /dev/null +++ b/contrib/psg/src/peersim/graph/GraphFactory.java @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2003-2005 The BISON Project + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +package peersim.graph; + +import java.util.*; + +/** +* Contains static methods for wiring certain kinds of graphs. The general +* contract of all methods is that they accept any graph and add edges +* as specified in the documentation. +*/ +public class GraphFactory { + +/** Disable instance construction */ +private GraphFactory() {} + +// ===================== public static methods ====================== +// ================================================================== + +/** +* Wires a ring lattice. +* The added connections are defined as follows. If k is even, links to +* i-k/2, i-k/2+1, ..., i+k/2 are added (but not to i), thus adding an +* equal number of predecessors and successors. +* If k is odd, then we add one more successors than predecessors. +* For example, for k=4: 2 predecessors, 2 successors. +* For k=5: 2 predecessors, 3 successors. +* For k=1: each node is linked only to its successor. +* All values are understood mod n to make the lattice circular, where n is the +* number of nodes in g. +* @param g the graph to be wired +* @param k lattice parameter +* @return returns g for convenience +*/ +public static Graph wireRingLattice(Graph g, int k) { + + final int n = g.size(); + + int pred = k/2; + int succ = k-pred; + + for(int i=0; i +* Note that it is possible to pass an undirected graph as a parameter. In that +* case the output is the directed graph produced by the method, converted to +* an undirected graph by dropping directionality of the edges. This graph is +* still not from the original undirected WS model though. +* @param g the graph to be wired +* @param k lattice parameter: this is the out-degree of a node in the +* ring lattice before rewiring +* @param p the probability of rewiring each +* @param r source of randomness +* @return returns g for convenience +*/ +public static Graph wireWS( Graph g, int k, double p, Random r ) { +//XXX unintuitive to call it WS due to the slight mods + final int n = g.size(); + for(int i=0; i= i ) newedge++; // random _other_ node + } + g.setEdge(i,newedge); + } + return g; +} + +// ------------------------------------------------------------------- + +/** +* Random graph. Generates randomly k directed edges out of each node. +* The neighbors +* (edge targets) are chosen randomly without replacement from the nodes of the +* graph other than the source node (i.e. no loop edge is added). +* If k is larger than N-1 (where N is the number of nodes) then k is set to +* be N-1 and a complete graph is returned. +* @param g the graph to be wired +* @param k samples to be drawn for each node +* @param r source of randomness +* @return returns g for convenience +*/ +public static Graph wireKOut( Graph g, int k, Random r ) { + + final int n = g.size(); + if( n < 2 ) return g; + if( n <= k ) k=n-1; + int[] nodes = new int[n]; + for(int i=0; i0) + { + int j = i^mask; + if(j> 1; + } + + } + return g; +} + +// ------------------------------------------------------------------- + +/** +* This contains the implementation of the Barabasi-Albert model +* of growing scale free networks. The original model is described in +* +http://arxiv.org/abs/cond-mat/0106096. +* It also works if the graph is directed, in which case the model is a +* variation of the BA model +* described in +http://arxiv.org/pdf/cond-mat/0408391. In both cases, the number of the +* initial set of nodes is the same as the degree parameter, and no links are +* added. The first added node is connected to all of the initial nodes, +* and after that the BA model is used normally. +* @param k the number of edges that are generated for each new node, also +* the number of initial nodes (that have no edges). +* @param r the randomness to be used +* @return returns g for convenience +*/ +public static Graph wireScaleFreeBA( Graph g, int k, Random r ) { + + final int nodes = g.size(); + if( nodes <= k ) return g; + + // edge i has ends (ends[2*i],ends[2*i+1]) + int[] ends = new int[2*k*(nodes-k)]; + + // Add initial edges from k to 0,1,...,k-1 + for(int i=0; i < k; i++) + { + g.setEdge(k,i); + ends[2*i]=k; + ends[2*i+1]=i; + } + + int len = 2*k; // edges drawn so far is len/2 + for(int i=k+1; i < nodes; i++) // over the remaining nodes + { + for (int j=0; j < k; j++) // over the new edges + { + int target; + do + { + target = ends[r.nextInt(len)]; + int m=0; + while( m