Hi I am having trouble determing the time complexity for this algorithm. What the algorithm does is it finds the maximum conversion from one currency to another. It uses DFS and Backtracking and it is clear that the space complexity is O(depth) but the time complexity is confusing me. I am not sure if it is O(V*E) or O(V+E) or O(V!). I would appreciate any help. Thanks!
def get_currency_exchange_rate(source, target, graph):
def backtrack(current, seen):
if current == target:
return 1
product = 0
if current in graph:
for neighbor in graph[current]:
if neighbor not in seen:
seen.add(neighbor)
product = max(product, graph[current][neighbor] * backtrack(neighbor, seen))
seen.remove(neighbor)
return product
return backtrack(source, {source})
g = {
"A": {"B": 6, "D": 1},
"B": {"A": 1/6, "D": 1/2, "E": 3, "C": 5},
"D": {"A": 1, "B": 2, "E": 1/3},
"E": {"B": 1/3, "D": 3, "C": 1/5},
"C": {"B": 1/5, "E": 5}
}
curr1 = "A"
curr2 = "C"
print(get_currency_exchange_rate(curr1, curr2, g))