Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

You are given a directed graph G(V,E)with (possibly negative) weighted edges, along with a specific node sVand a tree T=(V,E'),E'E. Give an algorithm that checks whether T is a shortest-path tree for G with starting point s . Your algorithm should run in linear time.

Short Answer

Expert verified

The algorithm to check whether T is a shortest-path tree for G with starting point s is explained, which runs in a linear time.

Step by step solution

01

Algorithm used for negative weighted directed graph.

Bellman Ford algorithmis an application of single source shortest path, which is used forfinding the shortest distance from one vertex to other vertices of a weighted directed graph.

It is almost similar to Dijkstra's algorithm but Dijkstra's algorithm is works only for the graph with a positive weight and Bellman Ford algorithm is works with graphs in which edges have negative weights in its graph.

02

Design the Algorithm .T=(V,E'),E'⊂E

Bellman-Ford algorithm applies to the graph for finding the single source’s shortest path.A directed graph with positive and negative edge weight, and returns the length of the shortest cycle in the graph and the graph is acyclic, which takes linear time. So, here the vertex A is the source vertex. now take an array as a data structure to evaluate single source shortest path between the source and the destination.

From A the distance of A is zero and take the distance of vertex A from each and every vertex is infinity. Now take A as the first vertex and evaluate the weight towards each vertex and draw a directed positive and negative weighted graph:

Choose the next vertex from the vertices which have minimum weight and select that node as the second vertex. Then again evaluate the distance of it from every vertex and as get the minimum weight of the node and consider it as the main node. Through this the series of the vertex arises.

Here the vertex A is the source vertex. now take a minheap as a data structure for evaluate single source shortest path between the source and the destination.

From A the distance of A is zero and take the distance of vertex A from each and every vertex is infinity.

All vertices will be released many times in the Bellman-Ford algorithm.

Select every vertex one by one and put it into the array as a data structure one by one as shown in the figure.

Hence, the shortest distance from vertex A to vertex D is evaluated in linear time

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.

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.

Section 4.5.2 describes a way of storing a complete binary tree of n nodes in an array indexed by 1, 2, K, n .

(a) Consider the node at position j of the array. Show that its parent is at position [j2]and its children are at 2 jand 2 j + 1 (if these numbers are n).

(b) What the corresponding indices when a complete d-ary tree is stored in an array?

Figure 4.16 shows pseudocode for a binary heap, modeled on an exposition by R.E. Tarjan. The heap is stored as an array , which is assumed to support two constant-time operations:

  • |h|, which returns the number of elements currently in the array;
  • h-1, which returns the position of an element within the array.

The latter can always be achieved by maintaining the values of h-1as an auxiliary array.

(c) Show that themakeheapprocedure takesO(n) time when called on a set of elements. What is the worst-case input? (Hint:Start by showing that the running time is at most 1=1nlog(ni)).

(d) What needs to be changed to adapt this pseudocode to d-ary heaps?

Suppose we want to run Dijkstra’s algorithm on a graph whose edge weights are integers in the range 0,1,........,W, where Wis a relatively small number.
(a) Show how Dijkstra’s algorithm can be made to run in time

O(W|V|+|E|)

(b) Show an alternative implementation that takes time just .

O((|V|+|E|)logW)

Generalized shortest-paths problem.In Internet routing, there are delays on lines but also, more significantly, delays at routers. This motivates a generalized shortest-paths problem.

Suppose that in addition to having edge lengths {Ie:eE} ,a graph also has vertex costs {cV:vV} . Now define the cost of a path to be the sum of its edge lengths, plusthe costs ofall vertices on the path (including the endpoints). Give an efficient algorithm for the followingproblem.

Input:A directed graph G={V,E} positive edge lengths Ie and positive vertex costs cv; a starting vertex sv.

Output:An array cost[.] such that for every vertex u,costu, is the least cost of any path from s to u (i.e., the cost of the cheapest path), under the defnition above.

Notice that cost[s]=c.

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