Skip to content

Commit

Permalink
removing redundant code which fails when parallel edges exist but the…
Browse files Browse the repository at this point in the history
… main algorithm works for it
  • Loading branch information
RonakGSahu committed Aug 25, 2023
1 parent 5cc5199 commit 317790e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 70 deletions.
74 changes: 4 additions & 70 deletions src/paths/steiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/igraph_steiner_tree_fpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/igraph_steiner_tree_fpt.out
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 317790e

Please sign in to comment.