From 894860db489b067049f9a8992e51dfcbf7628696 Mon Sep 17 00:00:00 2001 From: GAbdulWahab Date: Sun, 10 Apr 2022 00:52:51 +0200 Subject: [PATCH] added Breadth-first search in python #734 --- .../Python/searching/Breadth-first search.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 algorithms/Python/searching/Breadth-first search.py diff --git a/algorithms/Python/searching/Breadth-first search.py b/algorithms/Python/searching/Breadth-first search.py new file mode 100644 index 00000000..42fef860 --- /dev/null +++ b/algorithms/Python/searching/Breadth-first search.py @@ -0,0 +1,32 @@ +# this graph to check the algorithm +graph={ + 'S':['B','D','A'], + 'A':['C'], + 'B':['D'], + 'C':['G','D'], + 'S':['G'], +} +#function of BFS +def BFS(graph,start,goal): + Visited=[] + queue=[[start]] + while queue: + path=queue.pop(0) + node=path[-1] + if node in Visited: + continue + Visited.append(node) + if node==goal: + return path + else: + adjecent_nodes=graph.get(node,[]) + for node2 in adjecent_nodes: + new_path=path.copy() + new_path.append(node2) + queue.append(new_path) + + + + +Solution=BFS(graph,'S','G') +print('Solution is ',Solution) \ No newline at end of file