Breadth First Search Algorithm Explained (With Example and Code)

  Рет қаралды 11,171

FelixTechTips

FelixTechTips

Күн бұрын

Breadth First Search Algorithm Explained.
BFS explained with an example and code in Python.
This video is part of my basic algorithms series. Check out the other ones
to develop your algorithm skills and become better at programming or subscribe to this channel for more.

Пікірлер: 9
@sudonick2161
@sudonick2161 3 жыл бұрын
Damn...you're so underrated...I watched so many videos on the topic and yours was the only one that I found helpful. Thanks a lot, dude..for uploading this.
@olafschlammbeutel
@olafschlammbeutel 3 жыл бұрын
Must watch 2020
@tomarshiv7213
@tomarshiv7213 3 жыл бұрын
Pls dfs as well
@allwinr4802
@allwinr4802 2 жыл бұрын
This cannot be used to find the shortest path. For the input d = { 1: [2, 3], 2: [1, 3], 3: [1, 2] } and the shortest path between 1 and 3, It returns [1, 2, 3] as the shortest path instead of [1, 3]
@bonjerne4911
@bonjerne4911 2 жыл бұрын
Hello mate, got same problem with a bit more bigger graph. It will find the shortest path if we will swap 2 and 3 in your example (obviously it will change parent of 3). But I cant figure out what's wrong with algorithm, im pretty sure I miss something simple. Do you have thoughts where to dig? Thanks anyway. Edit. This is works. def bfs(graph, start, end): visit = {k: False for k in graph.keys()} parent = {k: None for k in graph.keys()} q = Queue() visit[start] = True q.put(start) while not q.empty(): x = q.get() for v in graph[x]: if not visit[v]: visit[v] = True parent[v] = x q.put(v) return short(parent, start, end) def short(parent, start, end): path = [] while end is not None: path.append(end) end = parent[end] return path[::-1]
@Arkonis1
@Arkonis1 Жыл бұрын
I think the issue could be fixed if we put after line 12 (in bfsShortestPath) the check that the neighbor node is the endNode, and in that case break the loop. for example if you add after line 12: if neighbor == endNode: return shortestPath(parentNodes, startNode, endNote) Then it works
@jonsmith568
@jonsmith568 11 ай бұрын
this version of bfs code only works for graphs without cycles, else u need to change the code
@tomarshiv7213
@tomarshiv7213 3 жыл бұрын
Dfs???
@tanyejie7694
@tanyejie7694 2 ай бұрын
def dfs(graph,startNode): visitedNodes = [] queue = [startNode] while queue: currentNode = queue.pop() if currentNode not in visitedNodes: visitedNodes.append(currentNode) for neighbour in reversed(graph[currentNode]): if neighbour not in visitedNodes: queue.append(neighbour) return visitedNodes ^Depth-search (use chatgpt for this)
IS THIS REAL FOOD OR NOT?🤔 PIKACHU AND SONIC CONFUSE THE CAT! 😺🍫
00:41
small vs big hoop #tiktok
00:12
Анастасия Тарасова
Рет қаралды 9 МЛН
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 185 МЛН
BFS Grid Python
12:32
DuoWei Education
Рет қаралды 3 М.
Merge Sort In Python Explained (With Example And Code)
13:35
FelixTechTips
Рет қаралды 195 М.
Graph Search Visualization in Python (BFS and DFS)
19:12
NeuralNine
Рет қаралды 15 М.
Uniform Cost Search
10:23
John Levine
Рет қаралды 388 М.
Breadth First Search - Finding Shortest Paths in Unweighted Graphs
14:23
Mary Elaine Califf
Рет қаралды 42 М.
Quicksort In Python Explained (With Example And Code)
14:13
FelixTechTips
Рет қаралды 134 М.
Top 5 Most Common Graph Algorithms for Coding Interviews
13:01
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1 МЛН
MacBook Air Японский Прикол!
0:42
Sergey Delaisy
Рет қаралды 569 М.
Samsung S24 Ultra professional shooting kit #shorts
0:12
Photographer Army
Рет қаралды 25 МЛН