def DFS(vertex: Vertex, stack: BasicStack): vertex.color = 'g' for u in vertex.bindedVertexes: if u.color == 'w': if not DFS(u, stack): return False elif u.color == 'g': return False vertex.color = 'b' stack.push(Node(vertex.index)) return True
def DFSIterative(vertex: Vertex, queue: BasicQueue, cache: VectorArray, invertedVertexes: VectorArray): currentVertex = vertex vertex.passed = True queue.enqueue(vertex.index) stack = BasicStack() stack.push(Node(None)) stack.push(Node(currentVertex.index)) cache.add(vertex.index) while True: for bindedVertex in currentVertex.bindedVertexes: if cache.getIndexByValue(bindedVertex.index) is not None: continue else: cache.add(bindedVertex.index) queue.enqueue(bindedVertex.index) stack.push(Node(bindedVertex.index)) bindedVertex.passed = True stackValue = stack.pop().getItem() if stackValue is None: return currentVertex = invertedVertexes[stackValue]
def generateVertexes(vertexCount): existingEdgesV2Arr = [] vertexes = [Vertex(vIndex) for vIndex in range(vertexCount)] prevVertex = None for vertex in vertexes: if (vertex.index != 0): edge = Edge(vertex, prevVertex, random.randint(1, 100)) vertex.incidentEdges.add(edge) v2 = random.choice([ v for v in vertexes if (v.index != vertex.index and v.index != vertex.index - 1 and v.index not in existingEdgesV2Arr) ]) existingEdgesV2Arr.append(v2.index) edge = Edge(vertex, v2, random.randint(1, 100)) vertex.incidentEdges.add(edge) prevVertex = vertex return vertexes
def DFS1(vertex: Vertex, queue: BasicQueue): vertex.passed = True for bindedVertex in vertex.bindedVertexes: if bindedVertex.passed is False: DFS1(bindedVertex, queue) if not queue.isValueExists(vertex.index): queue.enqueue(vertex.index)
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 makeVertex(index: int, vector: AdjacencyVector): vertex = Vertex(index) for vertexIndex in vector[index]: if vertexIndex is not None: vertex.addBindedVertex(Vertex())