From bfcf885e8fc0e7d9a7c5b8d6449ace350cba6493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sat, 4 Nov 2023 20:08:55 +0100 Subject: [PATCH] shrink: bug fixes --- edmonds.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/edmonds.cpp b/edmonds.cpp index d0bb86e..49ad801 100644 --- a/edmonds.cpp +++ b/edmonds.cpp @@ -77,43 +77,54 @@ Graph maximum_matching_from_initial_matching(Graph & graph) // Paths are not disjoint -> shrink blossom size_t distance_from_x = x_path.size() - 1; size_t distance_from_y = y_path.size() - 1; - while (x_path[distance_from_x] == y_path[distance_from_y]) + while (distance_from_x > 0 and distance_from_y > 0 and \ + x_path[distance_from_x - 1] == y_path[distance_from_y - 1]) { --distance_from_x; --distance_from_y; } // found first vertex of x_path \cap y_path - do + while (graph.root_of_ear_component(x_path[distance_from_x]) != x_path[distance_from_x]) { ++distance_from_x; ++distance_from_y; - } while (graph.root_of_ear_component(x_path[distance_from_x] != x_path[distance_from_x])); + }; // found first vertex fixed by root_of_ear_component - size_t x_position_of_blossom_root = distance_from_x + 1; - size_t y_position_of_blossom_root = distance_from_y + 1; - NodeId blossom_root = x_path[x_position_of_blossom_root]; - for(size_t i = 1; i <= x_position_of_blossom_root; i += 2) + NodeId const blossom = x_path[distance_from_x]; + + NodeId blossom_root_id = x_path[distance_from_x]; + for(size_t i = 1; i <= distance_from_x; i += 2) { - if (graph.root_of_ear_component(graph.ear_or_root_neighbor(x_path[i])) != x_path[i]) + if (graph.root_of_ear_component(graph.ear_or_root_neighbor(x_path[i])) != blossom_root_id) { graph.node(graph.ear_or_root_neighbor(x_path[i])).ear_or_root_neighbor = x_path[i]; } } - for(size_t i = 1; i <= y_position_of_blossom_root; i += 2) + for(size_t i = 1; i <= distance_from_y; i += 2) { - if (graph.root_of_ear_component(graph.ear_or_root_neighbor(y_path[i])) != y_path[i]) + if (graph.root_of_ear_component(graph.ear_or_root_neighbor(y_path[i])) != blossom_root_id) { graph.node(graph.ear_or_root_neighbor(y_path[i])).ear_or_root_neighbor = y_path[i]; } } - if (graph.root_of_ear_component(x_path.front()) != blossom_root) + if (graph.root_of_ear_component(x_path.front()) != blossom_root_id) { graph.node(x_path.front()).ear_or_root_neighbor = y_path.front(); } - if (graph.root_of_ear_component(y_path.front()) != blossom_root) + if (graph.root_of_ear_component(y_path.front()) != blossom_root_id) { graph.node(x_path.front()).ear_or_root_neighbor = x_path.front(); } + + // Update root indices + for(size_t i = 0; i <= distance_from_x; ++i) + { + graph.node(x_path[i]).root_of_ear_component = blossom_root_id; + } + for(size_t i = 0; i <= distance_from_y; ++i) + { + graph.node(y_path[i]).root_of_ear_component = blossom_root_id; + } } } }