Skip to content

NetworkGrid's method get_neighborhood returns neighborhood sorted by node id #2812

@Tosiekdev

Description

@Tosiekdev

In the implementation of the get_neighborhood method in the NetworkGrid the list of the neighboring nodes is returned sorted by the key.

I think that the main idea was to return nodes sorted by the distance from the source node.

According to the NetworkX 3.5 implementation of the single_source_shortest_path_length it returns dictionary consisting of pairs {node_id, distance}. Currently you are doing:

neighborhood = sorted(neighbors_with_distance.keys())

And I think it should be something like this:

neighbors_with_distance = {k: v for k, v in sorted(neighbors_with_distance.items(), key=lambda item: item[1])}
neighborhood = list(neighbors_with_distance.keys())

Thank you in the advance for looking at this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions