The Shortest Path Between Two Nodes AMD Algorithm: School of Computers and Information Engineering
The Shortest Path Between Two Nodes AMD Algorithm: School of Computers and Information Engineering
1
Table of Contents:
Abstact 3
Existing Methods of finding shortest path: 3
OUR METHOD 10
Limitations 17
2
Abstarct:
In real life we always want to find the shortest distance from our current location to
desired one. A computer scientist’s proposed many algorithms that deal with that issue.
For a given graph they put weight on the edges and calculated the shortest path. The
shortest path problem searches for a shortest path between two nodes in a graph, such
that we have the minimum sum of the weights of constituent edges. There are many
areas where shortest path problem can be implemented. One example, for a given map
is to find the shortest route between two points on a map, the services like Google
Maps answer to this problem. Other examples concern the devolopment of a ship
routing algorithm taking into consideration weather conditions or finding the shortest
path between two nodes in a network. Also, there is a shortest path problem in directed
network under arc state uncetainty where costs associated with arc. This leads to a
conclusion about existence real lofe problem for findong the shortest distance.
3
1
a b
2
2 5
c d
3
4 7
e
a b c d e
0 ∞ ∞ ∞
∞
a 1a 2a
b 6b
c 5d 6c
d
1a 2a 5d 6c
• By using relaxation algorithm calculate the distance from ‘a’ to adjacent vertices
• After calculating the distances select those distance with minimum one
• Every time by using relaxation algorithm and selecting the shortest path we find
the shortest-path tree
4
Bellman Ford Algorithm
As was previously mentioned Dijkstar’s algorithm cannot be used for calculating Shortest-path-
tree if a graph contains a negative weight edge. In this case Bellman Ford Algorithm can be
used.
Bellman Ford Algorithm used for finding the shortest path from the source node to all the other
nodes even if a graph has a negative edge. Algorithm can be used for both weighted and
unweighted graphs.
Algorithm:
Bellma-Ford(G,w,s) Initialize-Single-
Sourse(G,s) for i 1 to |V[G]|-1
do for each edge (u,v)
do Relax(u,v,w)
for each edge (u,v)
do if d[v]>d[u]+w(u,v)
then return FALSE
return TRUE
Firstly, we choose the source node, after that we relax edges |V|- 1 times. Finally, we perfom
relaxation on all vertices and check whether the weight of each node changed, in case of
negative change, it means that there is a negative loop.
Example
By creating the table we store how edges are connected to each other
SA 3
SE 4
ED 1
DA -4
CA 2
DC -1
5
Using the Bellman-Ford Algorithm check for presence of negative cycle. Since, graph
contains 5 vertex, there will be 4 itteration in firh one for checking for negative cycle.
Shortest path can be detected if there is no negative cycle, this case occure when there is no
change of the value in the Nth(N ia s number of vertex in a given graph) loop of the graph. If,
obtained value in last itteration is different from previous one, than there is negative cycle and
shortest-path cannot be find.
d[V] i ii iii iv v
S 0 0 0 0 0 0
A ∞ 3S 1D 1D 1D 1D 1D
E ∞ 4S 4S 4S 4S 4S
D ∞ 5E 5E 5E 5E 5E
C ∞ 4D 4D 4D 4D 4D
In the giving graph we have 5 nodes, so 4 itteration should be given and last one for checking
negative cycle. Since, no changes occurred after second itteration, shortest path can be found.
Time Complexety:
Time complexety of Bellman-Ford algorithm is O(VE)
Where V is the total number of vertices Where E is
the total number of edges.
Floyd-Warshall algorithm
If Dijkstra’s and Bellman-Ford algorithms find the shortest path from one to all nodes, then
Floyd-Warshall algortithm detect the shortest path between all pairs of vertices, where can be
negative edges also. In all three algorithms negative cycle’s is not allowed.
Algorithm
let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity) for
each edge (u,v) dist[u][v] ← w(u,v) // the weight of the edge (u,v) for each
vertex v
dist[v][v] ← 0 \\ the distance from one node to itself is 0
for k from 1 to |V| for
i from 1 to |V| for j
from 1 to |V|
if dist[i][j] > dist[i][k] + dist[k][j]
dist[i][j] ← dist[i][k] + dist[k][j] end if
6
Example
A B C
A 0 ∞ 1
B 4 0 1
C ∞ ∞ 0
In fist matrix we will take firs row and first column unchanged, in the second matrix we will take
second row and second column unchanged and recalculate the distance for remaining rows,
and substitute if shorter found.
Second Matrix
A B C
A 0 ∞ 1
7
B 4 0 1
C ∞ ∞ 0
Running time O( )
The Dijkstra’s, Bellman-Ford, and Floyd-Warshall algorithm’s find the shortest path between
two nodes taking into the account the weight of the graph.Namely, these algorithms assumes
that shortest path is a path with minimum weighted sum. But in real life the shortest path can
be calculated by finding the displacment between two locations.
8
In AMD algorithm weight is does not taken into account. This algorithm especially useful in
finding the shortest path for example, between the home and educational institution, or to be
more general how to reach desired location from one place to another in real life.
OUR METHOD:
■ ■ AMD SHORTEST PATH ALGORITHM ■ ■
GIVEN:
1. Adjacency matrix of the graph and coordinates of each node are included in their
structure.
2. Indexing goes from left to right
Input ← source, destination ; // two nodes which has to form the path with other nodes
float a,b ; //arguments of function y = ax + b
i ← source.index_num; float
range=0;
path ← false;
9
Node_List_Up{} ← source, destination; //list of nodes which form the shortest path on
upper bound
Node_List_Low{} ← source, destination // on lower bound
Node_List{} ; //list connections of both upper and lower
AMD()
{
Sp_Line (source, destination);
Vert_Det (source, destination, range);
}
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
▬
◄ Functions ►
♦ SP_LINE – DETERMINES DISPLACEMENT FROM SOURCE TO
DESTINATION ♦
Sp_Line(s, d)
{
x1, x2, y1, y2;
x1 ← source.x; x2 ←
destination.x;
y1 ← source.y;
y1 ← destination.y;
FindAB(x1, x2, y1, y2); //returns a & b using system of equations: y=ax+b;
return a,b;
}
Vert_Det (s, d, r)
{
i ← source.index_num;
10
//Path boolean function which tells if path is found or not
If( node[i].x == destination[i].x && path == false )
{
i = source.index_num; Vert_Det(s, d,
r++);
Connected ← IsConnected(node[i]);
IsConnected(*node){
11
Let arr[u][v] be a matrix that conyains the connections if nodes[i] and we will
check if they connected to each other as well as to soutce abd destination int
i 0, j1,k0
list{}=I //create empty list, further store connections
while(i<>arr.length_of_a_column){ point a:
if(arr[i][j]==1){
p[k++] = store connection
if(j <> length_of_a_row){
j++
goto a
}
}
fi
If(j = length_of_a_row){
i++
j=0
goto a
}else{
j++
} fi}
♦PATHFOUND – CHECKS WHETHER SOURCE AND DESTINATION NODES ARE CONNECTED TO OTHER
NODES EXISTING IN A NODE_LIST OR NOT♦
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
▬▬▬▬▬
Example
12
Solution
Firstly, we should construct our SD-line. By knowing A(2,2) and F(10,10), find a nd b.
13
Now, by using Vert_Det (s, d, r), find the shortest distance. We, will start
from B, because B has index 2. So calculate y’ for B,
y’= a*node[i].x + b => y’ = 1*4+0=4
• From y’+range and y’-range draw parralel lines for SD line and if B.y lies on one of that
lines we will save it on List. Let our range equal to 1.
• 4+1=5 and 4-1=3;
• From below graph we see that B does not lays on the parallel to the SD-line lines that
was drawn from 5 and 3
14
Next,calculate the y’ for D: y’=7a+b=7*1+0=7; y’+range=8 and y’-range=6. So, draw lines from 8 and 6
parallel to SD line and if D lays on ; y’+range or ; y’-range, the include D to the list. Else go to next
node;
Now go to E: y’=8a+b=8*1+0=8; y’+range=9 and y’-range=7. So, draw lines from 9 and 7 parallel to SD
line
15
So,we see that E lays on the line that is parallel to the our SD-line=> therefore E is included to the
LIST. In the same fashion we calculate y’ for C and see that C does not lays on the parallel lines.
Therefore our shortest path: AEF
16
Limitations:
17
18
Complexity (Run time on Mathematical Model)
Comment on how important is this subject in computer science and how you will be
benefit after studying this course.
Computer science world has a great deal of problems and a lot of solutions to problem. But
without Computer Algorithm subject it would be very difficult to decide which algorithm is
best.Best means time and complexity. There are many ways to analyze our algoritms, such that
average-case and amortized analysis. So, Computer Algorithms can find the best or optimal
algorithms. We analyze time and complexity because of our computers slow and memory is
limited.
In this course we learned how to connect algorithms to real world problems. Namely, how to
solve existing problem using algorithms, for example finding the shortest path, sorting, or
dynamic programming. Besides the course, we studied other algorithms related to our theme:
Floyd-Warshall, K-means Clustering algorithms. Also, we studied by ourselves that dividing
graph into many subgraph is in NP-problems. In addition, we can now easily calculate the
complexity for a given algorithm.
19
Refrences:
o https://www.geeksforgeeks.org/dijkstras-algorithm-for-adjacency-
listrepresentation-greedy-algo-8/
o https://www.youtube.com/watch?v=2raV0H9KqY8
o https://medium.com/basecs/finding-the-shortest-path-with-a-little-help-
fromdijkstra-613149fbdc8e
o https://www.youtube.com/watch?v=obWXjtg0L64 o
https://www.programiz.com/dsa/bellman-ford-algorithm o
https://brilliant.org/wiki/bellman-ford-algorithm/ o
https://www.youtube.com/watch?v=4OQeCuLYj-4 o
https://www.youtube.com/watch?v=oNI0rf2P9gE
o https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm o Lecture
notes of Professor Ashish
20