I’m sorry for the tricky title, here is the example
graph main_graph = //initialize graph
graph sub_graph = //pick a subset of edges from the main_graph
while ( ! sub_graph.size() == 0) {
select_edge(); //here I pick an edge basing on some heuristics
reduce_graph(); //here I remove some edges from the main_graph
sub_graph = //pick a subset of edges from the main_graph
}
So the point is that I have to write the very same code to define the sub_graph before entering the loop (because it could be already empty) and right before entering a new iteration.
This would not be that bad, if it wasn’t that I actually have three nested loops with the same problem, and the code to inizialize the sub_graph is a bunch of lines of code, so my code would look a lot replicated.
Any suggestion on how to better design this loop(s)? I have no restrictions (can use for, do-while…)
Even if this is pseudo-code, since is more a ‘design’ question, I’m coding in C++!
To avoid repeating lots of code, put the code in a function:
Then use it to initialize and recalculate your values: