def find_euler_cycle(self, graph: GraphRepresentation) -> list: graph.convert(GraphRepresentationType.ADJACENCY_MATRIX) self.adjacency_matrix = deepcopy(graph.math_repr) self.no_nodes = len(graph.math_repr) self.depth_first_search(0) string_repr = "->".join(str(first) for first in self.result) print(string_repr) return self.result
def randomize_graph(graph: GraphRepresentation, number_of_randomization: int) -> GraphRepresentation: """ randomize_graph method randomizes given graph specific number of times :param graph: GraphRepresentation type graph :param number_of_randomization: Int type number of randomization :return: Randomized graph :rtype: Adjacency Matrix GraphRepresentation """ graph.convert(GraphRepresentationType.ADJACENCY_MATRIX) adjacency_matrix = graph.math_repr number_of_nodes = len(adjacency_matrix) for step in range(number_of_randomization): while True: while True: node_a = randint(0, number_of_nodes - 1) node_b = randint(0, number_of_nodes - 1) if ((adjacency_matrix[node_a][node_b] == adjacency_matrix[node_b][node_a]) and ( adjacency_matrix[node_b][node_a] == 1)) and node_a != node_b: break while True: node_c = randint(0, number_of_nodes - 1) node_d = randint(0, number_of_nodes - 1) if ((adjacency_matrix[node_c][node_d] == adjacency_matrix[node_d][node_c]) and ( adjacency_matrix[node_c][node_d] == 1)) and node_c != node_d: break if (adjacency_matrix[node_a][node_d] == 0) and ( adjacency_matrix[node_b][node_c] == 0) and node_a != node_d and node_b != node_c \ and node_a != node_c: break adjacency_matrix[node_a][node_b] = 0 adjacency_matrix[node_b][node_a] = 0 adjacency_matrix[node_c][node_d] = 0 adjacency_matrix[node_d][node_c] = 0 adjacency_matrix[node_a][node_d] = 1 adjacency_matrix[node_d][node_a] = 1 adjacency_matrix[node_b][node_c] = 1 adjacency_matrix[node_c][node_b] = 1 randomized_graph = GraphRepresentation(GraphRepresentationType.ADJACENCY_MATRIX, adjacency_matrix) return randomized_graph
from core.graph_representation import GraphRepresentationType, GraphRepresentation if __name__ == "__main__": matrix = [[0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]] graph = GraphRepresentation(GraphRepresentationType.ADJACENCY_MATRIX, matrix) graph.convert(GraphRepresentationType.ADJACENCY_LIST) print(graph) graph.convert(GraphRepresentationType.ADJACENCY_MATRIX) print(graph) graph.convert(GraphRepresentationType.INCIDENCE_MATRIX) print(graph)
from algorithms.graph_randomization import randomize_graph from core.graph_representation import GraphRepresentationType, GraphRepresentation if __name__ == "__main__": adjacency_matrix = [[0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]] graph = GraphRepresentation(GraphRepresentationType.ADJACENCY_MATRIX, adjacency_matrix) graph.convert(GraphRepresentationType.ADJACENCY_LIST) graph.display() print(graph) randomized_graph = randomize_graph(graph, 10) randomized_graph.convert(GraphRepresentationType.ADJACENCY_LIST) randomized_graph.display() print(randomized_graph)
def __init__(self, graph: GraphRepresentation): graph.convert(GraphRepresentationType.ADJACENCY_MATRIX) self.adjacency_matrix = graph.math_repr self.number_of_nodes = len(self.adjacency_matrix) self.stack = [-1] * self.number_of_nodes self.stack[0] = 0