예제 #1
0
 def __init__(self, num_buckets=256):
     """Initializes a Map with the given number of buckets."""
     # the map is a DLL
     self.map = DoubleLinkedList()
     # we add a DLL for every bucket so now we have
     # a big DLL containing other tiny DLLs - containers        # for the key -value tuples
     for i in range(0, num_buckets):
         self.map.push(DoubleLinkedList())
예제 #2
0
def find_components(g: Graph):
    """
    Breadth First Search Alg. Which also:
    tests if the graph is connected,
    computes the distance from s to the other edges
    labels the vertices in the order they are visited
    :param g: Graph
    :return: (isConnected, {dict of components})
    """
    visited = set()
    components = dict()
    count = 1

    while len(visited) < len(g.vertices):
        for o in g.vertices:
            if o not in visited:
                v = o
                break
        queue = DoubleLinkedList()  # queue
        queue.append(v)
        visited.add(v)
        components[count] = [v]

        while len(queue) > 0:
            w = queue.pop()  # BFS so FIFO
            for n in w.neighbours:
                # If w not visited
                if n not in visited:
                    visited.add(n)
                    queue.append(n)
                    components[count].append(n)
        count += 1

    is_connected = len(components) == 1
    return is_connected, components
예제 #3
0
	def test_3(self):
		"""add two nodes. test that 2nd append makes changes tail to new node"""
		n1 = Node('A')
		n2 = Node('B')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		self.assertEqual(dl.tail._value, 'B')
예제 #4
0
	def test_4(self):
		"""check that head is still the first node added to list"""
		n1 = Node('A')
		n2 = Node('B')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		self.assertEqual(dl.head._value, 'A')
예제 #5
0
 def test_3(self):
     """ test append by tail """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.append(n1)
     dl.append(n2)
     self.assertEqual(dl.tail._value, 3)
예제 #6
0
 def test_6(self):
     """ prev of prior head is now node 2 (n1 here) """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     self.assertEqual(n1._prev._value, 3)
예제 #7
0
 def test_4(self):
     """ test that tail.next returns none """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.append(n1)
     dl.append(n2)
     self.assertEqual(dl.tail._next, None)
예제 #8
0
 def test_2(self):
     """ test append by head """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.append(n1)
     dl.append(n2)
     self.assertEqual(dl.head._next._value, 3)
예제 #9
0
 def test_5(self):
     """ added node is now head """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     self.assertEqual(dl.head._value, 3)
예제 #10
0
	def test_6(self):
		"""check that pop removes head"""
		n1 = Node('A')
		n2 = Node('B')
		dl = DoubleLinkedList()
		dl.append(n1) #head and tail at this point
		dl.append(n2) # A is head and B is now tail
		dl.pop() # removes A so head value should be B
		self.assertEqual(dl.head._value, 'B') 
예제 #11
0
 def test_7(self):
     """ next of prior head is now head """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     dl.pop()
     self.assertEqual(dl.head._value, 5)
예제 #12
0
 def test_8(self):
     """ prev of prior tail is now tail """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     dl.shift()
     self.assertEqual(dl.tail._value, 3)
예제 #13
0
	def test_5(self):
		"""check that push adds to front"""
		n1 = Node('A')
		n2 = Node('B')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		n3 = Node('C')  #will push C to front(head)
		dl.push(n3)
		self.assertEqual(dl.head._value, 'C')
예제 #14
0
 def test_10(self):
     """ remove by value, return an exception if doesn't exist """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     n3 = Node(6)
     dl.append(n1)
     dl.append(n2)
     dl.append(n3)
     dl.remove(7)
     self.assertEqual(dl.remove(7), "Node does not exist!")
예제 #15
0
	def test_7(self):
		"""check that shift removes last node"""
		n1 = Node('A')
		n2 = Node('B')
		n3 = Node('C')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		dl.append(n3)
		dl.shift()
		self.assertEqual(dl.tail._value, 'B')
예제 #16
0
 def test_9(self):
     """ remove by value, removed node's next now next of removed node's prev """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     n3 = Node(6)
     dl.append(n1)
     dl.append(n2)
     dl.append(n3)
     dl.remove(3)
     self.assertEqual(dl.head._next._value, 6)
예제 #17
0
	def test_8(self):
		"""test to remove tail by using remove method"""
		n1 = Node('A')
		n2 = Node('B')
		n3 = Node('C')
		dl = DoubleLinkedList()
		dl.append(n1)
		dl.append(n2)
		dl.append(n3)
		dl.remove('C')  #this removes C so tail should become BaseException
		self.assertEqual(dl.tail._value, 'B')
		
		
		
		
		
	
		
예제 #18
0
    def set(self, vertex: Vertex, color: int):
        """
        Adds the given vertex to the given color class

        Add the given vertex to the color class. If the color class does not yet exist, a new color class is created to
        which the vertex is added.
        :param color: the name of the color class
        :param vertex: the `Vertex` to add to the color class
        :raises KeyError when vertex already belongs to the coloring
        """

        if vertex in self._vertex_dict:
            raise KeyError('Vertex {} already in coloring, color: {}. '
                           'Use recolor instead'.format(
                               str(vertex), str(self.color(vertex))))

        self._dict.setdefault(color, DoubleLinkedList()).append(vertex)
        self._vertex_dict[vertex] = color
예제 #19
0
 def setUp(self):
     self.dll = DoubleLinkedList()
예제 #20
0
	def test_2(self):
		"""make a test head is set when add first node"""
		n1 = Node('A')
		dl = DoubleLinkedList()
		dl.append(n1)
		self.assertEqual(dl.head._value, 'A')
예제 #21
0
 def __init__(self):
     """Init for Queue."""
     self._new_dll = DoubleLinkedList()
예제 #22
0
 def __init__(self):
     """Initialize the deque."""
     self.deque = DoubleLinkedList()
예제 #23
0
def random_list(count):
    numbers = DoubleLinkedList()
    for i in range(count, 0, -1):
        numbers.shift(randint(0, 10000))
    return numbers
예제 #24
0
 def __init__(self, num_buckets=256):
     """Initializes a Map with the given number of buckets."""
     self.map = DoubleLinkedList()
     for i in range(0, num_buckets):
         self.map.push(DoubleLinkedList())
예제 #25
0
def dll():
    """Instantiate a dll for testing."""
    from dll import DoubleLinkedList
    double_link = DoubleLinkedList()
    return double_link
예제 #26
0
 def __init__(self, iterable=None):
     """Will init a new instance of the Stack class."""
     from dll import DoubleLinkedList
     self._dll = DoubleLinkedList()
     self._counter = 0