codeIIEST/Algorithms

View on GitHub
Competitive Coding/Graphs/Graph_Search/DepthFIrstSearch/README.md

Summary

Maintainability
Test Coverage
## Depth-first search (DFS)
 An algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking

----
![](https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif)
----
This is the algorithm for Depth First Search in a given graph.

* We take a starting vertex , and push all its adjacent vertexes in a stack.
* Now we pop an element from a stack and push all its vertexes in the stack , and we also mark down these vertexes as visited.
* After executing the code, we will get the vertex visited in a depth first manner.

Some real life applications of DFS are as follows:
1) For an unweighted graph, DFS traversal of the graph produces the minimum spanning tree and all pair shortest path tree.

2) Detecting cycle in a graph 
A graph has cycle if and only if we see a back edge during DFS. So we can run DFS for the graph and check for back edges. (See this for details)

3) Path Finding
We can specialize the DFS algorithm to find a path between two given vertices u and z.
i) Call DFS(G, u) with u as the start vertex.
ii) Use a stack S to keep track of the path between the start vertex and the current vertex.
iii) As soon as destination vertex z is encountered, return the path as the
contents of the stack

See this for details.

4) Topological Sorting
Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs. In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to perform in makefiles, data serialization, and resolving symbol dependencies in linkers [2].

5) To test if a graph is bipartite
We can augment either BFS or DFS when we first discover a new vertex, color it opposited its parents, and for each other edge, check it doesn’t link two vertices of the same color. The first vertex in any connected component can be red or black! See this for details.

6) Finding Strongly Connected Components of a graph A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex. (See this for DFS based algo for finding Strongly Connected Components)

7) Solving puzzles with only one solution, such as mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.)

[more info](https://en.wikipedia.org/wiki/Depth-first_search)