In cases where there are several different shortest paths between two nodes (and edges have varying length),the most convenient of these paths is often the one with fewest edges. Forinstance, if nodes represent cities and edge lengths represent costs of flying between cities, theremight be many ways to get from cityto city t which all have the same cost. The mostconvenientof these alternatives is the one which involves the fewest stopovers. Accordingly, for a specific starting node S , define

bestu=minimum number of edges in a shortest path from S to u .

In the example below, thebestvalues for nodes S,A,B,C,D,E,Fare 0,1,1,1,2,2,3, respectively.

Give an efficient algorithm for the following problem.

Input:Graph G=V,E; positive edge lengths le; starting node sV.

Output: The values of bestu should be set for all nodesuV

Short Answer

Expert verified

The efficient algorithm is as follows:

Input: G=V,E,positive edge length le; starting node sV

Output: the values of bestushould be set for all nodes uV.

While Qϕ do

uvin Q with smallest distance

For all edgesu,vE do

If dv>du+lu,v then

dvdu+lu,v

bestvbestu+1

If bestvbestu+1then

If bestv>bestu+1then

bestvbestu+1

Step by step solution

01

Step 1:Explain the given information

Consider that there exists several shortest paths between two nodes. The path with the few edges is often considered as the convenient one.

For specific starting node ,

bestu=minimum number of edges in a shortest path from s to u

02

Give an efficient algorithm for the given problem.

The efficient algorithm is as follows:

Input: G=V,E ,positive edge length le; starting node sV

Output: the values of bestu should be set for all nodes .

uV

While Qϕ do

uvin Q with smallest distance

For all edgesu,vE do

If dv>du+lu,v then

dvdu+lu,v

bestvbestu+1

If dv=du+lu,vthen

If bestv>bestu+1then

bestvbestu+1

The algorithm, checks for the empty queue and put the edge with shortest distance. If thesum of distance of the positive edge length and the distance of the next vertex less than the current vertex. Update the best vertex queue. If the sum of distance of the positive edge length and the distance of the next vertex equal to the current vertex. Then if the best of current vertex is greater than the previous one, update.

Therefore, the algorithm efficiently finds the path with the fewest edges.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Consider a directed graph in which the only negative edges are those that leaves; all other edges are positive. Can Dijkstra's algorithm, started at s, fail on such a graph? Prove your answer.

Just like the previous problem, but this time with the Bellman-Ford algorithm.

Professor F. Lake suggests the following algorithm for finding the shortest path from node to node t in a directed graph with some negative edges: add a large constant to each edge weight so that all the weights become positive, then run Dijkstra’s algorithm starting at node s , and return the shortest path found to node t .

Is this a valid method? Either prove that it works correctly, or give a counterexample.

Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most O|V3|.

Shortest path algorithms can be applied in currency trading. Let c1,c2,cn be various currencies; for instance, c1might be dollars, c2pounds, and c3 lire.

For any two currencies ci and cj , there is an exchange rate τi,j; this means that you can purchase τi,j units of currency cj in exchange for one unit of cj. These exchange rates satisfy the condition that rij.rji<1 so that if you start with a unit of currency cj, change it into currency and then convert back to currency localid="1658917254028" ci, you end up with less than one unit of currency ci (the difference is the cost of the transaction).

a. Give an efficient algorithm for the following problem: Given a set of exchange rates rij , and two currencies s and t , find the most advantageous sequence of currency exchanges for converting currency into currency . Toward this goal, you should represent the currencies and rates by a graph whose edge lengths are real numbers.

The exchange rates are updated frequently, rejecting the demand and supply of the various currencies. Occasionally the exchange rates satisfy the following property: there is a sequence of currencies ci1,ci2,.......ciksuch that ri1,ri2.i3,.........ri(k-1),ik,rik+1>1. This means that by starting with a unit of currency ci1and then successively converting it to currencies ci1,ci2.......cik, and finally back to ci1, you would end up with more than one unit of currency ci1 . Such anomalies Last only a fraction of a minute on the currency exchange, but they provide an opportunity for risk-free profits.

b. Give an efficientalgorithm for detecting the presence of such an anomaly. Use the graph representation you found above.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free