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

Daggy not detecting loop cycles #28

Closed
jdeschenes opened this issue Jun 9, 2020 · 0 comments · Fixed by #29
Closed

Daggy not detecting loop cycles #28

jdeschenes opened this issue Jun 9, 2020 · 0 comments · Fixed by #29
Labels

Comments

@jdeschenes
Copy link

Hello,

Daggy does not properly detect when a node has a node to itself(Loop).

The following test is failing:

#[test]
fn add_edges_err_loop() {
    let mut dag = Dag::<Weight, u32, u32>::new();
    let root = dag.add_node(Weight);
    let a = dag.add_node(Weight);
    let b = dag.add_node(Weight);
    let c = dag.add_node(Weight);

    let add_edges_result = dag.add_edges(
        once((root, a, 0))
            .chain(once((root, b, 1)))
            .chain(once((root, c, 2)))
            .chain(once((c, c, 3))),
    );

    match add_edges_result {
        Err(WouldCycle(returned_weights)) => assert_eq!(returned_weights, vec![3, 2, 1, 0]),
        Ok(_) => panic!("Should have been an error"),
    }
}

Several approaches can be used to fix this. An easy one is making the following modifications

fn must_check_for_cycle<N, E, Ix>(dag: &Dag<N, E, Ix>, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> bool
where
    Ix: IndexType,
{
// Add this check
    if a == b {
        return true;
    }
    dag.parents(a).walk_next(dag).is_some()
        && dag.children(b).walk_next(dag).is_some()
        && dag.find_edge(a, b).is_none()
}

This would ensure that the loop is properly checked.

mitchmindtree added a commit that referenced this issue Oct 30, 2021
Includes:

- Update petgraph to 0.6 #33
- Fix for issue where loops weren't detected as cycles #28

Closes #34

@azriel91 thanks for the ping!

For future reference, if anyone's after a new version, feel free to open
a PR with a version bump - I've think I've setup this repo to
auto-publish :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants