single-source shortest paths Flashcards
all shortest path algorithms (14 cards)
What is the primary purpose of shortest-path algorithms?
To find the shortest path between nodes in a graph.
True or False: Dijkstra’s algorithm can handle negative weight edges.
False.
Fill in the blank: The __________ algorithm is used for finding the shortest paths from a single source vertex to all other vertices in a graph with negative weight edges.
Bellman-Ford
Which algorithm is generally more efficient for graphs with non-negative weights, Dijkstra’s or Bellman-Ford?
Dijkstra’s algorithm.
What is the time complexity of Dijkstra’s algorithm using a priority queue?
O((V + E) log V), where V is the number of vertices and E is the number of edges.
what is the psduecode for bellman ford
BELLMAN-FORD.G; w; s/
1 INITIALIZE-SINGLE-SOURCE.G; s/
2 for i D 1 to jG:Vj 1
3 for each edge .u; / 2 G:E
4 RELAX.u; ; w/
5 for each edge .u; / 2 G:E
6 if :d > u:d C w.u; /
7 return FALSE
8 return TRUE
I Eat Really Cold Ice For Good
“I Eat Really Cold Ice For Good “
Breakdown:
I — Initialize single source
E — Edges loop |V| - 1 times
R — Relax each edge
C — Check for negative cycles
I — If d[v] > d[u] + w(u,v)
F — False → negative cycle
V — Valid path → no neg cycle
G — Good to return TRUE
T — Termination success
what is the pedosocde for INITIALIZE-SINGLE-SOURCE
INITIALIZE-SINGLE-SOURCE.G; s/
1 for each vertex v E G.V
v.d = infity
v.pi =NIL
s.d = 0
path relaztion proof
=[=p—
what is the pedosocde for Relax ?
RELAX(u,v,w)
if v.d > u.d +w(u,v)
v.d = u.d+w(u,v)
v.pi=u
psdeocode for dag shirest path
DAG-SHORTEST-PATH(G,w,s)
topologically sort the vertices
INIT-SINGLE-SOURCE(G,s)
for each vertex u, taken in topicallay sorted order
for each vertex v E G.adj[u]
relax(u,v,w)
corretness for dag shirtest path
- becausse we process vertices in topoglovally sorted order,edges of any path must be relaxed in order of appearance in the path.
- edges on any shortest path are relaxed in order
by path relaztio theroem, correct
psedocode for dikstra algoritm
DIJKSTRA(G, w, s):
INITIALIZE-SINGLE-SOURCE(G, s)
S = {} // The set of processed vertices
Q = G:V // The priority queue with all vertices
while Q ≠ ∅:
u = EXTRACT-MIN(Q) // Extract the vertex with the minimum distance
S = S ∪ {u} // Add u to the set of processed vertices
for each v ∈ G:Adj[u]:
RELAX(u,v,w) // Relaxation step
do example on page 680