Skip to content

Cedar - Lux#27

Open
k-nox wants to merge 2 commits intoAda-C16:masterfrom
k-nox:master
Open

Cedar - Lux#27
k-nox wants to merge 2 commits intoAda-C16:masterfrom
k-nox:master

Conversation

@k-nox
Copy link

@k-nox k-nox commented Jul 7, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? a heap maintains order and is a specific type of binary tree useful for priority queues
Could you build a heap with linked nodes? you could but it makes more sense to use an array
Why is adding a node to a heap an O(log n) operation? each time you traverse to the next node to check if it belongs there, you reduce the problem by 1/2 because you choose one of two nodes to look at
Were the heap_up & heap_down methods useful? Why? yes because they allow for recursive calls to balance the heap

@anselrognlie anselrognlie self-requested a review July 12, 2022 22:09
Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨💫 Nice job, Lux. I left some comments on your implementation below.

🟢

Comment on lines +22 to +23
Time Complexity: O(log n)
Space Complexity: O(log n) due to recursive stack calls

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Great. You're exactly right that it's due to the recursive call in heap_up that the space complexity is O(log n). If heap_up were implemented iteratively, this would only require O(1) space complexity since the stack size wouldn't depend on the heap depth.

Space Complexity: O(log n) due to recursive stack calls
"""
pass
if self.empty():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice use of your own helper method!

Comment on lines +33 to +34
Time Complexity: O(log n)
Space Complexity: O(log n) due to recursive stack calls

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice. Just as for add, you're right that the log space complexity remove is due to the recursive heap_down implementation. We could achieve O(1) space complexity if we used an iterative approach.

Comment on lines +58 to +59
Time complexity: O(1)
Space complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity: O(1)
"""
pass
return len(self.store) == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that an empty list is falsy

        return not self.store

Comment on lines +71 to +72
Time complexity: O(1)
Space complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 This is where the O(log n) time and space complexity in add comes from. Since heap_up calls itself recursively, the worst case will be when the new value needs to be moved all the way up the heap, which will have a height of log n. So both the time and space complexity (due to the stack growth) are O(log n). If we implemented this instead with an iterative approach, the space complexity would be O(1).

self.swap(parent_index, index)
self.heap_up(parent_index)

def heap_down(self, index):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice set of conditionals to narrow in on where to swap. Notice there's a little duplication since we call swap, heap_down in two places. We could try to fully determine which child we're going to swap with first, and then have a single code flow to swap and re-heapify.

Though not prompted, like heap_up, heap_down is also O(log n) in both time and space complexity. The worst case for re-heapifying is if the new root need to move back down to a leaf, and so the stack growth will be the height of the heap, which is log n. If we implemented this instead with an iterative approach, the space complexity would instead be O(1).

Comment on lines +5 to +6
Time Complexity: O(n log n) where n is the number of items in the list
Space Complexity: O(n) where n is the number of items in the list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the MinHeap does grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.

Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).

Comment on lines +11 to +13
for i in range(len(list)):
list[i] = heap.remove()
return list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input.

Also, in this situation, since we built the heap, we also "know" the number of items in the heap. So it's OK to iterate a fixed number of times. But if we were pulling things out of a heap more generally, we would want to make use of the empty helper as follows:

    result = []
    while not heap.empty():
        result.append(heap.remove())

    return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants