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...
-
A complaint was filed with the U.S. Commerce Departments ITA by U.S. telephone manufacturers AT&T, Comidial Corp., and Eagle Telephones, Inc., alleging that 12 Asian manufacturers of small business...
-
What are strategic competitiveness, strategy, competitive advantage, above-average returns, and the strategic management process?
-
Healthy Foods is a company that produces canned soups made without preservatives. The firm has assets that have a book value of $100 million. The assets are five years old and have been depreciated...
-
Lower-of-Cost-or-Market Remmers Company manufactures desks. Most of the company's desks are standard models and are sold on the basis of catalog prices. At December 31, 2010, the following finished...
-
Required information [The following information applies to the questions displayed below.] At the beginning of the year, Maroon Industries bought three used machines. The machines immediately were...
-
1. All players but the retailer should answer this question. What do you think the retailers customer demand pattern looked like? How did your customer orders vary throughout the game? 2. What...
-
Activity and selected costs for three production departments (Training, Independent, and Commercial) and two service departments (Accounting and Facilities) at DuBay Films for the past month follow:...
-
If Ricardian equivalence holds, then Group of answer choices a tax cut today financed by tax increases in the future will stimulate consumption. a tax cut today financed by tax increases in the...
-
The primary objective of this case is to investigate core issues that Facebook has encountered and to evaluate its associated implementation strategy, especially in terms of corporate ethics,...
-
Explain the meaning of inventory and explain what characteristics an asset is categorized into inventory. 2. Explain the impact on the financial statements if the company records inventory...
-
An Islamic financial institution (IFI) invested in a two years project valued at USD 1,000,000 by providing USD 700,000 of the capital based on the Musharakah contract with party A. The profit-...
-
Review the following research problems. For each question: 1.outline a research plan noting the research tasks necessary and sources (more than Google) you will utilize Lexis. 2.answer the question...
-
Part 2: A large movement in psychology has begun to discuss the limitations of published studies that rely on samples of participants who come from WEIRD (Western, educated, industrialized, rich, and...
-
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?
-
In its most recent corporate report, Williams, Inc., apologized to its stockholders for not paying a dividend. The report states that management will pay a \($1\) dividend next year. That dividend...
-
Nancy Ferris bought a building for \($120,000.\) She paid 15 percent down and agreed to pay the balance in 20 equal annual installments. What are the equal installments if the annual interest rate is...
-
You need \($25,000\) five years from now. You budget to make equal payments at the end of every year into an account that pays an annual interest rate of 7 percent. a. What are your annual payments?...
Study smarter with the SolutionInn App