-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.SchellingModelInternalAndExternalNodes.py
More file actions
119 lines (65 loc) · 2.61 KB
/
Copy path2.SchellingModelInternalAndExternalNodes.py
File metadata and controls
119 lines (65 loc) · 2.61 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Assigning People Of Two Types to Nodes
# Assign an attribute for this
# Assign 0,1,2 attribute corresponding to (empty, type-1, type-2)
import networkx as nx
import matplotlib.pyplot as plt
import random
# Size of the grid
N = 10
# Getting grid graph of dimension N*N
G = nx.grid_2d_graph(N,N)
# For proper ordering of nodes in 2-D grid shape
pos = dict( (n,n) for n in G.nodes() )
# for proper label assignment of nodes in grid
labels = dict( ((i,j),i*10+j) for i,j in G.nodes() )
# General Feature Of Graphs
# i here represents columns
# j here represents rows
# display graph function
# list of nodes that will be displayed with particular type of color corresponding to each attribute type
def display_graph(G):
nodes_g = nx.draw_networkx_nodes(G,pos, node_color='green', nodelist = type1_node_list)
nodes_r = nx.draw_networkx_nodes(G,pos, node_color='red', nodelist = type2_node_list)
nodes_w = nx.draw_networkx_nodes(G,pos, node_color='white', nodelist = empty_cell_list)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos,labels=labels)
plt.show()
# for returning a list of boundary nodesin graph
def get_boundary_nodes(G):
boundary_nodes = []
for (u,v) in G.nodes():
if u==0 or u==N-1 or v==0 or v==N-1:
boundary_nodes.append((u,v))
# print u,v, 'appended'
# check the list of appended nodes
return boundary_nodes
# adding forward and backward diagonal edges in the grid
# data = True is for extracting the value of attribute in later cases
for ((u,v),d) in G.nodes(data=True):
if(u+1<=N-1) and (v+1<=N-1):
G.add_edge((u,v),(u+1,v+1))
for ((u,v),d) in G.nodes(data=True):
if (u+1 <=N-1) and (v-1>=0):
G.add_edge((u,v),(u+1,v-1))
#plotting the graph
#nx.draw(G,pos,with_labels=False)
#nx.draw_networkx_labels(G, pos, labels = labels)
#plt.show()
# assigning the types randomly
for n in G.nodes():
G.node[n]['type'] = random.randint(0,2)
# getting each list of nodes that correspond to each of the attribute type
empty_cell_list = [n for (n,d) in G.nodes(data=True) if d['type'] == 0]
type1_node_list = [n for (n,d) in G.nodes(data=True) if d['type'] == 1]
type2_node_list = [n for (n,d) in G.nodes(data=True) if d['type'] == 2]
# Checking the respective list of nodes
#print empty_cell_list
#print type1_node_list
#print type2_node_list
# Visualize the graph in two different communities that exist in graph
display_graph(G)
# Calculate the nodes that unsatisfied i.e. their threshold is not reached till now ...
boundary_nodes = get_boundary_nodes(G)
internal_nodes = list(set(G.nodes())-set(boundary_nodes))
print boundary_nodes
print internal_nodes