You are given a directed graph with (possibly negative) weighted edges, in which the shortest path between any two vertices is guaranteed to have at most edges. Give an algorithm that finds the shortest path between two vertices u and v in O(KE)time.

Short Answer

Expert verified

Bellman Ford algorithm in which the edges have negative weights and it finds the shortest path between two vertices inOKE time.

Step by step solution

01

 Step 1: Algorithm used for negative weighted directed graph

Bellman-Ford algorithmis an application of a 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 works only for the graph with a positive weight and Bellman-Ford algorithm works with graphs in which edges have negative weights in its graph.

02

Design the Algorithm

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 time at mostOKE So, here the vertex A is the source vertex. Now take an array as a data structure to evaluate a single source’s 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. Draw a directed positive and negative weighted graph as shown below:

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 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 the vertex A to vertex D is evaluate in OKEtime.

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

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

There is a network of roads G=(V,E) connecting a set of cities . Each road in E has an associated length Ie. There is a proposal to add one new road to this network, and there is a list E' of pairs of cities between which the new road can be built. Each such potential road localid="1659075853079" e'E' has an associated length. As a designer for the public works department you are asked to determine the road localid="1659075866764" e'E'whose addition to the existing network G would result in the maximum decrease in the driving distance between two fixed cities s and t in the network. Give an efficient algorithm for solving this problem.

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.

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

Question: Often there are multiple shortest paths between two nodes of a graph. Give a linear-time algorithm for the following task.

Input: Undirected graph G = (V , E )with unit edge lengths; nodesu,vV

Output: The number of distinct shortest paths from utov.

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