def schelling_step_avg(self): adopted = [x for x in self.G.nodes if self.G.node[x]['apt_markakis'] > -1] candidates = [x for x in adopted if self.unhappy(x)] add_ctr = 0 rem_ctr = 0 if candidates: tar = np.random.choice(candidates) probs = [val for (_, val) in self.G.degree()] tot = sum([np.e**x for x in probs]) probs = [np.e**x / tot for x in probs] friends = list(set([n for i in self.G[tar] for n in self.G[i]])) friend_probs = [1 / (len(friends)) if x in friends else 0 for x in range(len(probs))] probs = [0.5 * probs[i] + 0.5 * friend_probs[i] for i in range(len(probs))] test_pts = np.random.choice(len(probs), p=probs, replace=False, size=len(probs)) test_pts = [test for test in test_pts if self.G.node[test]['apt_markakis'] > -1] bs = list(bridges(self.G)) for test in test_pts: neighbors = [x for x in self.G[tar] if self.G.node[x]['apt_markakis'] > -1] neighbor_dist = self.euclidian(tar, neighbors) if self.G.has_edge(test, tar) and (test, tar) not in bs and (tar, test) not in bs: neighbor_test = copy.deepcopy(neighbors) neighbor_test.remove(test) if self.euclidian(tar, neighbor_test) / neighbor_dist < self.thresh: self.G.remove_edge(test, tar) bs = list(bridges(self.G)) rem_ctr += 1 elif self.euclidian(tar, neighbors + [test]) / neighbor_dist < self.thresh: self.G.add_edge(test, tar) add_ctr += 1 if not self.unhappy(tar): break
def improved_schelling(self): adopted = [x for x in self.G.nodes if self.G.node[x]['apt_markakis'] > -1] candidates = [x for x in adopted if self.unhappy(x)] add_ctr = 0 rem_ctr = 0 if not candidates: return tar = np.random.choice(candidates) similars = self.similar(tar) sim_vals = [True for _ in similars] difs = self.dif_nodes(tar) dif_vals = [False for _ in difs] test_pts = similars + difs test_vals = sim_vals + dif_vals degrees = [self.G.degree[x] for x in test_pts] degrees = [degrees[i] / 4 if test_vals[i] else degrees[i] for i in range(len(degrees))] degrees = [x/sum(degrees) for x in degrees] indices = np.random.choice(len(degrees), p=degrees, replace=False, size=len(degrees)) bs = list(bridges(self.G)) for idx in indices: if test_vals[idx]: self.G.add_edge(tar, test_pts[idx]) else: if not (test_pts[idx], tar) in bs and not (tar, test_pts[idx]) in bs: self.G.remove_edge(test_pts[idx], tar) bs = list(bridges(self.G)) if not self.unhappy(tar): break
def bridge_components(G): """Finds all bridge-connected components G. Parameters ---------- G : NetworkX undirected graph Returns ------- bridge_components : a generator of 2-edge-connected components See Also -------- :func:`k_edge_subgraphs` : this function is a special case for an undirected graph where k=2. :func:`biconnected_components` : similar to this function, but is defined using 2-node-connectivity instead of 2-edge-connectivity. Raises ------ NetworkXNotImplemented: If the input graph is directed or a multigraph. Notes ----- Bridge-connected components are also known as 2-edge-connected components. Example ------- >>> # The barbell graph with parameter zero has a single bridge >>> G = nx.barbell_graph(5, 0) >>> from networkx.algorithms.connectivity.edge_kcomponents import bridge_components >>> sorted(map(sorted, bridge_components(G))) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]] """ H = G.copy() H.remove_edges_from(bridges(G)) for cc in nx.connected_components(H): yield cc
def getBridges(self, G): return nalgos.bridges(G)