def build_graph_from_file(path): file = open(path, "r") lines = [line.rstrip('\n') for line in file] file.close() graph_size = int(lines[0]) edges_size = int(lines[1]) edges = lines[2:] if len(edges) != edges_size: print("Podano niewłaściwą liczbę krawędzi - %d" % len(edges)) return graph = Graph(graph_size) for edge in edges: edge = edge.split() if len(edge) != 2: print("\n\nNie utworzono grafu") print("Nieprawidłowa linijka: ", edge) return vertex_from = int(edge[0]) - 1 vertex_to = int(edge[1]) - 1 graph.addEdge(vertex_from, vertex_to) graph.toString() return graph
def buildGraphMatrix(history): graphSize = len(history[0]) historySize = len(history) graph = Graph(graphSize) matrix = graph.getAdjacencyMatrix() degrees_indexes = graph.getAllDegreesWithIndexes() for i in range(1, historySize): connections_amount = history[i][0] print("Ilość połączeń do dodania: ", connections_amount) print("Historia: ", history[i]) graph.addVertex() for j in range(1, connections_amount + 1): searched_vertex_degree = history[i][j] - 1 print("Szukany stopień: ", searched_vertex_degree) # get index of a first vertex of searched degree from a degrees_indexes dictionary vertex_index = list(degrees_indexes.keys())[list(degrees_indexes.values()).index(searched_vertex_degree)] last_vertex_index = graph.getLastVertexIndex() # add an edge between found vertex of a given degree and the last vertex graph.addEdge(vertex_index, last_vertex_index) degrees_indexes = graph.getAllDegreesWithIndexes() print("\nMacierz sąsiedztwa:") for row in matrix: for value in row: print('{0:5}'.format(value), end=' ') print() return graph.getAdjacencyMatrix()
def knightGraph(boardSize): """Create a graph Arguments: boardSize {int} -- table size Returns: Graph -- graph created from provided board size """ ktGraph = Graph() for row in range(boardSize): for col in range(boardSize): nodeId = posToNodeId(row, col, boardSize) moves = legalMoves(row, col, boardSize) for move in moves: posibleNodeId = posToNodeId(move[0], move[1], boardSize) ktGraph.addEdge(nodeId, posibleNodeId) return ktGraph
def buildGraphFromFile(path): file = open(path, "r") lines = [line.rstrip('\n') for line in file] file.close() graph_size = int(lines[0]) edges_size = int(lines[1]) edges = lines[2:] if len(edges) != edges_size: print("Podano niewłaściwą liczbę krawędzi - %d" % len(edges)) return graph = Graph(graph_size) for edge in edges: edge = edge.split() vertex_from = int(edge[0]) vertex_to = int(edge[1]) graph.addEdge(vertex_from, vertex_to) print(graph.getAdjacencyMatrix()) return graph