Skip to content

Commit

Permalink
mapping is not possible when a node has no neighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderivrii committed Oct 5, 2023
1 parent 5ab599f commit 749e543
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rustworkx-core/src/token_swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ where
}
let id_node = self.rev_node_map[&node];
let id_token = self.rev_node_map[&tokens[&node]];

if self
.graph
.neighbors(id_node)
.collect::<Vec<G::NodeId>>()
.is_empty()
{
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 +715,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(_) => (),
};
}
}

0 comments on commit 749e543

Please sign in to comment.