Beispiel #1
0
# 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())

# define a vector of pairs of integers (size, count) and
# get a distribution of connected components (component size, count)
CntV = snap.TIntPrV()
snap.GetWccSzCnt(G9, CntV)
for p in CntV:
    print "size %d: count %d" % (p.GetVal1(), p.GetVal2())

# get degree distribution pairs (out-degree, count):
snap.GetOutDegCnt(G9, CntV)
for p in CntV:
Beispiel #2
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)
Beispiel #3
0

##########################################################################

f_in = snap.TFIn(FOLDED_POSTID_GRAPH_PATH)
post_graph = snap.TUNGraph.Load(f_in)
print "nodes", post_graph.GetNodes()
print "edges", post_graph.GetEdges()

COMMUNITIES_PATH = path.join(BASE_PATH,
                             'postid-communities-with-postbodies.txt')
COMMUNITIES_VEC_PATH = path.join(BASE_PATH, 'postid-communities.vector')

# remove degree-1 nodes
assert snap.CntSelfEdges(post_graph) == 0
snap.DelDegKNodes(post_graph, 1, 1)

comm_vec = snap.TCnComV()
modularity = snap.CommunityCNM(post_graph, comm_vec)

f_out = snap.TFOut(COMMUNITIES_VEC_PATH)
comm_vec.Save(f_out)
f_out.Flush()

f_in = snap.TFIn(COMMUNITIES_VEC_PATH)
comm_vec = snap.TCnComV()
comm_vec.Load(f_in)

print "communities", len(comm_vec)

pickle_file = open(POSTID_PICKLE, 'rb')
Beispiel #4
0
def aggregate(userList, N_CLAIM=5, inDEG=0):

    G = snap.TUNGraph.New()

    # keep the following codes for reading facebook data
    #for userId in userList:

    #    filename = str(userId) + '.egonet'
    #    read_nodeadjlist(egonetFolderName + filename, G, MAX_DEGREE = 5)
    
    claimantList = []
    for i in xrange(N_CLAIM):
        
        temp = snap.GenStar(snap.PUNGraph, random.randrange(3, 8), False)
        countID = G.GetNodes()
        
        for NI in temp.Nodes():
            G.AddNode(countID + NI.GetId())
            if random.random() < 0.5:
                claimantList.append(countID + NI.GetId())

        for E in temp.Edges():
            G.AddEdge(E.GetSrcNId()+countID, E.GetDstNId()+countID)


    # removing the NODES with low degrees
    snap.DelDegKNodes(G, inDEG, inDEG)
    print '\nAfter remove Nodes with In-Degree = %d and out-Degree = %d ' % (inDEG, inDEG)
    print 'Graph has %d Nodes and %d Edges\n' % (G.GetNodes(), G.GetEdges())

    # Multi-layer network recovery
    plot = plotting(G, snap.gvlNeato, True)
    plot.hirachical()
   
    # group split
    pos1 = 0
    pos2 = 0
    group = [0.5, 0.3, 0.2, 0.1] # specify the portion of data to split
    neighbor = []

    N = len(plot.community)
    # normalizatoin
    group = [float(i)/sum(group) for i in group]

    for i in xrange(len(group)):

        pos2 = pos1 + int(group[i] * N)

        temp = []
        for g in plot.community[pos1:pos2]:
            
            for NI in G.Nodes():
                
                if snap.GetShortPath(G, NI.GetId(), g) == 1:

                    temp.append(NI.GetId())
                
        neighbor.append(temp)
        pos1 = pos2


    # Add extra links
    ratio = [0.0, 0.4, 0.8] # the multi-layer effect by adding links layer by layer
    in_w = 0.3 # the density of in-group edges
    between_w = 0.4 # the density of between-group edges
    in_deg = 11
    between_deg = 12

    for r in xrange(len(ratio)):
        
        # multi-layer loop
        pos1 = 0
        pos2 = 0

        for i in xrange(len(group)):
        
            # sub-group loop
            pos2 = pos1 + int(N * group[i])
            
            for node in plot.community[pos1:pos2]:
                
                if random.random() > ratio[r]:
                    continue
                
                # inter-group
                for NI in neighbor[i]:

                    if NI not in plot.community \
                            and NI not in claimantList \
                            and random.random() < in_w:
                        if G.GetNI(node).GetInDeg() <= in_deg:
                                G.AddEdge(node, NI)
                
                #intra-group: adjusting the index periodically but leave off the last element for fraud ring 
                if i == len(group)-2: 
                    k = 0
                elif i == len(group)-1:
                    continue
                else:
                    k = i + 1
                
                for NI in neighbor[k]:

                    if NI not in plot.community \
                            and NI not in claimantList \
                            and random.random() < between_w:
                        if G.GetNI(node).GetInDeg() <= between_deg:
                                G.AddEdge(node, NI)

            plot.run('aggregate_plot_round={}_{}'.format(r, '.'.join(userList)),
                    claimantList,
                    title='USER_ID = {}'.format('.'.join(userList)))
            
            pos1 = pos2
Beispiel #5
0
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
G7 = snap.ConvertGraph(snap.PUNGraph, G6)
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)

# %%
# stats
# generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
G8 = snap.GenPrefAttach(1000, 3)
# 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)
Beispiel #6
0
import snap

mapping = snap.TStrIntSH()
G0 = snap.LoadEdgeListStr(snap.PNEANet, "dataList.txt", 0, 1, mapping)

snap.SaveEdgeList(G0, "testGraph.txt", "Save as tab-separated list of edges")

snap.PrintInfo(G0)
"""

snap.DelDegKNodes(G0,1,0)
snap.DelDegKNodes(G0,1,0)
snap.DelDegKNodes(G0,1,0)
snap.DelDegKNodes(G0,1,0)

snap.PrintInfo(G0)


DegToCntV = snap.TIntPrV()
snap.GetOutDegCnt(G0, DegToCntV)
for item in DegToCntV:
    print "%d nodes with out-degree %d" % (item.GetVal2(), item.GetVal1())
"""
"""
Components = snap.TCnComV()
snap.GetSccs(G0, Components)
for CnCom in Components:
    print "Size of component: %d" % CnCom.Len()
"""
"""
DegToCntV = snap.TIntPrV()