SlideShare a Scribd company logo
1 of 7
For any help regarding Algorithm Exam Help
Visit :- https://www.programmingexamhelp.com/ ,
Email :- support@programmingexamhelp.com or
call us at :- +1 678 648 4277
Graph Weights Name Running Time O(·)
DAG Any DAG Relaxation |V | + |E|
General Unweighted BFS |V | + |E|
General Non-negative Dijkstra
Bellman-Ford
|V | log |V | + |E|
General Any |V | · |E|
Graph Problems
• Graph reachability by BFS or DFS in O(|E|) time
• Graph exploration/connected components via Full-BFS or Full-DFS
• Topological sort / Cycle detection via DFS
• Negative-weight cycle detection via Bellman-Ford
• Single Source Shortest Paths (SSSP)
Restrictions SSSP Algorithm
• All Pairs Shortest Paths (APSP)
– Run a SSSP algorithm |V | times
– Johnson’s solves APSP with negative weights in O(|V |2 log |V | + |V ||E|)
Graph Problem Strategies
• Be sure to explicitly describe a graph in terms of problem parameters
• Convert problem into finding a shortest path, cycle, topo. sort, conn. comps., etc.
Introduction To Algorithms
programmingexamhelp.com
programmingexamhelp.com
• May help to duplicate graph vertices to encode additional information
• May help to add auxiliary vertices/edges to graph
• May help to pre-process the graph (e.g., to remove part of the graph)
Graph Problem Common Mistakes
• Define your graphs! Specify vertices, edges, and weights clearly (and count them!)
– (e.g., construct graph G = (V, E) with a vertex for each... and a
directed edge (u, v) with weight w for each...)
• State the problem you are solving, not just the algorithm you use to solve it
– (e.g., solve SSSP from s by running DAG Relaxation...)
• Connect the graph problem you solve back to the original problem
– (e.g., the weight of a path from s to t in G corresponds to the sum of tolls paid along a
driving route, so a path of minimum weight corresponds to a route minimizing tolls)
Problem 1. Counting Blobs
An image is a 2D grid of black and white square pixels where each white pixel is contained in
a
blob. Two white pixels are in the same blob if they share an edge of the grid. Black pixels
are not contained in blobs. Given an n × m array representing an image, describe an O(nm)-
time algorithm to count the number of blobs in the image.
Solution: Construct a graph G with a vertex per white pixel, with an undirected edge between
two vertices if the pixels associated with them are both white and share an edge of the grid. This
graph has size at most O(nm) vertices and at most O(nm) edges (as pixels share edges with at
most four other pixels), so can be constructed in O(nm) time. Each connected component of this
graph corresponds to a blob, so run Full-BFS or Full-DFS to count the number of connected
components in G in O(nm) time
programmingexamhelp.com
Problem 2. Unicycles
Given a connected undirected graph G = (V, E) with strictly positive weights w : E → Z+ where
|E| = |V |, describe an O(|V |)-time algorithm to determine a path from vertex s to vertex t of
minimum weight.
Solution: Given two vertices in a weighted tree containing only positive weight
edges, there is a unique simple path between them which is also the minimum weight path.
A depth-first search from any source vertex s in the tree results in a directed DFS tree in
O(|V |) time (since
|E| = |V | − 1). Then relaxing edges in topological sort order of the directed DFS tree
computes minimum weight paths from s in O(|V |) time. Since G has one cycle, our strategy
will be to break the cycle by removing an edge, and then compute the minimum weight path
from s to t in the resultant tree.
First, we find the vertex v closest to s on the cycle by running depth-first search from s in
O(|V |) time (since |E| = |V |). One edge e1 of the cycle will not be in the tree returned by
DFS (a back edge to v), with the other edge of the cycle incident to v being a single outgoing
DFS tree edge e2. If s is on the cycle, v = s; otherwise the unique path from s to v does not
programmingexamhelp.com
contain e1 or e2. A shortest path from s to t cannot traverse both edges e1 and e2, or else the
path would visit v at least twice, traversing a cycle of positive weight. Removing either e1 or e2
results in a tree, at least one of which contains the minimum weight path from s to t. Thus, find
the minimum weight path from s to t in each tree using the algorithm described above, returning
the minimum of the two in O(|V |) time.
Problem 3. Doh!-nut
Momer has just finished work at the FingSprield power plant at location p, and needs to
drive to his home at location h. But along the way, if his driving route ever comes within
driving distance k of a doughnut shop, he will stop and eat doughnuts, and his wife, Harge,
will be angry. Momer knows the layout of Fing Sprield, which can be modeled as a set of n
locations, with two-way roads of known driving distance connecting some pairs of locations
(you may assume that no location is incident to more than five roads), as well as the
locations of the d doughnut shops in the city. Describe an O(n log n)-time algorithm to find
the shortest driving route from the power plant back home that avoids driving within driving
distance k of a doughnut shop (or determine no such route exists).
Solution: Construct a graph G with a vertex for each of the n city locations, and an
undirected edge between two locations if there is a road connecting them, with each edge
weighted by the positive length of its corresponding road. The degree of each vertex is
bounded by a constant (i.e., 5), so the number of edges in G is O(n). First, we identify vertices
that are within driving distance k of a doughnut shop location: create an auxiliary vertex x
with a 0-weight outgoing edge from x to every doughnut shop location, and run Dijkstra
from x. Remove every vertex from the graph whose shortest path from x is less
programmingexamhelp.com
than or equal to k, resulting in graph G0 ⊂ G. If either p or h are not in G0, then no route
exists. Otherwise, run Dijkstra from p in G0. If no path exists to h, then no valid route exists.
Otherwise, Dijkstra finds a shortest path from p to h, so return it (via parent pointers). This
algorithm runs Dijkstra twice. Since the size of either graph is O(|V |), Dijkstra runs in
O(|V | log |V |) = O(n log n) time (e.g. using a binary heap to implement a priority queue).
Problem 4. Long Shortest Paths
Given directed graph G = (V, E) having arbitrary edge weights w : E → Z and two
vertices s, t ∈ V , describe an O(|V |3)-time algorithm to find the minimum weight of any path
from s to t containing at least |V | edges.
Solution: Our strategy will compute intermediate values for each vertex v ∈ V :
1. the minimum weight w1(v) of any path from s to v using exactly |V | edges, and then
2. the minimum weight w2(v) of any path from v to t using any number of edges.
First, to compute (1), we make a duplicated graph similar to Bellman-Ford, but without
edges corresponding to remaining at a vertex. Specifically, construct a graph G1 with
|V | + 1 vertices for each vertex v ∈ V : vertex vk for k ∈ {0, . . . , |V |} representing reaching
v from s along a path containing k edges; and
• |V | edges for each edge (u, v) ∈ E: edge (uk−1, vk) of the same weight for k ∈ {1, . . . , |V |}.
Now a path in G1 from s0 to v|V | for any v ∈ V corresponds to a path from s to v in G
through exactly |V | edges. So solve SSSPs in G1 from s0 to compute the minimum weight
of paths to each vertex traversing exactly |V | edges. This graph is acyclic, and has size O(|V
programmingexamhelp.com
|(|V | + |E|)) =O(|V |3), so we can solve SSSP on G1 via DAG relaxation in O(|V |3) time.
Second, to compute (2), we make a new graph G2 from G where every edge is reversed.
Then every path to t in G corresponds to a path in G2 from t, so compute SSSPs from t in G2
to find the minimum weight of any path from v to t in G using any number of edges, which
can be done in O(|V ||E|) = O(|V |3) time using Bellman-Ford.
Once computed, finding the minimum sum of w1(v) + w2(v) over all vertices v ∈ V will
provide the minimum weight of any path from s to t containing at least |V | edges, since
every such path can be decomposed into its first |V | edges and then the remainder. This loop
takes O(|V |) time, so the algorithm runs in O(|V |3) time in total.

More Related Content

Similar to Algorithm Exam Help

Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
meisamstar
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 

Similar to Algorithm Exam Help (20)

DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Algorithms Design Assignment Help
Algorithms Design Assignment HelpAlgorithms Design Assignment Help
Algorithms Design Assignment Help
 
Graph 3
Graph 3Graph 3
Graph 3
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Graphs
GraphsGraphs
Graphs
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Lecture13
Lecture13Lecture13
Lecture13
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Graph applications chapter
Graph applications chapterGraph applications chapter
Graph applications chapter
 

More from Programming Exam Help

More from Programming Exam Help (20)

Best Algorithms Design Exam Help
Best Algorithms Design Exam Help Best Algorithms Design Exam Help
Best Algorithms Design Exam Help
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
Design and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam HelpDesign and Analysis of Algorithms Exam Help
Design and Analysis of Algorithms Exam Help
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Computer Science Exam Help
Computer Science Exam HelpComputer Science Exam Help
Computer Science Exam Help
 
Programming Exam Help
Programming Exam Help Programming Exam Help
Programming Exam Help
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
Programming Exam Help
Programming Exam Help Programming Exam Help
Programming Exam Help
 
Programming Exam Help
Programming Exam Help Programming Exam Help
Programming Exam Help
 
Computer Science Exam Help
Computer Science Exam Help Computer Science Exam Help
Computer Science Exam Help
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Algorithm Exam Help

  • 1. For any help regarding Algorithm Exam Help Visit :- https://www.programmingexamhelp.com/ , Email :- support@programmingexamhelp.com or call us at :- +1 678 648 4277
  • 2. Graph Weights Name Running Time O(·) DAG Any DAG Relaxation |V | + |E| General Unweighted BFS |V | + |E| General Non-negative Dijkstra Bellman-Ford |V | log |V | + |E| General Any |V | · |E| Graph Problems • Graph reachability by BFS or DFS in O(|E|) time • Graph exploration/connected components via Full-BFS or Full-DFS • Topological sort / Cycle detection via DFS • Negative-weight cycle detection via Bellman-Ford • Single Source Shortest Paths (SSSP) Restrictions SSSP Algorithm • All Pairs Shortest Paths (APSP) – Run a SSSP algorithm |V | times – Johnson’s solves APSP with negative weights in O(|V |2 log |V | + |V ||E|) Graph Problem Strategies • Be sure to explicitly describe a graph in terms of problem parameters • Convert problem into finding a shortest path, cycle, topo. sort, conn. comps., etc. Introduction To Algorithms programmingexamhelp.com
  • 3. programmingexamhelp.com • May help to duplicate graph vertices to encode additional information • May help to add auxiliary vertices/edges to graph • May help to pre-process the graph (e.g., to remove part of the graph) Graph Problem Common Mistakes • Define your graphs! Specify vertices, edges, and weights clearly (and count them!) – (e.g., construct graph G = (V, E) with a vertex for each... and a directed edge (u, v) with weight w for each...) • State the problem you are solving, not just the algorithm you use to solve it – (e.g., solve SSSP from s by running DAG Relaxation...) • Connect the graph problem you solve back to the original problem – (e.g., the weight of a path from s to t in G corresponds to the sum of tolls paid along a driving route, so a path of minimum weight corresponds to a route minimizing tolls) Problem 1. Counting Blobs An image is a 2D grid of black and white square pixels where each white pixel is contained in a blob. Two white pixels are in the same blob if they share an edge of the grid. Black pixels are not contained in blobs. Given an n × m array representing an image, describe an O(nm)- time algorithm to count the number of blobs in the image.
  • 4. Solution: Construct a graph G with a vertex per white pixel, with an undirected edge between two vertices if the pixels associated with them are both white and share an edge of the grid. This graph has size at most O(nm) vertices and at most O(nm) edges (as pixels share edges with at most four other pixels), so can be constructed in O(nm) time. Each connected component of this graph corresponds to a blob, so run Full-BFS or Full-DFS to count the number of connected components in G in O(nm) time programmingexamhelp.com Problem 2. Unicycles Given a connected undirected graph G = (V, E) with strictly positive weights w : E → Z+ where |E| = |V |, describe an O(|V |)-time algorithm to determine a path from vertex s to vertex t of minimum weight. Solution: Given two vertices in a weighted tree containing only positive weight edges, there is a unique simple path between them which is also the minimum weight path. A depth-first search from any source vertex s in the tree results in a directed DFS tree in O(|V |) time (since |E| = |V | − 1). Then relaxing edges in topological sort order of the directed DFS tree computes minimum weight paths from s in O(|V |) time. Since G has one cycle, our strategy will be to break the cycle by removing an edge, and then compute the minimum weight path from s to t in the resultant tree. First, we find the vertex v closest to s on the cycle by running depth-first search from s in O(|V |) time (since |E| = |V |). One edge e1 of the cycle will not be in the tree returned by DFS (a back edge to v), with the other edge of the cycle incident to v being a single outgoing DFS tree edge e2. If s is on the cycle, v = s; otherwise the unique path from s to v does not
  • 5. programmingexamhelp.com contain e1 or e2. A shortest path from s to t cannot traverse both edges e1 and e2, or else the path would visit v at least twice, traversing a cycle of positive weight. Removing either e1 or e2 results in a tree, at least one of which contains the minimum weight path from s to t. Thus, find the minimum weight path from s to t in each tree using the algorithm described above, returning the minimum of the two in O(|V |) time. Problem 3. Doh!-nut Momer has just finished work at the FingSprield power plant at location p, and needs to drive to his home at location h. But along the way, if his driving route ever comes within driving distance k of a doughnut shop, he will stop and eat doughnuts, and his wife, Harge, will be angry. Momer knows the layout of Fing Sprield, which can be modeled as a set of n locations, with two-way roads of known driving distance connecting some pairs of locations (you may assume that no location is incident to more than five roads), as well as the locations of the d doughnut shops in the city. Describe an O(n log n)-time algorithm to find the shortest driving route from the power plant back home that avoids driving within driving distance k of a doughnut shop (or determine no such route exists). Solution: Construct a graph G with a vertex for each of the n city locations, and an undirected edge between two locations if there is a road connecting them, with each edge weighted by the positive length of its corresponding road. The degree of each vertex is bounded by a constant (i.e., 5), so the number of edges in G is O(n). First, we identify vertices that are within driving distance k of a doughnut shop location: create an auxiliary vertex x with a 0-weight outgoing edge from x to every doughnut shop location, and run Dijkstra from x. Remove every vertex from the graph whose shortest path from x is less
  • 6. programmingexamhelp.com than or equal to k, resulting in graph G0 ⊂ G. If either p or h are not in G0, then no route exists. Otherwise, run Dijkstra from p in G0. If no path exists to h, then no valid route exists. Otherwise, Dijkstra finds a shortest path from p to h, so return it (via parent pointers). This algorithm runs Dijkstra twice. Since the size of either graph is O(|V |), Dijkstra runs in O(|V | log |V |) = O(n log n) time (e.g. using a binary heap to implement a priority queue). Problem 4. Long Shortest Paths Given directed graph G = (V, E) having arbitrary edge weights w : E → Z and two vertices s, t ∈ V , describe an O(|V |3)-time algorithm to find the minimum weight of any path from s to t containing at least |V | edges. Solution: Our strategy will compute intermediate values for each vertex v ∈ V : 1. the minimum weight w1(v) of any path from s to v using exactly |V | edges, and then 2. the minimum weight w2(v) of any path from v to t using any number of edges. First, to compute (1), we make a duplicated graph similar to Bellman-Ford, but without edges corresponding to remaining at a vertex. Specifically, construct a graph G1 with |V | + 1 vertices for each vertex v ∈ V : vertex vk for k ∈ {0, . . . , |V |} representing reaching v from s along a path containing k edges; and • |V | edges for each edge (u, v) ∈ E: edge (uk−1, vk) of the same weight for k ∈ {1, . . . , |V |}. Now a path in G1 from s0 to v|V | for any v ∈ V corresponds to a path from s to v in G through exactly |V | edges. So solve SSSPs in G1 from s0 to compute the minimum weight of paths to each vertex traversing exactly |V | edges. This graph is acyclic, and has size O(|V
  • 7. programmingexamhelp.com |(|V | + |E|)) =O(|V |3), so we can solve SSSP on G1 via DAG relaxation in O(|V |3) time. Second, to compute (2), we make a new graph G2 from G where every edge is reversed. Then every path to t in G corresponds to a path in G2 from t, so compute SSSPs from t in G2 to find the minimum weight of any path from v to t in G using any number of edges, which can be done in O(|V ||E|) = O(|V |3) time using Bellman-Ford. Once computed, finding the minimum sum of w1(v) + w2(v) over all vertices v ∈ V will provide the minimum weight of any path from s to t containing at least |V | edges, since every such path can be decomposed into its first |V | edges and then the remainder. This loop takes O(|V |) time, so the algorithm runs in O(|V |3) time in total.