Skip to content

Randomized iteration order for PyGraph.neighbors #1501

@adamglos92

Description

@adamglos92

Information

  • rustworkx version: 0.17.1
  • Python version: 3.12.11
  • Rust version: (I believe it is not installed as I'm only using python version)
  • Operating system: Linux aarch64 (devcontainer in macOS arm64)

What is the current behavior?

For a fixed graph I'm getting neighbors in random order.

What is the expected behavior?

Arbitrary, but fixed order

Judging on implementation, set is the problem (but I'm no Rust expert)

    pub fn neighbors(&self, node: usize) -> NodeIndices {
        NodeIndices {
            nodes: self
                .graph
                .neighbors(NodeIndex::new(node))
                .map(|node| node.index())
                .collect::<HashSet<usize>>()
                .drain()
                .collect(),
        }
    }

Steps to reproduce the problem

import rustworkx.generators as gen

for _ in range(3):
    g = gen.path_graph(5)
    print(g.neighbors(2))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions