difference between BFS and DFS
BFS
DFS
1. DFS stands for Depth First Search.
2. DFS(Depth First Search) uses Stack data structure.
In DFS, we might traverse through more edges to reach a destination vertex from a source.
3. DFS is more suitable when there are solutions away from the source.
4. DFS is more suitable for game or puzzle problems. We make a decision, then explore all paths through this decision. And if this decision leads to win situation, we stop.
5. The Time complexity of DFS is also O(V + E) when Adjacency List is used, where V stands for vertices and E stands for edges.
Here, children are visited before the siblings
Difference between Prim’s and Kruskal’s algorithm for MST
Prim’s Algorithm
Kruskal’s Algorithm
What is the input and output for Dijkstra’s algorithm?
Input: graph = G(V, E) with positive weights.
output: dist array (the shortest distance from the source to each vertex) and prev array(indicating the previous vertex that the shortest path uses to get to each vertex).
What is the running time of Dijkstra’s algorithm using min-heap (aka priority queue) data structure?
O((V+E)*logV)
What is the main idea for Dijkstra’s algorithm?
The main idea of Dijkstra’s algorithm is to find the distances to the nearest nodes, then gradually expand the frontier of the search and determine the shortest paths to nodes that are further and further away. Since edge weights must be positive, there will never be a case where the shortest path must first go through a far away vertex (the path would already be longer than another one that had been found).
What is the main idea for the Depth First Search algorithm?
The DFS algorithm is a recursive algorithm that uses the idea of backtracking.
This recursive nature of DFS can be implemented using stacks. The basic idea is as follows: