# D. Scott # CCI - Problem 4.1 # Determine whether two nodes (S and E) in a directed graph are connected import sys nodes = ['A','L','K','S','P','E','V','F','R'] adj = {'A':('S'),'L':('A'),'K':('L'),'S':(),'V':('F','R'),'F':('P'),'P':('K'), 'R':(),'E':('V','F')} seen = [] path = [] curr = 'E' target = 'S' found = False def searchGraph(n): if (n not in seen): path.append(n) print("Checking ",n,"...") if(n == target): print("\nThere is a path from ",curr," to ",target) print(path) raise SystemExit seen.append(n) for x in adj: if (x == n): print("Checking adjacent list of ",x,"...") for y in adj[x]: searchGraph(y) searchGraph(curr) if (found == False): print("\nThere is no path from ",curr," to ",target)