0% found this document useful (0 votes)
37 views8 pages

Shortest Path Algorithms

The document discusses shortest path algorithms like Dijkstra's algorithm. Dijkstra's algorithm is a greedy approach that can find the single-source shortest path from a starting node to all other nodes in a weighted graph with non-negative edge weights. The algorithm works by maintaining a distance table that tracks the shortest known path to each node, prioritizing exploring closer nodes using a priority queue. In each step, it considers updating paths through neighboring nodes until the shortest paths to all nodes are determined. The all-pairs shortest path problem can be solved by running Dijkstra's algorithm from each node individually.

Uploaded by

Phạm Đăng
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)
37 views8 pages

Shortest Path Algorithms

The document discusses shortest path algorithms like Dijkstra's algorithm. Dijkstra's algorithm is a greedy approach that can find the single-source shortest path from a starting node to all other nodes in a weighted graph with non-negative edge weights. The algorithm works by maintaining a distance table that tracks the shortest known path to each node, prioritizing exploring closer nodes using a priority queue. In each step, it considers updating paths through neighboring nodes until the shortest paths to all nodes are determined. The all-pairs shortest path problem can be solved by running Dijkstra's algorithm from each node individually.

Uploaded by

Phạm Đăng
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
You are on page 1/ 8

Shortest Path Algorithms

Single Source Shortest Path


Dijkstra’s Algorithm

Shortest path examples

„ Highway system
– Distance
– Travel time
– Number of stoplights
– Krispy Kreme locations
„ Network of airports
– Travel time
– Fares
– Actual distance

1
Weighted path length
„ Consider an edge-weighted graph
G = (V,E).
„ Let C(vi,vj) be the weight on the edge
connecting vi to vj.
„ A path in G is a non-empty sequence of
vertices P = {v1, v2, v3, …, vk}.
„ The weighted path length is given by
k-1

∑C(v ,v
i=1
i i+1)

The general problem

„ Given an edge-weighted graph


G = (V,E) and two vertices, vs ∈ V and
vd ∈ V, find the path that starts at vs
and ends at vd that has the smallest
weighted path length

Single-source shortest path

„ Given an edge-weighted graph


G = (V,E) and a vertex, vs ∈ V, find the
shortest path from vs to every other
vertex in V

„ To find the shortest path from vs to vd,


we must find the shortest path from vs
to every vertex in G

2
Shortest weighted path
from b to f:
a
{b, a, c, e, f}
3 1 5

b 5 c 2 d

4 5

e 1 f

Shortest weighted path


from b to f:
a
{b, a, c, e, f}
3 1 5

b 5 c 2 d

4 5
Shortest unweighted
path from b to f: 1
e f
{b, c, e, f}

Shortest path problem


undefined for graphs
a
with negative-cost
cycles
3 1 5

b 5 c -8 d

4 5
{d, a, c, e, f} cost: 4
{d, a, c, d, a, c, e, f} -6
e f
cost: 2
{d, a, c, d, a, c, d, a, c, e, f} cost: 0

3
Dijkstra’s Algorithm

„ Greedy algorithm for solving shortest


path problem
„ Assume non-negative weights
„ Find shortest path from vs to each other
vertex

Dijkstra’s Algorithm

„ For each vertex v, need to know:


– kv: Is the shortest path from vs to v known?
(initially false for all v ∈ V)
– dv: What is the length of the shortest path
from vs to v? (initially ∞ for all v ∈ V, except
vs = 0)
– pv: What vertex precedes (is parent of) v on
the shortest path from vs to v? (initially
unknown for all v ∈ V)

v kv dv pv a

a F ∞ 3 1 5

b F 0
b 5 c 2 d
c F ∞
4 5
d F ∞
e 1 f
e F ∞
f F ∞

4
v kv dv pv a

a F 3 b 5
3 1

b T 0 --
b 5 c 2 d
c F 5 b
4 5
d F ∞
e 1 f
e F ∞
f F ∞

v kv dv pv a

a T 3 b 5
3 1

b T 0 --
b 5 c 2 d
c F 4 a
4 5
d F 8 a
e 1 f
e F ∞
f F ∞

v kv dv pv a

a T 3 b 5
3 1

b T 0 --
b 5 c 2 d
c T 4 a
4 5
d F 6 c
e 1 f
e F 8 c

f F ∞

5
v kv dv pv a

a T 3 b 5
3 1

b T 0 --
b 5 c 2 d
c T 4 a
4 5
d T 6 c
e 1 f
e F 8 c

f F 11 d

v kv dv pv a

a T 3 b 5
3 1

b T 0 --
b 5 c 2 d
c T 4 a
4 5
d T 6 c
e 1 f
e T 8 c

f F 9 e

v kv dv pv a

a T 3 b 5
3 1

b T 0 --
b 5 c 2 d
c T 4 a
4 5
d T 6 c
e 1 f
e T 8 c

f T 9 e

6
3
v kv dv pv a

a T 3 b

b T 0 -- 6
b 4 c d
c T 4 a

d T 6 c
9
e T 8 c 8 e f

f T 9 e

Dijsktra’s Algorithm
Dijsktra(G,s0)
{
//Initialize
n = |V|; O( )
createtable(n); //stores k,d,p O( )
createpq(|E|); //empty pq O( )
table[s0].d = 0; O( )
insertpq(0,s0); O( )

Dijsktra’s Algorithm
//con’t
while (!pq.isempty) O( )
{
v0 = getMin(); O( )
if (!table[v0].k) //not known O( )
{
table[v0].k = true; O( )
for each vi ∈ Adj[v0] O( )
{
newd=table[v0].d + weight(vi,v0); O( )

7
Dijsktra’s Algorithm
//con’t
// newd=table[v0].d + weight(vi,v0);
if (table[vi].d) > newd) O( )
{
table[vi].d = newd; O( )
table[vi].p = v0; O( )
insertpq(newd,vi); O( )
}
}
}
}

Dijsktra’s Algorithm
//con’t
for each v ∈ G(V,E) O( )
//build vertex set in T
v ∈ T(V,E’);

for each v ∈ G(V,E) O( )


//build edge set in T
(v,table[v].p) ∈ T(V,E’);
}

All-pairs shortest path problem

„ Given an edge-weighted graph


G = (V,E), for each pair of vertices in V
find the length of the shortest weighted
path between the two vertices

Solution: Run Dijkstra V times

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy