Ads Notes Unit Iii - 220417 - 131717
Ads Notes Unit Iii - 220417 - 131717
,MBA
ADS UNIT-III
Breadth-first search (BFS) is an algorithm that is used to graph data or searching tree or
traversing structures. The full form of BFS is the Breadth-first search.
The algorithm efficiently visits and marks all the key nodes in a graph in an accurate
breadthwise fashion. This algorithm selects a single node (initial or source point) in a graph
and then visits all the nodes adjacent to the selected node. Remember, BFS accesses these
nodes one by one.
Once the algorithm visits and marks the starting node, then it moves towards the nearest
unvisited nodes and analyses them. Once visited, all nodes are marked. These iterations
continue until all the nodes of the graph have been successfully visited and marked.
A graph traversal is a commonly used methodology for locating the vertex position in the
graph. It is an advanced search algorithm that can analyze the graph with speed and precision
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
along with marking the sequence of the visited vertices. This process enables you to quickly
visit each node in a graph without being locked in an infinite loop.
In the various levels of the data, you can mark any node as the starting or initial node to begin
traversing. The BFS will visit the node and mark it as visited and places it in the queue.
Now the BFS will visit the nearest and un-visited nodes and marks them. These values are
also added to the queue. The queue works on the FIFO model.
In a similar manner, the remaining nearest and un-visited nodes on the graph are analyzed
marked and added to the queue. These items are deleted from the queue as receive and
printed as the result.
There are numerous reasons to utilize the BFS Algorithm to use as searching for your dataset.
Some of the most vital aspects that make this algorithm your first choice are:
BFS is useful for analyzing the nodes in a graph and constructing the shortest path of
traversing through these.
ADS UNIT-III
The result of the BFS algorithm holds a high level of accuracy in comparison to other
algorithms.
BFS iterations are seamless, and there is no possibility of this algorithm getting
caught up in an infinite loop problem.
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two categories:
Visited
Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
Start by putting any one of the graph's vertices at the back of a queue.
Take the front item of the queue and add it to the visited list.
Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the
back of the queue.
The graph might have two different disconnected parts so to make sure that we cover every
vertex, we can also run the BFS algorithm on every node
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
STEP1:
STEP2:
ADS UNIT-III
STEP3:
STEP4:
Remaining 0 adjacent and unvisited nodes are visited, marked, and inserted into the queue.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
STEP5:
You mark any node in the graph as root and start traversing the data from it.
BFS traverses all the nodes in the graph and keeps dropping them as completed.
BFS visits an adjacent unvisited node, marks it as done, and inserts it into a queue.
Removes the previous vertex from the queue in case no adjacent vertex is found.
BFS algorithm iterates until all the vertices in the graph are successfully traversed and
marked as completed.
There are no loops caused by BFS during the traversing of data from any node.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Let’s take a look at some of the real-life applications where a BFS algorithm
implementation can be highly effective.
Un-weighted Graphs: BFS algorithm can easily create the shortest path and a
minimum spanning tree to visit all the vertices of the graph in the shortest time
possible with high accuracy.
P2P Networks: BFS can be implemented to locate all the nearest or neighboring nodes
in a peer to peer network. This will find the required data faster.
Web Crawlers: Search engines or web crawlers can easily build multiple levels of
indexes by employing BFS. BFS implementation starts from the source, which is the
web page, and then it visits all the links from that source.
Navigation Systems: BFS can help find all the neighboring locations from the main or
source location.
#include<bits/stdc++.h>
vector <int> adj[MAX]; // adjacency matrix, where adj[i] is a list, which denotes there are
edges from i to each vertex in the list adj[i]
bool visited[MAX]; // boolean array, hacing value true / false, which denotes if a vertex
'i' has been visited or not.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
int i;
init();
queue <int> q;
q.push(start);
while(q.empty() == false){
current_node = q.front();
q.pop();
if(visited[current_node] == true){
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
continue;
cout<<current_node<<" ";
visited[current_node] = true;
next_node = adj[current_node][iterator];
if(visited[next_node] == false) {
q.push(next_node);
int main(){
cin>>vertices;
ADS UNIT-III
cin>>edges;
int i;
cin>>source>>destination;
cout<<"Invalid Edge";
i--;
continue;
adj[source].push_back(destination);
adj[destination].push_back(source);
int start;
cin>>start;
BFS(start);
}
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
DFS uses a strategy that searches “deeper” in the graph whenever possible.
DFS Example-
The depth first search traversal order of the above graph is-
A, B, E, F, C, D
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
DFS (V,E)
do color[v] ← WHITE
π[v] ← NIL
time ← 0
do if color[v] ← WHITE
then Depth_First_Search(v)
Depth_First_Search (v)
color[v] ← GRAY
time ← time + 1
d[v] ← time
do if color[u] ← WHITE
π[u] ← v
Depth_First_Search(u)
color[v] ← BLACK
time ← time + 1
f[v] ← time
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Explanation-
The above depth first search algorithm is explained in the following steps-
Step-01
1. color[v]-
This variable represents the color of the vertex ‘v’ at the given point of time.
The possible values of this variable are- WHITE, GREY and BLACK.
WHITE color of the vertex signifies that it has not been discovered yet.
GREY color of the vertex signifies that it has been discovered and it is being processed.
BLACK color of the vertex signifies that it has been completely processed.
2. Π[v]-
3. d[v]-
3. f[v]-
This variable represents a timestamp when the processing of vertex ‘v’ is completed.
Step-02
color[v] = WHITE
π[v] = NIL
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-03
Repeat the following procedure until all the vertices of the graph become BLACK-
Consider any white vertex ‘v’ and call the following Depth_First_Search function on it.
Depth_First_Search (G,v)
1. color[v] = GRAY
2. time = time + 1
3. d[v] = time
4. For each adjacent WHITE vertex ‘u’ of ‘v’, set π[u] = v and call Depth_First_Search (G,u)
5. color[v] = BLACK
6. time = time + 1
7. f[v] = time
After a DFS traversal of any graph G, all its edges can be put in one of the following 4
classes-
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
1. Tree Edge-
2. Back Edge-
An edge from a vertex ‘u’ to one of its ancestors ‘v’ is called as a back edge.
DFS tries to extend the visit from a vertex ‘u’ to vertex ‘v’
And vertex ‘v’ is found to be an ancestor of vertex ‘u’ and grey at that time.
3. Forward Edge-
An edge from a vertex ‘u’ to one of its descendants ‘v’ is called as a forward edge.
DFS tries to extend the visit from a vertex ‘u’ to a vertex ‘v’
4. Cross Edge-
An edge from a vertex ‘u’ to a vertex ‘v’ that is neither its ancestor nor its descendant is
called as a cross edge.
DFS tries to extend the visit from a vertex ‘u’ to a vertex ‘v’
ADS UNIT-III
Problem-
Also, show the discovery and finishing time for each vertex and classify the edges.
Solution-
Initially for all the vertices of the graph, we set the variables as-
color[v] = WHITE
π[v] = NIL
time = 0 (Global)
Step-01:
color[U] = GREY
time = 0 + 1 = 1
d[U] = 1
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-02:
π[V] = U
color[V] = GREY
time = 1 + 1 = 2
d[V] = 2
Step-03:
π[Y] = V
color[Y] = GREY
time = 2 + 1 = 3
d[Y] = 3
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-04:
π[X] = Y
color[X] = GREY
time = 3 + 1 = 4
d[X] = 4
Step-05:
When DFS tries to extend the visit from vertex X to vertex V, it finds-
ADS UNIT-III
Step-06:
color[X] = BLACK
time = 4 + 1 = 5
f[X] = 5
Step-07:
color[Y] = BLACK
time = 5 + 1 = 6
f[Y] = 6
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-08
color[V] = BLACK
time = 6 + 1 = 7
f[V] = 7
Step-09:
When DFS tries to extend the visit from vertex U to vertex X, it finds-
Vertex X has already been completely processed i.e. vertex X has finished and is black.
ADS UNIT-III
Alternatively,
When DFS tries to extend the visit from vertex U to vertex X, it finds-
Color(X) = BLACK
Step-10:
color[U] = BLACK
time = 7 + 1 = 8
f[U] = 8
Step-11:
color[W] = GREY
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
time = 8 + 1 = 9
d[W] = 9
Step-12:
When DFS tries to extend the visit from vertex W to vertex Y, it finds-
Vertex Y has already been completely processed i.e. vertex Y has finished.
Alternatively,
When DFS tries to extend the visit from vertex W to vertex Y, it finds-
Color(Y) = BLACK
ADS UNIT-III
Step-13:
π[Z] = W
color[W] = GREY
time = 9 + 1 = 10
d[W] = 10
Step-14:
ADS UNIT-III
Step-15:
color[Z] = BLACK
time = 10 + 1 = 11
f[Z] = 11
Step-16:
color[W] = BLACK
time = 11 + 1 = 12
f[W] = 12
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
#include <bits/stdc++.h>
class Graph {
public:
// reachable from v
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
};
void Graph::DFS(int v)
// print it
visited[v] = true;
// to this vertex
list<int>::iterator i;
if (!visited[*i])
DFS(*i);
// Driver code
int main()
{
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.DFS(2);
return 0;
// improved by Vishnudev C
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
TOPOLOGICAL SORT-
if there is an edge in the DAG going from vertex ‘u’ to vertex ‘v’,
Topological Sorting is possible if and only if the graph is a Directed Acyclic Graph.
There may exist multiple different topological orderings for a given directed acyclic graph.
123456
123465
132456
132465
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Instruction Scheduling
Data Serialization
Problem-01:
Find the number of different topological orderings possible for the given graph-
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Solution-
The topological orderings of the above graph are found in the following steps-
Step-01:
Step-02:
ADS UNIT-III
Step-03:
Step-04:
There are two vertices with the least in-degree. So, following 2 cases are possible-
In case-01,
In case-02,
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-05:
Now, the above two cases are continued separately in the similar manner.
In case-01,
In case-02,
ADS UNIT-III
For the given graph, following 2 different topological orderings are possible-
ABCDE
ABDCE
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
PRIM’S ALGORITHM-
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Prim’s algorithm, the given graph must be weighted, connected and undirected.
Step-01:
The vertex connecting to the edge having least weight is usually selected.
Step-02:
Find all the edges that connect the tree to new vertices.
Find the least weight edge among those edges and include it in the existing tree.
If including that edge creates a cycle, then reject that edge and look for the next least weight
edge.
Step-03:
Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree
(MST) is obtained.
ADS UNIT-III
If adjacency list is used to represent the graph, then using breadth first search, all the vertices
can be traversed in O(V + E) time.
We traverse all the vertices of graph using breadth first search and use a min heap for storing
the vertices not yet included in the MST.
To get the minimum weight edge, we use min heap as a priority queue.
Min heap operations like extracting minimum element and decreasing key value takes
O(logV) time.
= O(E + V) x O(logV)
= O((E + V)logV)
= O(ElogV)
This time complexity can be improved and reduced to O(E + VlogV) using Fibonacci heap.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using Prim’s Algorithm-
Solution-
The above discussed steps are followed to find the minimum cost spanning tree using Prim’s
Algorithm-
Step-01:
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-02:
Step-03:
Step-04:
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-05:
Step-06:
Since all the vertices have been included in the MST, so we stop.
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
KRUSKAL’S ALGORITHM-
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Kruskal’s algorithm, the given graph must be weighted, connected and undirected.
Step-01:
Step-02:
Take the edge with the lowest weight and use it to connect the vertices of graph.
If adding an edge creates a cycle, then reject that edge and go for the next least weight edge.
Step-03:
Keep adding edges until all the vertices are connected and a Minimum Spanning Tree (MST)
is obtained.
Connect these vertices using edges with minimum weights such that no cycle gets
formed.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
= O(ElogV) or O(ElogE)
Analysis-
The next edge can be obtained in O(logE) time if graph has E edges.
Special Case-
If the edges are already sorted, then there is no need to construct min heap.
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm-
Solution-
Connect these vertices using edges with minimum weights such that no cycle gets formed.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-01:
Step-02:
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-03:
Step-04:
Step-05:
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-06:
Step-07:
Since all the vertices have been connected / included in the MST, so we stop.
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Shortest path problem is a problem of finding the shortest path(s) between vertices of
a given graph.
Shortest path between two vertices is a path that has the least cost as compared to all
other existing paths.
Shortest path algorithms are a family of algorithms used for solving the shortest path
problem.
Applications-
Google Maps
Road Networks
Logistics Research
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
It is a shortest path problem where the shortest path between a given pair of vertices is
computed.
A* Search Algorithm is a famous algorithm used for solving single-pair shortest path
problem.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
It is a shortest path problem where the shortest path from a given source vertex to all other
remaining vertices is computed.
Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for solving
single-source shortest path problem.
It is a shortest path problem where the shortest path from all the vertices to a single
destination vertex is computed.
By reversing the direction of each edge in the graph, this problem reduces to single-source
shortest path problem.
It is a shortest path problem where the shortest path between every pair of vertices is
computed.
Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for
solving All pairs shortest path problem.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Dijkstra Algorithm-
It computes the shortest path from one particular source node to all other remaining nodes of
the graph.
Conditions-
Dijkstra algorithm works only for those graphs that do not contain any negative weight edge.
The actual Dijkstra algorithm does not output the shortest paths.
By making minor modifications in the actual algorithm, the shortest paths can be easily
obtained.
Dijkstra Algorithm-
ADS UNIT-III
for all v ∈ neighbors[u] // For all the neighboring vertices of vertex 'u'
then dist[v] ← dist[u] + w(u,v) // The new value of the shortest path is
selected
Implementation-
Step-01:
One set contains all those vertices which have been included in the shortest path tree.
Other set contains all those vertices which are still left to be included in the shortest path tree.
In the beginning, this set contains all the vertices of the given graph.
Step-02:
For each vertex of the given graph, two variables are defined as-
d[v] which denotes the shortest path estimate of vertex ‘v’ from the source vertex.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL
The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0
The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] = ∞
Step-03:
The following procedure is repeated until all the vertices of the graph are processed-
Among unprocessed vertices, a vertex with minimum value of variable ‘d’ is chosen.
After relaxing the edges for that vertex, the sets created in step-01 are updated.
Here, d[a] and d[b] denotes the shortest path estimate for vertices a and b respectively from
the source vertex ‘S’.
Now,
ADS UNIT-III
Case-01:
Here,
For each neighbor of i, time taken for updating dist[j] is O(1) and there will be maximum V
neighbors.
Time taken for each iteration of the loop is O(V) and one vertex is deleted from Q.
Case-02:
Here,
With adjacency list representation, all vertices of the graph can be traversed using BFS in
O(V+E) time.
In min heap, operations like extract-min and decrease-key value takes O(logV) time.
So, overall time complexity becomes O(E+V) x O(logV) which is O((E + V) x logV) =
O(ElogV)
ADS UNIT-III
Problem-
Using Dijkstra’s Algorithm, find the shortest distance from source vertex ‘S’ to remaining
vertices in the following graph-
Solution-
Step-01:
Unvisited set : {S , a , b , c , d , e}
Visited set : { }
Step-02:
The two variables Π and d are created for each vertex and initialized as-
d[S] = 0
ADS UNIT-III
Step-03:
Now,
d[S] + 1 = 0 + 1 = 1 < ∞
d[S] + 5 = 0 + 5 = 5 < ∞
ADS UNIT-III
Unvisited set : {a , b , c , d , e}
Step-04:
ADS UNIT-III
Now,
d[a] + 2 = 1 + 2 = 3 < ∞
d[a] + 1 = 1 + 1 = 2 < ∞
d[b] + 2 = 1 + 2 = 3 < 5
Unvisited set : {b , c , d , e}
Visited set : {S , a}
Step-05:
ADS UNIT-III
Now,
d[d] + 2 = 2 + 2 = 4 < ∞
Unvisited set : {b , c , e}
Visited set : {S , a , d}
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Step-06:
Vertex ‘c’ may also be chosen since for both the vertices, shortest path estimate is least.
Now,
d[b] + 2 = 3 + 2 = 5 > 2
∴No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Unvisited set : {c , e}
Visited set : {S , a , d , b}
Step-07:
ADS UNIT-III
Now,
d[c] + 1 = 3 + 1 = 4 = 4
∴No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Visited set : {S , a , d , b , c}
Step-08:
Unvisited set : { }
Visited set : {S , a , d , b , c , e}
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Now,
It represents the shortest path from source vertex ‘S’ to all other remaining vertices.
S , a , d , b , c , e.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
It computes the shortest path between every pair of vertices of the given graph.
ADVANTAGES-
It is extremely simple.
It is easy to implement.
Algorithm-
Create a |V| x |V| matrix // It represents the distance between every pair of vertices as
given
if i = = j
if (i , j) is an edge in E
M[ i ][ j ] = weight(i,j) // If there exists a direct edge between the vertices, value = weight
of edge
else
ADS UNIT-III
if M[ i ][ j ] > M[ i ][ k ] + M[ k ][ j ]
M[ i ][ j ] = M[ i ][ k ] + M[ k ][ j ]
Time Complexity-
Floyd Warshall Algorithm consists of three loops over all the nodes.
This is because its complexity depends only on the number of vertices in the given graph.
ADS UNIT-III
Problem-
Using Floyd Warshall Algorithm, find the shortest path distance between every pair of
vertices.
Solution-
Step-01:
Remove all the self loops and parallel edges (keeping the lowest weight edge) from the
graph.
In the given graph, there are neither self edges nor parallel edges.
Step-02:
It represents the distance between every pair of vertices in the form of given weights.
For vertices having a direct edge between them, distance value = weight of that edge.
ADS UNIT-III
Step-03:
ADS UNIT-III
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Bellman-Ford algorithm is used to find the shortest path from the source vertex to every
vertex in a weighted graph. Unlike Dijkstra's algorithm, the bellman ford algorithm can also
find the shortest distance to every vertex in the weighted graph even with the negative edges.
The only difference between the Dijkstra algorithm and the bellman ford algorithm is that
Dijkstra's algorithm just visits the neighbour vertex in each iteration but the bellman ford
algorithm visits each vertex through each edge in every iteration.
Apart from Bellman-Ford Algorithm and Dijkstra's Algorithm, Floyd Warshall Algorithm is
also the shortest path algorithm. But Bellman-Ford algorithm is used to compute the shortest
path from the single source vertex to all other vertices whereas Floyd-Warshall algorithms
compute the shortest path from each node to every other node.
Bellman ford algorithm follows the dynamic programming approach by overestimating the
length of the path from the starting vertex to all other vertices. And then it starts relaxing the
estimates by discovering the new paths which are shorter than the previous ones. This process
is followed by all the vertices for N-1 times for finding the optimized result.
Algorithm
Begin
count := 1
distance[n] := ∞
pred[n] := ?
done
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
distance[source] := 0
for k := 0 to eCount, do
done
done
count := count + 1
done
return false
End
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
1) Initializing the source vertex with distance to itself as 0 and all other vertices as infinity.
Creating the array with size N
2) Calculate the shortest distance from the source vertex. Following this process for N-1
times where N is the number of vertices in the graph
For relaxing the path lengths for the vertices for each edge m-n:
3) Even after minimizing the path lengths for each vertex after N-1 times, if we can still relax
the path length where distance[n] > distance[m] + weight of edge mn then we can say that the
graph contains the negative edge cycle.
Therefore, the only limitation of the bellman ford algorithm is that it does not work with a
graph having a negative edge cycle.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Example:
Select the source vertex with path value 0 and all other vertices as infinity.
Visit the neighbouring edge from the source vertex and relax the path length of the
neighbouring vertex if the new calculated path length is smaller than the previous path length
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
This process must be followed N-1 times where N is the total number of vertices. This is
because in the worst-case scenario any vertex’s path length can be changed to an even
smaller path length for N times.
Therefore after N-1 iterations, we find our new path lengths and we can check if the graph
has a negative cycle or not.
R.PUSHPANATHAN.,MCA.,M.Phil.,MBA
ADS UNIT-III
Time Complexity
The time complexity of the bellman ford algorithm for the best case is O(E) while average-
case and worst-case time complexity are O(NE) where N is the number of vertices and E is
the total edges to be relaxed. Also, the space complexity of the bellman ford algorithm is
O(N) because the size of the array is N.
Applications
Used for distance-routing protocol helping in routing the data packets on the network