2. Minimum Weight Path in a Directed Graph Given a directed graph with weighted edges, determine...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
2. Minimum Weight Path in a Directed Graph Given a directed graph with weighted edges, determine the minimum weighted path from node 7 to the last node. We define a directed graph g such that: • The total number of nodes in the graph is g_nodes. • The nodes are numbered sequentially as 1, 2, 3,... g_nodes. • The total number of edges in the graph is g_edges. • Each edge connects two distinct nodes (i.e, no edge connects a node to itself). • The edge connecting nodes g_from[i] and g_to[i] is directed. In other words, it describes a path only in the direction g_from[il-g_toli]. • The weight of the edge connecting nodes g_from[i] and g_tofi] is g_weight[i]. We define the weight of a path from node 1 to node g_nodes to be the sum of all edges traversed on that path. You must find the path from node 1 to node g_nodes having the minimum possible weight. You can add extra directed edges having weight 7 (one) between any two distinct nodes that are not already connected by an edge. For example, consider you are given the following list of edges in a 4 node graph: Weight 3 From 1 1 2 1 To 2 3 1 4 235 In the graph below, an additional edge has been added from 3 to 4. The minimum total cost to get from node 7 to node 4 is 3, 1-23-14 In the graph below, an additional edge has been added from 3 to 4. The minimum total cost to get from node 1 to node 4 is 3, 1-23-14. Function Description 2 Complete the function minCost in the editor below. The function must return an integer denoting the minimum possible weight of any path from node 7 to node g_nodes. 5 minCost has the following parameter(s): g_nodes: the integer number of nodes in graph g Constraints g_from[g_from[1]....g_from[n]]: an array of integers representing edge origin nodes g_to[g_to[1]...g_to[n]]: an array of integers representing edge target nodes g_weight(g_weight[1]...g weight[n]): an array of integers representing edge weights . . 3 sg.nodes s 10³ 1sg edges s min(104, (anodes (g.nodes - • 1sg_weight(i) s 106 Sample Input 1 31 123 Sample Output 1 1 Explanation 1 3 As graph g has no edge between node 7 and node 3, we can add an extra edge from node 7 to node 3 having weight 7. Thus, the path 1-3 is the minimum weight path and the function returns 1. 2. Minimum Weight Path in a Directed Graph Given a directed graph with weighted edges, determine the minimum weighted path from node 7 to the last node. We define a directed graph g such that: • The total number of nodes in the graph is g_nodes. • The nodes are numbered sequentially as 1, 2, 3,... g_nodes. • The total number of edges in the graph is g_edges. • Each edge connects two distinct nodes (i.e, no edge connects a node to itself). • The edge connecting nodes g_from[i] and g_to[i] is directed. In other words, it describes a path only in the direction g_from[il-g_toli]. • The weight of the edge connecting nodes g_from[i] and g_tofi] is g_weight[i]. We define the weight of a path from node 1 to node g_nodes to be the sum of all edges traversed on that path. You must find the path from node 1 to node g_nodes having the minimum possible weight. You can add extra directed edges having weight 7 (one) between any two distinct nodes that are not already connected by an edge. For example, consider you are given the following list of edges in a 4 node graph: Weight 3 From 1 1 2 1 To 2 3 1 4 235 In the graph below, an additional edge has been added from 3 to 4. The minimum total cost to get from node 7 to node 4 is 3, 1-23-14 In the graph below, an additional edge has been added from 3 to 4. The minimum total cost to get from node 1 to node 4 is 3, 1-23-14. Function Description 2 Complete the function minCost in the editor below. The function must return an integer denoting the minimum possible weight of any path from node 7 to node g_nodes. 5 minCost has the following parameter(s): g_nodes: the integer number of nodes in graph g Constraints g_from[g_from[1]....g_from[n]]: an array of integers representing edge origin nodes g_to[g_to[1]...g_to[n]]: an array of integers representing edge target nodes g_weight(g_weight[1]...g weight[n]): an array of integers representing edge weights . . 3 sg.nodes s 10³ 1sg edges s min(104, (anodes (g.nodes - • 1sg_weight(i) s 106 Sample Input 1 31 123 Sample Output 1 1 Explanation 1 3 As graph g has no edge between node 7 and node 3, we can add an extra edge from node 7 to node 3 having weight 7. Thus, the path 1-3 is the minimum weight path and the function returns 1.
Expert Answer:
Answer rating: 100% (QA)
import heapq def dijkstragraph start end Initialize distances of all nodes from start to infinity di... View the full answer
Related Book For
Discrete Mathematics and Its Applications
ISBN: 978-0073383095
7th edition
Authors: Kenneth H. Rosen
Posted Date:
Students also viewed these electrical engineering questions
-
You can use any angular measureradians, degrees, or revolutionsin some of the equations in Chapter 9, but you can use only radian measure in others. Identify those for which using radians is...
-
You can use any remote access method or combination of remote access schemes. You must demonstrate understanding of the principles of remote access connections and technologies and show that they...
-
Use the python programming language to write the code for this work. In the complex plane, the Heighway dragon is the limit of the iterated function system(IFS) dened by the following transformations...
-
(a) Find the acceleration at time t = 5 min of a helicopter whose height is s(t) = 300t 4t 3 m. (b) Plot the acceleration s" for 0 t 6. How does this graph show that the helicopter is slowing down...
-
Honest Truck Company is a large trucking company that operates throughout the United States. Honest Truck Company uses the units-of-production (UOP) method to depreciate its trucks. Honest Truck...
-
Crude oil is flowing vertically downward through 60 m of DN 25 Schedule 80 steel pipe at a velocity of 0.64 m/s. The oil has a specific gravity of 0.86 and is at 0C. Calculate the pressure difference...
-
Find a sample settlement agreement. Of those covered in this chapter, what type of settlement agreement did you find?
-
New York City is the most expensive city in the United States for lodging. The mean hotel room rate is $204 per night (USA today, April 30, 2012). Assume that room rates are normally distributed with...
-
A company provided the following information regarding its only product: Sale price per unit Direct materials used $50 $160,000 Direct labor incurred $185,000 Variable manufacturing overhead $120,000...
-
Recording a Business Combination Petra Corporation paid $ 8 million in cash to acquire the assets and liabilities of Salisbury Company. Petra also agreed to make an additional cash payment in the...
-
A sample of 100 patients uses a new treatment for weight loss. Describe how one would go about testing the claim that the weight loss treatment is effective
-
If you do not use your 3 6 0 store credit today, it expires ( i . e . , becomes worthless ) ! You can either use the credit to buy the $ 3 6 0 pour over, the 3 6 0 cortado or let the credit...
-
The concept of opportunity cost is an important one in economics. It is essential to understand the distinction between explicit and implicit costs. For this assignment, consider an event at work...
-
An investment will pay you $700 at the end of each of the next 2 years, $600 at the end of year 3, $4500 at the end of year 4, and $250 at the end of year 5. If other investments of equal risk earn...
-
Assume that you have decided to investone-half of your money in Walmart and the remainder in Target. Calculate the monthlyholding-period returns for your two-stock portfolio as the average of the two...
-
Intel corporation made $ 8 . 3 billion profit in 2 0 2 2 and the profit is expect to grow at about 7 % for next 2 years and rate of growth of profit decline to 5 % there after. Intel Corporation is...
-
if your fingers apply a couple Considering the drill chuck where: F = 2.5lb (in both cases) and d = 2.2 in, what will be the magnitude of the moment 'M' tightening the drill if the chuck key gear...
-
Southwestern Punch was made by Frutayuda, Inc. and sold in 12-ounce cans to benefit victims of Hurricane Zero. The mean number of ounces placed in a can by an automatic fill pump is 11.7 with a...
-
Devise an algorithm similar to Prim's algorithm for constructing a maximum spanning tree of a connected weighted graph.
-
Let p and q be the propositions "Swimming at the New Jersey shore is allowed" and "Sharks have been spotted near the shore," respectively. Express each of these compound propositions as an English...
-
How can the matrix representing a relation R on a set A be used to determine whether the relation is irreflexive?
-
Winnipeg Enterprises Inc. reported the following summarized balance sheet at December 31, 2019: During 2020, Winnipeg Enterprise completed these transactions that affected shareholders' equity:...
-
Cones Inc. of Baie-Comeau completed the following transactions during 2020, the company's tenth year of operations: {Requirement} Analyze each transaction in terms of its effect (in dollars) on the...
-
The following accounts and related balances of Dove Designers, Inc., as of December 31, 2020 , are arranged in no particular order. {Requirements} 1. Prepare Dove's classified balance sheet in the...
Study smarter with the SolutionInn App