r/learnprogramming • u/KlutzyCompetition937 • 16d ago
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
9
u/MyNameIsRichardCS54 16d ago
Well done, you are learning about trade offs. There are very few perfect algorithms, so you have to choose between them and understand the cost.
4
u/aanzeijar 16d ago
Have you looked at wikipedia? They usually have at least a few buzzwords to look up for further research.
Also, IIRC that problem is NP-complete, so some resource exploding is expected. Heuristics for near optimal solutions should exist though.
4
u/Cobweb_Games 16d ago
You could look into iterative deepening DFS (IDDFS). It searches depth by depth like BFS but uses DFS style memory, so it can be a good fit when you're optimizing for minimum depth.
3
u/RyanDubois43 16d ago
Use Iterative Deepening Depth-First Search (IDDFS), which combines DFS memory use with BFS optimality for minimum depth. The beginner gotcha is you must re-explore nodes at each new depth limit, so expect repeated work but bounded memory.
2
u/W31337 9d ago
Ok you just found out what NP problems are.
In computer theory you have groups of problems that can be mapped to each other. Distinct sets have been found.
Polynomial time (P) programs that can scale linearly when the problem gets bigger.
Non polynomial time (NP) programs that explode when the problem gets bigger.
Quantum computers can solve NP problems in polynomial time.
So without a quantum computer you need to take a good look at what you are doing and prevent your problem from being in NP, or optimize the fuck out of it so your explosion is contained within what your computer can handle.
No known algorithm can solve NP problems in P time. If you find one then you've solved one of the biggest open mathematical problems.