def test_components(self): g = DirectedGraph(3) g.connect(2, 1) g.connect(1, 3) g.connect(3, 1) comps = g.components() assert comps[1] == comps[3] assert comps[2] != comps[1] assert comps[2] != comps[3]
def test_component_list(self): g = DirectedGraph(3) g.connect(2, 1) g.connect(1, 3) g.connect(3, 1) comps = g.component_list() assert [1, 3] in comps.values() assert [2] in comps.values() assert [2, 1] not in comps.values()
def test_connect(self): g = DirectedGraph(8) n1 = 1 n2 = 2 g.connect(n1, n2) assert g.is_connected(n1, n2) assert not g.is_connected(n2, n1) g.disconnect(n1, n2) assert not g.is_connected(n1, n2) g.connect(n1, n2) g.connect(n2, n1) assert g.is_connected(n1, n2) assert g.is_connected(n2, n1)
def test_is_connected_graph(self): g = DirectedGraph(3) g.connect(1, 2) g.connect(1, 3) g.connect(2, 3) assert not g.is_connected_graph() g.connect(3, 1) assert g.is_connected_graph()
def get_random_flow_network(N: int) -> DirectedGraph: """ Losowa sieć przepływu N - liczba warstw sieci źródło - Node #1 ujście - Node #len(graph) """ assert N >= 2 print(f"liczba warstw: {N}") # krok 1: tworzenie warstw node_count_in_layer = [1] + [random.randint(2, N) for _ in range(N)] + [1] node_layer = [None] layer_nodes = [] total = 1 for i, count in enumerate(node_count_in_layer): node_layer.extend([i] * count) layer_nodes.append(list(range(total, total + count))) total += count print(f"liczba wierzchołków w warstwie: {node_count_in_layer}") print(f"wierzchołki w warstwie: {layer_nodes}") # krok 2: losowanie krawędzi między warstwami g = DirectedGraph(size=sum(node_count_in_layer)) for i in range(1, N + 1): for node in layer_nodes[i]: # losowa krawędź wchodząca g.connect(random.choice(layer_nodes[i - 1]), node) # losowa krawędź wychodząca g.connect(node, random.choice(layer_nodes[i + 1])) # krok 3: odajemy 2N losowych łuków edges_added = 0 while edges_added < 2 * N: # brak krawędzi wychodzącej z ujścia n1 = random.randint(1, len(g) - 1) # brak krawędzi wchodzącej do źródła n2 = random.randint(2, len(g)) if n1 == n2 or g.is_connected(n1, n2) or g.is_connected(n2, n1): continue g.connect(n1, n2) edges_added += 1 # krok 4: przypisanie każdej krawędzi losowej przepustowości g.assign_random_weights() return g
def get_example(): exg = DirectedGraph(11) s = 1 a = 2 b = 3 c = 4 d = 5 e = 6 f = 7 g = 8 h = 9 i = 10 t = 11 exg.connect(s, a, 10) exg.connect(s, b, 3) exg.connect(s, c, 6) exg.connect(a, b, 8) exg.connect(a, d, 8) exg.connect(a, e, 6) exg.connect(b, e, 2) exg.connect(b, f, 10) exg.connect(c, d, 9) exg.connect(c, f, 1) exg.connect(d, h, 5) exg.connect(e, d, 1) exg.connect(e, i, 7) exg.connect(f, g, 9) exg.connect(g, t, 7) exg.connect(h, t, 5) exg.connect(i, t, 7) return exg