+ indep. WoS citations

Python and Networks -- Class notes -- 2016-02-23

1.  Measuring the speed of generating pairs of random numbers

1.1  Code (randpairtest.py)

import random
from sys import argv
import time

# read one parameter: requested item numbers
script, nList = argv

# for each requested N: generate N random pairs
for n in map(int,nList.split()):

    # save current time in seconds
    start = time.clock()

    # generate n pairs of random numbers
    for i in range(n):

        # the two random numbers
        r1 = -1; r2 = -1;

        # try until they are different
        while r1 == r2:
            r1 = random.randint(0,n-1)
            r2 = random.randint(0,n-1)

    # print result 
    print "N=%d random pairs in %d sec" % (n, time.clock()-start)                      

1.2  Usage and output

fij@hal:~/tmp$ python randpairtest.py "100000 1000000"
N=100000 random pairs in 0 sec
N=1000000 random pairs in 4 sec
fij@hal:~/tmp$

2.  Measuring the speed of searching in a set

2.1  Code (speed-of-set-search.py)

from sys import argv
from datetime import datetime

# read one parameter: requested item numbers
script, nList = argv

# for precision, perform search this number of times
repeatNum = 100000

# Output header
print "# Size of set"
print "#\tTime (in microseconds) to search this set"
print ""

# for each requested N: search in a set of size N
for n in map(int,nList.split()):

    # generate the set
    my_set = set(range(n))

    # save current time in microseconds
    start = datetime.now().microsecond

    # search in the set "repeatNum" times
    for x in range(repeatNum):
        if "-1" in my_set:
            pass

    # time (in seconds) used for one search
    dt = (datetime.now().microsecond-start) / ( 1.0 * repeatNum )

    # print result: time used
    print "%d\t%g" % (n,dt)

2.2  Usage and output

fij@hal:~/tmp$ python speed-of-set.py "100 1000 10000 100000 1000000"
# Size of set
#       Time (in microseconds) to search this set

100     0.12426
1000    0.11541
10000   0.11328
100000  0.11877
1000000 0.12013
fij@hal:~/tmp$ 

3.  Generating an Erdos-Renyi network

3.1  Python code (er2.py)

import random
from sys import argv

# read parameters: number of nodes (n) and links (e, edges)
script, n, e = argv

# === function definitions ===

# Generate Erdos-Renyi graph with N nodes and E links
def er2(n, e, node2neiSet):
    # assuming that n ( n - 1 ) / 2 >> e 

    # current number of links
    eNow = 0

    # add links one-by-one until we reach the requested number of links
    while (eNow < e):

        # the two randomly selected nodes
        r1 = -1; r2 = -1;

        # repeat until the two nodes are the same or they are neighbors
        while r1 == r2 or node2neiSet.has_key(r1) and r2 in node2neiSet[r1]:

            # the two nodes: two random integers
            r1 = random.randint(0,n-1)
            r2 = random.randint(0,n-1)

        # if either node has no neighbors yet, then declare its neighbor set
        for rNow in (r1,r2):
            if not node2neiSet.has_key(rNow):
                node2neiSet[rNow] = set()

        # save the 2nd node as a neighbor of the 1st node
        node2neiSet[r1].add(r2)

        # save the 1st node as a neighbor of the 2nd node
        node2neiSet[r2].add(r1)

        # change the number of links
        eNow += 1

# === main ===

# node2neiSet[i] is the set of neighbors of node i
node2neiSet = {}

# generate an ER network: return each node's neighbor set
er2(int(n), int(e), node2neiSet)