Breadth First Search: BFS Pseudocode
Breadth First Search: BFS Pseudocode
Breadth First Search: BFS Pseudocode
Traversal means visiting all the nodes of a graph. Breadth First Traversal or Breadth
First Search is a recursive algorithm for searching all the vertices of a graph or tree data
structure.
BFS pseudocode
Dijkstra's Algorithm
Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph.
It differs from the minimum spanning tree because the shortest distance between two vertices
might not include all the vertices of the graph.
Djikstra used this property in the opposite direction i.e we overestimate the distance of each
vertex from the starting vertex. Then we visit each node and its neighbors to find the shortest
subpath to those neighbors.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping
that the end result is the best solution for the whole problem.
We also want to be able to get the shortest path, not only know the length of the shortest path.
For this, we map each vertex to the vertex that last updated its path length.
Once the algorithm is over, we can backtrack from the destination vertex to the source vertex to
find the path.
A minimum priority queue can be used to efficiently receive the vertex with least path distance.
Floyd-Warshall Algorithm
In this tutorial, you will learn how floyd-warshall algorithm works. Also, you will find working
examples of floyd-warshall algorithm in C, C++, Java and Python.
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of
vertices in a weighted graph. This algorithm works for both the directed and undirected weighted
graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in
a cycle is negative).
Floyd-Warshall Algorithm
There are three loops. Each loop has constant complexities. So, the time complexity of the
Floyd-Warshall algorithm is O(n3).
Space Complexity
The space complexity of the Floyd-Warshall algorithm is O(n2).
Kruskal's Algorithm
Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds
the subset of the edges of that graph which
The most common way to find this out is an algorithm called Union Find. The Union-Find
algorithm divides the vertices into clusters and allows us to check if two vertices belong to the
same cluster or not and hence decide whether adding an edge creates a cycle.
Prim's Algorithm
Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the
subset of the edges of that graph which
Ford-Fulkerson Algorithm
Ford-Fulkerson algorithm is a greedy approach for calculating the maximum possible flow in a
network or a graph.
A term, flow network, is used to describe a network of vertices and edges with a source (S)
and a sink (T). Each vertex, except S and T, can receive and send an equal amount of stuff
through it. S can only send and T can only receive stuff.
We can visualize the understanding of the algorithm using a flow of liquid inside a network of
pipes of different capacities. Each pipe has a certain capacity of liquid it can transfer at an
instance. For this algorithm, we are going to find how much liquid can be flowed from the source
to the sink at an instance using the network.
Terminologies Used
Augmenting Path
Residual Capacity
It is the capacity of the edge after subtracting the flow from the maximum capacity.
Ford-Fulkerson Applications
● Water distribution pipeline
● Bipartite matching problem
● Circulation with demands
Edmonds-Karp
• Same with Ford-Fulkerson, only differs in
finding the augmenting path
• Use BFS instead of DFS
Disjoint SET
KMP
O(m+n)
GRAHAM SCAN
complexity O(n log n).
Monotone Chain
Approach: Monotone chain algorithm constructs
the convex hull in O(n * log(n)) time. We have to
sort the points first and then calculate the upper
and lower hulls in O(n) time. The