def fromEdgesToAdjacencyVector(self, edges: List[Edge], vertexesCount: int): vector = AdjacencyVector(vertexesCount) for edge in edges: vector.bind(edge.v2, edge.v1, edge.weight) return vector
def demukron(vertexIndexes: []): # Создаём из входного массива вектор смежности adjacencyVector = AdjacencyVector(len(vertexIndexes)) # Создаём из вектора смежности массив вершин, связываем вершины друг с другом # vertexes - массив вершин # inputVertexesCountArray - массив степеней захода i-й вершины vertexes, inputVertexesCountArray = makeVertexesArray( vertexIndexes, adjacencyVector) layers = [] while inputVertexesCountArray.has(0): newInputVertexesCountArray = copy.deepcopy(inputVertexesCountArray) layers.append([]) currentlayer = layers[-1] for index, elem in enumerate(inputVertexesCountArray): if elem == 0: currentlayer.append(index) newInputVertexesCountArray.replace(None, index) for vertex in vertexes[index].bindedVertexes: vertex.incomingVertexes.removeByVertexIndex(index) newInputVertexesCountArray.replace( newInputVertexesCountArray[vertex.index] - 1, vertex.index) inputVertexesCountArray = newInputVertexesCountArray return layers
def makeVertexesArray(vertexIndexes: [], adjacencyVector: AdjacencyVector): vertexes = VectorArray(len(vertexIndexes)) for masterVertexIndex, slaveVIndexesArr in enumerate(vertexIndexes): masterVertex = vertexes[masterVertexIndex] if masterVertex is None: masterVertex = Vertex(masterVertexIndex) vertexes.replace(masterVertex, masterVertexIndex) for slaveVertexIndex in slaveVIndexesArr: slaveVertex = vertexes[slaveVertexIndex] if slaveVertex is None: slaveVertex = Vertex(slaveVertexIndex) vertexes.replace(slaveVertex, slaveVertexIndex) masterVertex.addBindedVertex(slaveVertex) adjacencyVector.bind(slaveVertex.index, masterVertex.index) vertexes.clear_unused_memory() return vertexes
def makeAdjacencyVector(vertexIndexes: []): uniqueCount = 0 uniqueIndexes = SingleArray() for slaveVertexes in vertexIndexes: for vertexIndex in slaveVertexes: if not uniqueIndexes.getIndexByValue(vertexIndex) is None: continue uniqueIndexes.add(vertexIndex) uniqueCount += 1 return AdjacencyVector(uniqueCount)
def tarjan(vertexIndexes: []): # Создаём из входного массива вектор смежности adjacencyVector = AdjacencyVector(len(vertexIndexes)) vertexes = makeVertexesArray(vertexIndexes, adjacencyVector) stack = BasicStack() for vertex in vertexes: if vertex.color == 'w': if not DFS(vertex, stack): return False return stack.convertToArray()
def dijkstra(vertexIndexes: [], edges: List[List[Edge]], initialVertexIndex: int = 0): # Создаём из входного массива вектор смежности adjacencyVector = AdjacencyVector(len(vertexIndexes)) vertexes = makeVertexesArray(vertexIndexes, adjacencyVector) labels = {} for rowVector in adjacencyVector.vector: for vertexIndex in rowVector: if vertexIndex is not None: labels[vertexIndex] = float('inf') labels[initialVertexIndex] = 0 result = {} while len(labels) > 0: minLabel = min(labels, key=labels.get) vertex = vertexes[minLabel] label = labels[vertex.index] for edge in edges[minLabel]: if edge.v2Index not in labels: continue pathToV2Weight = label + edge.weight if pathToV2Weight < labels[edge.v2Index]: labels[edge.v2Index] = pathToV2Weight if edge.v2Index not in result: result[edge.v2Index] = {'path': [], 'weight': 0} result[edge.v2Index]['weight'] = pathToV2Weight if vertex.index in result.keys(): result[edge.v2Index]['path'] = deepcopy( result[vertex.index]['path']) result[edge.v2Index]['path'].append(edge) del labels[vertex.index] return result