- #1
Henry R
- 25
- 0
Hello... how to write the function of depth first search?
Thank you.
Thank you.
Henry R said:Hello... how to write the function of depth first search?
Thank you.
Depthfirstsearch(G)
for each v ∈ V
color[v]=white
p[v]=NIL
time=0
for each v ∈ V
if color[v]=white then
Visit(v)
Visit(u)
color[u]=gray
time=time+1
d[u]=time
for each v ∈ Adj[u]
if color[v]=white then
p[v]=u
Visit(v)
color[u]=black
time=time+1
f[u]=time
Hey! Thanks. But, did u get the code by any websites? Oh okay, I understand. Thanks for helping.evinda said:Hi! (Wave)
That's the algorithm of depth-first-search:
Code:Depthfirstsearch(G) for each v ∈ V color[v]=white p[v]=NIL time=0 for each v ∈ V if color[v]=white then Visit(v)
Code:Visit(u) color[u]=gray time=time+1 d[u]=time for each v ∈ Adj[u] if color[v]=white then p[v]=u Visit(v) color[u]=black time=time+1 f[u]=time
Henry R said:Hey! Thanks. But, did u get the code by any websites? Oh okay, I understand. Thanks for helping.
A graph in computer science is a data structure that consists of a set of vertices (or nodes) connected by edges. It is used to represent relationships between different objects or entities.
Depth first search is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It starts at a chosen vertex and explores its neighbors, then moves to the next unvisited vertex until it has visited all vertices in the graph.
DFS and BFS are both graph traversal algorithms, but they differ in the order in which they visit vertices. DFS explores deeper into a graph before backtracking, while BFS explores all neighbors of a vertex before moving to the next level of vertices.
The time complexity of DFS is O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges in the graph. This means that the time taken for DFS to traverse a graph is directly proportional to the number of vertices and edges in the graph.
DFS is commonly used for solving problems that involve finding a path or cycle in a graph, such as maze solving, topological sorting, and finding connected components. It is also used in many graph algorithms, including minimum spanning tree and shortest path algorithms.