Here is the graph:
g = {
0: [2, 5, 7],
1: [7],
2: [0, 6],
3: [5, 4],
4: [3, 6, 7],
5: [3, 4, 0],
6: [2, 4],
7: [0, 1, 4]
}
I have the following function in Python:
def dfs(graph, start, target, visited=None):
if visited is None:
visited = set()
visited.add(start)
for n in (set( graph[start] ) - visited):
dfs(graph, n, target, visited)
return visited
But it returns all the vertices that exists in the graph, and I want that it returns just the target vertex if it's present in the graph. Could someone help me?