def q2_3():
    print("============")
    g = load_graph_weights()
    v_mat = cal_initial_feature(g)
    for k in range(2):
        v_mat = aggregrate(v_mat, g)
    v_9 = v_mat[9]

    cos_sim = [(i, cal_cos_sim(v_9, v)) for i, v in enumerate(v_mat)]
    cos_sim.sort(key=lambda y: y[1], reverse=True)
    cos_sims = [u[1] for u in cos_sim]
    plt.hist(cos_sims, bins=20)
    plt.title("distribution of cosine similarity")
    plt.show()

    node_1, sim_1 = find_node(0, 0.05, cos_sim)
    subg_1 = get_2_nd_subgraph(node_1, g)
    subg_1_h = snap.TIntStrH()
    subg_1_h[node_1] = "blue"
    subg_1 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_1)
    snap.DrawGViz(subg_1, snap.gvlNeato, "subgraph_1.png",
                  "subgraph 1 sim: {0}".format(round(sim_1,
                                                     2)), True, subg_1_h)

    node_2 = 9  #find_node(0.4, 0.45, cos_sim)
    subg_2 = get_2_nd_subgraph(node_2, g)
    subg_2_h = snap.TIntStrH()
    subg_2_h[node_2] = "blue"
    subg_2 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_2)
    snap.DrawGViz(subg_2, snap.gvlNeato, "subgraph_center_9.png",
                  "subgraph_center_9", True, subg_2_h)

    node_3, sim_3 = find_node(0.6, 0.65, cos_sim)
    subg_3 = get_2_nd_subgraph(node_3, g)
    subg_3_h = snap.TIntStrH()
    subg_3_h[node_3] = "blue"
    subg_3 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_3)
    snap.DrawGViz(subg_3, snap.gvlNeato, "subgraph_3.png",
                  "subgraph 3 sim: {0}".format(round(sim_3,
                                                     2)), True, subg_3_h)

    node_4, sim_4 = find_node(0.9, 0.95, cos_sim)
    subg_4 = get_2_nd_subgraph(node_4, g)
    subg_4_h = snap.TIntStrH()
    subg_4_h[node_4] = "blue"
    subg_4 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_4)
    snap.DrawGViz(subg_4, snap.gvlNeato, "subgraph_4.png",
                  "subgraph 4 sim: {0}".format(round(sim_4,
                                                     2)), True, subg_4_h)

    print("============")
Пример #2
0
def CalculateLocalVector(Graph):
    # dim-1 : the degree of v,
    # dim-2 : the number of edges in the egonet of v
    # dim-3 : the number of edges that connect the egonet of v and the rest of the graph,
    #           i.e., the number of edges that enter or leave the egonet of v.

    N = Graph.GetNodes()
    V_local = np.zeros((N, 3))

    idx = 0
    for NI in Graph.Nodes():
        V_local[idx, 0] = NI.GetDeg()
        # print(idx, NI.GetId()) # they are equal

        V_ids = snap.TIntV()
        V_ids.Add(NI.GetId())
        degree_sum = NI.GetDeg()
        for Id in NI.GetOutEdges():
            V_ids.Add(Id)
            degree_sum += Graph.GetNI(Id).GetDeg()
            # print("edge (%d %d)" % (NI.GetId(), Id))
        G_ego = snap.ConvertSubGraph(snap.PUNGraph, Graph, V_ids)

        V_local[idx, 1] = G_ego.GetEdges()
        V_local[idx, 2] = degree_sum - 2 * G_ego.GetEdges()

        idx += 1
    return V_local
Пример #3
0
def DrawGraph(lower, upper, cos_sim, g, filename, title='', color='blue'):
    """
    output the graph
    """
    sub = GetSubgraph(FindNode(lower, upper, cos_sim), g)
    whole_graph = snap.TIntStrH()
    whole_graph[sub[0]] = color
    sub = snap.ConvertSubGraph(snap.PUNGraph, g, sub)
    snap.DrawGViz(sub, snap.gvlNeato, filename, title, True, whole_graph)
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]
Пример #5
0
    if net.GetIntAttrDatN(nid, "state") == 0:
        NIdColorH[nid] = "green"
    elif net.GetIntAttrDatN(nid, "state") == 1:
        NIdColorH[nid] = "red"
    else:
        print "Error! There is infectious node in the network!"

#plotting the population over time
plt.plot(echo, susceptible, color='g')
plt.plot(echo, infectious, color='r')
plt.xlabel('Echos')
plt.ylabel('Nodes')
plt.title('SIS simulation over %d echos' % MAX_ECHO)
plt.savefig("SIS_%dtl_%dpb_%decho.png" %
            (tl, contagion_probability_base, MAX_ECHO))

# Save the net in .dot file (plot with Gephi), snap.DrawGViz() too slow on big graph
snap.SaveGViz(net, "SISnet.dot", "SIR simulation network", True, NIdColorH)

#generate subgraph
V = snap.TIntV()
sub_node = []
for i in initial_list:
    for connectedNode in net.GetNI(i).GetOutEdges():
        sub_node = list(set(sub_node) | set([connectedNode]))
    sub_node = list(set(sub_node) | set([i]))
for nid in sub_node:
    V.Add(nid)
sub = snap.ConvertSubGraph(snap.PNEANet, net, V)
#Save subgraph
snap.SaveGViz(sub, "SISsub.dot", "SIR simulation subnet", True, NIdColorH)
Пример #6
0
def get_subgraph(graph, nodes=front_20_nodes):
    sg = snap.ConvertSubGraph(snap.PUNGraph, graph, front_20_nodes)
Пример #7
0
def algorithm(G, D):
    #Pruning Step
    P = 1
    T = 0
    while P == 1:
        P = 0
        for NI in G.Nodes():
            NID = NI.GetId()
            d = NI.GetDeg()
            if d <= D or d > G.GetNodes() - 2:
                if d <= D and d > 1:
                    for i in range(d - 1):
                        for j in range(i + 1, d):
                            a = NI.GetNbrNId(i)
                            b = NI.GetNbrNId(j)
                            if G.IsEdge(a, b):
                                T = T + 1
                if d > D and d > G.GetNodes() - 2:
                    T = T + G.GetEdges() - NI.GetDeg()
                P = 1
                G.DelNode(NID)
#Hierarchical Clustering Step
    if G.GetNodes() > 5:
        H = snap.ConvertGraph(type(G), G)
        S = []
        i = 0
        while H.GetNodes() > 0:
            S.append([])
            S[i].append(snap.GetMxDegNId(H))
            j = 1
            TTT = True
            while TTT:
                s = snap.TIntV()
                snap.GetNodesAtHop(H, S[i][0], j, s, True)
                if len(s) != 0:
                    S[i].append(s)
                    j = j + 1
                else:
                    TTT = False
            H.DelNode(S[i][0])
            for j in range(1, len(S[i])):
                for nodeID in S[i][j]:
                    H.DelNode(nodeID)
            i = i + 1
        subgraphs = [[] for x in range(len(S))]
        #Counting Step
        for i in range(len(S)):
            for j in range(1, len(S[i])):
                G01 = snap.ConvertSubGraph(snap.PUNGraph, G, S[i][j])
                subgraphs[i].append(G01)
            T = T + subgraphs[i][0].GetEdges()
            G.DelNode(S[i][0])
        for i in range(len(S)):
            for j in range(1, len(S[i])):
                for upnodeID in S[i][j]:
                    U = []
                    D = []
                    for t in range(G.GetNI(upnodeID).GetDeg()):
                        a = G.GetNI(upnodeID).GetNbrNId(t)
                        if j < len(S[i]) - 1:
                            if subgraphs[i][j].IsNode(a):
                                U.append(a)
                        if j > 1:
                            if subgraphs[i][j - 2].IsNode(a):
                                D.append(a)
                    for s in range(len(U)):
                        for t in range(s + 1, len(U)):
                            if subgraphs[i][j].IsEdge(U[s], U[t]):
                                T = T + 1
                    for s in range(len(D)):
                        for t in range(s + 1, len(D)):
                            if subgraphs[i][j - 2].IsEdge(D[s], D[t]):
                                T = T + 1
        for i in range(len(S)):
            for j in range(len(S[i]) - 1):
                T = T + algorithm(subgraphs[i][j], D)

    return T
Пример #8
0
def algorithm(G, M):
    #Pruning Step
    P = 1
    Count = 0
    while P == 1:
        P = 0
        for node in G.Nodes():
            nodeID = node.GetId()
            degree = node.GetDeg()
            if degree <= M or degree > G.GetNodes() - 2:
                if degree <= M and degree > 1:
                    for i in range(degree - 1):
                        for j in range(i + 1, degree):
                            a = node.GetNbrNId(i)
                            b = node.GetNbrNId(j)
                            if G.IsEdge(a, b):
                                Count = Count + 1
                if degree > M and degree > G.GetNodes() - 2:
                    Count = Count + G.GetEdges() - node.GetDeg()
                P = 1
                G.DelNode(nodeID)

    #Hierarchical Clustering Step
    if G.GetNodes() > 5:
        H = snap.ConvertGraph(type(G), G)
        S = []
        i = 0
        while H.GetNodes() > 0:
            S.append([])
            # randomly chosen a node with maximum degree
            S[i].append(snap.GetMxDegNId(H))
            j = 1
            HC = True

            while HC:
                # create an empty vector of integers
                s = snap.TIntV()
                # (Graph, StartNId, Hop: distance, NIdV: store nodes, IsDir: directed?)
                snap.GetNodesAtHop(H, S[i][0], j, s, True)
                if len(s) != 0:
                    S[i].append(s)
                    j = j + 1
                else:
                    HC = False

            H.DelNode(S[i][0])
            for j in range(1, len(S[i])):
                for nodeID in S[i][j]:
                    H.DelNode(nodeID)
            i = i + 1

        subgraphs = [[] for x in range(len(S))]

        #Counting Step
        for i in range(len(S)):
            for j in range(1, len(S[i])):
                G01 = snap.ConvertSubGraph(snap.PUNGraph, G, S[i][j])
                subgraphs[i].append(G01)
            Count = Count + subgraphs[i][0].GetEdges()
            G.DelNode(S[i][0])

        for i in range(len(S)):
            for j in range(1, len(S[i])):

                for nodeID in S[i][j]:
                    U = []
                    M = []
                    for t in range(G.GetNI(nodeID).GetDeg()):
                        a = G.GetNI(nodeID).GetNbrNId(t)
                        #check lower level
                        if j < len(S[i]) - 1:
                            if subgraphs[i][j].IsNode(a):
                                U.append(a)
                        #check upper level
                        if j > 1:
                            if subgraphs[i][j - 2].IsNode(a):
                                M.append(a)

                    for s in range(len(U)):
                        for t in range(s + 1, len(U)):
                            if subgraphs[i][j].IsEdge(U[s], U[t]):
                                Count = Count + 1

                    for s in range(len(M)):
                        for t in range(s + 1, len(M)):
                            if subgraphs[i][j - 2].IsEdge(M[s], M[t]):
                                Count = Count + 1

        for i in range(len(S)):
            for j in range(len(S[i]) - 1):
                Count = Count + algorithm(subgraphs[i][j], M)

    return Count
    def algorithm(self, G, threshold, clique_size):
        #Pruning Step
        P = 1
        T = 0
        all_cliques = []
        while P == 1:
            P = 0
            for NI in G.Nodes():
                NID = NI.GetId()
                d = NI.GetDeg()

                edgeList = {}
                if d <= threshold and d >= clique_size - 1:
                    neighbours = []
                    for i in range(d - 1):
                        for j in range(i + 1, d):
                            a = NI.GetNbrNId(i)
                            b = NI.GetNbrNId(j)
                            if G.IsEdge(a, b):
                                if a not in edgeList:
                                    edgeList[a] = [b]
                                else:
                                    edgeList[a].append(b)

                    for node in edgeList:
                        neighbours = edgeList[node]
                        if len(neighbours) == 1:
                            cliqueList = [NID, node, neighbours[0]]
                            if len(cliqueList) >= clique_size:
                                all_cliques = self.ensureNoOverlap(
                                    all_cliques, cliqueList)
                        elif len(neighbours) > 1:
                            for i in range(len(neighbours) - 1):
                                cliqueList = [NID, node, neighbours[i]]
                                for j in range(i + 1, len(neighbours)):
                                    if G.IsEdge(neighbours[i], neighbours[j]):
                                        cliqueList.append(neighbours[j])
                                if len(cliqueList) >= clique_size:
                                    all_cliques = self.ensureNoOverlap(
                                        all_cliques, cliqueList)

                if d <= threshold or d < clique_size - 1:
                    P = 1
                    G.DelNode(NID)

        for q in all_cliques:
            if len(q) == clique_size:
                print q, " Pruning"
                self.allCliques.append(q)
                T = T + 1

        #Hierarchical Clustering Step
        if G.GetNodes() >= clique_size:
            H = snap.ConvertGraph(type(G), G)
            S = []
            i = 0
            while H.GetNodes() > 0:
                S.append([])
                # randomly chosen a node with maximum degree
                S[i].append(snap.GetMxDegNId(H))
                j = 1
                TTT = True
                while TTT:
                    # create an empty vector of integers
                    s = snap.TIntV()
                    # (Graph, StartNId, Hop: distance, NIdV: store nodes, IsDir: directed?)
                    snap.GetNodesAtHop(H, S[i][0], j, s, True)
                    if len(s) != 0:
                        S[i].append(s)
                        j = j + 1
                    else:
                        TTT = False
                H.DelNode(S[i][0])
                for j in range(1, len(S[i])):
                    for nodeID in S[i][j]:
                        H.DelNode(nodeID)
                i = i + 1
            subgraphs = [[] for x in range(len(S))]

            #Counting Step
            for i in range(len(S)):
                for j in range(1, len(S[i])):
                    G01 = snap.ConvertSubGraph(snap.PUNGraph, G, S[i][j])
                    subgraphs[i].append(G01)

            for i in range(len(S)):
                C1 = snap.TIntV()
                C1.Add(S[i][0])
                for x in S[i][1]:
                    C1.Add(x)
                C01 = snap.ConvertSubGraph(snap.PUNGraph, G, C1)
                all_cliques = []
                for NI in C01.Nodes():
                    NID = NI.GetId()
                    d = NI.GetDeg()
                    edgeList = {}
                    for d1 in range(d - 1):
                        for d2 in range(d1 + 1, d):
                            a = NI.GetNbrNId(d1)
                            b = NI.GetNbrNId(d2)
                            if C01.IsEdge(a, b):
                                if a not in edgeList:
                                    edgeList[a] = [b]
                                else:
                                    edgeList[a].append(b)
                    for node in edgeList:
                        neighbours = edgeList[node]
                        if len(neighbours) == 1:
                            cliqueList = [NID, node, neighbours[0]]
                            if len(cliqueList) >= clique_size:
                                all_cliques = self.ensureNoOverlap(
                                    all_cliques, cliqueList)
                        elif len(neighbours) > 1:
                            for d1 in range(len(neighbours) - 1):
                                cliqueList = [NID, node, neighbours[d1]]
                                for d2 in range(d1 + 1, len(neighbours)):
                                    if G.IsEdge(neighbours[d1],
                                                neighbours[d2]):
                                        cliqueList.append(neighbours[d2])
                                if len(cliqueList) >= clique_size:
                                    all_cliques = self.ensureNoOverlap(
                                        all_cliques, cliqueList)

                for q in all_cliques:
                    if len(q) == clique_size:
                        print q, " C1"
                        self.allCliques.append(q)
                        T = T + 1
                G.DelNode(S[i][0])

            for i in range(len(S)):
                for j in range(1, len(S[i])):
                    for upnodeID in S[i][j]:
                        U = []
                        L = []
                        for t in range(G.GetNI(upnodeID).GetDeg()):
                            a = G.GetNI(upnodeID).GetNbrNId(t)
                            #check lower level
                            if j < len(S[i]) - 1:
                                if subgraphs[i][j].IsNode(a):
                                    U.append(a)
                            #check upper level
                            if j > 1:
                                if subgraphs[i][j - 2].IsNode(a):
                                    L.append(a)

                        edgeList = {}
                        for s in range(len(U)):
                            for t in range(s + 1, len(U)):
                                if subgraphs[i][j].IsEdge(U[s], U[t]):
                                    if U[s] not in edgeList:
                                        edgeList[U[s]] = [U[t]]
                                    else:
                                        edgeList[U[s]].append(U[t])

                        for s in range(len(L)):
                            for t in range(s + 1, len(L)):
                                if subgraphs[i][j - 2].IsEdge(L[s], L[t]):
                                    if L[s] not in edgeList:
                                        edgeList[L[s]] = [L[t]]
                                    else:
                                        edgeList[L[s]].append(L[t])

                        all_cliques = []
                        for node in edgeList:
                            neighbours = edgeList[node]
                            if len(neighbours) == 1:
                                cliqueList = [upnodeID, node, neighbours[0]]
                                if len(cliqueList) >= clique_size:
                                    all_cliques = self.ensureNoOverlap(
                                        all_cliques, cliqueList)
                            elif len(neighbours) > 1:
                                for d1 in range(len(neighbours) - 1):
                                    cliqueList = [
                                        upnodeID, node, neighbours[d1]
                                    ]
                                    for d2 in range(d1 + 1, len(neighbours)):
                                        if subgraphs[i][j].IsEdge(
                                                neighbours[d1],
                                                neighbours[d2]):
                                            cliqueList.append(neighbours[d2])
                                        elif subgraphs[i][j - 2].IsEdge(
                                                neighbours[d1],
                                                neighbours[d2]):
                                            cliqueList.append(neighbours[d2])
                                    if len(cliqueList) >= clique_size:
                                        all_cliques = self.ensureNoOverlap(
                                            all_cliques, cliqueList)

                        for q in all_cliques:
                            if len(q) == clique_size:
                                print q, " C2"
                                self.allCliques.append(q)
                                T = T + 1

        return T