Create dfs.py

This commit is contained in:
marina 2023-08-06 17:06:38 -07:00 committed by GitHub
parent e333844d4d
commit 35c888571e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

50
graphs/dfs.py Normal file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
from collections import deque
class Graph:
def __init__(self, edges, n):
self.adj_list = [[] for _ in range(n)]
for (src, dest) in edges:
self.adj_list[src].append(dest)
self.adj_list[dest].append(src)
def dfs(graph, v, discovered):
discovered[v] = True
print(v, end=' ')
for u in graph.adj_list[v]:
if not discovered[u]: #
dfs(graph, u, discovered)
def iterative_dfs(graph, v, discovered):
stack = deque()
stack.append(v)
while stack:
v = stack.pop()
if discovered[v]:
continue
discovered[v] = True
print(v, end=' ')
adj_list = graph.adjList[v]
for i in reversed(range(len(adj_list))):
u = adj_list[i]
if not discovered[u]:
stack.append(u)