From 317790e1dede5faa8400ef9e6766ca17069fabf4 Mon Sep 17 00:00:00 2001 From: RonakGSahu Date: Fri, 25 Aug 2023 15:23:18 -0400 Subject: [PATCH] removing redundant code which fails when parallel edges exist but the main algorithm works for it --- src/paths/steiner.cpp | 74 ++------------------------ tests/unit/igraph_steiner_tree_fpt.c | 23 ++++++++ tests/unit/igraph_steiner_tree_fpt.out | 5 ++ 3 files changed, 32 insertions(+), 70 deletions(-) diff --git a/src/paths/steiner.cpp b/src/paths/steiner.cpp index c431c1932c..90ad1f24cf 100644 --- a/src/paths/steiner.cpp +++ b/src/paths/steiner.cpp @@ -207,13 +207,13 @@ igraph_error_t igraph_steiner_dreyfus_wagner( } } - igraph_integer_t flag_terminals = 0; + igraph_bool_t flag_terminals = false; /* Handle the cases of the null graph and no terminals. */ if (no_of_nodes == 0 || no_of_terminals == 0 || no_of_terminals == 1) { igraph_vector_int_clear(res_tree); *res = 0.0; - flag_terminals = 1; + flag_terminals = true; } else if (no_of_terminals == 2) { igraph_vector_int_t vertices; igraph_real_t tree_weight = 0.0; @@ -236,10 +236,10 @@ igraph_error_t igraph_steiner_dreyfus_wagner( igraph_vector_int_destroy(&edges_res); IGRAPH_FINALLY_CLEAN(2); - flag_terminals = 1; + flag_terminals = true; } - if (flag_terminals == 1) { + if (flag_terminals == true) { if (!weights) { igraph_vector_destroy(&iweights); IGRAPH_FINALLY_CLEAN(1); @@ -306,72 +306,6 @@ igraph_error_t igraph_steiner_dreyfus_wagner( IGRAPH_FINALLY(igraph_vector_int_destroy, &steiner_terminals_copy); igraph_vector_int_sort(&steiner_terminals_copy); - /* - Case - Tree when number of terminals are 3. - Say, q,t2,t3 are terminals then Steiner Tree = min( distance(q,t1)+ distance(q,t2), distance(t1,t2) + distance(q,t1), distance(t1,t2) + distance(q,t2) ) - - */ - if (no_of_terminals == 3) { - - igraph_integer_t steiner_terminals_copy_size = igraph_vector_int_size(&steiner_terminals_copy); - igraph_real_t min_steiner_tree_dist = IGRAPH_INFINITY; - for (igraph_integer_t i = 0; i < steiner_terminals_copy_size; i++) { - - igraph_integer_t ter_node = VECTOR(steiner_terminals_copy)[i]; - igraph_real_t weight_inter = 0.0; - igraph_vector_int_t edges_inter; - IGRAPH_VECTOR_INT_INIT_FINALLY(&edges_inter, 0); - - for (igraph_integer_t j = 0; j < steiner_terminals_copy_size; j++) { - if (VECTOR(steiner_terminals_copy)[j] != ter_node) { - igraph_vector_int_t vertices_2; - igraph_real_t tree_weight = 0.0; - igraph_vector_int_t edges; - - IGRAPH_VECTOR_INT_INIT_FINALLY(&vertices_2, 0); - IGRAPH_VECTOR_INT_INIT_FINALLY(&edges, 0); - - IGRAPH_CHECK(igraph_get_shortest_path_dijkstra(graph, &vertices_2, &edges, ter_node, VECTOR(steiner_terminals_copy)[j], pweights, IGRAPH_ALL)); - igraph_integer_t tree_size = igraph_vector_int_size(&edges); - - for (igraph_integer_t k = 0; k < tree_size; k++) { - tree_weight += VECTOR(*pweights)[VECTOR(vertices_2)[k]]; - } - weight_inter += tree_weight; - IGRAPH_CHECK(igraph_vector_int_append(&edges_inter, &edges)); - - igraph_vector_int_destroy(&vertices_2); - igraph_vector_int_destroy(&edges); - IGRAPH_FINALLY_CLEAN(2); - } - } - if (weight_inter < min_steiner_tree_dist) { - igraph_vector_int_update(res_tree, &edges_inter); - *res = weight_inter; - min_steiner_tree_dist = weight_inter; - } - - igraph_vector_int_destroy(&edges_inter); - - IGRAPH_FINALLY_CLEAN(1); - } - - igraph_matrix_destroy(&distance); - igraph_vector_int_destroy(&steiner_terminals_copy); - IGRAPH_FINALLY_CLEAN(2); - flag_terminals = 1; - } - - if (flag_terminals == 1) { - - if (!weights) { - igraph_vector_destroy(&iweights); - IGRAPH_FINALLY_CLEAN(1); - } - - return IGRAPH_SUCCESS; - } - q = VECTOR(steiner_terminals_copy)[0]; igraph_vector_int_remove(&steiner_terminals_copy, 0); diff --git a/tests/unit/igraph_steiner_tree_fpt.c b/tests/unit/igraph_steiner_tree_fpt.c index 7f0e1b460b..87c5c8417b 100644 --- a/tests/unit/igraph_steiner_tree_fpt.c +++ b/tests/unit/igraph_steiner_tree_fpt.c @@ -429,11 +429,34 @@ int main(void) { igraph_vector_int_print(&tree_edges_new); printf("value: %f\n", value_new); + igraph_vector_destroy(&weights); + igraph_vector_int_destroy(&tree_edges_new); + + igraph_t g_new_4; + printf("\nA different graph than before with floating point weights\n"); + igraph_vector_init_real(&weights, 8, 0.218959, 0.383502, 0.5297, 0.417486, 0.526929, 0.910321, 0.328234, 0.247039); + igraph_small(&g_new_4, 6, IGRAPH_UNDIRECTED, + 5, 2, + 5, 2, + 2, 0, + 4, 1, + 5, 3, + 2, 0, + 2, 1, + 3, 1, + -1); + igraph_vector_int_init_int(&terminals_new, 3, 2, 0, 1); + igraph_vector_int_init(&tree_edges_new, 0); + igraph_steiner_dreyfus_wagner(&g_new_4, &terminals_new, &weights, &value_new, &tree_edges_new); + printf("Tree edges:\n"); + igraph_vector_int_print(&tree_edges_new); + printf("value: %f\n", value_new); igraph_destroy(&g_new); igraph_destroy(&g_new_1); igraph_destroy(&g_new_2); igraph_destroy(&g_new_3); + igraph_destroy(&g_new_4); igraph_vector_int_destroy(&tree_edges_new); igraph_vector_int_destroy(&terminals_new); igraph_vector_destroy(&weights); diff --git a/tests/unit/igraph_steiner_tree_fpt.out b/tests/unit/igraph_steiner_tree_fpt.out index 8385bc2222..65f3415dbb 100644 --- a/tests/unit/igraph_steiner_tree_fpt.out +++ b/tests/unit/igraph_steiner_tree_fpt.out @@ -79,3 +79,8 @@ A 3 vertices square graph with weights Tree edges: 0 1 3 8 11 value: 500.000000 + +A different graph than before with floating point weights +Tree edges: +2 6 +value: 0.857934 \ No newline at end of file