Question: def solution ( N , A , B ) : graph = [ [ ] for i in range ( N + 1 ) ]

def solution(N, A, B):
graph =[[] for i in range(N+1)]
for i in range(len(A)):
graph[A[i]].append(B[i])
graph[B[i]].append(A[i])
visited =[False]*(N+1)
path =[1]
visited[1]= True
return dfs(graph, visited, path, N)
def dfs(graph, visited, path, N):
if path[-1]== N:
return True
for neighbor in graph[path[-1]]:
if not visited[neighbor] and neighbor == path[-1]+1:
visited[neighbor]= True
path.append(neighbor)
if dfs(graph, visited, path, N):
return True
path.pop()
visited[neighbor]= False
return False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!