Esempio n. 1
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())
Esempio n. 2
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
Esempio n. 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
Esempio n. 4
0
 def __init__(self, start, end):
     self.graph = DirectedGraph()
     self.__activities = []
     self.__sortedActivities = []
     self.__criticalActivities = []
     self.start = start
     self.end = end
     self.graph.addVertex(start)
     self.graph.addVertex(end)
Esempio n. 5
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
Esempio n. 6
0
def createRandomDAGIter(node_count=10):
    self = DirectedGraph()
    # Create the nodes:
    for n in range(node_count):
        self.addNode(n)

    for node in self.nodes:
        connect = node
        while connect is node:  # Keeps going if it tries to connect to itself.
            connect = random.choice(self.nodes)

        self.addDirectedEdge(node.val, connect.val)

    return self
Esempio n. 7
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
Esempio n. 8
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
Esempio n. 9
0
from directedGraph import DirectedGraph
from controller import Controller
from UI import Console

graph = DirectedGraph()
graph.loadFromFile("graph1k.txt")

controller = Controller(graph)
console = Console(controller)

console.run()