|
| 1 | +# Deque in Python |
| 2 | + |
| 3 | +## Definition |
| 4 | +A deque, short for double-ended queue, is an ordered collection of items that allows rapid insertion and deletion at both ends. |
| 5 | + |
| 6 | +## Syntax |
| 7 | +In Python, deques are implemented in the collections module: |
| 8 | + |
| 9 | +```py |
| 10 | +from collections import deque |
| 11 | + |
| 12 | +# Creating a deque |
| 13 | +d = deque(iterable) # Create deque from iterable (optional) |
| 14 | +``` |
| 15 | + |
| 16 | +## Operations |
| 17 | +1. **Appending Elements**: |
| 18 | + |
| 19 | + - append(x): Adds element x to the right end of the deque. |
| 20 | + - appendleft(x): Adds element x to the left end of the deque. |
| 21 | + |
| 22 | + ### Program |
| 23 | + ```py |
| 24 | + from collections import deque |
| 25 | + |
| 26 | + # Initialize a deque |
| 27 | + d = deque([1, 2, 3, 4, 5]) |
| 28 | + print("Initial deque:", d) |
| 29 | + |
| 30 | + # Append elements |
| 31 | + d.append(6) |
| 32 | + print("After append(6):", d) |
| 33 | + |
| 34 | + # Append left |
| 35 | + d.appendleft(0) |
| 36 | + print("After appendleft(0):", d) |
| 37 | + |
| 38 | + ``` |
| 39 | + ### Output |
| 40 | + ```py |
| 41 | + Initial deque: deque([1, 2, 3, 4, 5]) |
| 42 | + After append(6): deque([1, 2, 3, 4, 5, 6]) |
| 43 | + After appendleft(0): deque([0, 1, 2, 3, 4, 5, 6]) |
| 44 | + ``` |
| 45 | + |
| 46 | +2. **Removing Elements**: |
| 47 | + |
| 48 | + - pop(): Removes and returns the rightmost element. |
| 49 | + - popleft(): Removes and returns the leftmost element. |
| 50 | + |
| 51 | + ### Program |
| 52 | + ```py |
| 53 | + from collections import deque |
| 54 | + |
| 55 | + # Initialize a deque |
| 56 | + d = deque([1, 2, 3, 4, 5]) |
| 57 | + print("Initial deque:", d) |
| 58 | + |
| 59 | + # Pop from the right end |
| 60 | + rightmost = d.pop() |
| 61 | + print("Popped from right end:", rightmost) |
| 62 | + print("Deque after pop():", d) |
| 63 | + |
| 64 | + # Pop from the left end |
| 65 | + leftmost = d.popleft() |
| 66 | + print("Popped from left end:", leftmost) |
| 67 | + print("Deque after popleft():", d) |
| 68 | + |
| 69 | + ``` |
| 70 | + |
| 71 | + ### Output |
| 72 | + ```py |
| 73 | + Initial deque: deque([1, 2, 3, 4, 5]) |
| 74 | + Popped from right end: 5 |
| 75 | + Deque after pop(): deque([1, 2, 3, 4]) |
| 76 | + Popped from left end: 1 |
| 77 | + Deque after popleft(): deque([2, 3, 4]) |
| 78 | + ``` |
| 79 | + |
| 80 | +3. **Accessing Elements**: |
| 81 | + |
| 82 | + - deque[index]: Accesses element at index. |
| 83 | + |
| 84 | + ### Program |
| 85 | + ```py |
| 86 | + from collections import deque |
| 87 | + |
| 88 | + # Initialize a deque |
| 89 | + d = deque([1, 2, 3, 4, 5]) |
| 90 | + print("Initial deque:", d) |
| 91 | + |
| 92 | + # Accessing elements |
| 93 | + print("Element at index 2:", d[2]) |
| 94 | + |
| 95 | + ``` |
| 96 | + |
| 97 | + ### Output |
| 98 | + ```py |
| 99 | + Initial deque: deque([1, 2, 3, 4, 5]) |
| 100 | + Element at index 2: 3 |
| 101 | + |
| 102 | + ``` |
| 103 | + |
| 104 | +4. **Other Operations**: |
| 105 | + |
| 106 | + - extend(iterable): Extends deque by appending elements from iterable. |
| 107 | + - extendleft(iterable): Extends deque by appending elements from iterable to the left. |
| 108 | + - rotate(n): Rotates deque n steps to the right (negative n rotates left). |
| 109 | + |
| 110 | + ### Program |
| 111 | + ```py |
| 112 | + from collections import deque |
| 113 | + |
| 114 | + # Initialize a deque |
| 115 | + d = deque([1, 2, 3, 4, 5]) |
| 116 | + print("Initial deque:", d) |
| 117 | + |
| 118 | + # Extend deque |
| 119 | + d.extend([6, 7, 8]) |
| 120 | + print("After extend([6, 7, 8]):", d) |
| 121 | + |
| 122 | + # Extend left |
| 123 | + d.extendleft([-1, 0]) |
| 124 | + print("After extendleft([-1, 0]):", d) |
| 125 | + |
| 126 | + # Rotate deque |
| 127 | + d.rotate(2) |
| 128 | + print("After rotate(2):", d) |
| 129 | + |
| 130 | + # Rotate left |
| 131 | + d.rotate(-3) |
| 132 | + print("After rotate(-3):", d) |
| 133 | + |
| 134 | + ``` |
| 135 | + |
| 136 | + ### Output |
| 137 | + ```py |
| 138 | + Initial deque: deque([1, 2, 3, 4, 5]) |
| 139 | + After extend([6, 7, 8]): deque([1, 2, 3, 4, 5, 6, 7, 8]) |
| 140 | + After extendleft([-1, 0]): deque([0, -1, 1, 2, 3, 4, 5, 6, 7, 8]) |
| 141 | + After rotate(2): deque([7, 8, 0, -1, 1, 2, 3, 4, 5, 6]) |
| 142 | + After rotate(-3): deque([1, 2, 3, 4, 5, 6, 7, 8, 0, -1]) |
| 143 | + |
| 144 | + ``` |
| 145 | + |
| 146 | + |
| 147 | +## Example |
| 148 | + |
| 149 | +### 1. Finding Maximum in Sliding Window |
| 150 | +```py |
| 151 | +from collections import deque |
| 152 | + |
| 153 | +def max_sliding_window(nums, k): |
| 154 | + if not nums: |
| 155 | + return [] |
| 156 | + |
| 157 | + d = deque() |
| 158 | + result = [] |
| 159 | + |
| 160 | + for i, num in enumerate(nums): |
| 161 | + # Remove elements from deque that are out of the current window |
| 162 | + if d and d[0] <= i - k: |
| 163 | + d.popleft() |
| 164 | + |
| 165 | + # Remove elements from deque smaller than the current element |
| 166 | + while d and nums[d[-1]] <= num: |
| 167 | + d.pop() |
| 168 | + |
| 169 | + d.append(i) |
| 170 | + |
| 171 | + # Add maximum for current window |
| 172 | + if i >= k - 1: |
| 173 | + result.append(nums[d[0]]) |
| 174 | + |
| 175 | + return result |
| 176 | + |
| 177 | +# Example usage: |
| 178 | +nums = [1, 3, -1, -3, 5, 3, 6, 7] |
| 179 | +k = 3 |
| 180 | +print("Maximums in sliding window of size", k, "are:", max_sliding_window(nums, k)) |
| 181 | + |
| 182 | +``` |
| 183 | + |
| 184 | +Output |
| 185 | +```py |
| 186 | +Maximums in sliding window of size 3 are: [3, 3, 5, 5, 6, 7] |
| 187 | +``` |
| 188 | + |
| 189 | + |
| 190 | +## Applications |
| 191 | +- **Efficient Queues and Stacks**: Deques allow fast O(1) append and pop operations from both ends, |
| 192 | +making them ideal for implementing queues and stacks. |
| 193 | +- **Sliding Window Maximum/Minimum**: Used in algorithms that require efficient windowed |
| 194 | +computations. |
| 195 | + |
| 196 | + |
| 197 | +## Advantages |
| 198 | +- Efficiency: O(1) time complexity for append and pop operations from both ends. |
| 199 | +- Versatility: Can function both as a queue and as a stack. |
| 200 | +- Flexible: Supports rotation and slicing operations efficiently. |
| 201 | + |
| 202 | + |
| 203 | +## Disadvantages |
| 204 | +- Memory Usage: Requires more memory compared to simple lists due to overhead in managing linked |
| 205 | +nodes. |
| 206 | + |
| 207 | +## Conclusion |
| 208 | +- Deques in Python, provided by the collections.deque module, offer efficient double-ended queue |
| 209 | +operations with O(1) time complexity for append and pop operations on both ends. They are versatile |
| 210 | +data structures suitable for implementing queues, stacks, and more complex algorithms requiring |
| 211 | +efficient manipulation of elements at both ends. |
| 212 | + |
| 213 | +- While deques excel in scenarios requiring fast append and pop operations from either end, they do |
| 214 | +consume more memory compared to simple lists due to their implementation using doubly-linked lists. |
| 215 | +However, their flexibility and efficiency make them invaluable for various programming tasks and |
| 216 | +algorithmic solutions. |
0 commit comments