def getDeg(G):
	d = snap.TIntIntH()
	user = snap.TIntIntH()
	game = snap.TIntIntH()
	for ni in G.Nodes():
		deg = ni.GetDeg()
		update(d, deg)
		if ni.GetId() >= 600000:
			update(user, deg)
		else:
			update(game, deg)
	return d, user, game
Пример #2
0
    def __init__(self, fileName):
        self.fileName = fileName
        self.rootDir = self.getRootDir()
        self.attrVal1 = attrVal1
        self.attrVal2 = attrVal2

        self.graphName = self.getGraphName()

        self.lblV = snap.TStrV()
        self.lblV.Add(attrVal1)
        self.lblV.Add(attrVal2)

        self.lblNH = snap.TStrIntH()  # Node count with attached label

        self.lblEH = snap.TIntIntH()  # Edge count with attached src dst labels

        #self.G = self.getGraph(snap.PUNGraph)
        self.G = self.getLblGraph()
        self.saveGraph()

        ufileName = os.path.abspath(self.rootDir + "/" + self.graphName +
                                    ".txt")
        self.snapG = snap.LoadEdgeList(snap.PUNGraph, ufileName)
        self.nxG = nx.read_edgelist(ufileName)

        self.gNAH = self.getNodeIdLabel(self.G)
Пример #3
0
    def getNEStats(self):
        lblEH = snap.TIntIntH()
        lblNH = snap.TStrIntH()

        NI = self.G.BegNI()
        cN = 0
        while NI < self.G.EndNI():
            NId = NI.GetId()
            label = self.walkNodeAttributes(NId)
            if (label not in lblNH):
                lblNH[label] = 0
            lblNH[label] += 1
            NI.Next()
            cN += 1


        EI = self.G.BegEI()
        ECount = 0
        while EI < self.G.EndEI():
            srcId = EI.GetSrcNId()
            dstId = EI.GetDstNId()

            srcLbl = self.walkNodeAttributes(srcId)
            dstLbl = self.walkNodeAttributes(dstId)

            EdgeLblId = self.getEdgeLbl(srcLbl, dstLbl)

            if (EdgeLblId not in lblEH):
                lblEH[EdgeLblId] = 0

            lblEH[EdgeLblId] += 1

            ECount += 1
            EI.Next()
        return lblNH,lblEH
Пример #4
0
    def __init__(self, filename, mVals, pVal, tau):
        self.fileName = fileName
        self.mVals = mVals
        self.pVal = pVal
        self.pVals = []
        self.pVals.append(pVal)
        self.pVals.append(1 - pVal)
        self.tau = tau
        self.nLH = snap.TIntStrH()
        self.lblNH = snap.TStrIntH()  # Node count with attached label
        self.lblEH = snap.TIntIntH()  # Edge count with attached src dst labels

        self.RH = snap.TIntFltPrH()
        self.BH = snap.TIntFltPrH()

        self.cRV = snap.TIntV()
        self.cBV = snap.TIntV()

        self.G = self.getGraph(snap.PUNGraph)
        self.NG = snap.TNEANet()
        self.graphName = self.getGraphName()
        self.rootDir = self.getParentDir(self.fileName)
        self.absrootDir = os.path.abspath(self.rootDir)

        self.cR_count = 0
        self.cB_count = 0

        self.RH_count = 0
        self.BH_count = 0
Пример #5
0
def outdegree(rankCommands, Graph, conn, cur):
    OutDegV = snap.TIntPrV()
    before_time = time.time()
    snap.GetNodeOutDegV(Graph, OutDegV)
    print "Total handling time is: ", (time.time() - before_time)
    DegH = snap.TIntIntH()
    slist = sortNodes(OutDegV, DegH)
    createTable(rankCommands, slist, DegH, conn, cur)
Пример #6
0
def community_partition(G):
    CommuV = snap.TCnComV()
    modularity = snap.CommunityCNM(G, CommuV)
    ComutyH = snap.TIntIntH()
    partition = 0
    for community in CommuV:
        for NI in community:
            ComutyH[NI] = partition
        partition = partition + 1
    return ComutyH
Пример #7
0
def vote_for_node(node, PartitionH, G, Kcore):
    VoteNodeH = snap.TIntIntH()
    for neighbor in G.GetNI(node).GetOutEdges():
        if Kcore.IsNode(neighbor):
            key = PartitionH[neighbor]
            if VoteNodeH.IsKey(key):
                VoteNodeH[key] += 1
            else:
                VoteNodeH[key] = 1
    VoteNodeH.Sort(False, True)
    return VoteNodeH.GetKey(0)
Пример #8
0
def recover_partition_by_kcore(Partition_KcoreH, SortH, G):
    Recover_PartitionH = snap.TIntIntH()
    Recover_PartitionH = Partition_KcoreH

    i = 0
    for node in SortH:
        if SortH[node] == 0:
            Recover_PartitionH[node] = i - 1
        else:
            Recover_PartitionH[node] = vote_for_node(node, Partition_KcoreH, G,
                                                     Kcore)
    return Recover_PartitionH
Пример #9
0
def sort_by_neighbor(NodeV_Diff, Kcore, G):
    SortH = snap.TIntIntH()

    for node in NodeV_Diff:
        SortH[node] = 0
        for neighbor in G.GetNI(node).GetOutEdges():
            if Kcore.IsNode(neighbor):
                SortH[node] += 1
    SortH.Sort(False, True)
    #SortedV = snap.TIntV()
    #for key in temp_hash_table:
    #	SortedV.Add(key)
    return SortH
Пример #10
0
import random
import os
import sys
import time

import snap

print("---------- 1 ---------")

Hash = snap.TIntIntH()

Hash.AddDat(5, 4)
Hash.AddDat(1, 2)
Hash.AddDat(4, 8)

Hash[17] = 15
Hash[11] = 14
Hash[15] = 18
#Hash.AddDat(TInt(3),TInt(5))
#Hash.AddDat(TInt(4),TInt(6))
#Hash.AddDat(TInt(1),TInt(8))
#Hash.AddDat(TInt(6),TInt(2))

print("len", Hash.Len())

Iter = Hash.BegI()
Key = Iter.GetKey()
Value = Iter.GetDat()
print("iter", Key, Value)

print("Iter < Hash.EndI", Iter < Hash.EndI())
def getDeg2(G):
	d = snap.TIntIntH()
	for ni in G.Nodes():
		deg = ni.GetDeg()
		update(d, deg)
	return d
Пример #12
0
def gen_sample(G,p, noise_model='main'):
    """Creates a noisy sample graph of an underlying graph

    Args:
        G: the underlying graph
        p: probability that each vertex is hidden (this is needed only if noise_model='main')
        noise_model: which model to use when generating the sample. It can have the values 'main' for 
                        our main noise model, 'different_p_i' for the model where vertices hide different
                        portions of their incident edges and 'random_walk' for the random walk sampling method
        
    Returns:
        G_sample: the noisy sample
    """
        
    if noise_model == 'main':
        
        #-------------------------------------------main sample generation model------------------------------------
        G_sample = copy_graph(G)
        coins = bernoulli.rvs(p, size=G.GetNodes())
        r = snap.TIntIntH()
        i = 0
        for NI in G.Nodes():
            r[NI.GetId()] = coins[i]       #each node performs an independent bernoulli trial
            i += 1
        for e in G.Edges():
            if (r[e.GetSrcNId()] + r[e.GetDstNId()] == 2):
                G_sample.DelEdge(e.GetSrcNId(),e.GetDstNId())
                
                
    elif noise_model == 'different_p_i':
        #-------------------alternative sample generation model: different p_is-------------------------------------
        G_sample = copy_graph(G)
        probs =  np.random.uniform(low=0, high=0.7, size=G.GetNodes())

        probsHash = snap.TIntFltH()
        r = snap.TIntPrIntH()
        #initialize the edge hash
        for e in G.Edges():
            r[snap.TIntPr(e.GetSrcNId(),e.GetDstNId())] = 0
            r[snap.TIntPr(e.GetDstNId(),e.GetSrcNId())] = 0

        i = 0
        for NI in G.Nodes():
            probsHash[NI.GetId()] = probs[i]       #each node has its own p_i
            i += 1

        random_num = snap.TFlt
        for NI in G.Nodes():
            for k in NI.GetOutEdges():
                if random_num.GetRnd()<probsHash[NI.GetId()]: 
                    random_coin = 1
                else:
                    random_coin = 0
                r[snap.TIntPr(NI.GetId(),k)] += random_coin
                r[snap.TIntPr(k,NI.GetId())] += random_coin

        for e in G.Edges():
            edge_pair = snap.TIntPr(e.GetSrcNId(),e.GetDstNId())
            if (r[edge_pair] >= 1):
                G_sample.DelEdge(e.GetSrcNId(),e.GetDstNId())
            
            
    elif noise_model == 'random_walk': 
        #-------------------alternative sample generation model: random walk -------------------------------------
        Rnd = snap.TRnd(42)
        Rnd.Randomize()

        #initialize the sample graph as an empty graph
        G_sample = snap.TUNGraph.New()
        for NI in G.Nodes():
            G_sample.AddNode(NI.GetId())

        my_float = snap.TFlt
        my_int = snap.TInt
        #begin with a node iterator randomly initialized
        current = snap.TUNGraphNodeI(G.GetRndNI(Rnd))
        while True:        
            random_num = my_float.GetRnd()        #it is faster to use snap's random num generator
            if random_num < 0.15:
                #restart the walk with prob=0.15
                current = snap.TUNGraphNodeI(G.GetRndNI(Rnd))
            else:
                #select next vertex from the neighborhood
                old_visiting_node_id = current.GetId()
                degree = current.GetDeg()
                random_number = my_int.GetRnd(degree)
                random_neighbor_id = current.GetNbrNId(random_number)
                current = G.GetNI(random_neighbor_id)
                if not G_sample.IsEdge(old_visiting_node_id,current.GetId()) and not G_sample.IsEdge(current.GetId(),old_visiting_node_id):
                    G_sample.AddEdge(old_visiting_node_id,current.GetId())
            if G_sample.GetEdges() >= G.GetEdges() * 0.35: break
        
    return G_sample