Can you help me with the time complexity of this bit of code. I think it's 3^n but I'm a newbie so I'm probably wrong.
public void find(int n) throws Exception
{
if (n == vertices){
throw new Exception("Solution found");
}
for (int r = 1; r <= 3; r++)
{
if (control(n, r))
{
color[n] = r;
find(n + 1);
color[n] = 0;
}
}
}
public boolean control(int n, int r)
{
for (int i = 0; i < vertices; i++){
if (graph[n][i] == 1 && r == color[i]){
return false;
}
}
return true;
}
Any help is appreciated!
Edit: I guess I misunderstood some things and the complexity is n.
O(3^n)?controlandfindmethods.O(n)3 times, that's not3^n- it'sO(n)still.