• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: What is the time complexity for this generic bruteforce/backtracking function?

Resolved: What is the time complexity for this generic bruteforce/backtracking function?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am having trouble reasoning what the time complexity of this is. I was writing a backtracking function to solve a problem. To simplify, let’s just say I have a list of size “a” and I am allowed to place down 0 or 1 into each element of the list. After trying all combinations, I return. This is clearly 2^(nm).
However, what if during each recursive call I have a constant amount of work to do? I am stuck reasoning through what the complexity is here. Can you point me to sources? From my undergrad studies all I remember is Master’s theorem, but this approach is not relevant. (We subtract rather than divide to get the subproblem)

Answer:

In your case, the time complexity is T(n) = m + 2T(n - 1). Although we can’t apply Master’s theorem here, we can use substitution:
Evaluating this, we have m2^n or ϴ(2^n).
Recursion doesn’t really offer you any benefits here over readability. You could see savings if you combined this with memory of what you’ve evaluated, however. In that case, evaluating the time complexity becomes more… complex.

If you have better answer, please add a comment about this, thank you!

algorithm backtracking complexity-theory recursion time-complexity
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Convert function is not working with {fn } in SQL Server

24/03/2023

Resolved: Why reference in pointer array doesn’t have data?

24/03/2023

Resolved: EntityFramework creates/runs migrations using parameterless DataContext instance

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.