Example #1
0
def createRandomDAGIter(n):
    newGraph = DirectedGraph()
    # Create n nodes
    for k in range(0, n):
        newGraph.addNode(k)

    # Assign each node 2 random vertices (might assign 0, 1 or 2 vertices)
    for node in newGraph.vertices:
        randomVertices = set()
        # Generate 2 random numbers which will represent direction up and right
        neighborNodeIndex1 = random.randint(node.val,
                                            len(newGraph.vertices) - 1)
        neighborNodeIndex2 = random.randint(node.val,
                                            len(newGraph.vertices) - 1)
        # If number generated is not being used yet and it doesn't represent node.val
        if node.val != neighborNodeIndex1 and neighborNodeIndex1 not in randomVertices:
            newGraph.addDirectedEdge(newGraph.vertices[node.val],
                                     newGraph.vertices[neighborNodeIndex1])
            randomVertices.add(neighborNodeIndex1)
        # If second number generated is not being used yet and it doesn't represent node.val
        if node.val != neighborNodeIndex2 and neighborNodeIndex2 not in randomVertices:
            newGraph.addDirectedEdge(newGraph.vertices[node.val],
                                     newGraph.vertices[neighborNodeIndex2])
            randomVertices.add(neighborNodeIndex2)

    return newGraph
Example #2
0
def main():
    keyString = f.readline().rstrip()
    defragmenter = Defragmenter(keyString)
    grid = defragmenter.getGrid()
    expandedGrid = [list(row) for row in grid]

    graph = DirectedGraph()
    for rowIdx, row in enumerate(expandedGrid):
        for columnIdx, column in enumerate(row):
            if column == '1':
                coordinate = (rowIdx, columnIdx)
                graph.addNode(coordinate)

                if (rowIdx > 0 and expandedGrid[rowIdx - 1][columnIdx] == '1'):
                    graph.addEdge(coordinate, (rowIdx - 1, columnIdx))
                if (columnIdx + 1 < 128
                        and expandedGrid[rowIdx][columnIdx + 1] == '1'):
                    graph.addEdge(coordinate, (rowIdx, columnIdx + 1))
                if (rowIdx + 1 < 128
                        and expandedGrid[rowIdx + 1][columnIdx] == '1'):
                    graph.addEdge(coordinate, (rowIdx + 1, columnIdx))
                if (columnIdx > 0
                        and expandedGrid[rowIdx][columnIdx - 1] == '1'):
                    graph.addEdge(coordinate, (rowIdx, columnIdx - 1))

    graph.findRegions()
    print(graph.getRegionCount())
Example #3
0
def test_directedGraph():
    defragmenter = Defragmenter('flqrgnkx')
    grid = defragmenter.getGrid()
    expandedGrid = [list(row) for row in grid]

    graph = DirectedGraph()
    for rowIdx, row in enumerate(expandedGrid):
        for columnIdx, column in enumerate(row):
            if column == '1':
                coordinate = (rowIdx, columnIdx)
                graph.addNode(coordinate)

                if (rowIdx > 0 and expandedGrid[rowIdx - 1][columnIdx] == '1'):
                    graph.addEdge(coordinate, (rowIdx - 1, columnIdx))
                if (columnIdx + 1 < 128
                        and expandedGrid[rowIdx][columnIdx + 1] == '1'):
                    graph.addEdge(coordinate, (rowIdx, columnIdx + 1))
                if (rowIdx + 1 < 128
                        and expandedGrid[rowIdx + 1][columnIdx] == '1'):
                    graph.addEdge(coordinate, (rowIdx + 1, columnIdx))
                if (columnIdx > 0
                        and expandedGrid[rowIdx][columnIdx - 1] == '1'):
                    graph.addEdge(coordinate, (rowIdx, columnIdx - 1))

    graph.findRegions()
    assert graph.getRegionCount() == 1242
Example #4
0
def createRandomDAGIter(n: int) -> DirectedGraph:
    graph = DirectedGraph()
    for i in range(n):
        graph.addNode(i)
    for node in graph.nodes:
        for _node in graph.nodes:
            if node is not _node and bool(getrandbits(1)):
                if not hasCycle(graph, node, _node):
                    graph.addDirectedEdge(node, _node)
    return graph
Example #5
0
    def createRandomDAGIter(n):
        graph = DirectedGraph()
        nums = list(range(n))
        for num in nums:
            graph.addNode(num)

        for node in graph.getAllNodes():
            numNodes = random.randint(0, n - node)
            numsAdded = set()
            while len(numsAdded) != numNodes:
                num = random.randint(node + 1, n)
                if num not in numsAdded:
                    numsAdded.add(num)
                    graph.addDirectedEdge(node, num)

        return graph
Example #6
0
    def createRandomDAGIter(n):
        graph = DirectedGraph()
        i=1
        nodeVal=1
        graph.addNode(1)
        while nodeVal<=n:
            if nodeVal+1<=n:
                graph.addNode(nodeVal+1)
            if nodeVal+2<=n:
                graph.addNode(nodeVal+2)
            if nodeVal+3<=n:
                graph.addNode(nodeVal+3)
            for l in range(3):
                nodeVal = nodeVal + 1
                if nodeVal<=n:
                    graph.addDirectedEdge(i,nodeVal)

            i=i+1
        return graph