Function iarandGraph_and

Synopse

The iarandGraph_and() function a random graph or digraph.

  • g = iaiarandGraph_and(order, avgDegree, phi, gType, seed)
    • Output
      • g: tuple, (A,W). A: ndarray, 2D, boolean, square, size=order. W: ndarray, 2D, float, square, size=order. Adjacency matrix A with 1 where there is an edge/arc, and 0 where there is not. Weight matrix W with random values in interval [0,1).
    • Input
      • order: integer, default=4 (optional). Determines the graph order, i.e., the number of nodes.
      • avgDegree: float, default=1 (optional). The desired average degree of graph nodes.
      • phi: float, [0,1] (optional). The probability with which a pair of nodes will be connected by an edge/arc. Computed from avgDegree if not specified
      • gType: graph or digraph, default=graph (optional). Specifies whether the output is a graph or a digraph.
      • seed: integer, default=time() (optional). Define the pseudo random sequence.

Description

The iarandGraph_and() function generate a random graph or digraph with specified order and density.

Function Code

 1 import time
 2 
 3 digraph = 0
 4 graph = 1
 5 
 6 def iarandGraph_and(order=4, avgDegree=1, phi=None, gType=graph, seed=time.time()):
 7     from numpy import bool, triu
 8     import numpy.random as rnd
 9 
10     rnd.seed([seed])
11 
12     # create phi from desired average degree, if it is not specified
13     if phi is None:
14         phi = float(avgDegree)/(order - 1.0)
15 
16     # create a square matrix with size order with all elements random at interval [0,1)
17     G = rnd.rand(order,order)
18 
19     seed = round(G.sum())
20     # thresholding values with phi
21     G = (G<phi)
22 
23     # turn diagonal elements into zeros
24     i = range(order)
25     G[i,i] = 0
26     W = rnd.rand(order,order)
27     W[G==0] = 0
28 
29     if gType==graph:
30         # make a graph by mirroring the upper triangular
31         G = triu(G)
32         G = G + G.transpose()
33         W = triu(W)
34         W = W + W.transpose()
35 
36     rnd.seed([seed])
37     return (G.astype(bool), W)

Examples

Create a random graph with order=400 and phi=0.5.

1 import numpy as np
2 from iarandGraph_and import *
3 
4 t = time.time()
5 A,W = iarandGraph_and(400,phi=0.5)
6 print 'Computational time: ', time.time() - t
7 adshow(A, 'A')
8 adshow(np.round(W*255).astype(np.uint8), 'W')
Computational time:  0.0271871089935

A

W

Create a random digraph with order=1000 and phi=0.2.

1 t = time.time()
2 A,W = iarandGraph_and(1000,phi=0.2, gType=digraph)
3 print 'Computational time: ', time.time() - t
4 adshow(A, 'A')
5 adshow(np.round(W*255).astype(np.uint8), 'W')
Computational time:  0.132164001465

A

W

Contributions

  • André Costa, 1o semestre de 2012.