Ejemplo n.º 1
0
def DFS2Iterative(
    vertexIndex: int,
    components: Dictionary,
    component_index: int,
    vertexes: VectorArray,
    cache: SingleArray,
):
    currentVertex = vertexes[vertexIndex]
    stack = BasicStack()
    stack.push(Node(None))
    stack.push(Node(vertexIndex))
    cache.add(vertexIndex)

    while True:
        components.insert(component_index, currentVertex.index)
        for u in currentVertex.bindedVertexes:
            if components[u.index] is not None \
                    or cache.getIndexByValue(u.index) is not None:
                continue
            else:
                cache.add(u.index)
                stack.push(Node(u.index))
                components.insert(component_index, u.index)
        stackValue = stack.pop().getItem()
        if stackValue is None:
            return
        currentVertex = vertexes[stackValue]
Ejemplo n.º 2
0
    def get_unique_count(self):
        uniqueVertexes = SingleArray()

        for row in self._array:
            if uniqueVertexes.getIndexByValue(row[0]) is None:
                uniqueVertexes.add(row[0])
            if uniqueVertexes.getIndexByValue(row[1]) is None:
                uniqueVertexes.add(row[1])

        return uniqueVertexes.size()
Ejemplo n.º 3
0
class Vertex:
    def __init__(self, index: int):
        self.index = index
        self.bindedVertexes = SingleArray()
        self.label = float("inf")

    def addBindedVertex(self, vertex):
        self.bindedVertexes.add(vertex)

    def clearBindedIndexes(self):
        self.bindedVertexes = SingleArray()
Ejemplo n.º 4
0
class Vertex:
    def __init__(self, index: int):
        self.index = index
        self.passed = False
        self.bindedVertexes = SingleArray()

    def addBindedVertex(self, vertex):
        self.bindedVertexes.add(vertex)

    def clearBindedIndexes(self):
        self.bindedVertexes = SingleArray()
Ejemplo n.º 5
0
 def get_figures_by_code(self, code: str):
     res = SingleArray()
     for i in range(self._figures.rows_size()):
         row = self._figures.get_row(i)
         for j in range(8):
             figure = row.get(j)
             if figure is None:
                 continue
             if figure.code == code:
                 res.add(figure)
     return res
Ejemplo n.º 6
0
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 test_single_array(self):
     singleArray = SingleArray()
     for i in range(30):
         singleArray.add(i)
     print(singleArray.get(15))
     print(singleArray.size())
     singleArray.insert('test', 15)
     print(singleArray.get(15))
     print(singleArray.size())
     print(singleArray.remove(15))
     print(singleArray.get(15))
     print(singleArray.size())
    def get_table(self):
        res = SingleArray()
        all_count = 0

        for j in range(8):
            for move_key in self._moves:
                if get_col_char(j) in move_key:
                    all_count += self._moves[move_key].size()
                    res.add(self._moves[move_key].join(' '))

        res_all = "\n".join(res.get_all())
        if len(res_all) > 0:
            return str(all_count) + "\n" + res_all
        else:
            return str(all_count)
Ejemplo n.º 9
0
class Vertex:
    def __init__(self, index: int):
        self.index = index
        self.passed = False
        self.bindedVertexes = SingleArray()
        self.incomingVertexes = SingleArray()
        self.color = 'w'
        self.incomingValue = 0
        self.outgoingValue = 0
        self.visited = False

    def addBindedVertex(self, vertex):
        self.bindedVertexes.add(vertex)
        vertex.incomingVertexes.add(self)

    def clearBindedIndexes(self):
        self.bindedVertexes = SingleArray()

    def isVisited(self):
        return self.visited == True
class Vertex:
    def __init__(self, index: int):
        self.index = index
        self.incidentEdges = SingleArray()

    def addIncidentEdge(self, edge):
        self.incidentEdges.add(edge)

    def addIncidentEdges(self, edges):
        for edge in edges:
            self.incidentEdges.add(edge)

    def clearIncidentEdges(self):
        self.incidentEdges = SingleArray()

    def findMinIncidentEdge(self):
        minEdge = None
        for edge in self.incidentEdges:
            if (minEdge is None or edge.weight < minEdge.weight):
                minEdge = edge

        return minEdge
Ejemplo n.º 11
0
class MatrixArray(IArray):
    def __init__(self, rows_count: int = None, vector: int = None):
        self._array = SingleArray()
        self._vector = vector if vector else 10
        self._size = 0
        self._rows_size = 0
        if rows_count is not None:
            for i in range(rows_count):
                self._array.add(VectorArray(self._vector))
                self._rows_size += 1

    def size(self):
        return self._size

    def rows_size(self):
        return self._rows_size

    def __resize_replace(self):
        if self.size() == self._array.size() * self._vector:
            self._array.add(VectorArray(self._vector))
            self._rows_size += 1

    def __resize_insert(self):
        if self._array.size() == 0 or self._array.get(
                self._array.size() - 1).size() == self._vector:
            self._array.add(VectorArray(self._vector))
            self._rows_size += 1

    def __shift_right(self, index: int = None):
        if index is not None:
            stop_row = int(index / self._vector)
            stop_col = int(index % 10) - 1
            single_array_size = self._array.size() - 1
            for i in range(single_array_size, stop_row - 1, -1):
                vector_array = self._array.get(i)
                vector_array_size = vector_array.size()
                col_index = stop_col if i == stop_row else 0
                for j in range(vector_array_size - 1, col_index - 1, -1):
                    element = vector_array.get(j)
                    if j + 1 == vector_array_size:
                        if i == single_array_size - 1:
                            self._array.get(i + 1).insert(element, 0)
                        else:
                            self._array.get(i + 1).replace(element, 0)
                    else:
                        vector_array.replace(element, j + 1)

    def add(self, item):
        self.__resize_insert()
        self._array.get(int(self._size / self._vector)).add(item)
        self._size += 1

    def insert(self, item, index: int):
        self.__resize_insert()
        self.__shift_right(index)
        self._array.get(int(index / self._vector)).replace(
            item,
            int(index % self._vector) - 1)
        self._size += 1

    def replace(self, item, index: int):
        self.__resize_replace()
        self._array.get(int(index / self._vector)).replace(
            item,
            int(index % self._vector) - 1)
        self._size += 1

    def replace_by_indexes(self, item, x: int, y: int):
        self.get_row(x).replace(item, y)
        self._size += 1

    def remove(self, index: int):
        row_index = int(index / self._vector)
        col_index = int(index % self._vector) - 1

        self._array.get(row_index).replace(None, col_index)
        if self._array.get(row_index).size() == 0:
            self._array.remove(row_index)

        self._size -= 1

    def get(self, index: int, default: str = None):
        try:
            return self._array.get(int(
                index / self._vector)).get(int(index % self._vector) - 1)
        except IndexError:
            return default

    def get_row(self, row: int, default: str = None):
        try:
            return self._array.get(row)
        except IndexError:
            return default