0% found this document useful (0 votes)
101 views20 pages

The Shortest Path Between Two Nodes AMD Algorithm: School of Computers and Information Engineering

This document summarizes three algorithms for finding the shortest path between nodes in a graph: Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. It also proposes a new AMD algorithm that finds the shortest path based on distance displacement rather than edge weights. Key points: - Dijkstra's algorithm finds the shortest path from a single source node to all other nodes but does not work for graphs with negative edge weights. - Bellman-Ford algorithm can find the shortest path even when negative edge weights are present, but no negative cycles are allowed. - Floyd-Warshall algorithm finds the shortest paths between all pairs of nodes, allowing for negative edge weights but no
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views20 pages

The Shortest Path Between Two Nodes AMD Algorithm: School of Computers and Information Engineering

This document summarizes three algorithms for finding the shortest path between nodes in a graph: Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. It also proposes a new AMD algorithm that finds the shortest path based on distance displacement rather than edge weights. Key points: - Dijkstra's algorithm finds the shortest path from a single source node to all other nodes but does not work for graphs with negative edge weights. - Bellman-Ford algorithm can find the shortest path even when negative edge weights are present, but no negative cycles are allowed. - Floyd-Warshall algorithm finds the shortest paths between all pairs of nodes, allowing for negative edge weights but no
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

School of Computers and Information Engineering

The shortest path between two nodes


AMD algorithm

Malika Sadullaeva Abdullokh Askarov Dilmurod Nasibullaev


[email protected] [email protected] d.nasibullayev @student.inha.uz

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.

Existing Methods of finding shortest path:


One way of finding the shortest path is Dijkstra algorithm. Dijkstra’s algorithm is a
greedy type algorithm: searches for the best result. Algorithm finds its implementation
in computer and mobile networks. But it does not take into account the negative edge
weights.

Algorithm (Shortest-path-tree(from source to all other nodes))


Initialize(G, s);
S := ; Q := V[G]; while Q  
do u := Extract-Min(Q);
S := S  {u}; for each v 
Adj[u] do Relax(u,
v, w) od
od
By creating empty set S we will store vertices whose shortest path has been determined.
Every time we will selects u from V-S with minimum shortest path.
Example: First assume that source vertex is ‘a’ and other nodes unreachable

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

So, our shortest-path tree


Running time:
1. Since extract-minimum is a linear search through all vertex of Q, its running time in
worst case O(|V|^2)
2. Q is an array which contains a vertex set, so its running time is O(|E|) So, the running
time is O(V|^2+|E|)

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

SA 3
SE 4
ED 1
DA -4
CA 2
DC -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

Where V is a number of vertices in a given graph.

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

M[2,2] compare M[2,1] + M[1,2]


0 < 4 + ∞
Since, zero is less than for plus infinity, distance do not updated

M[2,3] compare M[2,1]+M[1,3]


1 < 4 + 0
Because, one is less than four distance does not updated to the new value.
In this fashion we will update or not update the distances for remaining rows.

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_DEST - VERTEX DETECTION IN A RANGE FROM SOURCE TO DESTINATION ♦

Vert_Det (s, d, r)
{

i ← source.index_num;

while( node[i].x >= source.x && node[i].x <= destination.x )


{

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++);

Int y’ = a*node[i].x + b; // y’ – position of our displacement line at point x on y axis

If(y’ < node[i].y && y’ + r >node[i].y )


{
// IsConnected() – is checking whether node is connected with nodes in
Node_List{} or not
Connected ← IsConnected(node[i]);

If (Connected) { Node_List_Up{} ← node[i]; }


If(PathFound( Node_List_Up{} )) {break;}

If(y’ > node[i].y && node[i].y > y’ - r)


{

Connected ← IsConnected(node[i]);

If (Connected) { Node_List_Low{} ← node[i]; }


If(PathFound( Node_List_Low{} )) {break;}
}

If(y’ – r < node[i].y && node[i].y < y’ + r)


{
Connected ← IsConnected(node[i]);

If (Connected) { Node_List{} ← node[i]; }


If(PathFound( Node_List{} )) {break;}
}
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, j1,k0
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

Find the shortest distance between A and F using AMD algorithm.

12
Solution
Firstly, we should construct our SD-line. By knowing A(2,2) and F(10,10), find a nd b.

y=ax+b By solving,this system of equations we obtain that a = 1


and b=0

Then,we index all nodes from left to right

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: AEF

16
Limitations:

17
18
Complexity (Run time on Mathematical Model)

Complexity AMD Function Description a function

Calculates and returns a,b


O(1) Sp_Line(s, d) in the system of equations of
y=ax+b
Check if a nodes connected with
O(E) IsConnected(*node) each other.

Checks if nodes connected with


O(E) PathFound( Node_List{}) source and dest,return true or
false
Find the shortest
O( ) Vert_Det (s, d, r) distance.Contains inside
IsConnected function and
PathFound(Node_List{})

Complexity= O(1)+ O(E)( O(E)+O(E))= O( )

 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

You might also like