Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Modul Shortest Path

The document discusses single-source shortest path (SSSP) algorithms for finding the shortest path from a source vertex to all other vertices in a graph. It describes breadth-first search (BFS) and Dijkstra's algorithm for unweighted and weighted graphs respectively, as well as Bellman-Ford which can handle graphs with negative edge weights. The document also mentions the Floyd-Warshall algorithm for solving the all-pairs shortest path problem.

Uploaded by

Nasywa Syifa
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)
51 views

Modul Shortest Path

The document discusses single-source shortest path (SSSP) algorithms for finding the shortest path from a source vertex to all other vertices in a graph. It describes breadth-first search (BFS) and Dijkstra's algorithm for unweighted and weighted graphs respectively, as well as Bellman-Ford which can handle graphs with negative edge weights. The document also mentions the Floyd-Warshall algorithm for solving the all-pairs shortest path problem.

Uploaded by

Nasywa Syifa
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/ 5

Single-Source Shortest Paths (SSSP)

Given a graph G, we want to find a


shortest path from a given source
vertex s to each vertex v.
• For example
• From node 0, then:
• 0 to 1 : ...
• 0 to 2 : ...
• So on

A complete graph has n(n-1)/2 edges. n = |V|

SSSP Algorithms
• Breadth-First Search
• Dijkstra
• Bellman-Ford

Breadth-First Search (BFS)


• We can use BFS to perform SSSP, but only under a condition
• The graph is unweighted
The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2) when Adjacency
Matrix is used

• We want to find all vertices which we can get to from a starting point, call it A
The algorithm:
• We visit all the closest vertices
first
• Then once all the closest vertices are visited, branch further out
• It is similar with level-order traversal
• We’re going to use a queue
Algorithm:
• Start at a vertex, call it current
• If there is an unvisited neighbor, mark it, and insert it into the queue
• If there is not:
• If the queue is empty, we are done, otherwise:
• Remove a
vertex
from the
queue and
set current
to that
vertex, and
repeat the
process
Bellman-Ford Algorithm
Dijkstra doesn’t work for Graphs with negative weights, Bellman-Ford works for such graphs.
Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems. But time
complexity of Bellman-Ford is O(VE), which is more than Dijkstra. Bellman is a dynamic programming
algorithm. While dijkstra is greedy.

Cannot solve if there is


negative weight cycle
All-Pairs Shortest Path
• The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to find all
pair shortest path problem from a given weighted graph. It is a dynamic programming algorithm
with O(|V|3) time complexity and O(|V|2) space complexity
• As a result of this algorithm, it will generate a matrix, which will represent the minimum distance
from any node to all other nodes in the graph.

Matrix A1 = distance with 1 as intermediate vertex, A2 =


distance with 2 as intermediate vertex (use matrix A1 for
this), A3 = distance with 3 as intermediate, and so on until we
have all four vertexes as intermediate distances.

You might also like