def phase1(self): # choose node pairs from (degree) weighted distribution while sum(self.remaining_degree.values()) >= 2 * self.dmax**2: u,v = sorted(random_weighted_sample(self.remaining_degree, 2)) if self.graph.has_edge(u,v): continue if random.random() < self.p(u,v): # accept edge self.graph.add_edge(u,v) self.update_remaining(u,v)
def generate_sf(n, m0, m): """ Create Barabási–Albert graph (working around https://github.com/networkx/networkx/issues/3281) """ G = nx.complete_graph(m0) while len(G) < n: degrees = {node: val for (node, val) in G.degree()} node = len(G) G.add_node(node) targets = random_weighted_sample(degrees, m) G.add_edges_from(zip([node] * m, targets)) return G
def test_random_weighted_sample(): mapping = {'a': 10, 'b': 20} s = random_weighted_sample(mapping, 2) assert_equal(sorted(s), sorted(mapping.keys())) assert_raises(ValueError, random_weighted_sample, mapping, 3)
def test_random_weighted_sample(): mapping = {"a": 10, "b": 20} s = random_weighted_sample(mapping, 2, seed=1) s = random_weighted_sample(mapping, 2) assert sorted(s) == sorted(mapping.keys()) pytest.raises(ValueError, random_weighted_sample, mapping, 3)