You are given a directed graph in which each nodeuV, has an associated pricepu which is a positive integer. Define the array cost as follows: for each uV,

cost[u] = price of the cheapest node reachable fromu (includingu itself).

For instance, in the graph below (with prices shown for each vertex), the cost values of the nodes A,B,C,D,E,Fare2,1,4,1,4,5,respectively.

Your goal is to design an algorithm that fills in the entire cost array (i.e., for all vertices).

(a) Give a linear-time algorithm that works for directed acyclic graphs.

(b) Extend this to a linear-time algorithm that works for all directed graphs.

Short Answer

Expert verified
  1. The linear time algorithm that works for directed acyclic graphs is given below.
  2. Extension of this to a linear-time algorithm that works for all directed graphs is shown below.

Step by step solution

01

Step 1: Directed acyclic graph.

DAG (directed acyclic graph) is a graph which contain directed edges between all the vertices and they are not contain any cycle in graph. This type of graph are called directed acyclic graph.

02

Step 2: Linear-time algorithm that works for directed acyclic graphs.

a)

Consider the vertices of the DAG in topological order. Let a liberalized order. Vertices reachable from any vertex will be among the vertices (smaller post number) in the linear zed order. Once we have updated costs for vertices. Cost of vertices will be minimum of cost of vertices connected to (including itself). This is because any path from vertex will be through its adjacent vertices of costs for which are already calculated. We implement an algorithm which linearisation the DAG and calculates cost in reverse topological order.

Thealgorithm that works for directed acyclic graphs in Linear-time is shown as:

Pseudo code:

cost(G)DFS(G,u)//whereuisarandomlypickedvertex.(v1,v2,:::,vn)

decreasingorderofpost[vi]Fori=nto1:cost[vi]=price(vi)forall(vi,vj)

The time for linearizing a DAG is linear. And for finding the minimum cost, we visit each edge at most once and hence the time is linear.

03

Extension to a linear-time algorithm.

b).

For a general directed graph, the cost of any two nodes in the same strongly

connected component will be same since both are reachable from each other. Hence, it is sufficient to run the above algorithm on the DAG of the strongly connected components of the graph. For a node corresponding to strongly connected component, we take price,C=min(priceu) for all.

Once we have found SCCs, meta-nodes can be topologically sorted by arranging them in decreasing order of their highest post numbers.

Pseudo code:

procedurecheapestcost(G)DFS(GR;u)//whereuisarandomlypickedvertexRunundirectedconnectedcomponentsalgorithmonGprocessing

verticesindecreasingorderofpostnumberspost[C]=max(post(u))foralluinCForallcinC(whereCisthesetofallSCC's):price[c]=min(price(u))foralluinc

(c1,c2,:::,cn)decreasingorderofpost(ci)

Fori=nto1:cost[ci]=price[ci]forall(ci,cj)ifcost[cj]<cost[ci]

cost[ci]=cost[cj]

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

Pouring water.

We have three containers whose sizes are 10 pints, 7 pints, and 4 pints, respectively. The 7-pint and 4-pint containers start out full of water, but the 10-pint container is initially empty. We are allowed one type of operation: pouring the contents of one container into another, stopping only when the source container is empty or the destination container is full. We want to know if there is a sequence of pouring’s that leaves exactly 2 pints in the 7- or 4-pint container.

(a) Model this as a graph problem: give a precise definition of the graph involved and state the specific question about this graph that needs to be answered.

(b) What algorithm should be applied to solve the problem?

(c) Find the answer by applying the algorithm.

Perform a depth-first search on the following graph; whenever there’s a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the pre and post number of each vertex.

You are given tree T=(V,E) along with a designated root node rV. The parent of any node Vr, denoted p(V), is defined to be the node adjacent to v in the path from r to v . By convention, p(r)=r. For k>1, define pk(v)pk-1(pv)andp1(v)=p(v)(so pk(v)is the k th ancestor of v ). Each vertex v of the tree has an associated non-negative integer label l(v). Given a linear-time algorithm to update the labels of all the vertices T according to the following rule: lnew(v)=l(plvv).

Question:Undirected vs. directed connectivity.

(a) Prove that in any connected undirected graph G =(V , E)there is a vertexvV whose removal leaves G connected. (Hint: Consider the DFS search tree for G.)

(b) Give an example of a strongly connected directed graph G(V ,E)such that, for everyvV, removing v from G leaves a directed graph that is not strongly connected.

(c) In an undirected graph with two connected components it is always possible to make the graph connected by adding only one edge. Give an example of a directed graph with two strongly connected components 0 such that no addition of one edge can make the graph strongly connected.

Rewrite the explore procedure (Figure 3.3) so that it is non-recursive (that is, explicitly use a stack). The calls to pre visit and post visit should be positioned so that they have the same effect as in the recursive procedure.

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