def boruvki(graph): if not graph.oriented: sv = isConnected(graph.to_matrix(), graph.oriented) if sv == "Граф не связный": return sv else: return "Граф ориентированный" size = graph.size() edges = [] for v in graph.vertexes: for to in graph.vertexes[v]: edge = [] edge.append(graph.vertexes[v][to][0][0]) edge.append(int(v) - 1) edge.append(int(to) - 1) edges.append(edge) for i in edges: for j in edges: if i[0] == j[0] and i[1] == j[2] and i[2] == j[1] and i != j: edges.remove(j) edges.sort() tree = [] cost = 0 g = [[0]*size for i in range(size)] for i in range(size): tree.append(i) for i in range(len(edges)): a = edges[i][1] b = edges[i][2] l = edges[i][0] if tree[a] != tree[b]: cost += l g[a][b] = l g[b][a] = l old_id = tree[b] new_id = tree[a] for j in range(size): if tree[j] == old_id: tree[j] = new_id gr = Graph.from_matrix(g) gr.oriented = graph.oriented return gr
def binary_operation(a: Graph, b: Graph, op: str) -> Optional[Graph]: if a.size() != b.size(): return None if a.size() < b.size(): a, b = b, a size = a.size() g = [[0 for i in range(size)] for i in range(size)] operation = Operation(op) a = a.to_matrix() b = b.to_matrix() for i in range(size): for j in range(size): if i == j: continue g[i][j] = int(operation(a[i][j], b[i][j])) return Graph.from_matrix(g)
def addition(self): self.textEdit.setText("") matrix = algorithm.additional(self.graph.to_matrix(with_weight=False)) matrix_copy = deepcopy(matrix) for i in range(len(matrix_copy)): matrix[i][i] = 0 is_full = not any(sum(matrix_copy, [])) if is_full: self.textEdit.setText("Граф полный") return g = Graph.from_matrix(matrix) g.oriented = self.graph.oriented g.vertexes_coordinates = deepcopy(self.graph.vertexes_coordinates) self.graph = g self.graphModel.setGraph(g) g.update() self.tabWidget.currentWidget().drawGraph()
def extreme(base1: str, base2: str) -> Optional[Dict[str, Graph]]: base1, base2 = translate_base(base1), translate_base(base2) if not check(base1) or not check(base2): return None if base1 is None or base2 is None: return None size, size2 = get_max(base1), get_max(base2) if size != size2: return None m1 = get_matrix_from_base(base1, size) m2 = get_matrix_from_base(base2, size) res = { 'add1': Graph.from_matrix(algorithm.additional(m1)), 'add2': Graph.from_matrix(algorithm.additional(m2)), 'union': Graph.from_matrix(algorithm.binary_operation_with_matrix(m1, m2, 'or')), 'and': Graph.from_matrix(algorithm.binary_operation_with_matrix(m1, m2, 'and')), 'aminusb': Graph.from_matrix(algorithm.binary_operation_with_matrix(m1, m2, 'minus')), 'bminusa': Graph.from_matrix(algorithm.binary_operation_with_matrix(m2, m1, 'minus')), } for name, graph in res.items(): print(name) print('Рёбра') degree = {str(i): 0 for i in range(1, graph.size() + 1)} for v_from, to_dict in graph.vertexes.items(): for v_to, to_list in graph.vertexes[v_from].items(): print(f'"{v_from}" -> "{v_to}" ({to_list[0][0]})') degree[v_from] += 1 print('Вектор степеней') print([d for d in degree.values()]) matrix = graph.to_matrix() base = [] if algorithm.complex2_from_vector.check_extreme_2d(matrix): for i in range(len(matrix) // 2): for j in range(i + 1, len(matrix)): if i == j or not matrix[i][j]: continue if (j == len(matrix) - 1 and not matrix[i + 1][j]) \ or (j < len(matrix) - 1 and matrix[i][j] and not matrix[i][j + 1] and not matrix[i + 1][j]): base.append((i + 1, j + 1)) print('База') print(base) print() return res
def prima(matr, oriented): if not oriented: sv = isConnected(matr, oriented) if sv == "Граф не связный": return sv else: return "Граф ориентированный" toVisit = [i for i in range(1, len(matr))] # города кроме начального(0) vizited = [0] result = [0] # начнем с минска size = len(matr) g = [[0]*size for i in range(size)] for index in toVisit: weight, ind1, ind2 = search_min(matr, vizited) g[ind1][ind2] = weight g[ind2][ind1] = weight result.append(weight) # в результат будут заноситься веса vizited.append(ind1) # содержит карту пути gr = Graph.from_matrix(g) gr.oriented = False return gr