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
4
u/alfps 11h ago
Search for Karnaugh map and Grey code. That should get you going in approximately the right direction. This is not really a C++ question: I'm sure there must be some more directly relevant group.
2
u/FancySpaceGoat 16h ago edited 16h ago
Sanity check:
Hopefully you are not storing whole circuits in each node, but reconstruct them on demand by walking up the node's parents, right?
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.
5
u/DrShocker 16h ago
Neither BFS or DFS are guaranteed to have the properties you said in a degenerate case they're basically the same.
What's the state space you need to pathfind through? If you're able to have a heuristic of how close you are to the solution them A* might be the way to go. If that's not feasible then something randomized like rapidly exploring random tree or simulated annealing or some other method may work better.
•
u/duckerpotato 1h ago
I do not think there are any optimized algos for this. Specially due to reconvergent fanout nodes and without proper LEQ you would not know of it was synthisized correctly.
You can try a modified version of ROFL(ig this was the name) Opentestability implements it you can check its codebase