Skip to content

Commit

Permalink
Some reduction in code
Browse files Browse the repository at this point in the history
  • Loading branch information
RonakGSahu committed Jul 21, 2023
1 parent 926da0c commit 3fc5920
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
37 changes: 18 additions & 19 deletions src/paths/steiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static igraph_error_t generateSubsets(const igraph_vector_int_t *steinerTerminal
return IGRAPH_SUCCESS;
}

static void generateD_E(const int_set &D, std::set<int_set> &allSubsets, int D1 = -1) {
static void generateD_E(const int_set &D, std::set<int_set> &allSubsets, igraph_integer_t D1 = -1) {
/*
D1 is the element that is always going to be there in all the subsets that we generate
D has elements for which we are creating subsets
Expand All @@ -87,7 +87,7 @@ static void generateD_E(const int_set &D, std::set<int_set> &allSubsets, int D1

for (igraph_integer_t i = 0; i < count - 1; i++) {
int_set newSubset;
if (D1 != -1) {
if (D1 != (igraph_integer_t) -1) {
newSubset.insert(D1);
}
for (igraph_integer_t j = 0; j < n; j++) {
Expand Down Expand Up @@ -208,7 +208,6 @@ static igraph_error_t generate_steiner_tree_exact(const igraph_t *graph, const i
std::set<igraph_integer_t> edgelist_all_set;
while (D.size() > 1) {
std::set<igraph_integer_t>::iterator itr;

indexD = fetchIndexofMapofSets(D, subsetMap);

// Finding the bridge vertex from m to subset D which is part of shortest path.
Expand All @@ -233,39 +232,35 @@ static igraph_error_t generate_steiner_tree_exact(const igraph_t *graph, const i
if (D.size() > 2) {

/*
We iterate through the subset D and split it into E and F where E is a subset of D but not equal to D during every iteration.
We iterate through the subset D and split it into E and F where E is singleton set during every iteration.
This process leads to find out value where distance (E,k) + (F,k) is minimum.
*/
igraph_real_t min_value = IGRAPH_INFINITY;
igraph_integer_t D_size = D.size();
std::set<int_set> Subsets = std::set<int_set>();
;
generateD_E(D, Subsets);

for (auto E : Subsets) {
if (E.empty() == true) {
continue;
}
for (igraph_integer_t i = 0; i < D_size; i++) {
igraph_integer_t E_raw = *next(D.begin(), i);
int_set F;
int_set E;
E.insert(E_raw);
std::set_difference(D.begin(), D.end(), E.begin(), E.end(), std::inserter(F, F.end()));

igraph_integer_t indexF = (F.size() == 1) ? *F.begin() : fetchIndexofMapofSets(F, subsetMap);
igraph_integer_t indexE = (E.size() == 1) ? *E.begin() : fetchIndexofMapofSets(E, subsetMap);

igraph_real_t temp_value = MATRIX(*dp_cache, indexE, k) + MATRIX(*dp_cache, indexF, k);
igraph_integer_t indexF = fetchIndexofMapofSets(D, subsetMap);
igraph_real_t temp_value = MATRIX(*dp_cache, *E.begin(), k) + MATRIX(*dp_cache, indexF, k);
if (temp_value < min_value) {
min_value = temp_value;
min_E_value = indexE;
min_E_value = *E.begin();
min_F = F;
}
}

igraph_vector_int_t edgelist_1;
IGRAPH_VECTOR_INT_INIT_FINALLY(&edgelist_1, 0);

IGRAPH_CHECK(igraph_get_shortest_path_dijkstra(graph, nullptr, &edgelist_1, k, min_E_value, weights, IGRAPH_ALL));

for (int i = 0; i < igraph_vector_int_size(&edgelist_1); i++) {
edgelist_all_set.insert(VECTOR(edgelist_1)[i]);
}

igraph_vector_int_destroy(&edgelist_1);
IGRAPH_FINALLY_CLEAN(1);
} else {
Expand All @@ -274,9 +269,11 @@ static igraph_error_t generate_steiner_tree_exact(const igraph_t *graph, const i
k to first element of subset and k to second element of subset is sufficient.
*/
igraph_integer_t E1, F1;

E1 = *D.begin();
F1 = *next(D.begin(), 1);


igraph_vector_int_t edgelist_1;
IGRAPH_VECTOR_INT_INIT_FINALLY(&edgelist_1, 0);

Expand All @@ -295,6 +292,7 @@ static igraph_error_t generate_steiner_tree_exact(const igraph_t *graph, const i
edgelist_all_set.insert(VECTOR(edgelist_2)[i]);
}


igraph_vector_int_destroy(&edgelist_1);
igraph_vector_int_destroy(&edgelist_2);

Expand All @@ -318,6 +316,7 @@ static igraph_error_t generate_steiner_tree_exact(const igraph_t *graph, const i
return IGRAPH_SUCCESS;
}


/**
* \function igraph_steiner_dreyfus_wagner
* \brief Finds a Steiner tree on an undirected graph using the Dreyfus-Wagner algorithm.
Expand Down Expand Up @@ -658,7 +657,7 @@ igraph_error_t igraph_steiner_dreyfus_wagner(
if (i!=0)
C_prime.insert(igraph_vector_int_get(&steiner_terminals_copy,i));
}
generateD_E(C_1, C_prime, E_subsets);
generateD_E(C_prime, E_subsets,C_1);

igraph_real_t distance2 = IGRAPH_INFINITY;

Expand Down
76 changes: 38 additions & 38 deletions tests/unit/igraph_steiner_tree_fpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,44 +353,44 @@ int main(void) {
igraph_vector_int_print(&tree_edges_new);
printf("value: %f\n", value_new);

// igraph_vector_int_destroy(&tree_edges_new);
// igraph_vector_int_destroy(&terminals_new);

// printf("\nA graph with 7 terminals and 2 removed to test if it forces it to go through:\n");
// igraph_vector_int_init_int(&terminals_new, 7, 0, 3, 4, 5, 6, 7, 8);
// igraph_vector_int_init(&tree_edges_new, 0);
// igraph_steiner_dreyfus_wagner(&g_new, &terminals_new, NULL, &value_new, &tree_edges_new);
// printf("Tree edges:\n");
// igraph_vector_int_print(&tree_edges_new);
// printf("value: %f\n", value_new);

// igraph_vector_int_destroy(&tree_edges_new);
// igraph_vector_int_destroy(&terminals_new);

// printf("\nA graph with different structure that previous\n");
// igraph_t g_new_1;
// igraph_real_t value_new_1;
// igraph_small(&g_new_1, 9, IGRAPH_UNDIRECTED,
// 0, 1, 0, 2, 0, 3, 0, 6,
// 2, 7, 2, 9,
// 3, 4,
// 4, 5,
// 7, 8,
// -1
// );
// igraph_vector_int_init_int(&terminals_new, 7, 0, 1, 2, 3, 4, 5, 7);
// igraph_vector_int_init(&tree_edges_new, 0);
// igraph_steiner_dreyfus_wagner(&g_new_1, &terminals_new, NULL, &value_new_1, &tree_edges_new);
// printf("Tree edges:\n");
// igraph_vector_int_print(&tree_edges_new);
// printf("value: %f\n", value_new_1);



// igraph_destroy(&g_new);
// igraph_destroy(&g_new_1);
// igraph_vector_int_destroy(&tree_edges_new);
// igraph_vector_int_destroy(&terminals_new);
igraph_vector_int_destroy(&tree_edges_new);
igraph_vector_int_destroy(&terminals_new);

printf("\nA graph with 7 terminals and 2 removed to test if it forces it to go through:\n");
igraph_vector_int_init_int(&terminals_new, 7, 0, 3, 4, 5, 6, 7, 8);
igraph_vector_int_init(&tree_edges_new, 0);
igraph_steiner_dreyfus_wagner(&g_new, &terminals_new, NULL, &value_new, &tree_edges_new);
printf("Tree edges:\n");
igraph_vector_int_print(&tree_edges_new);
printf("value: %f\n", value_new);

igraph_vector_int_destroy(&tree_edges_new);
igraph_vector_int_destroy(&terminals_new);

printf("\nA graph with different structure that previous\n");
igraph_t g_new_1;
igraph_real_t value_new_1;
igraph_small(&g_new_1, 9, IGRAPH_UNDIRECTED,
0, 1, 0, 2, 0, 3, 0, 6,
2, 7, 2, 9,
3, 4,
4, 5,
7, 8,
-1
);
igraph_vector_int_init_int(&terminals_new, 7, 0, 1, 2, 3, 4, 5, 7);
igraph_vector_int_init(&tree_edges_new, 0);
igraph_steiner_dreyfus_wagner(&g_new_1, &terminals_new, NULL, &value_new_1, &tree_edges_new);
printf("Tree edges:\n");
igraph_vector_int_print(&tree_edges_new);
printf("value: %f\n", value_new_1);



igraph_destroy(&g_new);
igraph_destroy(&g_new_1);
igraph_vector_int_destroy(&tree_edges_new);
igraph_vector_int_destroy(&terminals_new);



Expand Down

0 comments on commit 3fc5920

Please sign in to comment.