Professional Documents
Culture Documents
Discrete Report Gaurav Gulati 2K20 - CO - 163 Garvit Batra 2K20 - CO - 159
Discrete Report Gaurav Gulati 2K20 - CO - 163 Garvit Batra 2K20 - CO - 159
BACHELOR OF TECHNOLOGY
in
COMPUTER ENGINEERING
Submi ed by
tt
ti
ACKNOWLEDGEMENT
In performing our major project, we had to take the help and guideline of
some respected persons, who deserve our greatest gra tude. The
comple on of this assignment gives us much pleasure. We would like to
show our gra tude to Mrs. Anuma Garg , Mentor for the this project.
Giving us a good guideline for report throughout numerous consulta ons.
We would also like to extend our deepest gra tude to all those who have
directly and indirectly guided us in wri ng this assignment.
Many people, our classmates and team members itself, have made
valuable comment sugges ons on this proposal which gave us inspira on
to improve our assignment.
We thank all the people for their help directly and indirectly to complete
our assignment.
ti
ti
ti
ti
ti
ti
ti
ti
CERTIFICATE
tt
ti
tt
ti
ti
ti
Introduc on
What are Graphs
A graph can be de ned as group of ver ces and edges that are used to connect
these ver ces. A graph can be seen as a cyclic tree, where the ver ces (Nodes)
maintain any complex rela onship among them instead of having parent child
rela onship.
A graph G can be de ned as an ordered set G(V, E) where V(G) represents the set of
ver ces and E(G) represents the set of edges which are used to connect these
ver ces.
A Graph G(V, E) with 5 ver ces (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following gure.
ti
ti
ti
fi
ti
ti
fi
fi
ti
ti
ti
fi
tt
ti
ti
ti
exists between vertex A and B then the ver ces can be traversed from B to A as well
as A to B.
In a directed graph, edges form an ordered pair. Edges represent a speci c path from
some vertex A to another vertex B. Node A is called ini al node while node B is
called terminal node.
Graph Terminology
Path
A path can be de ned as the sequence of nodes that are followed in order to reach
some terminal node V from the ini al node U.
Closed Path
A path will be called as closed path if the ini al node is same as terminal node. A
path will be closed path if V0=VN.
Simple Path
If all the nodes of the graph are dis nct with an excep on V0=VN, then such path P is
called as closed simple path.
fi
ti
ti
fi
ti
ti
ti
ti
fi
Cycle
A cycle can be de ned as the path which has no repeated edges or ver ces except
the rst and last ver ces.
Connected Graph
A connected graph is the one in which some path exists between every two ver ces
(u, v) in V. There are no isolated nodes in connected graph.
Complete Graph
A complete graph is the one in which every node is connected with all other nodes.
A complete graph contain n(n-1)/2 edges where n is the number of nodes in the
graph.
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight.
The weight of an edge e can be given as w(e) which must be a posi ve (+) value
indica ng the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with
some direc on and the traversing can be done only in the speci ed direc on.
Loop
An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called
as neighbours or adjacent nodes.
A degree of a node is the number of edges that are connected with that node. A
node with degree 0 is called as isolated node.
fi
ti
ti
fi
ti
fi
ti
ti
ti
ti
Graph Representa ons
In graph theory, a graph representa on is a technique to store graph into the
memory of computer.
To represent a graph, we just need the set of ver ces, and for each vertex the
neighbours of the vertex (ver ces which is directly connected to it by an edge). If it is
a weighted graph, then the weight will be associated with each edge.
There are di erent ways to op mally represent a graph, depending on the density of
its edges, type of opera ons to be performed and ease of use.
1. Adjacency Matrix
• It is used to represent which nodes are adjacent to each other. i.e. is there any
edge connec ng nodes to a graph.
Note, even if the graph on 100 ver ces contains only 1 edge, we s ll have to have a
100x100 matrix with lots of zeroes.
• If there is any weighted graph then instead of 1s and 0s, we can store the
weight of the edge.
Example:-
ff
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
In the above examples, 1 represents an edge from row vertex to column vertex, and
0 represents no edge from row vertex to column vertex.
ti
ti
Cons: It takes a lot of space and me to visit all the neighbours of a vertex, we have
to traverse all the ver ces in the graph, which takes quite some me.
2. Incidence Matrix
In Incidence matrix representa on, graph can be represented using a matrix of size:
It means if a graph has 4 ver ces and 6 edges, then it can be represented using a
matrix of 4X6 class. In this matrix, columns represent edges and rows represent
ver ces.
Example
fi
ti
ti
ti
ti
ti
ti
ti
ti
3. Adjacency List
• In this representa on, for each vertex in the graph, we maintain the list of its
neighbors. It means, every vertex of the graph contains list of its adjacent
ver ces.
• We have an array of ver ces which is indexed by the vertex number and for
each vertex v, the corresponding array element points to a singly linked list of
neighbors of v.
Example
Let's see the following directed graph representa on implemented using linked list:
ti
ti
ti
ti
ti
Pros:
• Such kind of representa on is easy to follow and clearly shows the adjacent
nodes of node.
Cons:
• The adjacency list allows tes ng whether two ver ces are adjacent to each
other but it is slower to support this opera on.
In graph theory, the shortest path problem is the problem of nding a path between
two ver ces (or nodes) in a graph such that the sum of the weights of its cons tuent
edges is minimised.
The problem of nding the shortest path between two intersec ons on a road map
may be modelled as a special case of the shortest path problem in graphs, where the
ver ces correspond to intersec ons and the edges correspond to road segments,
each weighted by the length of the segment.
ti
ti
fi
ti
ti
ti
ti
ti
fi
ti
ti
The shortest path problem can be de ned for graphs whether undirected, directed,
or mixed. It is de ned here for undirected graphs; for directed graphs the de ni on
of path requires that consecu ve ver ces be connected by an appropriate directed
edge.
Two ver ces are adjacent when they are both incident to a common edge. A path in
an undirected graph is a sequence of ver ces such that is adjacent too for . Such a
path is called a path of length from too . (The are variables; their numbering here
relates to their posi on in the sequence and needs not to relate to any canonical
labelling of the ver ces).
Let be the edge incident to both and . Given a real-valued weight func on , and an
undirected (simple) graph , the shortest path from to is the path (where and ) that
over all possible minimises the sum When each edge in the graph has unit weight
or , this is equivalent to nding the path with fewest edges.
The problem is also some mes called the single-pair shortest path problem, to
dis nguish it from the following varia ons:
These generalisa ons have signi cantly more e cient algorithms than the simplis c
approach of running a single-pair shortest path algorithm on all relevant pairs of
ver ces. We are going to make a project that provide a complete analysis over
shortest path algorithms on graph, mainly the 3 algorithms djikstra, Floyd Warshall,
Bellman ford , these all aims towards providing shortest path in graph but in a
di erent way.
ff
ti
ti
ti
ti
ti
fi
ti
ti
ti
fi
ti
ti
ti
ti
fi
ti
ti
fi
ti
ti
ffi
fi
fi
ti
ti
ti
fi
fi
ti
ti
Dijkstra’s algorithm
What if you are provided with a graph of nodes where every node is linked to several
other nodes with varying distance. Now, if you begin from one of the nodes in the
graph, what is the shortest path to every other node in the graph?
Well simply explained, an algorithm that is used for nding the shortest distance,
or path, from star ng node to target node in a weighted graph is known as
Dijkstra’s Algorithm.
This algorithm makes a tree of the shortest path from the star ng node, the source,
to all other nodes (points) in the graph. Dijkstra's algorithm makes use of weights of
the edges for nding the path that minimizes the total distance (weight) among the
source node and all other nodes. This algorithm is also known as the single-source
shortest path algorithm.
It is important to note that Dijkstra’s algorithm is only applicable when all weights
are posi ve because, during the execu on, the weights of the edges are added to
nd the shortest path.
And therefore if any of the weights are introduced to be nega ve on the edges of
the graph, the algorithm would never work properly. However, some algorithms like
the Bellman-Ford Algorithm can be used in such cases. It is also a known fact that
breadth- rst search(BFS) could be used for calcula ng the shortest path for an
unweighted graph, or for a weighted graph that has the same cost at all its edges.
But if the weighted graph has unequal costs at all its edges, then BFS infers uniform-
cost search. Now what? Instead of extending nodes in order of their depth from the
root, uniform-cost search develops the nodes in order of their costs from the root.
And a variant of this algorithm is accepted as Dijkstra’s Algorithm.
fi
ff
ti
ti
fi
fi
ti
fi
ti
ti
ti
ti
ti
fi
ti
ti
Also, the es mated distance to every node is always an overvalue of the true
distance and is generally subs tuted by the least of its previous value with the
distance of a recently determined path.
It uses a priority queue to greedily choose the nearest node that has not been
visited yet and executes the relaxa on process on all of its edges.
How to Implement the Dijkstra Algorithm?
Before proceeding the step by step process for implemen ng the algorithm, let us
consider some essen al characteris cs of Dijkstra’s algorithm;
• Basically, the Dijkstra’s algorithm begins from the node to be selected, the
source node, and it examines the en re graph to determine the shortest path
among that node and all the other nodes in the graph.
• The algorithm maintains the track of the currently recognized shortest
distance from each node to the source code and updates these values if it
iden es another shortest path.
• Once the algorithm has determined the shortest path amid the source code to
another node, the node is marked as “visited” and can be added to the path.
• This process is being con nued ll all the nodes in the graph have been added
to the path, as this way, a path gets created that connects the source node to
all the other nodes following the plausible shortest path to reach each node.
ti
ti
fi
fi
fi
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
5. Compare the recently measured distance with the current distance assigned
to the neighbouring node and make it as the new current distance of the
neighbouring node,
6. A er that, consider all of the unvisited neighbours of the current node, mark
the current node as visited,
7. If the des na on node has been marked visited then stop, an algorithm has
ended, and
8. Else, choose the unvisited node that is marked with the least distance, x it as
the new current node, and repeat the process again from step 4.
In the above sec on, you have gained the step by step process of Dijkstra’s
algorithm, now let’s study the algorithm with an explained example. We will
calculate the shortest path between node C and the other nodes in the graph.
1. During the execu on of the algorithm, each node will be marked with its
minimum distance to node C as we have selected node C.
ft
ti
ti
ti
ti
fi
In this case, the minimum distance is 0 for node C. Also, for the rest of the nodes, as
we don’t know this distance, they will be marked as in nity (∞), except node
C (currently marked as red dot).
2. Now the neighbours of node C will be checked, i.e, node A, B, and D. We start
with B, here we will add the minimum distance of current node (0) with the
weight of the edge (7) that linked the node C to node B and get 0+ 7= 7.
Now, this value will be compared with the minimum distance of B (in nity), the least
value is the one that remains the minimum distance of B, like in this case, 7 is
less than in nity, and marks the least value to node B.
fi
ti
fi
fi
3. Now, the same process is checked with neighbour A. We add 0 with 1 (weight
of edge that connects node C to A), and get 1. Again, 1 is compared with the
minimum distance of A (in nity), and marks the lowest value.
fi
Since, all the neighbours of node C have checked, so node C is marked as visited with
a green check mark.
4. Now, we will select the new current node such that the node must be
unvisited with the lowest minimum distance, or the node with the least
number and no check mark. Here, node A is the unvisited with minimum
distance 1, marked as current node with red dot.
We repeat the algorithm, checking the neighbour of the current node while ignoring
the visited node, so only node B will be checked.
For node B, we add 1 with 3 (weight of the edge connec ng node A to B) and obtain
4. This value, 4, will be compared with the minimum distance of B, 7, and
mark the lowest value at B as 4.
ti
ti
Assign Node B a minimum distance value
5. A er this, node A marked as visited with a green check mark. The current
node is selected as node D, it is unvisited and has a smallest recent distance.
We repeat the algorithm and check for node B and E.
For node B, we add 2 to 5, get 7 and compare it with the minimum distance value of
B, since 7>4, so leave the smallest distance value at node B as 4.
For node E, we obtain 2+ 7= 9, and compare it with the minimum distance of E which
is in nity, and mark the smallest value as node E as 9. The node D is marked
as visited with a green check mark.
ft
fi
ti
6. The current node is set as node B, here we need to check only node E as it is
unvisited and the node D is visited. We obtain 4+ 1=5, compare it with the
minimum distance of the node.
We mark D as visited node with a green check mark, and node E is set as the current
node.
So, we are done as no unvisited node is le . The minimum distance of each node is
now represen ng the minimum distance of that node from node C.
Pseudo Code:-
func on dijkstra(G, S)
distance[S] <- 0
ti
fi
ft
previous[V] <- U
Time Complexity:-
The given graph G=(V, E) is represented as an adjacency matrix. Here w[u, v] stores
the weight of edge (u, v).
Let |E| and |V| be the number of edges and ver ces in the graph, respec vely. Then
the me complexity is calculated:
Removing the node with minimal dist takes O(|V|) me, and we only need O(1) to
recalculate dist[u] and update Q. Since we use an adjacency matrix here, we’ll need
to loop for |V| ver ces to update the dist array.
The me taken for each itera on of the loop is O(|V|), as one vertex is deleted from
Q per loop.
Just like Dijkstra’s algorithm, the Floyd Warshall algorithm is used to nd the shortest
path between all ver ces in the weighted graph. This algorithm works with both
directed and undirected graphs but it does not work along with the graph with
nega ve cycles. Therefore, if the distance from the vertex v from itself is nega ve
then we can assume that the graph has the presence of a nega ve cycle. This
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
algorithm follows the dynamic programming approach as its working pa ern. Here
the algorithm doesn’t construct the path itself but it can reconstruct the path with a
simple modi ca on. Floyd Warshall algorithm is also known as Roy Warshall
algorithm or Roy-Floyd algorithm. Let us study the working of the Floyd Warshall
algorithm.
Algorithm
We construct a matrix D that gives the length of the shortest path between each pair
of nodes.
The algorithm ini alizes D to L, that is, to the direct distances between nodes. It then
does n itera ons, a er itera on k, D gives the length of the shortest paths that only
use nodes in {1,2….k} as intermediate nodes.
A er n itera ons, D, therefore, gives the length of shortest paths using any of the
nodes in N as an intermediate node. If Dk represents the matrix D a er kth itera on
it can be implemented by
ti
ti
fi
ti
ti
ft
ti
ti
ft
tt
ti
Crea ng matrix D0 contains the distance between each node with ‘0’ as an
intermediate node.
Upda ng matrix D1 which contains the distance between each node with ‘1’ as an
intermediate node. Update distance if minimum distance value smaller than exis ng
distance value found.
Here distance (3, 2) is updated from in nity to 35, and distance (4, 2) is updated
from in nity to 20 as shown below.
ti
ti
fi
fi
fi
fi
ti
Upda ng matrix D2 contains the distance between two nodes with ‘2’ as an
intermediate node.
Update distance if minimum distance value smaller than exis ng distance value
found. Here distance (1, 3) is updated from in nity to 20, and distance (1, 4) is
updated from in nity to 10.
ti
fi
fi
fi
fi
ti
Upda ng matrix D3 contains the distance between two nodes with ‘3’ as an
intermediate node.
Update distance if minimum distance value smaller than exis ng distance value
found. Here distance (2, 1) is updated from 50 to 45
Upda ng matrix D4 contains the distance between two nodes with ‘4’ as an
intermediate node. Update distance if minimum distance value smaller than exis ng
distance value found.
ti
ti
ti
ti
Here distance (1, 3) is updated from 20 to 15; distance (2, 1) is updated from 45 to
20, and distance (2, 3) is updated from 15 to 10
Pseudo Code:-
Time Complexity
There are three loops for compu ng the shortest path in the graph and each of
these loops has constant complexi es. Therefore, due to this, the me complexity of
the Floyd Warshall algorithm is O(n3). Also, the space complexity of the Floyd
Warshall algorithm is O(n2).
Bellman–Ford Algorithm
Bellman Ford algorithm helps us nd the shortest path from a vertex to all other
ver ces of a weighted graph.
It is similar to Dijkstra's algorithm but it can work with graphs in which edges can
have nega ve weights.
Why would one ever have edges with nega ve weights in real life?
Nega ve weight edges might seem useless at rst but they can explain a lot of
phenomena like cash ow, the heat released/absorbed in a chemical reac on, etc.
For instance, if there are di erent ways to reach from one chemical A to another
chemical B, each method will have sub-reac ons involving both heat dissipa on and
absorp on.
If we want to nd the set of reac ons where minimum energy is required, then we
will need to be able to factor in the heat absorp on as nega ve weights and heat
dissipa on as posi ve weights.
Nega ve weight edges can create nega ve weight cycles i.e. a cycle that will reduce
the total path distance by coming back to the same point.
ti
ti
ti
ti
ti
ti
fi
ti
fl
ff
ti
fi
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
Nega ve weight cycles can give an incorrect result when trying to nd out the
shortest path
Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a
cycle can give an incorrect result because they can go through a nega ve weight
cycle and reduce the path length.
Bellman Ford algorithm works by overes ma ng the length of the path from the
star ng vertex to all other ver ces. Then it itera vely relaxes those es mates by
nding new paths that are shorter than the previously overes mated paths.
By doing this repeatedly for all ver ces, we can guarantee that the result is
op mized.
ti
ti
ti
ti
ti
ti
fi
ti
ti
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 ver ces.
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 des na on vertex to the source vertex to
nd the path.
func on bellmanFord(G, S)
for each vertex V in G
distance[V] <- in nite
previous[V] <- NULL
distance[S] <- 0
fi
ti
ti
ti
Time Complexity
Space Complexity
Applica ons
Dijkstra’s Algorithm Applica ons
• For map applica ons, it is hugely deployed in measuring the least possible distance and
check direc on amidst two geographical regions like Google Maps, discovering map
loca ons poin ng to the ver ces of a graph, calcula ng tra c and delay- ming, etc.
• For telephone networks, this is also extensively implemented in the conduc ng of data in
networking and telecommunica on domains for decreasing the obstacle taken place for
transmission.
• Wherever addressing the need for shortest path explica ons either in the domain of
robo cs, transport, embedded systems, laboratory or produc on plants, etc, this algorithm
is applied.
• Besides that, other applica ons are road condi ons, road closures and construc on, and IP
rou ng to detect Open Shortest Path First.
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ffi
ti
ti
ti
ti
ti
1. Each node calculates the distances between itself and all other nodes within the AS and
stores this informa on as a table.
2. Each node sends its table to all neighboring nodes.
3. When a node receives distance tables from its neighbors, it calculates the shortest routes
to all other nodes and updates its own table to re ect any changes.
ff
fi
fi
fi
ti
ti
ti
ti
ti
fi
ti
fi
ti
fi
fl
ti
ti
ti
fi
ti
fi
ti
ti
ti
fl
Conclusion
The current study focused on two areas: single-source shortest path and the shortest
path among all ver ces [6]. The advantages and disadvantages of an algorithm are
mainly measured from two aspects: the execu on me of the algorithm and the
storage space. By summarizing the papers from others [2] [3] [7], obtaining Table 1.
The Dijkstra algorithm and the Bellman-Ford algorithm are similar in some place.
Both of them use the method of relaxa on calcula on, which is to nd the shortest
path by modifying the values of D[i] during the process of traversing the ver ces and
edges of the graph. The Dijkstra algorithm is mainly aimed at the graph with non-
nega ve weight nodes, while the bellman-ford algorithm can deal with the shortest
path problem with nega ve weights. They are used to draw the op mal solu on of
the shortest path, but the Bellman-Ford algorithm has huge redundancy and lower
e ciency, which is shown in Table 1.
The Dijkstra algorithm can only be used in single-source shortest path problem. But
the Floyd-Warshall algorithm is available to nd a shortest path between any two
points [7]. It is suitable for nding the shortest path among all ver ces or in a small
data scope. In conclusion, the Dijkstra algorithm applies spares graph. In actual
pplica on, the algorithm is always op mized, like heap op miza on. The me
ffi
ti
ti
ti
ti
fi
ti
ti
fi
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
complexity cuts to n*logn [8]. The Bellman-Ford algorithm is ine cient but it is easy
to implement. When dealing with the problem with a large number
References
1. Rosen Discrete Mathema cs And its Applica ons 7th edi on.
2. h p s : / / i o p s c i e n c e . i o p . o r g / a r c l e /
10.1088/1742-6596/1087/2/022011/pdf
3. h ps://brilliant.org/wiki/shortest-path-algorithms/
tt
tt
ti
ti
ffi
ti
ti