Skip to content
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

change space complexity of linked list's __len__ from O(n) to O(1) #8183

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __iter__(self) -> Iterator[Any]:
break

def __len__(self) -> int:
return len(tuple(iter(self)))
return sum(1 for _ in self)

def __repr__(self):
return "->".join(str(item) for item in iter(self))
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __len__(self):
>>> len(linked_list) == 5
True
"""
return len(tuple(iter(self)))
return sum(1 for _ in self)

def insert_at_head(self, data):
self.insert_at_nth(0, data)
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/merge_two_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __len__(self) -> int:
>>> len(SortedLinkedList(test_data_odd))
8
"""
return len(tuple(iter(self)))
return sum(1 for _ in self)

def __str__(self) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/singly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __len__(self) -> int:
>>> len(linked_list)
0
"""
return len(tuple(iter(self)))
return sum(1 for _ in self)
Copy link
Contributor

Choose a reason for hiding this comment

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

The sum will call iter(self) inside, so this code is still O(n).

Copy link
Contributor

Choose a reason for hiding this comment

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

For the classical linked list, the best length is O(n).

PS. collections.deque is not a real linked list, it's a wrapper around it, so it basically counts how many append/delete methods were called. https://github.com/python/cpython/blob/main/Modules/_collectionsmodule.c#L196

https://wiki.python.org/moin/TimeComplexity

Copy link
Contributor Author

@amirsoroush amirsoroush Mar 17, 2023

Choose a reason for hiding this comment

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

Are you talking about "Time" complexity? This line would fix the "Space" complexity as it only fetches one item at a time and adds 1 to the final result as opposed to tuple() which loads all the items into the memory.

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed that, you definitely right.

Copy link
Member

@cclauss cclauss Apr 1, 2023

Choose a reason for hiding this comment

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

Fun exercise... Go to https://pyodide.org/en/latest/console.html to get a current Python repl running on WASM.

Paste in the following code and hit return.

>>> from timeit import timeit
setup="from itertools import product; from string import ascii_letters"
timeit("sum(1 for _ in product(ascii_letters, repeat=4))", number=10, setup=setup)
timeit("len(tuple(product(ascii_letters, repeat=4)))", number=10, setup=setup)

5.0610000000000355
4.121999999999957

sum() is slower than len() for 7,311,616 items.

Refresh the webpage to clear out any clutter in memory...

Paste in the following code and hit return.

>>> from timeit import timeit
setup="from itertools import product; from string import ascii_letters"
timeit("sum(1 for _ in product(ascii_letters, repeat=5))", number=1, setup=setup)
timeit("len(tuple(product(ascii_letters, repeat=5)))", number=1, setup=setup)

26.686000000000035
Traceback (most recent call last):
  ...
MemoryError

sum() delivers an answer for 380,204,032 items while len() raises a MemoryError.

These numbers are for long iterators but still good to know.


def __repr__(self) -> str:
"""
Expand Down