-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Queue Optimized - dequeu is not O(1) #989
Comments
邮件已收到。我会尽快回复!祝您一切顺利!兰雨田
|
The text says that it's O(1) on average, or "O(1) amortized". Same as appending to an array. When it happens it's slow but it only happens rarely, so it doesn't matter. (Unless you need a guarantee that it's "never slower than X", which this doesn't give.) |
"Same as appending to an array" is not correct. Appending element in array in |
Appending an element to a Swift array is also O(1) amortized, which is not the same as O(1). Just to make it clear: this Queue implementation does not mean to be optimal. It just shows one way that dequeuing can be made faster. Using a linked list would give O(1) dequeuing indeed but comes with other trade-offs. |
Can you please tell me what is the other trade-offs if we use |
The advantage of using an array as the backing store for the queue is that this is a contiguous area of memory. That means it's really easy to iterate through the queue (just increment or decrement the index). It also helps with efficient cache access. With a linked list, the nodes are connected with pointers so it's slower to go from one node to the next, plus the nodes could be located anywhere in memory, which is worse for the cache. |
You are right about appending an element to a Swift array is also O(1) amortized. Thanks for that. https://developer.apple.com/documentation/swift/array/3126937-append The trade-off to use Thank you for the discussion. |
Memory + the fact that reference types are always stored in the heap. This means every modification to the linked list (such as appending / removing a node) would require a locking operation on the heap. That has a cost to performance - whereas simple values in a Swift array can sit in the stack - which doesn't require a lock |
I don't think that appending / removing nodes uses any kind of lock, plus there is no guarantee that a Swift array will be stored on the stack. (Allocating new nodes, however, might require a lock.) |
Hmm, I was under the assumption that if the array housed "simple" types (i.e. [Int]), then it could be guaranteed in the stack. |
import Foundation // Node for the linked list
} // Queue implemented using a doubly linked list
} // Example usage let queue = LinkedListQueue() print(queue.dequeue() ?? "Queue is empty") // Outputs: 1 |
Brief Intro
Queue Optimized - dequeue is not O(1)
More Details
If the condition happens, the complexity will be
O(n/4)
~O(n)
I think you should use
LinkedList
to implement aQueue
to archiveO(1)
in all cases ofdequeue
The text was updated successfully, but these errors were encountered: