def add_s(cls, graph) -> DirectedGraph: graph_copy = graph.copy() s = Node(len(graph.get_nodes())) for i in range(len(graph.get_nodes())): graph_copy.add_weighted_edge(s, Node(i), weight=0) return graph_copy
def graph_generator_consistent_weighted(n: int, w_min: int, w_max: int) -> Graph: vertices = set([v for v in range(n)]) e = set() l_min = n if n == 2: l_min = 1 l = randint(l_min, (n * (n - 1)) / 2) while True: comb = combinations(vertices, 2) random_comb = sample(list(comb), l) for c in random_comb: e.add((Node(c[0]), Node(c[1]), randint(w_min, w_max))) graph = Graph(is_weighted=True) for v in vertices: graph.add_node(Node(v)) graph.add_edges_from(e) if graph.is_consistent(): return graph
def create_graph_from_adjacency_list(cls, input_data: List[List[int]]): edges = set() for index, i in enumerate(input_data): for j in i: edges.add((Node(index), Node(j))) graph = Graph() graph.add_edges_from(edges) return graph
def create_graph_from_adjacency_matrix(cls, input_data: List[List[int]]): edges = set() for index_i, i in enumerate(input_data): for index_j, j in enumerate(i): if j == 1: edges.add((Node(index_i), Node(index_j))) graph = Graph() graph.add_edges_from(edges) return graph
def create_graph_from_coordinates(cls, input_data): edges = list() for i in range(0, len(input_data) - 1): for j in range(i + 1, len(input_data)): w = np.sqrt((input_data[i][0] - input_data[j][0])**2 + (input_data[i][1] - input_data[j][1])**2) edges.append((Node(i), Node(j), w)) graph = Graph() graph.is_weighted = True graph.add_edges_from(edges) return graph
def graph_generator_nl(n: int, l: int) -> Graph: vertices = set([v for v in range(n)]) e = set() comb = combinations(vertices, 2) random_comb = sample(list(comb), l) for c in random_comb: e.add((Node(c[0]), Node(c[1]))) graph = Graph() for v in vertices: graph.add_node(Node(v)) graph.add_edges_from(e) return graph
def graph_generator_np(n: int, p: float) -> Graph: vertices = set([v for v in range(n)]) e = set() for c in combinations(vertices, 2): tmp = random() if tmp < p: e.add((Node(c[0]), Node(c[1]))) graph = Graph() for v in vertices: graph.add_node(Node(v)) graph.add_edges_from(e) return graph
def create_graph_from_incidence_matrix(cls, input_data: List[List[int]]): edges = list() transposed = np.array(input_data).T for i in transposed: first_node = None second_node = None for index_j, j in enumerate(i): if j == -1: first_node = index_j if j == 1: second_node = index_j edges.append((Node(first_node), Node(second_node))) graph = DirectedGraph() graph.add_edges_from(edges) return graph
def create_graph_from_incidence_matrix(cls, input_data: List[List[int]]): edges = set() transposed = np.array(input_data).T for i in transposed: first_node = None second_node = None for index_j, j in enumerate(i): if j == 1: if first_node is None: first_node = index_j else: second_node = index_j break edges.add((Node(first_node), Node(second_node))) graph = Graph() graph.add_edges_from(edges) return graph
def directed_graph_generator_np(n: int, p: float) -> DirectedGraph: vertices = list([v for v in range(n)]) e = list() for c in combinations(vertices, 2): tmp = random() if tmp < p: e.append((Node(c[0]), Node(c[1]))) tmp2 = random() if tmp2 < p: e.append((Node(c[1]), Node(c[0]))) graph = DirectedGraph() for v in vertices: graph.add_node(Node(v)) graph.add_edges_from(e) return graph
def breadth_first_search(cls, graph, s): ps = list([]) ds = list() for v in graph.get_nodes(): ps.append(list([None])) ds.append(math.inf) ds[s] = 0 queue = list() queue.append(Node(s)) while (len(queue)): v = queue.pop(0) neighbours = list([]) for e in graph.get_edges(): if e.get_nodes_ids()[0] == v: if graph.find_edge( e.get_nodes_ids()[0].get_id(), e.get_nodes_ids()[1].get_id()).get_capacity() != 0: neighbours.append(e.get_nodes_ids()[1]) for u in neighbours: if ds[u.get_id()] == math.inf: ds[u.get_id()] = ds[v.get_id()] + 1 ps[u.get_id()] = v queue.append(u) if (ds[-1] == math.inf): return None # print(ps) # print(ds) l = list() layers = graph.get_layers() l.append(Node(layers[-1][0])) l.append(ps[-1]) while not (l[-1] == Node(s)): l.append(ps[l[-1].get_id()]) return l
from graphlib.representations.AdjacencyList import DirectedAdjacencyList np_graph = Generator.directed_graph_generator_np(5, 0.7) np_graph.draw("graph_2.png") print("ilość spójnych składowych:", np_graph.SCCs(np_graph)) DirectedAdjacencyList(np_graph).print() print("------ example ------") # ex_g = DirectedGraph.create_from_file(os.path.dirname(__file__) + "/files/digraph_ex.json", "adjacency_list") ex_g = DirectedGraph.create_from_file( os.path.dirname(__file__) + "/files/example.json", "adjacency_list") DirectedAdjacencyList(ex_g).print() comp3 = ex_g.SCCs(ex_g) print("ilość spójnych składowych:", comp3) print("------ example ------") g = DirectedGraph() g.add_weighted_edge(Node(0), Node(1), weight=-1) g.add_weighted_edge(Node(1), Node(0), weight=4) g.add_weighted_edge(Node(0), Node(2), weight=-4) g.add_weighted_edge(Node(2), Node(1), weight=2) D = Algorithms().johnson(g) for i in range(len(D)): print(D[i])
def flow_network_generator(N: int) -> FlowNetwork: layers = list([]) layers.append(list([0])) counter = 1 for i in range(0, N): layers.append(list()) tmp = randrange(2, N + 1) for j in range(0, tmp): layers[i + 1].append(counter) counter += 1 layers.append(list([counter])) fn = FlowNetwork() for l in layers: fn.add_layer(l) for i in l: fn.add_node(Node(i)) print(fn.layers) e = list() for i in fn.layers[1]: e.append((Node(0), Node(i))) for i in range(1, len(fn.layers) - 2): for j in range(fn.layers[i][0], fn.layers[i][-1] + 1): tmp = randrange(fn.layers[i + 1][0], fn.layers[i + 1][-1] + 1) e.append((Node(j), Node(tmp))) fn.add_edges_from(e) e.clear() for i in range(2, len(fn.layers) - 1): for j in range(fn.layers[i][0], fn.layers[i][-1] + 1): is_connected = False for x in fn.get_edges(): nodes = x.get_nodes_ids() if (nodes[1].get_id() == j): is_connected = True break if not (is_connected): tmp = randrange(fn.layers[i - 1][0], fn.layers[i - 1][-1] + 1) e.append((Node(tmp), Node(j))) for i in fn.layers[-2]: e.append((Node(i), Node(fn.layers[-1][0]))) fn.add_edges_from(e) last_index = fn.layers[len(fn.layers) - 1][0] for i in range(0, 2 * N): while (True): tmp1 = randrange(0, last_index) tmp2 = randrange(1, last_index + 1) if not (tmp1 == tmp2 or fn.is_edge_in_graph(tmp1, tmp2) or fn.is_edge_in_graph(tmp2, tmp1)): fn.add_edge(Node(tmp1), Node(tmp2)) break for i in fn.get_edges(): i.set_capacity(randrange(1, 11)) return fn