/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q25E You are given a directed graph i... [FREE SOLUTION] | 91影视

91影视

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 91影视!

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

Infinite paths.Let G=(V,E) be a directed graph with a designated 鈥渟tart vertex鈥 sV,asetVGV, a set of 鈥済ood鈥 vertices, and a set VBV of 鈥渂ad鈥 vertices. An infinite trace of is an infinite sequence of vertices viV such that (1)v0=s, and (2) for all i0, (vi,vi+1)E. That is, p is an infinite path in G starting at vertex s. Since the setV of vertices is finite, every infinite trace of Gmust visit some vertices infinitely often.

  1. If p is an infinite trace, let Inf(p)V be the set of vertices that occur infinitely often in p. Show that Inf(p) is a subset of a strongly connected component of G.
  2. Describe an algorithm that determines if role="math" G has an infinite trace.
  3. Describe an algorithm that determines if G has an infinite trace that visits some good vertex in VG infinitely often.
  4. Describe an algorithm that determines if role="math" localid="1659627728759" G has an infinite trace that visits some good vertex in VG infinitely often, but visits no bad vertex in VB infinitely often.

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

On page 102, we defined the binary relation 鈥渃onnected鈥 on the set of vertices of a directedgraph. Show that this is an equivalence relation(see Exercise 3.29), and conclude that it partitions the vertices into disjoint strongly connected components.

For each node in an undirected graph, let twodegreeube the sum of the degrees of鈥檚 neighbors. Show how to compute the entire array of two degree. values in linear time, given a graph in adjacency list format

Design a linear-time algorithm which, given an undirected graph G and a particular edge ein it, determines whetherGhas a cycle containing.

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.