Our implementation of shortestPathLengths in Code Fragment 14.13 relies on use of ?infinity? as a numeric value,
Question:
Our implementation of shortestPathLengths in Code Fragment 14.13 relies on use of ?infinity? as a numeric value, to represent the distance bound for vertices that are not (yet) known to be reachable from the source. Reimplement that method without such a sentinel, so that vertices, other than the source, are not added to the priority queue until it is evident that they are reachable.
Transcribed Image Text:
1 /** Computes shortest-path distances from src vertex to all reachable vertices of g. */ 2 public static
1 /** Computes shortest-path distances from src vertex to all reachable vertices of g. */ 2 public static Map, Integer> 3 shortestPathLengths(Graph g, Vertex src) { 4 // d.get(v) is upper bound on distance from src to v Map, Integer> d = new ProbeHashMap<>(); // map reachable v to its d value 7 5 Map, Integer> cloud = new ProbeHashMap<>(); 8 // pq will have vertices as elements, with d.get(v) as key AdaptablePriorityQueue> pq; 10 9 pq = new HeapAdaptablePriorityQueue<>(); // maps from vertex to its pq locator 12 11 Map, Entry>> pqTokens; pqTokens = new ProbeHashMap<>(); 13 14 15 // for each vertex v of the graph, add an entry to the priority queue, with // the source having distance 0 and all others having infinite distance for (Vertex v : g.vertices( )) { if (v == src) d.put(v,0); else 16 17 18 19 20 21 d.put(v, Integer.MAX_VALUE); pq Tokens.put(v, pq.insert(d.get(v), v)); } // now begin adding reachable vertices to the cloud while (!pq.isEmpty()) { Entry> entry = pq.removeMin( ); int key = entry.getKey(); Vertex u = entry.getValue(); cloud.put(u, key); pq Tokens.remove(u); for (Edge e : g.outgoingEdges(u)) { Vertex v = g.opposite(u,e); if (cloud.get(v) = // perform relaxation step on edge (u,v) int wgt = e.getElement( ); if (d. d.put(v, d.get(u) + wgt); pq.replaceKey(pqTokens.get(v), d.get(v)); // update the pq entry } } } } return cloud; 22 // save entry for future updates 23 24 25 26 27 28 // this is actual distance to u // u is no longer in pq 29 30 31 32 33 null) { 34 35 // better path to v? // update the distance 36 (u) + wgt < d.get(v)) - 37 38 39 40 41 42 43 // this only includes reachable vertices 44 }
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 44% (9 reviews)
Here is the reimplemented shortestPathLengths method without the use of a sentinel ...View the full answer
Answered By
Akshay Shete
I have extensive experience as a tutor, both online and in-person. I have worked with students of all ages and abilities, and am skilled at adapting my teaching style to meet the needs of each individual student. I have a strong background in a variety of subjects, including math, science, and English, and am able to break down complex concepts in a way that is easy for students to understand. In addition to my subject matter expertise, I am also a patient and supportive teacher, and am committed to helping my students succeed. Whether I am working with a struggling student who needs extra help to catch up, or an advanced student looking to get ahead, I am able to provide the guidance and support they need to reach their goals. Overall, my hands-on experience as a tutor has prepared me to be a confident and effective teacher, and I am excited to use my skills to help students succeed.
0.00
0 Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
Use the following description of the operations of the RC_Charter2 Company to complete this exercise. ¢ The RC_Charter2 Company operates a fleet of aircraft under the Federal Air Regulations...
-
On July 1, 2013, Lula Plume created a new self-storage business, Safe Storage Co. The following transactions occurred during the companys first month. July 1 Plume invested $30,000 cash and buildings...
-
On April 1, 2011, Jiro Nozomi created a new travel agency, Adventure Travel. The following transactions occurred during the companys first month. April 1 Nozomi invested $32,000 cash and computer...
-
Suppose that In Example 18.6 the electrical firm does not have enough prior information regarding the population mean length of life to be able to assume a normal distribution for p. The firm...
-
Capitol Life Insurance Company ("Capitol") was incorporated in the U.S.A. in the state of Colorado at the turn of the century. Its head office had always been in Denver, Colorado. Capitol was a...
-
WHICH COLLABORATION INFORMATION SYSTEM IS RIGHT FOR YOUR TEAM?
-
How effective were you in your persuasion?
-
Bohemian Links Inc. produces sausages in three production departmentsMixing, Casing and curing, and Packaging. In the Mixing Department, meats are prepared and ground and then mixed with spices. The...
-
I know headquarters wants us to add that new product line, said Dell Havasi, manager of Billings Companys Office Products Division. But I want to see the numbers before I make any move. Our divisions...
-
The Lead-Tin phase diagram: a) Show the eutectic three phase reaction on this phase diagram. b) A Pb-45% Sn alloy is cooled from 300C to 100C. What phases are present for this alloy at this...
-
Give an example of a weighted directed graph G with negative-weight edges, but no negative-weight cycle, such that Dijkstras algorithm incorrectly computes the shortest-path distances from some start...
-
An old MST method, called Baruvka?s algorithm, works as follows on a graph G having n vertices and m edges with distinct weights: Prove that this algorithm is correct and that it runs in O(mlogn)...
-
Prove that the tangent line at a point P on the cycloid always passes through the top point on the rolling circle as indicated in Figure 22. Assume the generating circle of the cycloid has radius 1....
-
a. Calculate the volume of the solid of revolution created by rotating the curve y=2+4 exp(-5 x) about the x-axis, for x between 2 and 4. Volume: b. The equation of a circle of radius r, centered at...
-
The simply supported timber beam of Figure 1 is made-up by gluing together three 300 mm by 150 mm planks as shown. The beam has to carry a uniformly distributed vertically downward load of 8 kN/m for...
-
Simon Company's year-end balance sheets follow. At December 31 Assets Cash Accounts receivable, net Merchandise inventory Prepaid expenses Plant assets, net Total assets Liabilities and Equity...
-
Openthe Phetsimulation Charges and Field from this link: ( https://phet.colorado.edu/en/simulation/charges-and-fields ) . In this simulation, a little different model is used: the little yellow "E...
-
A particle of mass m that is moving along the x-axis is experiencing a restoring force of the form F = -k+x, where kf is the spring constant. The Hamiltonian for this system is given as: = d 2m dx +...
-
Explain how you would change the stored procedure in Figure 10A-58 to connect the customer to all artists who either (a) Were born before 1900 or (b) Had a null value for Birthdate.
-
Suppose the concentration of glucose inside a cell is 0.1 mm and the cell is suspended in a glucose solution of 0.01 mm. a. What would be the free energy change involved in transporting 10-o mole of...
-
What is the purpose of the SNMP trap message?
-
In Section 5.7 we saw that it was preferable to transport SNMP messages in unreliable UDP data-grams. Why do you think the designers of SNMP chose UDP rather than TCP as the transport protocol of...
-
What are the purposes of the SNMP Get Request and Set Request messages?
-
Winter Time Adventures is going to annual dividend if $2.61 a share on its common stock next week. This year, the company paid a dividend of $2.50 a share. The company adheres to a constant rate of...
-
Small Factory : -Regular time 8 hours per day. -1 hour daily lunch break. -25 working days per month. -50 workers. -Worker productivity 2.5 units per hour. -sold for $ 150 per unit. -cost of Labor...
-
$500 is invested for 7 years at 10 % p.a. simple interest. How much will the investment be worth after this period
Study smarter with the SolutionInn App