3. DFS: Depth First Search Implementation in Python | Graph Data Structure
Вставка
- Опубліковано 13 жов 2024
- Depth First Search Implementation (DFS) in Python,
The time complexity for DFS if O(|V| + |E|), where |V|, |E| are the no of vertices and Edges in the graph respectively.
Explore a graph using DFS traversal,
Complete Algorithm of DFS including visiting and finished time tracking of vertices during DFS.
Author: Shahrukh Khan
Links
Blog: pytech-solutio...
#datastructures
#Graph Traversal
#DFS
Super easy explanation
Thank you, Ayushi Jain
@@PyTechVision brother your explanation and implementation is so easy. Can you kindly make a video on djkastra, prims and krushkal algorithms please!!
This guy deserves more subs and likes.Perfect explanation ever
bahut tagda....!!!!!!
great work easy explanation
Excellent Explanation... You will live on in this video forever.... 👏
dude your voice is asmr ! calmness is much needed while coding.
why don't you do more videos on dsa in python.. it's much needed all other folks just do it in c++
Sir pls post video on topological sort .... U have explained this in a best way !💕
Sure I will
Great explanation..
Glad it was helpful!
brother I wished you have explained MinMax Algo, A*, Alpha-Beta and Bayseain because your explanation are the best.
Cool video! Keep Up the good work!! Its odd that only Indian viewers are only watching/commenting.
Yeah thanks
@@PyTechVision nigerian here watching lol
Fantastic explanation
Really good explanation brother.
Thanks for the video.
For stack, does it have to be directed graph example only?
From movie star to youtube code tutorials. Didn't know Shahrukh Khan was so diverse
Please continue making video lectures
in the case of undirected graph, when u add code to traverse each node that is not visited then why the node 'C' was traversed first (start time of c node is 0). Node 'A' should be traversed first or else i am missing something. The output should be [A,B,D E,C,F]
dfs_util("A")
print(dfs_traversal_output)
are giving me a type error 'dict is not callable'
Bro nice work, why don't you upload more videos??
Sir can i insert the end target node? So we can traverse just to the target? The problem is in recursion whenever i try to stop at the target node, the other node also appended to the dfsoutput can u explain plz?
Using bfs, can I print all *possible* paths to undirected graph node from start to end?
Super explanation. Can i get to know the time & space complexity of this algorithm?
Thanks Ashwini, The time complexity for DFS if O(|V| + |E|), where |V| , |E| are the no of vertices and Edges in the graph respectively.
I will also update this in video description.
@@PyTechVision okay thank-you
@@PyTechVision though we consider 2 extra dictionaries, that won't affect space complexity right?
@@ashwinib.bharmnaikar376 that will add an extra constant space, overall space complexity should remain same.
does this work if you start from any node? (i.e. what if there is no root or its not obvious what the root is - can you just pick a node at random to start?)
Yes, it will.
For the function dfs_util what is the need for writing time+=1 why do we increment after completion of the function? It is unnecessary i think sir can u explain plz!!?
In general DFS it is not needed, but for topological sorting or other application u may need it. I tried to cover all possible cases ( for details you can refer to Introduction to Algorithms by Cormen)
Super explanation......❤️
Thank you 🙂
Sir, How to visualize this graph as a diagram?
no one can explain this topic better than u ......
Thanks @4017 Balaguru
Can you suggest some books on Python for problem-Solving?
elements of programming interview in python
thanks for video
Most welcome @Rebaz Abbas
hlo sharukh sir , i try to write some code for dfs only ; is it also giving write output ; but still dont know if it pass all cases or not ; can you please check ?
#code ; here root means starting vertex
#adjacency list using set
class Graph :
def __init__(self , isdirected = False):
self.graph = {}
self.isdirected = isdirected
self.parent = {}
self.level = {}
def add_vertix(self,vertex):
self.graph[vertex] = set({})
def add_edge(self,u,v):
self.graph[u].add(v)
if self.isdirected == False :
self.graph[v].add(u)
def find_degree(self,vertex):
return len(self.graph[vertex])
def printgraph(self):
for x in self.graph :
print(str(x) + ' --> ' + str(self.graph[x]))
def bfs(self,root = None):
ans = []
if root is None :
for x in self.graph :
root = x
break
q = [root] ; self.level[root] = 0
while q :
vertex = q.pop(0)
ans.append(vertex)
for x in self.graph[vertex] :
if (x not in q) and (x not in ans):
self.parent[x] = vertex
self.level[x] = self.level[vertex] + 1
q.append(x)
return ans
def dfs(self,root = None ):
if root is None :
for x in self.graph :
root = x
break
stack = [root] ; v = root ; ans = [v]
while stack :
bool1 = True
for x in self.graph[v] :
if (x not in stack) and (x not in ans) :
bool1 = False
self.parent[x] = v
v = x
ans.append(v)
stack.append(v)
break
if bool1 :
v = stack.pop(-1)
return ans
g = Graph(True)
g.add_vertix('a') ; g.add_vertix('b') ; g.add_vertix('c') ; g.add_vertix('d') ;
g.add_vertix('e') #; g.add_vertix('b')
g.add_edge('b' , 'a') ;
g.add_edge('a' , 'e') ; g.add_edge('b' , 'c') ; g.add_edge('b' , 'e')
g.add_edge('e' , 'c') ; g.add_edge('c' , 'd') ; g.add_edge('d' , 'e')
g.printgraph()
#print(g.find_degree('d'))
#print(g.bfs())
#print(g.level)
print(g.dfs())
print(g.parent)
How you are compiling the code... In which software
he is using Jupiter notebook on anaconda software
jupyter.org/
what does ".append" work?? For what ".append" is used??
used to add something to a list at the end. Here, adj_list.append(whatever you wanna add to the list)
can we use same code
for undirected graph ?
Yes
Can you please upload implementation of Dijkstra algo and Floyd warshall algo also.
Sure Rachana, I will upload soon
Great stuff!
Thanks Mark.
One word.perfect.
Thanks
You are awesome brother!!!!!
You too!!
plz give the code file.......I have done same to same like your code but error showing
i am getting error
NameError: name 'dfs_transversal_output' is not defined
Spell check, it's dfs_traversal_output as opposed to dfs_transversal_output
very nice!
Thanks Pangea
Make video on how to delete node in a tree in python bro !!!
Seriously ... Just now I wanted to ask ... Please make it bro !
Sure, I will start Tree Data Structure soon.
Thank you so much
Welcome!
you can send face book for me