From 76d3bf22aa0f1663d5403e3c7fc5fcbbae2206f5 Mon Sep 17 00:00:00 2001 From: Ghada AbdulWahab Date: Wed, 20 Apr 2022 15:37:34 +0200 Subject: [PATCH] chore(Python): add breath-first search (#738) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/Python/README.md | 1 + .../Python/searching/breadth-first-search.py | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 algorithms/Python/searching/breadth-first-search.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 2fe7de1a..d9f61cfc 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -36,6 +36,7 @@ - [Ternary Search](searching/ternary_search.py) - [Interpolation Search](searching/interpolation_search.py) - [Uniform Cost Search](searching/uniform_cost_search.py) +- [Breath First Search](searching/breadth-first-search.py) ## Sorting - [Bubble Sort](sorting/bubble_sort.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..9b55a829 --- /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)