示例#1
0
def CN_similarity_max(graph, player, dic_path, n=5, directed=True):
    """Find the n most similar players to player by CN metrics"""
    with open(dic_path, 'rb') as dic_id:
        mydict = pickle.load(dic_id)
        player_id = mydict[player]

        player_neighbor = snap.TIntV()
        snap.GetNodesAtHop(graph, player_id, 1, player_neighbor, directed)

        player_cn = {}
        for node in graph.Nodes():
            nodeId = node.GetId()
            if nodeId != player_id:
                node_neighbor = snap.TIntV()
                snap.GetNodesAtHop(graph, nodeId, 1, node_neighbor, directed)
                Inter = snap.TIntV()
                player_neighbor.Intrs(node_neighbor, Inter)
                x = Inter.Len()
                player_cn[nodeId] = x

        player_cn = Counter(player_cn)

        print("5 most similar players to {} by CN metrics:".format(player))
        for k, v in player_cn.most_common(n):
            print('{}: {}'.format(
                list(mydict.keys())[list(mydict.values()).index(k)], v))
示例#2
0
def extend_subgraph(G, k, sg, v_ext, node_id, motifs, verbose=False):
    """Recursive function in the ESU algorithm"""
    if len(sg) is k:
        count_iso(G, sg, motifs, verbose)
        return
    while len(v_ext) != 0:
        w = v_ext[0]
        del v_ext[0]
        v_ext_bis = copy.deepcopy(v_ext)
        neighbors = snap.TIntV()
        snap.GetNodesAtHop(G, w, 1, neighbors, False)
        for node in neighbors:
            if not (node in sg):
                if node > node_id:
                    bool = False
                    nb = snap.TIntV()
                    snap.GetNodesAtHop(G, node, 1, nb, False)
                    for n in sg:
                        b = (n in nb)
                        bool = bool | b
                    if not bool:
                        v_ext_bis.append(node)
        sg_bis = copy.deepcopy(sg)
        sg_bis.append(w)
        extend_subgraph(G, k, sg_bis, v_ext_bis, node_id, motifs, verbose)
    return
示例#3
0
 def generate_scores(self):
     scores = {}
     common_neighbor_scores = {}
     for e in self.g.Edges():
         # common_neighbor_scores[(e.GetSrcNId(), e.GetDstNId())] = snap.GetCmnNbrs(self.g, e.GetSrcNId(), e.GetDstNId())
         n1 = snap.TIntV()
         n2 = snap.TIntV()
         snap.GetNodesAtHop(self.g, e.GetSrcNId(), 1, n1, True)
         snap.GetNodesAtHop(self.g, e.GetDstNId(), 1, n2, True)
         common_neighbor_scores[(e.GetSrcNId(), e.GetDstNId())] = len(set(n1) & set(n2))
     Nodes = snap.TIntFltH()
     Edges = snap.TIntPrFltH()
     snap.GetBetweennessCentr(self.g, Nodes, Edges, self.node_frac, True)
     edge_betweenness_scores = {}
     for e in Edges:
         edge_betweenness_scores[(e.GetVal1(), e.GetVal2())] = Edges[e]
     max_cn = max(common_neighbor_scores.values())
     max_eb = max(edge_betweenness_scores.values())
     print(common_neighbor_scores)
     print(edge_betweenness_scores)
     for e in self.g.Edges():
         src = e.GetSrcNId()
         dst = e.GetDstNId()
         scores[(src, dst)] = self.l * common_neighbor_scores[(src,dst)] / max_cn + (1-self.l) * edge_betweenness_scores[(src,dst)] / max_eb
     return scores
示例#4
0
def TSS_opt(p2p, degrees, ths):
    V = [node for node in ths.keys()]
    S = []
    neighbors = snap.TIntV()
    while len(V) > 0:
        for node in V:
            if ths[node] == 0:
                snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
                for neighbor in neighbors:
                    ths[neighbor] = max(ths[neighbor] - 1, 0)
                    degrees[neighbor] -= 1
            elif degrees[node] < ths[node]:
                S.append(node)
                #print(node)
                snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
                for neighbor in neighbors:
                    ths[neighbor] -= 1
                    degrees[neighbor] -= 1
            else:
                max_value = max((ths[n]/(degrees[n]*degrees[n]+1)) for n in V)
                for n in V:
                    if (ths[n]/(degrees[n]*degrees[n]+1)) == max_value:
                        node = n
                        break
                snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
                for neighbor in neighbors:
                    degrees[neighbor] -= 1

            V.remove(node)
            p2p.DelNode(node)

    print("S:", len(S))
    return len(S)
示例#5
0
def runRecursive(G, testNodes, k=2):
    dicts = [{} for i in range(k)]

    # Subset
    #print testNodes

    # Now for each iteration, we keep going
    for i in range(k):
        # Handle base case separately
        print "Feature level", i
        counter = 1
        if i == 0:
            for node in testNodes:
                print 'Node', counter
                counter += 1
                dicts[0][node] = getFeatureVecBasic(G, node)

                # Also need to get all of the neighbor feature vecs
                Nbrs = snap.TIntV()
                snap.GetNodesAtHop(G, node, 1, Nbrs, True)
                for nbr in Nbrs:
                    dicts[0][nbr] = getFeatureVecBasic(G, nbr)

                    # And for neighbors' neighbors
                    NbrsNbrs = snap.TIntV()
                    snap.GetNodesAtHop(G, nbr, 1, NbrsNbrs, True)
                    for nbrnbr in NbrsNbrs:
                        dicts[0][nbrnbr] = getFeatureVecBasic(G, nbrnbr)

        # Otherwise, we do it the normal way
        else:
            for node in testNodes:
                print 'Node', counter
                counter += 1
                # Get all of the neighbors of our node
                Nbrs = snap.TIntV()
                snap.GetNodesAtHop(G, node, 1, Nbrs, True)
                sumVec = np.zeros(3**(i))

                # For each neighbor, get mean and sum
                for nbr in Nbrs:
                    sumVec += dicts[i - 1][nbr]

                # Handle edge case
                meanVec = None
                if len(Nbrs) == 0:
                    meanVec = np.zeros(3**(i))
                else:
                    meanVec = sumVec / float(len(Nbrs))

                # Now concatenate
                dicts[i][node] = np.concatenate(
                    (dicts[i - 1][node], meanVec, sumVec))

        # Pickle to save
        with open('feats_' + str(i) + '_v1.pkl', 'wb') as f:
            pickle.dump(dicts[i], f)

    return dicts[-1]
def create_weighted_cosponsorship_graph(chamber, session):
    print("Creating weighted cosponsorship graph (wcg)...")
    m = np.load('raw_data/govtrack_cosponsor_temp/m_%s_%s.npy' %
                (chamber, session))
    b = np.load('raw_data/govtrack_cosponsor_temp/b_%s_%s.npy' %
                (chamber, session)).item()
    to_bills = np.load('raw_data/govtrack_cosponsor_temp/to_bills_%s_%s.npy' %
                       (chamber, session)).item()
    g, node_info, id_to_nid = read_bcg(chamber, session)
    edge_weights = {}
    sponsored_bills = {}
    wcg = snap.TUNGraph.New()
    for node in tqdm(node_info, total=len(node_info), position=0):
        if node_info[node]['type'] == 'bill':
            continue
        if not wcg.IsNode(node):
            wcg.AddNode(node)
        connected = snap.TIntV()
        if not g.IsNode(node):
            print("F**K WHY IS %s NOT A NODE" % (node, ))
            continue
        snap.GetNodesAtHop(g, node, 2, connected, False)
        if node in sponsored_bills:
            num_bills = sponsored_bills[node]
        else:
            bills = snap.TIntV()
            snap.GetNodesAtHop(g, node, 1, bills, False)
            num_bills = len(bills)
            sponsored_bills[node] = num_bills
        for node2 in connected:
            if node == node2:
                continue
            if not wcg.IsNode(node2):
                wcg.AddNode(node2)
            if node2 in sponsored_bills:
                num_bills2 = sponsored_bills[node2]
            else:
                bills2 = snap.TIntV()
                snap.GetNodesAtHop(g, node2, 1, bills2, False)
                num_bills2 = len(bills2)
                sponsored_bills[node2] = num_bills2
            common_bills = len(
                get_cosponsorship(node_info[node]['info']['id'],
                                  node_info[node2]['info']['id'], to_bills))
            edge_weights[(node, node2)] = common_bills / len(
                to_bills[node_info[node]['info']['id']])
            edge_weights[(node2, node)] = common_bills / len(
                to_bills[node_info[node2]['info']['id']])
            wcg.AddEdge(node, node2)
    snap.SaveEdgeList(wcg,
                      'govtrack_data/wcg_%s_%s.graph' % (chamber, session))
    np.save('govtrack_data/wcg_edge_weights_%s_%s.npy' % (chamber, session),
            edge_weights)
    np.save('govtrack_data/wcg_sponsored_bills_%s_%s.npy' % (chamber, session),
            sponsored_bills)
    print("Completed weighted cosponsorship graph!")
示例#7
0
def CN_similarity(graph, id1, id2, directed=True):
    """Computes CN similarity between nodes id1 and id2 in graph"""
    neighbors1 = snap.TIntV()
    snap.GetNodesAtHop(graph, id1, 1, neighbors1, directed)
    neighbors2 = snap.TIntV()
    snap.GetNodesAtHop(graph, id2, 1, neighbors2, directed)

    Inter = snap.TIntV()
    neighbors1.Intrs(neighbors2, Inter)
    x = Inter.Len()
    return x
def sim_rank_wrapper(G, n1, n2, gamma, directed=False):
    if n1 == n2: return 1
    constant = gamma / preferential_attachment(G, n1, n2)
    n1_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n1, 1, n1_neighbors, directed)
    n2_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n2, 1, n2_neighbors, directed)
    result = 0
    for a in n1_neighbors:
        for b in n2_neighbors:
            result += sim_rank_wrapper(G, a, b, gamma)
    return result * constant
def common_neighbors_2(G, n1, n2, directed=False):
    deleted = False
    if G.IsEdge(n1, n2):
        G.DelEdge(n1, n2)
        deleted = True
    n1_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n1, 1, n1_neighbors, directed)
    n2_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n2, 2, n2_neighbors, directed)
    common_neighbors = set(n1_neighbors) & set(n2_neighbors)
    if deleted: G.AddEdge(n1, n2)
    return len(common_neighbors)
示例#10
0
def getFeat():
    #G1, id2, synset2, _,_,_ = generate_word_graph(True, False, False)

    #G2, id2, synset2, _,_,_ = generate_word_graph(False, True, False)
    #G3, id2, synset2, _,_,_ = generate_word_graph(False, False, True)
    G4, id2, synset2, _, _, _ = generate_meaning_graph(True, False, False)
    #G5, id2, synset2, _,_,_ = generate_meaning_graph(False, True, False)
    #G6, id2, synset2, _,_,_ = generate_meaning_graph(False, False, True)
    #G7, id2, synset2, _,_,_ = generate_meaning_graph(True, False, True)

    degVec = {}
    egoNet = {}
    egoNetDeg = {}
    G = G4
    it = 0
    for node in G.Nodes():
        it += 1
        deg = node.GetDeg()
        nodeName = node.GetId()
        degVec[nodeName] = deg
        egoNet[nodeName] = 0
        egoNetDeg[nodeName] = 0
        commNeib = 0
        NodeVec = snap.TIntV()
        nodeList = snap.GetNodesAtHop(G, nodeName, 1, NodeVec, False)
        for node2 in NodeVec:
            nodeIt = G.GetNI(node2)
            dest = nodeIt.GetDeg()
            egoNetDeg[nodeName] += dest
            NodeVec2 = snap.TIntV()
            nodeList = snap.GetNodesAtHop(G, node2, 1, NodeVec2, False)
            for el in NodeVec2:
                if el in NodeVec:
                    commNeib += 1
        egoNet[nodeName] = deg + commNeib / 2
        egoNetDeg[nodeName] = egoNetDeg[nodeName] - commNeib - deg
    print egoNet[numImp], egoNetDeg[numImp], degVec[numImp]

    featDictOne = {}
    for el in G.Nodes():
        numId = el.GetId()
        featVec = [degVec[numId], egoNet[numId], egoNetDeg[numId]]
        featDictOne[numId] = featVec
    for i in range(0, 2):
        featDict = {}
        for el in G.Nodes():
            numId = el.GetId()
            featVec = recFeat(featDictOne, G, numId)
            featDict[numId] = featVec
        featDictOne = featDict
    getSim(featDictOne)
示例#11
0
def clustering_coffecient(G):
    cluster_dict = []
    nodes = []
    # All nodes are stored
    for s in G.Nodes():
        nodes.append(s.GetId())
    k = len(nodes)

    if (k > 2):
        for s in nodes:
            neighbors_node = []
            neighbors_mutual = []
            # node ids of all the nodes that are at distance Hop from node StartNId
            NodeVec = snap.TIntV()
            snap.GetNodesAtHop(G, s, 1, NodeVec, False)
            # nodes are stored for comparing
            for neighbor in NodeVec:
                neighbors_node.append(neighbor)

            for neighbor_2 in NodeVec:
                Second_NodeVec = snap.TIntV()
                snap.GetNodesAtHop(G, neighbor_2, 1, Second_NodeVec, False)
                # Finding triangles
                for second_neighbor in Second_NodeVec:
                    if second_neighbor in neighbors_node:
                        neighbors_mutual.append(second_neighbor)

            neighbors_mutual = list(neighbors_mutual)

            clust_node = 0

            if len(neighbors_mutual):
                # Clustering Coefficient for each node
                clust_node = (float(len(neighbors_mutual))) / (
                    (float(len(NodeVec)) * (float(len(NodeVec)) - 1)))

            cluster_dict.append(clust_node)
    else:
        pass

    # Average Clustering Coefficient for all nodes in a graph
    Average_Value = 0
    l = len(cluster_dict)
    if (l is not 0):
        for val in cluster_dict:
            Average_Value = Average_Value + val

        return Average_Value / l
    else:
        return 0
def jaccard_2(G, n1, n2, directed=False):
    deleted = False
    if G.IsEdge(n1, n2):
        G.DelEdge(n1, n2)
        deleted = True
    n1_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n1, 1, n1_neighbors, directed)
    n2_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n2, 2, n2_neighbors, directed)
    total_neighbors = set(n1_neighbors) | set(n2_neighbors)
    common_neighbors = set(n1_neighbors) & set(n2_neighbors)
    if len(total_neighbors) == 0: result = 0.0
    else: result = float(len(common_neighbors)) / float(len(total_neighbors))
    if deleted: G.AddEdge(n1, n2)
    return result
示例#13
0
def inc_ex_prune(i, j, Graph):
    donors_to_del = set()

    for d1 in donors2:
        if degree_map[d1] == j:
            l1 = donor_to_loans[d1]
            loan_neighbor_sets = []
            for loan in l1:
                n_set = set()
                NIdV = snap.TIntV()
                snap.GetNodesAtHop(Graph, loan, 1, NIdV, False)
                for n in NIdV:
                    n_set.add(n)
                loan_neighbor_sets.append(n_set)
            shared_neighbors = set.intersection(*loan_neighbor_sets)
            num_shared_neighbors = len(shared_neighbors)  #donors in core
            if num_shared_neighbors < i:
                #continue
                Graph.DelNode(d1)
                donors_to_del.add(d1)
            else:
                core_count[(i, j)] += 1
                cores[(i, j)].append(list(shared_neighbors) + list(l1))
                donors_to_del.add(d1)  #NOTE: try with and without a few times
    for d in donors_to_del:
        donors2.remove(d)
示例#14
0
def bfs(start, end):
    queue = [[start]]
    visited = set()

    while queue:
        # Gets the first path in the queue
        path = queue.pop(0)

        # Gets the last node in the path
        vertex = path[-1]

        # Checks if we got to the end
        if vertex == end:
            return path
        # We check if the current node is already in the visited nodes set in order not to recheck it
        elif vertex not in visited:
			# enumerate all adjacent nodes, construct a new path and push it into the queue
			NodeVec = snap.TIntV()
			n_nodes = snap.GetNodesAtHop(G,vertex,1,NodeVec,False)
			nodes = [item for item in NodeVec]
			for current_neighbour in nodes:
			    new_path = list(path)
			    new_path.append(current_neighbour)
			    queue.append(new_path)

			# Mark the vertex as visited
			visited.add(vertex)
示例#15
0
def clustering_coef(G, mode_for_end_nodes='put_zeros'):
    '''
    Calculate vector of clustering coefficient for each node 
    G is an undirected Graph 
    mode_for_end_nodes = 'nan' - end/disconnected nodes will be ignored i.e. non included in output list
    mode_for_end_nodes = 'put_zero' - assign zero for end/disconnected
    
    list_clusterning_coefs_allnodes = clustering_coef(UGraph)
    https://www.kaggle.com/alexandervc/hw1-ac/notebook
    '''
    list_clusterning_coefs_allnodes = []
    for n in G.Nodes():
        NodeVec = snap.TIntV()
        snap.GetNodesAtHop(G, n.GetId(), 1, NodeVec,
                           False)  # Get neigbours of current node
        current_degree = len(NodeVec)  # same as n.GetDeg()
        if current_degree <= 1:  # skip disconnected&end nodes - impossible to calculate for them - getting division by zero
            if mode_for_end_nodes == 'nan':
                continue
            else:
                list_clusterning_coefs_allnodes.append(0)
                continue
        count_edges_between_neigbours = 0
        for neigbor1 in NodeVec:
            for neigbor2 in NodeVec:
                if neigbor1 >= neigbor2:
                    continue
            if G.IsEdge(neigbor1, neigbor2):
                count_edges_between_neigbours += 1
        clustering_coef_current_node = 2 * count_edges_between_neigbours / (
            current_degree * (current_degree - 1))
        list_clusterning_coefs_allnodes.append(clustering_coef_current_node)
    return list_clusterning_coefs_allnodes
def adamic_adar_2(G, n1, n2, directed=False):
    deleted = False
    if G.IsEdge(n1, n2):
        G.DelEdge(n1, n2)
        deleted = True
    n1_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n1, 1, n1_neighbors, directed)
    n2_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n2, 2, n2_neighbors, directed)
    common_neighbors = set(n1_neighbors) & set(n2_neighbors)

    aa = 0.0
    for n in common_neighbors:
        aa += 1.0 / math.log(G.GetNI(n).GetDeg())

    if deleted: G.AddEdge(n1, n2)
    return aa
示例#17
0
def JA_similarity(graph, id1, id2, directed=True):
    """Computes JA similarity between nodes id1 and id2 in graph"""
    if id1 == id2:
        return 1
    else:
        neighbors1 = snap.TIntV()
        snap.GetNodesAtHop(graph, id1, 1, neighbors1, directed)
        neighbors2 = snap.TIntV()
        snap.GetNodesAtHop(graph, id2, 1, neighbors2, directed)

        Inter = snap.TIntV()
        Union = snap.TIntV()
        neighbors1.Intrs(neighbors2, Inter)
        neighbors1.Union(neighbors2, Union)
        x = Inter.Len()
        y = Union.Len()
        return x / y
def friends_measure(G, n1, n2, directed=False):
    deleted = False
    if G.IsEdge(n1, n2):
        G.DelEdge(n1, n2)
        deleted = True

    n1_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n1, 1, n1_neighbors, directed)
    n2_neighbors = snap.TIntV()
    snap.GetNodesAtHop(G, n2, 1, n2_neighbors, directed)
    fm = 0
    for a in n1_neighbors:
        for b in n2_neighbors:
            if a == b or G.IsEdge(a, b) or G.IsEdge(b, a):
                fm += sim_rank(G, a, b)
    if deleted: G.AddEdge(n1, n2)
    return fm
示例#19
0
def bfs(graph, roots, depth):
    nodes = set()
    for r in roots:
        for i in range(depth + 1):
            NodeVec = snap.TIntV()
            snap.GetNodesAtHop(graph, r, i, NodeVec, False)
            for x in NodeVec:
                nodes.add(x)
    return nodes
示例#20
0
def snowball_sample(G, num_waves, seeds):
    """
    Parameters:
      G - SNAP graph or network to sample frpm
      num_waves - number of snowball waves 
      seeds - SNAP vector (TIntV) of seeds (node ids) to start snowball sample 
             from
    
    Return value:
      SNAP network (TNEANet) snowball sampled from G with each node having 
      an integer "zone" attribute for snowball sampling zone 
       (0=seed, 1=first wave, etc.)
      [TNEANet needed to allow zone attribute, not actually using multigraph 
       capability].

    Note directions on directed graph are ignored - can sample in undirected
    or directed graph.
    """
    assert (len(seeds) == len(set(seeds)))  # no duplicate node ids
    # It seems like GetSubGraph does not preserve node attributse
    # so instead of adding attributes ot nodes on N, make a Python
    # dictionary mapping node ids to zone and then add them back
    # ass attributes on the subgraph (node ids are preserved so we
    # can do this)
    zonedict = dict()  # map nodeid : zone
    N = snap.ConvertGraph(snap.PNEANet, G)  # copy graph/network G to network N
    nodes = set(seeds)  # will accumulate all nodes (including seeds) here
    for seed in seeds:
        zonedict[seed] = 0  # seed nodes are zone 0
    newNodes = set(nodes)
    for i in range(num_waves):
        wave = i + 1
        #print 'wave',wave
        for node in set(newNodes):
            neighbours = snap.TIntV()
            snap.GetNodesAtHop(G, node, 1, neighbours,
                               False)  # neighbours of node
            newNeighbours = set(
                neighbours) - nodes  # neighbours that are not already in nodes
            for node in newNeighbours:
                if not zonedict.has_key(node):
                    zonedict[node] = wave
            newNodes.update(
                newNeighbours
            )  # newNodes gets set union of itslf and newNeighbours
        nodes.update(newNodes)
    # have to convert nodes set into TIntV for use in SNAP
    NodeVec = snap.TIntV()
    for node in nodes:
        NodeVec.Add(node)
    sampleN = snap.GetSubGraph(N, NodeVec)
    # now put the zones as attributes on the subgraph nodes (which depends
    # on nodeids being preserved in the subgraph)
    sampleN.AddIntAttrN("zone", -1)  # add zone attribute init to -1
    for (nodeid, zone) in zonedict.iteritems():
        sampleN.AddIntAttrDatN(nodeid, zone, "zone")
    return sampleN
示例#21
0
def get_co_reviewer(G, User):
    co_reviewer_dict = defaultdict(set)
    for NI in G.Nodes():
        v = NI.GetId()
        NbrVec = snap.TIntV()
        snap.GetNodesAtHop(G, v, 2, NbrVec)
        if v in User:
            co_reviewer_dict[v] |= set(NbrVec)
    return co_reviewer_dict
示例#22
0
def get_two_hop_dist(filename):
    nv = snap.TIntV()
    graph = snap.LoadEdgeList(snap.PUNGraph, filename)
    two_hop = collections.defaultdict(int)
    ratio_list = []
    for node in graph.Nodes():
        nodes_at_two_hop = snap.GetNodesAtHop(graph, node.GetId(), 2, nv,
                                              False)
        nodes_at_one_hop = snap.GetNodesAtHop(graph, node.GetId(), 1, nv,
                                              False)
        if nodes_at_two_hop > 0:
            two_hop[nodes_at_two_hop] += 1
            ratio_list.append(float(nodes_at_two_hop) / nodes_at_one_hop)

    print "mean ratio of two hop to one hop was " + str(np.mean(ratio_list))

    for i in two_hop:
        two_hop[i] = two_hop[i] / float(graph.GetNodes())
    return two_hop
示例#23
0
def iterative_prune(i, j, Graph):
    while (True):
        print "PRUNING"
        print "CURRENT NODE NUMBER: ", Graph.GetNodes()
        print "CURRENT EDGE NUMBER: ", Graph.GetEdges()
        deleted = False
        NIdV = snap.TIntV()

        DonorsToDel = snap.TIntV()
        for d in donors2:
            num_neighbors = snap.GetNodesAtHop(Graph, d, 1, NIdV, False)
            if num_neighbors < j:
                deleted = True
                DonorsToDel.Add(d)
        for nid in DonorsToDel:
            donors2.remove(nid)
        snap.DelNodes(Graph, DonorsToDel)

        LoansToDel = snap.TIntV()
        for l in loans2:
            num_neighbors = snap.GetNodesAtHop(Graph, l, 1, NIdV, False)
            if num_neighbors < i:
                deleted = True
                LoansToDel.Add(l)
        for nid in LoansToDel:
            loans2.remove(nid)
        snap.DelNodes(Graph, LoansToDel)

        if not deleted:
            break

    print "POST-ITERATIVE PRUNING NODE NUMBER: ", Graph.GetNodes()
    print "POST-ITERATIVE PRUNING: ", Graph.GetEdges()

    print "POST-ITERATIVE PRUNING DONOR NUMBER: ", len(donors2)
    print "POST-ITERATIVE PRUNING LOAN NUMBER: ", len(loans2)

    print "Updating Donor Degree Map"

    for donor in donors2:
        NIdV = snap.TIntV()
        degree_map[donor] = snap.GetNodesAtHop(Graph, donor, 1, NIdV, False)
        donor_to_loans[donor] = set([nid for nid in NIdV])
def business(examples, G, methods, outfiles):
	hop2s = dict()
	nodes=[N.GetId() for N in snap.Nodes(G)]
	print "Getting 2 hops..."
	for u in examples:
		for v in examples[u]:
			if int(v) not in hop2s:
				nodeid = int(v)
		    	# Get nodes at hop 2
				if nodeid in nodes:
					hop2nodes = snap.TIntV()
					snap.GetNodesAtHop(G, nodeid, 2, hop2nodes, True)
					tempset = Set()
					for i in hop2nodes:
						tempset.add(i)
					hop2s[nodeid] = tempset
	print "Getting users..."
	neighbors = dict()
	for u in examples:
		if int(u) not in neighbors and int(u) in nodes:
			# Get neighbors of the user
			temp = snap.TIntV()
			snap.GetNodesAtHop(G, int(u), 1, temp, True)
			tempset = Set()
			for i in temp:
				tempset.add(i)
			neighbors[int(u)] = tempset
	print "Beginning computation..."
	for m,f in zip(methods,outfiles):
		b_sim=defaultdict(dict)
		for u in examples:
			for v in examples[u]:
				if (int(u) in nodes and int(v) in nodes):
					if (m=='common_neighbors'):
						b_sim[u][v] = common_neighbors(hop2s[int(v)], neighbors[int(u)])
					elif (m=='jaccard'):
						b_sim[u][v] = jaccard(hop2s[int(v)], neighbors[int(u)])
					elif (m=='adamic_adar'):
						b_sim[u][v] = adamic_adar(hop2s[int(v)], neighbors[int(u)],G)
				else:
					b_sim[u][v]=0
		util.write_json(b_sim, f)
示例#25
0
def TSS(p2p, degrees, ths):
    V = [node for node in ths.keys()]
    S = []
    neighbors = snap.TIntV()
    while len(V) > 0:
        zero = -1
        less = -1
        for n in V:
            if ths[n] == 0:
                zero = n
                break
            elif degrees[n] < ths[n]:
                less = n
                break
        if zero != -1:
            node = zero
            snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
            for neighbor in neighbors:
                ths[neighbor] = max(ths[neighbor] - 1, 0)
                degrees[neighbor] -= 1
        elif less != -1:
            node = less
            S.append(node)
            snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
            for neighbor in neighbors:
                ths[neighbor] -= 1
                degrees[neighbor] -= 1
        else:
            max_value = max((ths[n]/(degrees[n]*degrees[n]+1)) for n in V)
            for n in V:
                if (ths[n]/(degrees[n]*degrees[n]+1)) == max_value:
                    node = n
                    break
            snap.GetNodesAtHop(p2p, node, 1, neighbors, True)
            for neighbor in neighbors:
                degrees[neighbor] -= 1
        
        V.remove(node)
        p2p.DelNode(node)

    print("S:", len(S))
    return len(S)
示例#26
0
def temporal_neighbors(graphs, nodeID, directed=True):
    """Return a snap.TIntV() set of nodes that have been at least once a neighbor of node nodeID in graphs"""
    t_neighbors = snap.TIntV()
    for g in graphs:
        neighbors = snap.TIntV()
        snap.GetNodesAtHop(g, nodeID, 1, neighbors, directed)
        for n in neighbors:
            if n not in t_neighbors:
                t_neighbors.Add(n)
        # t_neighbors.Union(neighbors)
    return t_neighbors
def get_2_hops(G, n1, n2, directed=False):
    deleted = False
    if G.IsEdge(n1, n2):
        G.DelEdge(n1, n2)
        deleted = True
    result = 0
    if (n1 >= 18630353 and n1 < 31097109) and (n2 < 18630353
                                               or n2 >= 31097109):
        b_neighbors = snap.TIntV()
        snap.GetNodesAtHop(G, n1, 1, b_neighbors, directed)
        for n in b_neighbors:
            result += snap.GetCmnNbrs(G, n, n2)
    elif (n2 >= 18630353 and n2 < 31097109) and (n1 < 18630353
                                                 or n1 >= 31097109):
        b_neighbors = snap.TIntV()
        snap.GetNodesAtHop(G, n2, 1, b_neighbors, directed)
        for n in b_neighbors:
            result += snap.GetCmnNbrs(G, n, n1)
    if deleted: G.AddEdge(n1, n2)
    return result
示例#28
0
def enumerate_subgraph(G, motifs, k=3, verbose=False):
    """Main function of ESU algorithm"""
    global motif_counts
    motif_counts = [0] * len(motifs)
    for node in G.Nodes():
        id = node.GetId()
        neighbors = snap.TIntV()
        snap.GetNodesAtHop(G, id, 1, neighbors, False)
        v_ext = [n for n in neighbors if n > id]
        extend_subgraph(G, k, [id], v_ext, id, motifs, verbose)
    return
def basic_features_node(graph, nodeId, directed=False):
    """Compute basic features of node nodeId in graph for Rolx and ReFex (see HW2)"""
    deg = graph.GetNI(nodeId).GetDeg()
    neighbors = snap.TIntV()
    snap.GetNodesAtHop(graph, nodeId, 1, neighbors, directed)
    neighbors.Add(nodeId)
    egonet = snap.ConvertSubGraph(snap.PUNGraph, graph, neighbors, False)
    in_ego = egonet.GetEdges()
    out_ego = -2 * in_ego
    for node in neighbors:
        out_ego += graph.GetNI(node).GetDeg()
    return [deg, in_ego, out_ego]
示例#30
0
def RankDegreeAdjacentNodeDSC(graph, nodeCenter):
    output = {}
    sortedGraphDictionary = RankDegreeGraphDSC(graph)  #DICITIONARY GRAPH UTAMA
    NodeVec = snap.TIntV()
    snap.GetNodesAtHop(graph, nodeCenter, 1, NodeVec, False)
    #NodeVec=getNeighbours(graph,nodeCenter)
    for item in NodeVec:
        output[item] = sortedGraphDictionary.get(item)

    #Sorting DSC
    output = OrderedDict(
        sorted(output.items(), key=lambda x: x[1], reverse=True))
    #print(output)
    return output