0% found this document useful (0 votes)
22 views5 pages

DSA Shortest Path

The shortest path problem involves finding the shortest route between two vertices in a graph, which can represent various networks. Algorithms like Dijkstra's and Bellman-Ford are used to determine these paths, with Dijkstra's applicable only to graphs with positive edge weights, while Bellman-Ford can handle negative weights but struggles with negative cycles. Negative cycles complicate the problem as they allow for infinite reductions in path weight, making it impossible to determine a definitive shortest path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

DSA Shortest Path

The shortest path problem involves finding the shortest route between two vertices in a graph, which can represent various networks. Algorithms like Dijkstra's and Bellman-Ford are used to determine these paths, with Dijkstra's applicable only to graphs with positive edge weights, while Bellman-Ford can handle negative weights but struggles with negative cycles. Negative cycles complicate the problem as they allow for infinite reductions in path weight, making it impossible to determine a definitive shortest path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/15/25, 7:30 PM DSA Shortest Path

 Tutorials  Exercises  Services   Plus For Teachers Spaces Get Certified Sign Up Log in

C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT

DSA Shortest Path


❮ Previous Next ❯

The Shortest Path Problem


The shortest path problem is famous in the field of computer science.

To solve the shortest path problem means to find the shortest possible route or path between two vertices (or nodes) in a Graph.

In the shortest path problem, a Graph can represent anything from a road network to a communication network, where the vertices can be
intersections, cities, or routers, and the edges can be roads, flight paths, or data links.

4
F
3 2
A C 3
B
5
4 4 4 5

2 5
D E G

The shortest path from vertex D to vertex F in the Graph above is D->E->C->F, with a total path weight of 2+4+4=10. Other paths from D to F
are also possible, but they have a higher total weight, so they can not be considered to be the shortest path.

Solutions to The Shortest Path Problem


Dijkstra's algorithm and the Bellman-Ford algorithm find the shortest path from one start vertex, to all other vertices.

To solve the shortest path problem means to check the edges inside the Graph until we find a path where we can move from one vertex to
another using the lowest possible combined weight along the edges.

This sum of weights along the edges that make up a path is called a path cost or a path weight.

Algorithms that find the shortest paths, like Dijkstra's algorithm or the Bellman-Ford algorithm, find the shortest paths from one start vertex to all
other vertices.

To begin with, the algorithms set the distance from the start vertex to all vertices to be infinitely long. And as the algorithms run, edges between
the vertices are checked over and over, and shorter paths might be found many times until the shortest paths are found at the end.

Every time an edge is checked and it leads to a shorter distance to a vertex being found and updated, it is called a relaxation, or relaxing an
edge.

Positive and Negative Edge Weights


Some algorithms that find the shortest paths, like Dijkstra's algorithm, can only find the shortest paths in graphs where all the edges are positive.
Such graphs with positive distances are also the easiest to understand because we can think of the edges between vertices as distances between
locations.

4
A
3
C
2
4
7 3
5 B
3
D 3
E

[Link] 1/5
1/15/25, 7:30 PM DSA Shortest Path
If we interpret the edge weights as money lost by going from one vertex to another, a positive edge weight of 4 from vertex A to C in the graph
 Tutorials
above means  weExercises
that 
must spend $4 to go from A to C.
Services Plus For Teachers Spaces Get Certified Sign Up Log in

C#ButBOOTSTRAP
graphs can alsoREACT MYSQLedges,
have negative JQUERY EXCEL
and for such XML
graphs DJANGO
the Bellman-Ford NUMPY PANDAS
algorithm can be usedNODEJS R TYPESCRIPT
to find the shortest paths. ANGULAR GIT

4
A
-3
C
-4
4
7 3
5 B
2
D 3
E

And similarly, if the edge weights represent money lost, the negative edge weight -3 from vertex C to A in the graph above can be understood as
an edge where there is more money to be made than money lost by going from C to A. So if for example the cost of fuel is $5 going from C to A,
and we get paid $8 for picking up packages in C and delivering them in A, money lost is -3, meaning we are actually earning $3 in total.

Negative Cycles in Shortest Path Problems


Finding the shortest paths becomes impossible if a graph has negative cycles.

Having a negative cycle means that there is a path where you can go in circles, and the edges that make up this circle have a total path weight
that is negative.

In the graph below, the path A->E->B->C->A is a negative cycle because the total path weight is 5+2-4-4=-1.

A
5
-2
C
-4
1

-4
4
B
7 3
5 5
2
0 3
3
D E

The reason why it is impossible to find the shortest paths in a graph with negative cycles is that it will always be possible to continue running an
algorithm to find even shorter paths.

Let's say for example that we are looking for the shortest distance from vertex D in graph above, to all other vertices. At first we find the distance
from D to E to be 3, by just walking the edge D->E. But after this, if we walk one round in the negative cycle E->B->C->A->E, then the distance
to E becomes 2. After walking one more round the distance becomes 1, which is even shorter, and so on. We can always walk one more round in
the negative cycle to find a shorter distance to E, which means the shortest distance can never be found.

Luckily, the the Bellman-Ford algorithm, that runs on graphs with negative edges, can be implemented with detection for negative cycles.

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

ADVERTISEMENT

[Link] 2/5
1/15/25, 7:30 PM DSA Shortest Path

 Tutorials  Exercises  Services   Plus For Teachers Spaces Get Certified Sign Up Log in

C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT

COLOR PICKER


ADVERTISEMENT

[Link] 3/5
1/15/25, 7:30 PM DSA Shortest Path

 Tutorials  Exercises  Services   Plus For Teachers Spaces Get Certified Sign Up Log in

C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT

India to Thailand
from
from India
View

India to Dubai
from
from India
View

India to Malaysia
from
from India
View

New Delhi to Dubai


from
from New Delhi
View

Nairobi to Namibia
from
from Nairobi
View
*

ADVERTISEMENT ADVERTISEMENT

 PLUS SPACES GET CERTIFIED FOR TEACHERS FOR BUSINESS CONTACT US

Top Tutorials Top References Top Examples Get Certified


HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial [Link] Reference Python Examples Python Certificate
[Link] Tutorial Bootstrap Reference [Link] Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

[Link] 4/5
1/15/25, 7:30 PM DSA Shortest Path
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

 Tutorials  Exercises  Services   Plus For Teachers Spaces Get Certified Sign Up Log in

C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT

[Link] 5/5

You might also like