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

mapping is not possible when a node has no neighbors #995

Merged
merged 3 commits into from
Oct 6, 2023
Merged
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
20 changes: 20 additions & 0 deletions rustworkx-core/src/token_swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ where
}
let id_node = self.rev_node_map[&node];
let id_token = self.rev_node_map[&tokens[&node]];

if self.graph.neighbors(id_node).next().is_none() {
return Err(MapNotPossible {});
}

for id_neighbor in self.graph.neighbors(id_node) {
let neighbor = self.node_map[&id_neighbor];
let dist_neighbor: DictMap<G::NodeId, usize> = dijkstra(
Expand Down Expand Up @@ -705,4 +710,19 @@ mod test_token_swapper {
Err(_) => (),
};
}

#[test]
fn test_edgeless_graph_fails() {
let mut g = petgraph::graph::UnGraph::<(), ()>::new_undirected();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
let d = g.add_node(());
g.add_edge(c, d, ());
let mapping = HashMap::from([(a, b), (b, a)]);
match token_swapper(&g, mapping, Some(10), Some(4), Some(50)) {
Ok(_) => panic!("This should error"),
Err(_) => (),
};
}
}