In [1]:
import numpy as np
from numpy.linalg import matrix_rank
from random import randint
import random 
import matplotlib.pyplot as plt
import networkx as nx

In [73]:
number_of_iterations=100
number_of_layers=100
matrix_size=50
number_of_unused_nodes=0
#===========================================================
AllRank=np.zeros(shape=(number_of_iterations,1))
All_Unused_nodes=np.zeros(shape=(number_of_iterations,1))
Prob_of_sparsity=np.zeros(shape=(number_of_layers,1))
#===========================================================

#print(M)
Status=[0, 1, 2] #0  is constant, 1 is increasing sparsity, 2 is decreasing sparsity
prob_init=[0, 0.25, 0.5, 0.75, 1]#a vector of probability
for s in range(len(Status)):
    for ini in range(len(prob_init)):
        init=prob_init[ini]
        print('ppp',prob_init[ini])
        #print (Probability_of_zero)
        for k in range(number_of_iterations):
            M=np.eye(matrix_size, dtype=int)
            Probability_of_zero=init
            number_of_unused_nodes=0
            for i in range(number_of_layers):
                #print ('i',i)
                matrix=np.random.choice([0, 1], size=(matrix_size,matrix_size), p=[Probability_of_zero, 1-Probability_of_zero])
                #print ('MaT',matrix)
                matrix=Have_atleast_one_connection(matrix)
                #Count number of unused nodes.
                SUM=sum(matrix.T)
                number_of_unused_nodes+=np.count_nonzero(SUM==0)
                #print ('MaTafterCHECK',matrix)
                M=np.dot(M,matrix)
                #print('DO*',M)
                prob_prev=Probability_of_zero
                if Status[s]==1:
                    #Probability_of_zero=random.uniform(prob_prev,1)
                    Probability_of_zero+=((1-init)/number_of_layers)
                elif Status[s]==2:
                    #Probability_of_zero=random.uniform(0,prob_prev)
                    Probability_of_zero-=((init-0)/number_of_layers)
                #print ('Rank:', matrix_rank(M))
                #print (M)
                #print(Probability_of_zero)
            #print(matrix_rank(M))
                if(k==50):
                    Prob_of_sparsity[i,0]=Probability_of_zero
            AllRank[k,0]=matrix_rank(M)
            All_Unused_nodes[k,0]=number_of_unused_nodes
        '''print('Status', Status[s],'Prob', prob_init[ini])
        plt.plot(np.arange(number_of_iterations), AllRank, 'ro')
        plt.axis([1, number_of_iterations, 1,matrix_size])
        plt.xlabel('Iterations')
        plt.ylabel('Rank')
        plt.savefig('RANK_ITER Probability= '+str(prob_init[ini])+' Status='+str(Status[s])+'.png')
        #plt.show()
        plt.clf()             
        plt.plot(np.arange(number_of_layers), Prob_of_sparsity, 'ro')
        plt.axis([1, number_of_layers, 0, 1])
        plt.xlabel('Iterations')
        plt.ylabel('Propability')
        plt.savefig('Probability= '+str(prob_init[ini])+' Status='+str(Status[s])+'.png')
        plt.clf()
        #plt.show()
'''     
        print('Status', Status[s],'Prob', prob_init[ini])
        plt.plot(AllRank,All_Unused_nodes, 'ro')
        plt.xlabel('Rank')
        plt.ylabel('Unused_nodes')
        plt.savefig('Probability= '+str(prob_init[ini])+' Status='+str(Status[s])+'.png')
        plt.clf()


ppp 0
Status 0 Prob 0
ppp 0.25
Status 0 Prob 0.25
ppp 0.5
Status 0 Prob 0.5
ppp 0.75
Status 0 Prob 0.75
ppp 1
Status 0 Prob 1
ppp 0
Status 1 Prob 0
ppp 0.25
Status 1 Prob 0.25
ppp 0.5
Status 1 Prob 0.5
ppp 0.75
Status 1 Prob 0.75
ppp 1
Status 1 Prob 1
ppp 0
Status 2 Prob 0
ppp 0.25
Status 2 Prob 0.25
ppp 0.5
Status 2 Prob 0.5
ppp 0.75
Status 2 Prob 0.75
ppp 1
Status 2 Prob 1

In [29]:
def Have_atleast_one_connection(matrix):
    SUM=sum(matrix)
    index=np.where( SUM==0 )
    num = (index[0]).shape[0]
    make_a_connection_index=np.random.randint(matrix.shape[1], size=num)
    matrix[make_a_connection_index,index[0]]=1
    return matrix

In [205]:
Status=[5,4,6]
Status[0]


Out[205]:
5

In [36]:
Probability_of_zero=0.5
matrix=np.random.choice([0, 1], size=(9,9), p=[Probability_of_zero, 1-Probability_of_zero])
A=Have_atleast_one_connection(matrix)
G = nx.from_numpy_matrix(np.array(A)) 
A


Out[36]:
array([[0, 0, 1, 0, 0, 1, 0, 1, 0],
       [1, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 1, 0, 1, 0, 1, 0, 1, 1],
       [1, 0, 1, 0, 1, 1, 1, 0, 1],
       [1, 0, 1, 0, 0, 1, 1, 0, 1],
       [0, 1, 0, 1, 1, 1, 1, 0, 1],
       [1, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 0, 0, 1, 1, 1],
       [0, 0, 1, 1, 0, 0, 1, 0, 0]])

In [65]:
A=np.array([[0,0,0],[1,1,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]])
SUM=sum(A.T)
np.count_nonzero(SUM==0)


Out[65]:
5

In [26]:
import networkx as nx

def hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, 
                  pos = None, parent = None):
    '''If there is a cycle that is reachable from root, then this will see infinite recursion.
       G: the graph
       root: the root node of current branch
       width: horizontal space allocated for this branch - avoids overlap with other branches
       vert_gap: gap between levels of hierarchy
       vert_loc: vertical location of root
       xcenter: horizontal location of root
       pos: a dict saying where all nodes go if they have been assigned
       parent: parent of this branch.'''
    if pos == None:
        pos = {root:(xcenter,vert_loc)}
    else:
        pos[root] = (xcenter, vert_loc)
    neighbors = G.neighbors(root)
    if parent != None:   #this should be removed for directed graphs.
        neighbors.remove(parent)  #if directed, then parent not in neighbors.
    if len(neighbors)!=0:
        dx = width/len(neighbors) 
        nextx = xcenter - width/2 - dx/2
        for neighbor in neighbors:
            nextx += dx
            pos = hierarchy_pos(G,neighbor, width = dx, vert_gap = vert_gap, 
                                vert_loc = vert_loc-vert_gap, xcenter=nextx, pos=pos, 
                                parent = root)
    return pos

In [ ]: