Professional Documents
Culture Documents
DISCRETE MATHEMATICS
A-6
SURAJ KUMAR 2K19/CO/396
SURYA KUMAR SAHANI 2K19/CO/399
ACKNOWLEDGEMENT
The success and final outcomes of this project required guidance and
research from many sources and we are extremely privileged to have got
this all along with the completion of this project. All that we have done is
only due to the aforementioned sources.
We respect and thank our teacher Mr. Ajay Kumar, for providing us an
opportunity to do this project work and giving us all the support and
guidance which made us complete the project duly. We are extremely
thankful to her, for if not for her, we would not have come across the
nooks and cranny of our project.
As always, the World Wide Web acted as a lifesaver, for all the great,
helpful articles and materials stored there that immensely aided us in
thoroughly understanding the subject matter, with all the empirical
evidence presented before us, acting as cherries on top of the cake.
In the end, we are extremely grateful for this project itself and for the
insightful knowledge we gained while working on it.
Thank You!
Abstract
The project started with an aim to study the important algorithms
of Graph Theory.
In the initial period of our study, we studied a general
explanation of both algorithms. Also, we got to know the
differences between them and when to use each one. They do
the same work of finding shortest paths yet are different from
each other in both requirement and implementation.
I. Assert the statements made and conclusions drawn are an outcome of our
research work. We further certify that
II. The work contained in the report is original and has been done by us under the
general supervision of our supervisor.
III. The work has not been submitted to any other Institution for any other
degree/diploma/certificate in this university or any other University of India or
abroad.
VI. We have followed the guidelines provided by the university in writing the
report.
V. Whenever we have used materials (data, theoretical analysis, and text) from
other sources, we have given due credit to them in the text of the report and giving
their details in the references.
Code
• Dijkstra’s Algorithm
Code
• Comparison
• Conclusion
• References and Bibliography
Introduction
What is the shortest path problem?
“In graph theory, the shortest path problem is the problem of finding a
path between two vertices (or nodes) in a graph such that the sum of the
weights of its constituent edges is minimized.”
As we can see, the first two steps are to initialize the distances with a
large value and to add the source node to the queue. Next, we perform
iterations until the queue becomes empty. In each iteration, we extract
a node from the queue that has the shortest distance from the source
node.
After that, we visit all the neighbours of the extracted node and check
the new distance we were able to achieve. If the new distance is better
than the old one, we update the distance of this node and push it to the
queue. Finally, the algorithm moves to perform one more iteration until
the queue becomes empty.
After the algorithm ends, we’ll have the shortest paths from the source
node to all other nodes in the graph.
However, since graphs are either weighted or unweighted, we can’t use
the exact same algorithm for both cases. Therefore, we have two
algorithms. BFS calculates the shortest paths in unweighted graphs. On
the other hand, Dijkstra’s algorithm calculates the same thing in weighted
graphs.
Now the next thing we investigated was the weighted graphs and the
unweighted graphs, the differences between them and the way of
implementation through code. We have not included theoretical
explanations of weighted and Unweighted graph to keep the study concise.
BFS Algorithm
When dealing with unweighted graphs, we always care about reducing
the number of visited edges. Therefore, we are sure that all the direct
neighbours of the source node have a distance equal to one. The next
thing that we can be sure about is that all the second neighbours of the
source node have a distance equal to two, and so on.
This idea continues to be valid until we cover all the nodes of the graph.
The only exception is if we reached a node that has been visited before.
In this case, we should ignore it. The reason is that we must have reached
it with a shorter path before.
It’s worth noting that in weighted graphs, where all edges have the same
weight, the BFS algorithm calculates the shortest paths correctly. The
reason is that it cares about reducing the number of visited edges, which
is true in case of equal weights for all edges.
Here we think about the updates that need to be done to our general
algorithm which we studied initially. The main approach we follow is to
always start from a node we reached earlier. This looks similar to the
concept of FIFO (First In First Out) that we can find in a simple queue.
Therefore, we use a simple queue with BFS. We can see that there are
no more updates that we should do to our general algorithm.
Since we’re using an ordinary queue, we have an O(1) time complexity for
push and pop operations. Therefore, the total time complexity is O(V +
E), where V is the number of vertices and E is the number of edges in the
graph.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
}
void BFS(vector<int> adj[] , int s , int v){
bool visited[v];
for (size_t i = 0; i < v; i++)
{
visited[i] = false;
}
queue<int> q;
visited[s] = true;
q.push(s);
while (q.empty() == false)
{
int u = q.front();
q.pop();
cout<<u<<" ";
for(int v : adj[u]){
if (visited[v] == false)
{
visited[v] = true;
q.push(v);
}
}
}
}
int main(){
int v = 7;
vector <int>adj[v];
addEdge(adj , 0 , 1);
addEdge(adj , 0 , 2);
addEdge(adj , 2 , 3);
addEdge(adj , 3 , 4);
addEdge(adj , 4 , 5);
addEdge(adj , 4 , 6);
addEdge(adj , 5 , 6);
cout<<"Graph : {\n";
//print all the connected vertices
printAdj(adj , v);
cout<<"\t}\n";
cout<<"======================\n";
//print BFS
cout<<"BFS : ";
BFS(adj , 0 , v);
return 0;
}
Output :
Dijkstra’s Algorithm
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
return min_index;
}
dijkstra(graph, 0);
return 0;
}
Output :
Example
Take a look at the following graph. It contains an example of applying
the BFS algorithm to a simple, unweighted graph. The letter
corresponds to the node index, while the number shows the stored
distance after performing the BFS algorithm:
We can clearly see that the algorithm visits the nodes, level by level.
First, all the neighbors are visited on level 1. Next, all the second
neighbors are visited on level 2, and so on. As a result, we’re able to
calculate the shortest path to all nodes starting from the source node .
Now, let’s take a look at the result of applying Dijkstra’s algorithm to
the weighted version of the same graph:
Although node A is one step away from the source S, Dijkstra’s algorithm
first explored node B because the edge to B has the lowest cost.
Next, from B, it found a shorter path to node A, which is the path that
the algorithm stored.
We can also note that although it’s one step away from S, we didn’t
visit C directly. The reason is that the edge between S and C has a very
large cost. Luckily, we were able to reach C from a shorter path than
the direct edge.
Comparison
The following table shows a summarized comparison between the two
algorithms:
Conclusion
In this study, we presented the general algorithm for BFS and Dijkstra’s
algorithms. Next, we explained the main similarities and differences
between the two algorithms.
https://stackoverflow.com/questions/25449781/what-is-difference-between-
bfs-and-dijkstras-algorithms-when-looking-for-shorte/43752467
Pathfinding Algorithms
https://medium.com/swlh/pathfinding-algorithms-
6c0d4febe8fd#:~:text=Path%20finding%20algorithms%20are%20important,t
o%20other%20applications%20as%20well