Python Forum

Full Version: converting python list to a list by user input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I have this algorithm for breadth search and depth search. AS you can see below, the variable parents holds my nodes. How can I change it so that it becomes an user input. Example: parents=4, but by user input it is: parents=input(). Same thing for the parents list in my code I need.

def DFS_dist_from_node(query_node, parents):
result = {}
stack =
stack.append((query_node, 0))
while len(stack) > 0:
print("stack=", stack)
node, dist = stack.pop()
result[node] = dist
if node in parents:
for parent in parents[node]:
stack_members = [x[0] for x in stack]
if parent not in stack_members:
stack.append((parent, dist + 1))
return result


def BFS_dist_from_node(query_node, parents):
result = {}
queue =
queue.append((query_node, 0))
while queue:
print("queue=", queue)
node, dist = queue.pop(0)
result[node] = dist
if node in parents:
for parent in parents[node]:
queue_members = [x[0] for x in queue]
if parent not in result and parent not in queue_members:
queue.append((parent, dist + 1))
return result


if __name__ == "__main__":
parents = dict()
parents = {'N1': ['N2', 'N3', 'N4'], 'N3': ['N6', 'N7'], 'N4': ['N3'], 'N5': ['N4', 'N8'], 'N6': ['N13'],
'N8': ['N9'], 'N9': ['N11'], 'N10': ['N7', 'N9'], 'N11': ['N14'], 'N12': ['N5']}
print("Depth-first search:")
dist = DFS_dist_from_node('N1', parents)
print(dist)

print("Breadth-first search:")
dist = BFS_dist_from_node('N1', parents)
print(dist)
Your question is still unclear to me.
Maybe you could be more detailed.