Breadth First Search: BFS Pseudocode

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Breadth first search

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

BFS Algorithm Complexity


The time complexity of the BFS algorithm is represented in the form of O(V + E), where V is the
number of nodes and E is the number of edges.

The space complexity of the algorithm is O(V).

BFS Algorithm Applications


1. To build index by search index
2. For GPS navigation
3. Path finding algorithms
4. In Ford-Fulkerson algorithm to find maximum flow in a network
5. Cycle detection in an undirected graph
6. In minimum spanning tree algorithm

Depth First Search (DFS)


Depth first Search or Depth first traversal is a recursive algorithm for searching all the vertices of
a graph or tree data structure. Traversal means visiting all the nodes of a graph.

DFS Pseudocode (recursive implementation)


The pseudocode for DFS is shown below. In the init() function, notice that we run the DFS
function on every node. This is because the graph might have two different disconnected parts
so to make sure that we cover every vertex, we can also run the DFS algorithm on every node.

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.

How Dijkstra's Algorithm works


Dijkstra's Algorithm works on the basis that any subpath B -> D of the shortest path A -> D
between vertices A and D is also the shortest path between vertices B and D.

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.

Djikstra's algorithm pseudocode


We need to maintain the path distance of every vertex. We can store that in an array of size v,
where v is the number of vertices.

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.

Dijkstra's Algorithm Complexity


Time Complexity: O(E Log V)

where, E is the number of edges and V is the number of vertices.

Space Complexity: O(V)

Dijkstra's Algorithm Applications


● To find the shortest path
● In social networking applications
● In a telephone network
● To find the locations in the map

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

Floyd Warshall Algorithm Complexity


Time Complexity

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).

Floyd Warshall Algorithm Applications


● To find the shortest path is a directed graph
● To find the transitive closure of directed graphs
● To find the Inversion of real matrices
● For testing whether an undirected graph is bipartite

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

● form a tree that includes every vertex


● has the minimum sum of weights among all the trees that can be formed from the graph

Kruskal Algorithm Pseudocode


Any minimum spanning tree algorithm revolves around checking if adding an edge creates a
loop or not.

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.

Kruskal's Algorithm Complexity


The time complexity Of Kruskal's Algorithm is: O(E log E)

Kruskal's Algorithm Applications


● In order to layout electrical wiring
● In computer network (LAN connection)

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

● form a tree that includes every vertex


● has the minimum sum of weights among all the trees that can be formed from the graph

Prim's Algorithm pseudocode


The pseudocode for prim's algorithm shows how we create two sets of vertices U and V-U. U
contains the list of vertices that have been visited and V-U the list of vertices that haven't. One
by one, we move vertices from set V-U to set U by connecting the least weight edge.

Prim's vs Kruskal's Algorithm


Kruskal's algorithm is another popular minimum spanning tree algorithm that uses a different
logic to find the MST of a graph. Instead of starting from a vertex, Kruskal's algorithm sorts all
the edges from low weight to high and keeps adding the lowest edges, ignoring those edges
that create a cycle.

Prim's Algorithm Complexity


The time complexity of Prim's algorithm is O(E log V).

Prim's Algorithm Application


● Laying cables of electrical wiring
● In network designed
● To make protocols in network cycles

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

It is the path available in a flow network.


Residual Graph

It represents the flow network that has additional possible flow.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy