PROGLIB_ACADEMY Telegram 2787
💬 DFS vs BFS на Python

Если хотите разобраться, как алгоритмы поиска в глубину и поиска в ширину работают на практике, то вот минимальный пример, который покажет разницу.

graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [], 'E': [], 'F': []
}

def dfs(node, visited=set()):
if node in visited: return
print(node)
visited.add(node)
for neighbor in graph[node]:
dfs(neighbor, visited)

def bfs(start):
queue = [start]
visited = set()
while queue:
node = queue.pop(0)
if node in visited: continue
print(node)
visited.add(node)
queue += graph[node]


➡️ Что делает

— dfs проходит глубоко: A → B → D → E → C → F
— bfs — по уровням: A → B → C → D → E → F
— Обе функции показывают порядок обхода графа

🔵 Чтобы знать об алгоритмах все, забирайте наш курс «Алгоритмы и структуры данных»

Proglib Academy #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tgoop.com/proglib_academy/2787
Create:
Last Update:

💬 DFS vs BFS на Python

Если хотите разобраться, как алгоритмы поиска в глубину и поиска в ширину работают на практике, то вот минимальный пример, который покажет разницу.

graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [], 'E': [], 'F': []
}

def dfs(node, visited=set()):
if node in visited: return
print(node)
visited.add(node)
for neighbor in graph[node]:
dfs(neighbor, visited)

def bfs(start):
queue = [start]
visited = set()
while queue:
node = queue.pop(0)
if node in visited: continue
print(node)
visited.add(node)
queue += graph[node]


➡️ Что делает

— dfs проходит глубоко: A → B → D → E → C → F
— bfs — по уровням: A → B → C → D → E → F
— Обе функции показывают порядок обхода графа

🔵 Чтобы знать об алгоритмах все, забирайте наш курс «Алгоритмы и структуры данных»

Proglib Academy #буст

BY Proglib.academy | IT-курсы




Share with your friend now:
tgoop.com/proglib_academy/2787

View MORE
Open in Telegram


Telegram News

Date: |

During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. bank east asia october 20 kowloon You can invite up to 200 people from your contacts to join your channel as the next step. Select the users you want to add and click “Invite.” You can skip this step altogether. Invite up to 200 users from your contacts to join your channel The visual aspect of channels is very critical. In fact, design is the first thing that a potential subscriber pays attention to, even though unconsciously.
from us


Telegram Proglib.academy | IT-курсы
FROM American