diff --git a/.vs/AlgoBook/FileContentIndex/344dc6c1-a9fc-4aee-b964-f42f1dafac3e.vsidx b/.vs/AlgoBook/FileContentIndex/344dc6c1-a9fc-4aee-b964-f42f1dafac3e.vsidx new file mode 100644 index 000000000..75b492058 Binary files /dev/null and b/.vs/AlgoBook/FileContentIndex/344dc6c1-a9fc-4aee-b964-f42f1dafac3e.vsidx differ diff --git a/.vs/AlgoBook/FileContentIndex/451291bc-44e2-4204-8a29-78f16c5844b1.vsidx b/.vs/AlgoBook/FileContentIndex/451291bc-44e2-4204-8a29-78f16c5844b1.vsidx new file mode 100644 index 000000000..d70a363fd Binary files /dev/null and b/.vs/AlgoBook/FileContentIndex/451291bc-44e2-4204-8a29-78f16c5844b1.vsidx differ diff --git a/.vs/AlgoBook/FileContentIndex/8ed5d0b7-785b-4ce7-b6f3-545030c69557.vsidx b/.vs/AlgoBook/FileContentIndex/8ed5d0b7-785b-4ce7-b6f3-545030c69557.vsidx new file mode 100644 index 000000000..0ff0238d9 Binary files /dev/null and b/.vs/AlgoBook/FileContentIndex/8ed5d0b7-785b-4ce7-b6f3-545030c69557.vsidx differ diff --git a/.vs/AlgoBook/FileContentIndex/read.lock b/.vs/AlgoBook/FileContentIndex/read.lock new file mode 100644 index 000000000..e69de29bb diff --git a/.vs/AlgoBook/v17/.wsuo b/.vs/AlgoBook/v17/.wsuo new file mode 100644 index 000000000..4e6057705 Binary files /dev/null and b/.vs/AlgoBook/v17/.wsuo differ diff --git a/.vs/AlgoBook/v17/Browse.VC.db b/.vs/AlgoBook/v17/Browse.VC.db new file mode 100644 index 000000000..92969a291 Binary files /dev/null and b/.vs/AlgoBook/v17/Browse.VC.db differ diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 000000000..0cf5ea503 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "No Configurations" +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 000000000..236109e28 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,9 @@ +{ + "ExpandedNodes": [ + "", + "\\python", + "\\python\\graph_algorithms" + ], + "SelectedNode": "\\python\\graph_algorithms\\breath_first_search_algorithm.py", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 000000000..d92ef92ba Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/python/graph_algorithms/breath_first_search_algorithm.py b/python/graph_algorithms/breath_first_search_algorithm.py new file mode 100644 index 000000000..f14094471 --- /dev/null +++ b/python/graph_algorithms/breath_first_search_algorithm.py @@ -0,0 +1,39 @@ +from collections import defaultdict, deque + +class Graph: + def __init__(self): + self.graph = defaultdict(list) + + def add_edge(self, u, v): + # Add an edge from vertex u to vertex v + self.graph[u].append(v) + + def bfs(self, start_vertex): + visited = set() # Set to keep track of visited vertices + queue = deque() # Queue for BFS traversal + + visited.add(start_vertex) # Mark the start vertex as visited + queue.append(start_vertex) # Add the start vertex to the queue + + while queue: + vertex = queue.popleft() # Get the next vertex from the queue + print(f"Visited vertex: {vertex}") + + for neighbor in self.graph[vertex]: + if neighbor not in visited: + visited.add(neighbor) # Mark the neighbor as visited + queue.append(neighbor) # Add the neighbor to the queue + +# Example usage and test +if __name__ == "__main__": + # Create a sample graph + g = Graph() + g.add_edge(0, 1) + g.add_edge(0, 2) + g.add_edge(1, 2) + g.add_edge(2, 0) + g.add_edge(2, 3) + g.add_edge(3, 3) + + print("Breadth-First Traversal (starting from vertex 2):") + g.bfs(2) \ No newline at end of file