-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphTools.py
63 lines (50 loc) · 1.87 KB
/
GraphTools.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
import snap
import random
def load_graph_from_txt(txt):
g = snap.LoadEdgeList(snap.PNGraph, txt, 0, 1)
return g
def deferred_decisions_with_uniform_probability(g):
edge_to_remove = []
for n in g.Nodes():
for edge_id in n.GetOutEdges():
edge_probability = random.random()
remove_edge_probability = random.random()
if edge_probability > remove_edge_probability:
edge_to_remove.append(edge_id)
for rm in edge_to_remove:
g.DelEdge(n.GetId(), rm)
edge_to_remove.remove(rm)
return g
def deferred_decisions_with_proportional_to_the_degree(g):
edge_to_remove = []
for n in g.Nodes():
for edge_id in n.GetOutEdges():
edge_probability = 1 / n.GetDeg()
remove_edge_probability = random.random()
if edge_probability > remove_edge_probability:
edge_to_remove.append(edge_id)
for rm in edge_to_remove:
g.DelEdge(n.GetId(), rm)
edge_to_remove.remove(rm)
return g
def constant_threshold_assignment(g, constant_value):
g = snap.ConvertGraph(snap.PNEANet, g)
for n in g.Nodes():
g.AddIntAttrDatN(n.GetId(), constant_value, "threshold")
return g
def proportional_to_the_degree_threshold_assignment(g):
g = snap.ConvertGraph(snap.PNEANet, g)
for n in g.Nodes():
deg = n.GetDeg()
value = 5
if deg > 0:
value = int((1 /(deg + value)) * (g.GetEdges() / g.GetNodes()))
if value == 0:
value = 5
g.AddIntAttrDatN(n.GetId(), value, "threshold")
return g
def random_threshold_assignment(g):
g = snap.ConvertGraph(snap.PNEANet, g)
for n in g.Nodes():
g.AddIntAttrDatN(n.GetId(), random.randint(1, 5), "threshold")
return g