Beispiel #1
0
    def obtain_graph(args, suffix=""):
        """Build a Bipartite graph according to command line arguments

        Arguments:
        - `args`: command line options
        """

        if getattr(args, "bp" + suffix) is not None:

            l, r, p = getattr(args, "bp" + suffix)
            G = bipartite_random_graph(l, r, p)

        elif getattr(args, "bm" + suffix) is not None:

            l, r, m = getattr(args, "bm" + suffix)
            G = bipartite_gnmk_random_graph(l, r, m)

        elif getattr(args, "bd" + suffix) is not None:

            l, r, d = getattr(args, "bd" + suffix)
            G = bipartite_random_left_regular(l, r, d)

        elif getattr(args, "bregular" + suffix) is not None:

            l, r, d = getattr(args, "bregular" + suffix)
            G = bipartite_random_regular(l, r, d)

        elif getattr(args, "bshift" + suffix) is not None:

            N, M, pattern = getattr(args, "bshift" + suffix)
            G = bipartite_shift(N, M, pattern)

        elif getattr(args, "bcomplete" + suffix) is not None:

            l, r = getattr(args, "bcomplete" + suffix)
            G = complete_bipartite_graph(l, r)
            # Workaround: the bipartite labels are missing in old version of networkx
            for i in range(0, l):
                G.add_node(i, bipartite=0)
            for i in range(l, l + r):
                G.add_node(i, bipartite=1)

        elif getattr(args, "graphformat" + suffix) is not None:

            try:
                print("INFO: reading bipartite graph {} from '{}'".format(
                    suffix,
                    getattr(args, "input" + suffix).name),
                      file=sys.stderr)
                G = readGraph(getattr(args, "input" + suffix), "bipartite",
                              getattr(args, "graphformat" + suffix))
            except ValueError, e:
                print("ERROR ON '{}'. {}".format(
                    getattr(args, "input" + suffix).name, e),
                      file=sys.stderr)
                exit(-1)
Beispiel #2
0
    def obtain_graph(args,suffix=""):
        """Build a Bipartite graph according to command line arguments

        Arguments:
        - `args`: command line options
        """

        if getattr(args,"bp"+suffix) is not None:

            l,r,p = getattr(args,"bp"+suffix)
            G=bipartite_random_graph(l,r,p)

        elif getattr(args,"bm"+suffix)  is not None:

            l,r,m = getattr(args,"bm"+suffix)
            G=bipartite_gnmk_random_graph(l,r,m)

        elif getattr(args,"bd"+suffix) is not None:

            l,r,d = getattr(args,"bd"+suffix)
            G=bipartite_random_left_regular(l,r,d)

        elif getattr(args,"bregular"+suffix)  is not None:

            l,r,d = getattr(args,"bregular"+suffix)
            G=bipartite_random_regular(l,r,d)

        elif getattr(args,"bshift"+suffix) is not None:

            N,M,pattern = getattr(args,"bshift"+suffix)
            G=bipartite_shift(N,M,pattern)
            
        elif getattr(args,"bcomplete"+suffix) is not None:
            
            l,r = getattr(args,"bcomplete"+suffix)
            G=complete_bipartite_graph(l,r)
            # Workaround: the bipartite labels are missing in old version of networkx
            for i in range(0,l):
                G.add_node(i,bipartite=0)
            for i in range(l,l+r):
                G.add_node(i,bipartite=1)
            
        elif getattr(args,"graphformat"+suffix) is not None:

            try:
                print("INFO: reading bipartite graph {} from '{}'".format(suffix,getattr(args,"input"+suffix).name),
                      file=sys.stderr)
                G=readGraph(getattr(args,"input"+suffix),
                            "bipartite",
                            getattr(args,"graphformat"+suffix))
            except ValueError,e:
                print("ERROR ON '{}'. {}".format(getattr(args,"input"+suffix).name,e),file=sys.stderr)
                exit(-1)
Beispiel #3
0
    def obtain_graph(args):
        """Build a Bipartite graph according to command line arguments

        Arguments:
        - `args`: command line options
        """

        if args.bp is not None:

            l,r,p = args.bp
            G=bipartite_random_graph(l,r,p)

        elif args.bm is not None:

            l,r,m = args.bm
            G=bipartite_gnmk_random_graph(l,r,m)

        elif args.bd is not None:

            l,r,d = args.bd
            G=bipartite_random_left_regular(l,r,d)

        elif args.bregular is not None:

            l,r,d = args.bregular
            G=bipartite_random_regular(l,r,d)

        elif args.bcomplete is not None:
            
            l,r = args.bcomplete
            G=complete_bipartite_graph(l,r)

        elif args.graphformat is not None:

            try:
                G=readGraph(args.input, "bipartite", args.graphformat)
            except ValueError,e:
                print("ERROR ON '{}'. {}".format(args.input.name,e),file=sys.stderr)
                exit(-1)
def synthetic_three_level(n1,n2,n3,p1,p2,p3,q1,q2,q3,no_isolates=True):

    #,isolate_up=True,isolate_down=True):
    #J_isolates=True,F_isolates=True,D_isolates=True

    k=n1

    J=nx.erdos_renyi_graph(n1,p1) #The first layer graph
    Jis = nx.isolates(J)
    F=nx.erdos_renyi_graph(n2,p2) #The second layer graph
    Fis = nx.isolates(F)
    D=nx.erdos_renyi_graph(n3,p3) #The third layer graph
    Dis = nx.isolates(D)
     
    H1=nx.bipartite_gnmk_random_graph(n1,n2,int(n1*n2*q1)) #The bipartite graph that bridges layers 1 and 2
    # H1=nx.bipartite_random_graph(n1,n2,q1)
    H2=nx.bipartite_gnmk_random_graph(n2,n3,int(n2*n3*q2)) #The bipartite graph that bridges layers 2 and 3
    # H2=nx.bipartite_random_graph(n2,n3,q2)
    H3=nx.bipartite_gnmk_random_graph(n3,n1,int(n1*n3*q3)) #The bipartite graph that bridges layers 1 and 3
    # H3=nx.bipartite_random_graph(n3,n1,q3) 
    
    G=nx.Graph()  #The synthetic two-layer graph
    
    # Relabing nodes maps
    
    mappingF={}
    for i in range(n3+n2):
        mappingF[i]=n1+i
    HH2=nx.relabel_nodes(H2,mappingF,copy=True)
    FF=nx.relabel_nodes(F,mappingF,copy=True)
    
    mappingD={}
    for i in range(n1+n3):
        if i >n3-1:
            mappingD[i]=i-n3
        else:
            mappingD[i]=n1+n2+i
    HH3=nx.relabel_nodes(H3,mappingD,copy=True)
    DD=nx.relabel_nodes(D,mappingD,copy=True)

    # Removing isolates
    
    if  no_isolates:
        J.remove_nodes_from(Jis) 
        H1.remove_nodes_from(Jis)
        HH3.remove_nodes_from(Jis)
        Fis = [mappingF[i] for i in Fis]
        FF.remove_nodes_from(Fis) 
        H1.remove_nodes_from(Fis)
        HH2.remove_nodes_from(Fis)
        Dis = [mappingD[i] for i in Dis]
        DD.remove_nodes_from(Dis) 
        HH2.remove_nodes_from(Dis)
        HH3.remove_nodes_from(Dis)

    # if  J_isolates:
    #     J.remove_nodes_from(Jis) 
    #     H1.remove_nodes_from(Jis)
    #     HH3.remove_nodes_from(Jis)
    # if  F_isolates:
    #     Fis = [mappingF[i] for i in Fis]
    #     FF.remove_nodes_from(Fis) 
    #     H1.remove_nodes_from(Fis)
    #     HH2.remove_nodes_from(Fis)
    # if  D_isolates:
    #     Dis = [mappingD[i] for i in Dis]
    #     DD.remove_nodes_from(Dis) 
    #     HH2.remove_nodes_from(Dis)
    #     HH3.remove_nodes_from(Dis)

    G.add_edges_from(J.edges())
    G.add_edges_from(H1.edges())
    G.add_edges_from(DD.edges())
    G.add_edges_from(HH2.edges())
    G.add_edges_from(HH3.edges())
    G.add_edges_from(FF.edges())

    edgeList = []
    for e in H1.edges():
        edgeList.append(e)
    for e in HH2.edges():
        edgeList.append(e)
    for e in HH3.edges():
        edgeList.append(e)

    return G, J, FF, DD, edgeList
def synthetic_three_level(n1,
                          n2,
                          n3,
                          p1,
                          p2,
                          p3,
                          q1,
                          q2,
                          q3,
                          no_isolates=True):

    k = n1

    J = nx.erdos_renyi_graph(n1, p1)  #The first layer graph
    Jis = nx.isolates(J)
    F = nx.erdos_renyi_graph(n2, p2)  #The second layer graph
    Fis = nx.isolates(F)
    D = nx.erdos_renyi_graph(n3, p3)  #The third layer graph
    Dis = nx.isolates(D)

    H1 = nx.bipartite_gnmk_random_graph(n1, n2, int(
        n1 * n2 * q1))  #The bipartite graph that bridges layers 1 and 2
    H2 = nx.bipartite_gnmk_random_graph(n2, n3, int(
        n2 * n3 * q2))  #The bipartite graph that bridges layers 2 and 3
    H3 = nx.bipartite_gnmk_random_graph(n3, n1, int(
        n1 * n3 * q3))  #The bipartite graph that bridges layers 1 and 3

    G = nx.Graph()  #The synthetic two-layer graph

    # Relabing nodes maps

    mappingF = {}
    for i in range(n3 + n2):
        mappingF[i] = n1 + i
    HH2 = nx.relabel_nodes(H2, mappingF, copy=True)
    FF = nx.relabel_nodes(F, mappingF, copy=True)

    mappingD = {}
    for i in range(n1 + n3):
        if i > n3 - 1:
            mappingD[i] = i - n3
        else:
            mappingD[i] = n1 + n2 + i
    HH3 = nx.relabel_nodes(H3, mappingD, copy=True)
    DD = nx.relabel_nodes(D, mappingD, copy=True)

    # Removing isolates

    if no_isolates:
        J.remove_nodes_from(Jis)
        H1.remove_nodes_from(Jis)
        HH3.remove_nodes_from(Jis)
        Fis = [mappingF[i] for i in Fis]
        FF.remove_nodes_from(Fis)
        H1.remove_nodes_from(Fis)
        HH2.remove_nodes_from(Fis)
        Dis = [mappingD[i] for i in Dis]
        DD.remove_nodes_from(Dis)
        HH2.remove_nodes_from(Dis)
        HH3.remove_nodes_from(Dis)

    G.add_edges_from(J.edges())
    G.add_edges_from(H1.edges())
    G.add_edges_from(DD.edges())
    G.add_edges_from(HH2.edges())
    G.add_edges_from(HH3.edges())
    G.add_edges_from(FF.edges())

    edgeList = []
    for e in H1.edges():
        edgeList.append(e)
    for e in HH2.edges():
        edgeList.append(e)
    for e in HH3.edges():
        edgeList.append(e)

    return G, J, FF, DD, edgeList
Beispiel #6
0
    def obtain_graph(args, suffix=""):
        """Build a Bipartite graph according to command line arguments

        Arguments:
        - `args`: command line options
        """

        if getattr(args, "bp"+suffix) is not None:

            l, r, p = getattr(args, "bp"+suffix)
            G = bipartite_random_graph(l, r, p)

        elif getattr(args, "bm"+suffix) is not None:

            l, r, m = getattr(args, "bm"+suffix)
            G = bipartite_gnmk_random_graph(l, r, m)

        elif getattr(args, "bd"+suffix) is not None:

            l, r, d = getattr(args, "bd"+suffix)
            G = bipartite_random_left_regular(l, r, d)

        elif getattr(args, "bregular"+suffix) is not None:

            l, r, d = getattr(args, "bregular"+suffix)
            G = bipartite_random_regular(l, r, d)

        elif getattr(args, "bshift"+suffix) is not None:

            N, M, pattern = getattr(args, "bshift"+suffix)
            G = bipartite_shift(N, M, pattern)

        elif getattr(args, "bcomplete"+suffix) is not None:

            l, r = getattr(args, "bcomplete"+suffix)
            G = complete_bipartite_graph(l, r)
            # Workaround: the bipartite labels are missing in old version of networkx
            for i in range(0, l):
                G.add_node(i, bipartite=0)
            for i in range(l, l+r):
                G.add_node(i, bipartite=1)

        elif getattr(args, "graphformat"+suffix) is not None:

            try:
                print("INFO: reading bipartite graph {} from '{}'".format(suffix, getattr(args, "input"+suffix).name),
                      file=sys.stderr)
                G = readGraph(getattr(args, "input"+suffix),
                              "bipartite",
                              getattr(args, "graphformat"+suffix))
            except ValueError as e:
                print("ERROR ON '{}'. {}".format(
                    getattr(args, "input"+suffix).name, e), file=sys.stderr)
                exit(-1)

        else:
            raise RuntimeError(
                "Command line does not specify a bipartite graph")

        # Graph modifications
        if getattr(args, "plantbiclique"+suffix) is not None:
            l, r = getattr(args, "plantbiclique"+suffix)
            left, right = bipartite_sets(G)
            if l > len(left) or r > len(right):
                raise ValueError("Clique cannot be larger than graph")

            left = random.sample(left, l)
            right = random.sample(right, r)

            for v, w in product(left, right):
                G.add_edge(v, w)

        if getattr(args, "addedges"+suffix) is not None:

            k = getattr(args, "addedges"+suffix)

            G.add_edges_from(sample_missing_edges(G, k))

            if hasattr(G, 'name'):
                G.name = "{} with {} new random edges".format(G.name, k)

        # Output the graph is requested
        if getattr(args, "savegraph"+suffix) is not None:
            writeGraph(G,
                       getattr(args, "savegraph"+suffix),
                       'bipartite',
                       getattr(args, "graphformat"+suffix))

        return G
Beispiel #7
0
 def setup(self):
     self.G = nx.bipartite_gnmk_random_graph(40, 60, 50)
     self.M = nx.bipartite.biadjacency_matrix(self.G, range(40))
Beispiel #8
0
 def setup(self):
     self.G = nx.bipartite_gnmk_random_graph(40, 60, 50)
     self.M = nx.bipartite.biadjacency_matrix(self.G, range(40))
def utest3():
    G = NX.bipartite_gnmk_random_graph(20, 10, 50)
    AM = NX.to_numpy_matrix(G).tolist()
    write_jsongraph("gd1.json", adjmatrix_tojson(AM))