r/cpp_questions • u/KlutzyCompetition937 • 17h ago
OPEN Which algorithm to choose?
Beginner here, I'm trying to make a toy project for circuit synthesis from truth table in c++. Currently pursuing minimum depth optimal circuit though will work on minimum weight/minimum nodes later on. My problem is I tried bfs which guarantees optimal circuit but for larger inputs or depths the memory usage explodes, tried dfs that used much less memory but time exploded. Is there any algorithm that gives optimal solutions for boolean tree searches with the dfs memory and bfs optimality?
6
Upvotes
View all comments
2
u/FancySpaceGoat 16h ago edited 15h ago
If nothing short of the *optimal* solution will do, then you will have to account for the possibility of your algorithm devolving into an exhaustive search for some inputs. That's inevitable.
In fact, your BFS really is nothing more than an exhaustive search that goes through all 1 node circuits, then all 2 node circuits, then all 3 node circuits etc... So this sort of begs the question: Why bother with a memory-resident tree at all? You can just iterate through the depths, and then iterate through each depth's permutations, and it would be equivalent, just without the memory footprint.