예제 #1
0
    def draw(self):
        self.read_nodes()
        self.Network = self.var_network.get()
        self.nPoints = sum(int(i) for i in self.lst_Nodes)

        if self.Network == "GenStar":
            print "GenStar is the network with points ", self.nPoints
            self.graph = snap.GenStar(snap.PNGraph, self.nPoints, True)

        if self.Network == "GenRndGnm":
            print "GenRndGnm is the network with points ", self.nPoints
            self.graph = snap.GenRndGnm(snap.PNGraph, self.nPoints,
                                        self.nPoints)

        if self.Network == "GenForestFire":
            print "GenForestFire is the network with points ", self.nPoints
            self.graph = snap.GenForestFire(self.nPoints, 0.5, 0.5)

        if self.Network == "GenFull":
            print "GenFull is the network with points ", self.nPoints
            self.graph = snap.GenFull(snap.PNGraph, self.nPoints)

        if self.Network == "GenCircle":
            print "GenCircle is the network with points ", self.nPoints
            self.graph = snap.GenCircle(snap.PNGraph, self.nPoints, 10, 10)

        self.create_nodes(self.graph)
예제 #2
0
def gen_ff(args):
    """Generate FF Graph"""

    for i in range(args.num_graphs):

        fp = np.random.uniform(0, 0.5)
        bp = np.random.uniform(0, 0.5)

        Graph = snap.GenForestFire(args.num_vertices, fp, bp)
        snap.SaveEdgeList(Graph, f'{args.data_loc}/FF/FF_{i}.edges')

        print(f"FF Graph {i} Generated and Saved")
예제 #3
0
def generate_graphs():
    for path in GRAPHS:
        name = path.split('/')[-1].split('.')[0]

        metrics = Metrics(path, True).calculate_basic()

        print metrics

        # Generate Erdos-Renyi (Random) Graph
        # args: type, num_nodes, num_edges
        er = snap.GenRndGnm(snap.PNGraph, metrics.num_nodes, metrics.num_edges)
        snap.SaveEdgeList(er, "{}_er.elist".format(name))

        # Generate Watts-Strogatz (Small World) Graph
        # args: num_nodes, node_out_degree (average out degree will be twice this value, rewire_prob)
        ws = snap.GenSmallWorld(metrics.num_nodes,
                                int(metrics.avg_degree) / 2, 0.2)
        snap.SaveEdgeList(ws, "{}_ws.elist".format(name))

        # Generate Barabasi-Albert model (scale-free with preferential attachment) Graph
        # args: (num_nodes, degree of each node desired)
        ba = snap.GenPrefAttach(metrics.num_nodes, int(metrics.avg_degree) / 2)
        snap.SaveEdgeList(ba, "{}_ba.elist".format(name))

        # Generate Forest Fire model Graph
        # args: (num_nodes, forward_prob, backward_prob)
        if name == "USairport_2010":
            ff = snap.GenForestFire(
                metrics.num_nodes, 0.3599,
                0.3599)  # Selected value for US Airports data-set
            snap.SaveEdgeList(ff, "{}_ff.elist".format(name))

            ff = snap.GenForestFire(int(metrics.num_nodes / 10), 0.3599,
                                    0.3599)
            snap.SaveEdgeList(ff, "{}_ffdiv10.elist".format(name))

            ff = snap.GenForestFire(metrics.num_nodes * 10, 0.3599, 0.3599)
            snap.SaveEdgeList(ff, "{}_ffx10.elist".format(name))
        else:
            ff = snap.GenForestFire(metrics.num_nodes, 0.3467,
                                    0.3467)  # selected
            snap.SaveEdgeList(ff, "{}_ff.elist".format(name))

            ff = snap.GenForestFire(int(metrics.num_nodes / 10), 0.3467,
                                    0.3467)
            snap.SaveEdgeList(ff, "{}_ffdiv10.elist".format(name))

            ff = snap.GenForestFire(metrics.num_nodes * 10, 0.3467, 0.3467)
            snap.SaveEdgeList(ff, "{}_ffx10.elist".format(name))
 def SimpleNetworkGenerator(self, params):
     #print params
     Rnd = snap.TRnd(1, 0)
     G = None
     try:
         if params[0] == GlobalParameters.NetworkType[0]:
             G = snap.GenSmallWorld(int(params[1]), int(params[2]),
                                    float(params[3]), Rnd)
         if params[0] == GlobalParameters.NetworkType[1]:
             G = snap.GenPrefAttach(int(params[1]), int(params[2]), Rnd)
         if params[0] == GlobalParameters.NetworkType[2]:
             G = snap.GenForestFire(int(params[1]), float(params[2]),
                                    float(params[3]))
         if params[0] == GlobalParameters.NetworkType[3]:
             G = snap.GenRndGnm(snap.PUNGraph, int(params[1]),
                                int(params[2]), False, Rnd)
         if params[0] == GlobalParameters.NetworkType[4]:
             G = snap.GenCircle(snap.PUNGraph, int(params[1]),
                                int(params[2]), False)
         if params[0] == GlobalParameters.NetworkType[5]:
             G = snap.GenFull(snap.PUNGraph, int(params[1]))
         return G
     except:
         return None
예제 #5
0
##        #print EI.GetId(),EI.GetOutDeg()
##
##        for j in Graph.Edges():
##
##                if j.GetSrcNId() == EI.GetId():
##                        print i,"edge: (%d, %d)" % (j.GetSrcNId(), j.GetDstNId())
##                        i = i + 1
##
##
##print  "*" * 30
####for EI in Graph.Edges():
####        print i
####        print "edge: (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())
####        i = i + 1
##
##UGraph = snap.GenRndGnm(snap.PUNGraph, 3,3)
##i =1
##for EI in UGraph.Edges():
##    print i,"edge: (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())
##    i = i + 1
##
##print  "*" * 30
##Network = snap.GenRndGnm(snap.PNEANet, 3,3)
##i=1
##for EI in Network.Edges():
##    print i,"edge: (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())
##    i = i + 1
Graph = snap.GenForestFire(10, 0.5, 0.5)
for EI in Graph.Edges():
    print "edge: (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())
예제 #6
0
# save and load from a text file
snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1)
print "G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges())

# create a directed random graph on 10k nodes and 5k edges
G6 = snap.GenRndGnm(snap.PNGraph, 10000, 5000)
print "G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())
# convert to undirected graph
G7 = snap.ConvertGraph(snap.PUNGraph, G6)
print "G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges())
# get largest weakly connected component
WccG = snap.GetMxWcc(G6)

# generate a network using Forest Fire model
G8 = snap.GenForestFire(1000, 0.35, 0.35)
print "G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges())

# get a subgraph induced on nodes {0,1,2,3,4}
SubG = snap.GetSubGraph(G8, snap.TIntV.GetV(0, 1, 2, 3, 4))

# get 3-core of G8
Core3 = snap.GetKCore(G8, 3)
print "Core3: Nodes %d, Edges %d" % (Core3.GetNodes(), Core3.GetEdges())

# delete nodes of out degree 3 and in degree 2
snap.DelDegKNodes(G8, 3, 2)

# create a directed random graph on 10k nodes and 1k edges
G9 = snap.GenRndGnm(snap.PNGraph, 10000, 1000)
print "G9: Nodes %d, Edges %d" % (G9.GetNodes(), G9.GetEdges())
    def drawNode(self):

        global GNodesItemNo

        if self.Network == "GenStar":
            print "GenStar is the network with points ",self.nPoints
            Graph = snap.GenStar(snap.PNGraph, self.nPoints, True)

        if self.Network == "GenRndGnm":
            print "GenRndGnm is the network with points ",self.nPoints
            Graph = snap.GenRndGnm(snap.PNGraph,self.nPoints, self.nPoints)

        if self.Network == "GenForestFire":
            print "GenForestFire is the network with points ",self.nPoints
            Graph = snap.GenForestFire(self.nPoints, 0.5,0.5)

        if self.Network == "GenFull":
            print "GenFull is the network with points ",self.nPoints
            Graph = snap.GenFull(snap.PNGraph,self.nPoints)

        if self.Network == "GenCircle":
            print "GenCircle is the network with points ",self.nPoints
            Graph = snap.GenCircle(snap.PNGraph,self.nPoints,10,10)

        shape = 0
        self.dnodes.reverse()
        nodeCounter = self.dnodes.pop()

        for i in Graph.Nodes():
            self.node = Node()
            self.node.id = i.GetId()

            for EI in Graph.Edges():
                if EI.GetSrcNId() == i.GetId():
                    if EI.GetSrcNId() <> EI.GetDstNId() :
                        self.node.follower.append(EI.GetDstNId())

            if shape ==0:
                self.node.shape = "oval"
            if shape ==1:
                self.node.shape = "triangle"
            if shape ==2:
                self.node.shape = "rectangle"
            if shape ==3:
                self.node.shape = "circle"

            nodeCounter = nodeCounter - 1
            if nodeCounter <=0:
                if len(self.dnodes)<> 0:
                    nodeCounter = self.dnodes.pop()
                    print "Completed Community: ",shape
                shape = shape + 1

            self.CoordinateSelection()
            self.Radius = 5
##            if len(self.node.follower) <> 0:
##                self.Radius = len(self.node.follower)

            if self.node.shape == "oval":
                _Oval = Gcanvas.create_oval(self.node.x,self.node.y,self.node.x+self.Radius,
                                            self.node.y+self.Radius,outline="#ff00ff",fill="white", width=2)

            if self.node.shape == "triangle":
                traingle = [self.node.x,self.node.y,self.node.x+self.Radius,self.node.y+self.Radius]
                _Oval = Gcanvas.create_oval(traingle,outline="#252568",fill="white", width=2)

            if self.node.shape == "rectangle":
                _Oval = Gcanvas.create_oval(self.node.x,self.node.y,self.node.x+self.Radius,
                                            self.node.y+self.Radius,outline="#474bcc",fill="white", width=2)

            if self.node.shape == "circle":
                _Oval = Gcanvas.create_oval(self.node.x,self.node.y,self.node.x+self.Radius,
                                            self.node.y+self.Radius,outline="#364949",fill="white", width=2)

            self.OvalNo[_Oval]=self.node.id#[self.node.x,self.node.y]
            Gcanvas.tag_bind( _Oval, '<ButtonPress-1>', self.__showAttriInfo)
            self.itemNo.append(_Oval)
            self.pAll.append(self.node)


        GNodesItemNo = self.itemNo
예제 #8
0
def intro():

    # create a graph PNGraph
    G1 = snap.TNGraph.New()
    G1.AddNode(1)
    G1.AddNode(5)
    G1.AddNode(32)
    G1.AddEdge(1, 5)
    G1.AddEdge(5, 1)
    G1.AddEdge(5, 32)
    print("G1: Nodes %d, Edges %d" % (G1.GetNodes(), G1.GetEdges()))

    # create a directed random graph on 100 nodes and 1k edges
    G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000)
    print("G2: Nodes %d, Edges %d" % (G2.GetNodes(), G2.GetEdges()))

    # traverse the nodes
    for NI in G2.Nodes():
        print("node id %d with out-degree %d and in-degree %d" %
              (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
    # traverse the edges
    for EI in G2.Edges():
        print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()))

    # traverse the edges by nodes
    for NI in G2.Nodes():
        for Id in NI.GetOutEdges():
            print("edge (%d %d)" % (NI.GetId(), Id))

    # generate a network using Forest Fire model
    G3 = snap.GenForestFire(1000, 0.35, 0.35)
    print("G3: Nodes %d, Edges %d" % (G3.GetNodes(), G3.GetEdges()))

    # save and load binary
    FOut = snap.TFOut("test.graph")
    G3.Save(FOut)
    FOut.Flush()
    FIn = snap.TFIn("test.graph")
    G4 = snap.TNGraph.Load(FIn)
    print("G4: Nodes %d, Edges %d" % (G4.GetNodes(), G4.GetEdges()))

    # save and load from a text file
    snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
    G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1)
    print("G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges()))

    # generate a network using Forest Fire model
    G6 = snap.GenForestFire(1000, 0.35, 0.35)
    print("G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges()))
    # convert to undirected graph
    G7 = snap.ConvertGraph(snap.PUNGraph, G6)
    print("G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges()))
    # get largest weakly connected component of G
    WccG = snap.GetMxWcc(G6)
    # get a subgraph induced on nodes {0,1,2,3,4,5}
    SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4))
    # get 3-core of G
    Core3 = snap.GetKCore(G6, 3)
    # delete nodes of out degree 10 and in degree 5
    snap.DelDegKNodes(G6, 10, 5)
    print("G6a: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges()))

    # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
    G8 = snap.GenPrefAttach(1000, 3)
    print("G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges()))
    # vector of pairs of integers (size, count)
    CntV = snap.TIntPrV()
    # get distribution of connected components (component size, count)
    snap.GetWccSzCnt(G8, CntV)
    # get degree distribution pairs (degree, count)
    snap.GetOutDegCnt(G8, CntV)
    # vector of floats
    EigV = snap.TFltV()
    # get first eigenvector of graph adjacency matrix
    snap.GetEigVec(G8, EigV)
    # get diameter of G8
    snap.GetBfsFullDiam(G8, 100)
    # count the number of triads in G8, get the clustering coefficient of G8
    snap.GetTriads(G8)
    snap.GetClustCf(G8)
예제 #9
0
# traverse the nodes
for NI in G2.Nodes():
    print("node id %d with out-degree %d and in-degree %d" %
          (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
# traverse the edges
for EI in G2.Edges():
    print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()))
# traverse the edges by nodes
for NI in G2.Nodes():
    for Id in NI.GetOutEdges():
        print("edge (%d %d)" % (NI.GetId(), Id))

# %%
# I/O
# generate a network using Forest Fire model
G3 = snap.GenForestFire(1000, 0.35, 0.35)
# save and load binary
FOut = snap.TFOut("test.graph")
G3.Save(FOut)
FOut.Flush()
FIn = snap.TFIn("test.graph")
G4 = snap.TNGraph.Load(FIn)
# save and load from a text file
snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1)

# %%
# Manipulate
# generate a network using Forest Fire model
G6 = snap.GenForestFire(1000, 0.35, 0.35)
# convert to undirected graph