-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection_Algorithm.py
90 lines (73 loc) · 2.24 KB
/
Selection_Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8
import networkx as nx
import math
# In[ ]:
def graph():
import graph
Graph = graph.H
return Graph
H = graph()
from itertools import combinations
def nodes_in_triangle(H, n):
"""
Returns the nodes in a graph `G` that are involved in a triangle relationship with the node `n`.
"""
triangle_nodes = set([n])
# Iterate over all possible triangle relationship combinations
for n1, n2 in combinations(H.neighbors(n), 2):
# Check if n1 and n2 have an edge between them
if H.has_edge(n1, n2):
# Add n1 to triangle_nodes
triangle_nodes.add(n1)
# Add n2 to triangle_nodes
triangle_nodes.add(n2)
return triangle_nodes
#equetion 1:selection constrain
def selection_constrain_of(i):
# degree of N_i:
N_i = subgraph_of(i).number_of_nodes()
# sum(sdeg_j):
sum_sdeg_j = subgraph_of(i).number_of_edges()
# number of triangles: nodes
NT_i = nx.triangles(H,i)
if NT_i > 1:
#equetion 1:selection constrain
TR_i = (N_i-(sum_sdeg_j/2))
return TR_i
#Selection Algorithm
def Selection_Algorithm():
# main part of AL loop
i=0
valid_set = []
for i in H.nodes:
subgraph = subgraph_of(i)
# degree of Ni:
N_i = subgraph.number_of_nodes()
# number of triangles: nodes
NT_i = nx.triangles(H,i)
# Extract the nodes of interest: nodes
nodes = [n for n, d in subgraph.nodes(data=True)]
# Create the set of nodes: nodeset
nodeset = set(nodes)
#equetion 1:selection constrain
if NT_i > 1:
TR_i = selection_constrain_of(i)
i+=1
list1 = i, NT_i
valid_set.append(list1)
return valid_set
# In[ ]:
def sel_subgraphs():
sel_subgraphs = []
for i in H:
sel_subgraph = H.subgraph(nodes_in_triangle(H, i))
NT_i = nx.triangles(H,i)
if NT_i > 1:
row = i
sel_subgraphs.append(row)
return sel_subgraphs
# In[ ]:
#return subgraphs
def subgraph_of(i):
subgraph = H.subgraph(nodes_in_triangle(H, i))
return subgraph