Professional Documents
Culture Documents
Background Theory
Background Theory
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.
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”.
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.
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
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
A
4
7
1 C
B
2
6
D
E
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
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
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
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
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.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.
d=R∗c Equation
4.5
Where,
a = the square of half of the straight-line distance between the two points
31
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.