Skip to content

Performance Enhancement: Replace list with collections.deque in graph algorithms #12857

@riokuma

Description

@riokuma

Feature description

Summary

The current implementation of the breadth_first_search_shortest_path_2.py uses a Python list to simulate a queue. However, using a list for this purpose might be inefficient because removing the first element (pop(0)) has a time complexity of O(n) and affects the overall performance. Python's collections.deque is optimized for such use cases and offers O(1) time complexity for appending and popping from both ends.

Suggested update

Replace the list used as a queue with a collections.deque to improve performance and align with best practices for implementing BFS in Python.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis PR modified some existing files

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions