Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

CHAPTER 3

BACKGROUND THEORY

The shortest path computations are one of the problems in graph theory. In
shortest path problems, a directed weighted graph is given and the goal is to
determine the shortest path among vertices (nodes). The shortest path problem can be
categorized into two different problems; single-source shortest path problem and all
pair shortest problems. In single-source shortest path problems, a graph is being given
and the goal is to find a shortest path from a given fixed vertex to all other vertices of
the graph. In all pair shortest path problems, the goal is to find the shortest paths
between all pairs of vertices of a graph.

3.1. Introduction to Shortest Path System


Dijkstra`s algorithm is used in SPF, shortest path first. There are two types of
routing, Link State routing and Distance Vector routing. Dijkstra`s algorithm is based
on Link State routing. In the Link-state routing approach, using Dijkstra`s algorithm
each router calculates the shortest path to each node into the route table. Dijkstra’s
algorithm is a graph search algorithm that solves the single-source shortest path
problem for a graph with nonnegative edge path costs, producing a shortest path tree.
The shortest distance between two points is a straight line. But in the real world, if
those two points are located at opposite ends of the country, or even in different
neighborhoods, it is unlikely to find a route that enables to travel from origin to
destination via one straight road. Find out a map to determine the fastest way to drive
somewhere, but these days, it is just as likely to use a Web-based service or a
handheld device to help with driving directions. The popularity of mapping
applications for mainstream consumer use once again has brought new challenges to
the research problem known as the “shortest-path problem” are increasingly present in
industrial and service robotics.
Several mobility configurations can be found in different Shortest Path Finding
and Tracking System Based on Dijkstra's Algorithm for Mobile Robot applications.
18

E. Hart et. al [34] defined that the shortest distance between two points is a
straight line. But in the real world, if those two points are located at opposite ends of
the country, or even in different neighborhoods, it is unlikely to find a route that
enables to travel from origin to destination via one straight road. Find out a map to
determine the fastest way to drive somewhere, but these days, it is just as likely to use
a Web-based service or a handheld device to help with driving directions. The
popularity of mapping applications for mainstream consumer use once again has
brought new challenges to the research problem known as the “shortest-path
problem”.

3.2. Overview of Dijkstra’s Algorithm


Dutch computer scientist Edsger D. published in 1959 [35], introduced
Dijkstra’ algorithm to solve the single-source shortest path problem for a graph with
nonnegative edge path costs, producing a shortest path tree. This algorithm is often
used in routing and as a subroutine in other graph algorithms.
Dijkstra's algorithm is an algorithm for finding the shortest paths between
nodes in a graph, which may represent, for example, road networks. This algorithm is
a solution to the single-source shortest path problem in graph theory. The algorithm
exists in many variants; Dijkstra's original variant found the shortest path between
two nodes, but a more common variant fixes a single node as the "source" node and
finds shortest paths from the source to all other nodes in the graph, producing a
shortest path tree.
For a given source node in the graph, the algorithm finds the shortest path
between that node and every other. It can also be used for finding the shortest paths
from a single node to a single destination node by stopping the algorithm once the
shortest path to the destination node has been determined. For example, it the nodes of
the graph represent cities and edge path costs represent driving distances between
pairs of cities connected by a direct road, Dijkstra’s algorithm can be used to find the
shortest route between one city and all other cities.
For a given source vertex (node) in the graph, the algorithm finds the path
with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It
can also be used for finding costs of shortest paths from a single vertex to a single
destination vertex by stopping the algorithm once the shortest path to the destination
vertex has been determined. Dijkstra's algorithm can be used to find the shortest route
19

between one city and all other cities. As a result, the shortest path first is widely used
in network routing protocols, most OSPF (Open Shortest Path First).
Let the node at which are starting be called the initial node. Let the weight of
node Y set the distance from the initial node to Y. Dijkstra's algorithm will assign
some initial distance values and will try to improve them step by step.
 Assign to every node a distance value: set it to zero for the initial node and
to infinity for all other nodes.
 Mark all nodes except the initial node as unvisited. Set the initial node as
current.
 Create a set of the unvisited nodes called the unvisited set consisting of all
the nodes except the initial node.
 For the current node, consider all of its unvisited neighbors and calculate
their tentative distances. For example, if the current node A is marked with
a distance of 6, and the edge connecting it with a neighbor B has length 2,
then the distance to B (through A) will be 6+2=8.
 If this distance is less than the previously recorded distance, then overwrite
that distance. Even though a neighbor has been examined, it is not marked
as visited at this time, and it remains in the unvisited set.
 When users are done considering all of the neighbors of the current node,
mark the current node as visited and remove it from the unvisited set. A
visited node will never be checked again; its distance recorded now is final
and minimal.
 The next current node will be the node marked with the lowest distance in
the unvisited set.
 If the unvisited set is empty, then stop. The algorithm has finished.

3.2.1. Principles of Dijkstra’s Algorithm


A path from a source vertex v to a target vertex u is said to be a shortest path if
the total distance is minimum among all v to u paths. Dijkstra’s algorithm is a classic
shortest path algorithm, which is described in many cases. The order is conceptually
simple to start, mark the distance to every intersection on the map with infinity. This
is done not to imply there is an infinite distance, but to note that that intersection has
not yet been visited. (Some variants of this method simply leave the intersection
20

unlabeled.) Now, at each iteration, select a current intersection. For the first iteration
the current intersection will be the starting point and the distance to it (the
intersection's label) will be zero. For subsequent iterations (after the first) the current
intersection will be the closest unvisited intersection to the starting point this will be
easy to find.
From the current intersection, update the distance to every unvisited
intersection that is directly connected to it. This is done by determining the sum of the
distance between an unvisited intersection and the value of the current intersection,
and relabeling the unvisited intersection with this value if it is less than its current
value. In effect, the intersection is relabeled if the path to it through the current
intersection is shorter than the previously known paths. To facilitate shortest path
identification, in pencil, mark the road with an arrow pointing to the relabeled
intersection if the user label or label it again, and erase all others pointing to it. After
the distances have been updated to each neighboring intersection, mark the current
intersection as visited and select the unvisited intersection with lowest distance (from
the starting point) as the current intersection. Nodes marked as visited are labeled with
the shortest path from the starting point to it and will not be revisited or returned to.
This process of updating the neighboring intersections would be continued
with the shortest distances, then marking the current intersection as visited and
moving onto the closest unvisited intersection until the destination have been marked
as visited. Once the destination have been marked as visited (as is the case with any
visited intersection) the shortest path have been determined to it, from the starting
point, and can trace the way back, following the arrows in reverse.
In the accompanying animated graphic, the starting and destination
intersections are colored in light pink and blue and labeled a and b respectively. The
visited intersections are colored in red, and the current intersection in a pale blue.
Of note is the fact that this algorithm makes no attempt to direct "exploration"
towards the destination as one might expect. Rather, the sole consideration in
determining the next "current" intersection is its distance from the starting point. In
some sense, this algorithm "expands outward" from the starting point, iteratively
considering every node that is closer in terms of shortest path distance until it reaches
the destination. When understood in this way, it is clear how the algorithm necessarily
finds the shortest path, however it may also reveal one of the algorithm's weaknesses:
its relative slowness in some topologies.
21

3.2.2 Pseudocode for Dijkstra’s Algorithm


In common presentations of Dijkstra's algorithm, initially all nodes are entered
into the priority queue and the weights of all nodes are set as infinity. Dijkstra’s
algorithm finds a shortest path tree from a single source node, by building a set of
nodes that have minimum distance from the source. There are vertices, or nodes,
denoted in the algorithm by u or v. Weighted edges that connect two nodes: (u, v)
denotes an edge, and w (u,v) denotes its weight.
This is done by initializing three values:
 dist, an array of distances from the source node to each node in the graph,
initialized the following way: dist (s) = 0; and for all other nodes v , dist
(v) = ∞. This is done at the beginning because as the algorithm proceeds,
the dist from the source to each node v in the graph will be recalculated
and finalized when the shortest distance to v is found.
 Q, a queue of all nodes in the graph. At the end of the algorithm's progress,
the graph will be empty.
 S, an empty set, to indicate which nodes the algorithm has visited. At the
end of the algorithm's run, the empty set S will contain all the nodes of the
graph.
The algorithm proceeds as follows:
 While Q is not empty, pop the node v, that is not already in S, Q from with
the smallest dist (v). In the first run, source node s will be chosen because
dist (s) was initialized to 0. In the next run, the next node with the smallest
dist value is chosen.
 Add node v to S, to indicate that v has been visited
 Update dist values of adjacent nodes of the current node v as follows: for
each new adjacent node u,
 if dist (v) +weight (u,v) < dist (u), there is a new minimal distance found
for u, so update dist (u) to the new minimal distance value;
 Otherwise, no updates are made to dist (u).

The algorithm has visited all nodes in the graph and found the smallest
distance to each node. The distance now contains the shortest path tree from source s.
This is pseudocode for Dijkstra's algorithm, mirroring Python syntax. It can be
used in order to implement the algorithm in any language.
22

function Dijkstra(Graph, source):


dist[source] := 0
for each vertex v in Graph:
if v ≠ source
dist[v] := infinity
add v to Q
while Q is not empty:
v := vertex in Q with min dist[v]
remove v from Q
for each neighbor u of v:
alt := dist[v] + length(v, u)
if alt < dist[u]:
dist[u] := alt
return dist[]
end function

3.3. Graph Theory of Dijkstra’s Algorithm


Dijkstra’ algorithm is based on graph theory. Dijkstra's algorithm can be used
to determine the shortest path from one node in a graph to every other node within the
same graph data structure, provided that the nodes are reachable from the starting
node. A graph is defined as a set of nodes and a set of edges, where each edge is a
pair of nodes. A weighted graph is interesting because it has little to do with whether
the graph is directed, undirected, or contains cycles. At its core, a weighted graph is a
graph whose edges have some sort of value that is associated with them.
The value that is attached to an edge is what gives the edge its “weight”. A
common way to refer to the “weight” of a single edge is by thinking of it as distance
between two nodes. For a given source vertices, it finds the minimum weight, that is
the shortest path, for every other vertices.
Dijkstra's algorithm is sometimes called the single-source shortest path
because it solves the single-source shortest-path difficulty on a subjective, directed
graph (G = V, E).
The single-source shortest path can be computed only if the graph has the
following facts:
23

 Both directed and undirected graphs


 All edges must have nonnegative weights
 Graph must be connected
 Find shortest path from source node to each other vertex

3.3.1. Types of Graphs


There are many variants of graphs depending upon the number of vertices,
number of edges, interconnectivity, and their overall structure.. The first property is
the directionality of its edges as shown in Figure 3.1. The edges can either be
unidirectional or bidirectional. If the edges are unidirectional, the graph is called a
directed graph. If the edges are bidirectional (meaning they go both ways), the graph
is called an undirected graph. In the case where some edges are directed and others
are not, the bidirectional edges should be swapped out for two directed edges that
fulfill the same functionality. That graph is now fully directed.

A
4
7

1 C
B

2
6

D
E

Figure 3.1. Directed Graph

The second property of a graph has to do with the weights of the edges. If the
edges of graph have no weight, the type of graph is called unweighted graph as shown
in Figure 3.2. Otherwise edges have weight, the graph is said to be weighted graph.
There is an extra caveat here: graphs can be allowed to have negative weight edges.
The inclusion of negative weight edges prohibits the use of some shortest path
algorithms. An undirected graph is graph that is contained vertices or nodes that are
connected together, where all the edges are bidirectional.
24

C
B

D
E

Figure 3.2. Unweighted Graph

The third property of graphs that affects what algorithms can be used is the
existence of cycles as shown in Figure 3.3. A cycle is defined as any path through a
graph, G, that visits that same vertex v, more than once. So, if a graph has any path
that has a cycle in it, that graph is said to be cyclic. Acyclic graphs that have no
cycles, allow more freedom in the use of algorithms.

4
C
B
1
7

2
D
E
3
Figure 3.3. Cyclic Graph with Cyclic Path A -> E -> D -> B -> A

3.3.2. Testing Panel of Graph Theory using Dijkstra’s Algorithm


The undirected graph is shown in Figure 3.3. The objective is to find the
shortest paths from origin to all other nodes. To calculate Dijkstra’s algorithm, it
needs to assign zero to initial node (node A) while assigning infinity to all other
nodes. Then calculate a value gradually to get smallest value up to the destination,
which is node G using Dijkstra’ algorithm.
25

Step 1: Node A is set to become current node. Zero is assigned to node A an


infinity to all other nodes. This is shown in Figure 3.4.
∞ ∞
25
B E
16 12
∞ 14 8
0 35 19
D
A G

15 17
9 ∞ ∞ 14
C 22 F

Figure 3.4. Step 1 of Dijkstra’s Algorithm

Step 2: Consider all unvisited neighbors and tentative distance will be calculated.
Previously recorded value will be replaced since new value is less than
infinity. Node A has three neighbor nodes (B, C, D). The algorithm
calculates from node A to its neighbors and assigns result from the
calculation to the nodes of neighbors with new values.

(16, A) 25 ∞
B E
16 8
(35, A) 14
0 35 D 19 ∞
A G

9 15 17
14
C 22 F
(9, C) ∞
Figure 3.5. Step 2 of Dijkstra’s Algorithm

Step 3: Since all neighbors of node A have been taken into account, it is struck as
visited and will not be tested again. The next least distance from node A,
26

node C has the minimum distance among node A’ neighbour node. Node
C now will be marked as current node. Its neighbouring nodes will be
updated with the new minimal distance value. Node C has two neighbour,
D and F.
(16, A) ∞
25
B E
16 12 8
(24, C) 14
0 A 35 D 19 G ∞
(35, A)
15 17 14
9
C F
22
(9, A) (31, C)
Figure 3.6. Step 3 of Dijkstra’s Algorithm

Step 4: Since all neighbors of node C have been taken into account, it is marked as
visited and will not be checked over. The next minimal distance from node
A, node B will now be marked as current node. Its neighboring nodes will
be updated with the new minimal distance.
(16, A) 25 (41, B)

B E
16 12 14 8
(24, C)
0 35 D 19 ∞
A G

9 15 17 14

C F
22
(9, A) (31, C)
Figure 3.7. Step 4 of Dijkstra’s Algorithm

Step 5: Since all neighbours of node D have been accounted for, it is marked as
visited and will not be tested over. The next available minimal space from
27

node B, which is node D now will be taken as present node. Its


neighbouring nodes will be updated with afresh minimal distance value. In
node D, it has three neighbour nodes.
(16, A) (41, B)(38, D)
25
B E

16 12 (24, C) 14 8

D
0 A 35 19 G (43, D)

9 15 17 14

C F
22
(9, C) (41, D)(31, C)
Figure 3.8. Step 5 of Dijkstra’s Algorithm

Step 6: Meanwhile, all neighbours of node F have been taken into account, it is
marked as visited and will not be checked. The next shortest distance from
node D is node F, which will be chosen as current node. Its neighbouring
nodes will be updated with afresh minimal distance value. In node F, it has
one neighbour nodes.

(16, A) 25 (38, D)

B E
16 12 8
(24, C) 14
35 19 (45, F)
D
0 A G (43,D)
9 15 17 14

22
C F

(9, A) (31, C)
Figure 3.9. Step 6 of Dijkstra’s Algorithm
28

Step 7: All neighbours of node E have been taken into account, it is marked as
visited and will not be checked. The next minimal distance from node D,
node E will now be marked as current node. Its neighboring nodes will be
updated with the new minimal distance value.
(16, A) (38, D)
25
B E

16 12 (24, C) 14 8
(46, E)
35 19
0 D (43, D)
A G

9 15 17 14
22

C F

(9, C) (31, C)
Figure 3.10. Step 7 of Dijkstra’s Algorithm

Step 8: The next shortest distance from D is node G. Since all the nodes have been
visited, the shortest route from node A to node G is found. The shortest
path between source node (A) and destination node (G) is 43.

(16, A) (38, D)
25
B E
16
12 (24, C) 14 8

35 D 19
0 A G (43, D)

9 15 17 14
22
C F

(9, A) (31, C)
Figure 3.11. Shortest Path by Dijkstra’s Algorithm
29

Table 3.1. Routing Table for Dijkstra’s Algorithm


Queue(Q) Visited A B C D E F G
nodes (S)
{A,B,C,D, {-} 0,- ∞, - ∞, - ∞, - ∞, - ∞, - ∞, -
E,F,G}
1 {B,C,D, {A} A 16, 9,A 35,A ∞,A ∞,A ∞,A
E,F,G} Ꙟ A
2 {B, D,E, {A, C} B 16, Ꙟ 24,C ∞,C 31,C ∞,C
F,G} A
3 {D,E,F,G} {A,B,C} C 24,C 41,B 31,C ∞,B
4 {E,F,G} {A,B,C,D} D Ꙟ 38,D 31,C 43,D
5 {F,G} {A,B,C, E 38,D 43,D
D,E}
6 {G} {A,B,C, F 43,D
D,E,F}
7 {-} {A,B,C, G Ꙟ
D,E,F,G}

In Table 3.1, it includes Queue(Q), Visited nodes (S) and seven nodes (A to
G). In first step, source node assigns zero and other nodes is assigned infinity. To
calculate shortest path between source node and destination node, it is calculated step
by step using Dijkstra’s algorithm. The shortest distance from Node A to Node G is:
A => C => D => G. The total distance between source node and destination node is
43.

3.4. Advantages and disadvantages of Dijkstra’s Algorithm


Many researchers pointed out that the advantages and disadvantages of
Dijkstra’ algorithm are suggested. The advantages of this algorithm are as follows:
 It is used in Google Maps.
 It is used in finding Shortest Path.
 It is used in geographical Maps.
 To find locations of Map which refers to vertices of graph.
30

 Distance between the locations refers to edges.


 It is used in IP routing to find Open Shortest Path First.
 It is used in the telephone network.
The disadvantages of this algorithm are as follows:
 It does blind search so wastes lot of time while processing.
 It cannot handle negative edges.
 This leads to acyclic graphs and most often cannot obtain the right shortest
path.

3.5. Harversine
Haversine formula is suitable in calculating distance for spatial data because it
provides better accuracy. It assumes the earth to be spherical and ignores the
ellipsoidal effects. It provides the good results in mathematically and computationally.
Let lat1, lon1 be latitude and longitude of a source location and lat2, lon2 be latitude
and longitude of a destination location respectively.

Δ(lat)=lat 2−lat 1 Equation


4.1
Δ(lon)=lon 2 −lon 1 Equation
4.2
2 2
Δ(lat ) Δ(lon)
(
a= sin
2 ) (
+cos( lat1 )cos( lat2 ) sin
2 ) Equation
4.3

c=2atan2 ( √ a, √ 1−a ) Equation


4.4

d=R∗c Equation
4.5

Where,
a = the square of half of the straight-line distance between the two points
31

c = the great circle distance in radians


d = the distance between the two points
R= radius of the earth(R=6371.01km)

In the system, Haversine distance formula is used for measuring the distance
form source to target. The reason of choosing haversine is the calculation based on
ellipsoid geometry. The calculation result is more precise than Euclidian geometry
calculation distance on earth’s surface.

3.6. Summary
In this chapter, the introduction of shortest path system has been described and
Dijkstra’s algorithm has been completely stated. In the next chapter, implementation
and experimental results of shortest path system using Dijkstra’s algorithm will be
described.

You might also like