Question

Python 3 question Suppose that a directed and weighted graph represented as adjacency matrix, how could...

Python 3 question

Suppose that a directed and weighted graph represented as adjacency matrix, how could it perform uniform cost search by using priority queue?

Homework Answers

Answer #1

import queue as Q

def search(graph, start, end):
if start not in graph:
raise TypeError(str(start) + ' not found in graph !')
return
if end not in graph:
raise TypeError(str(end) + ' not found in graph !')
return
  
queue = Q.PriorityQueue()
queue.put((0, [start]))
  
while not queue.empty():
node = queue.get()
current = node[1][len(node[1]) - 1]
  
if end in node[1]:
print("Path found: " + str(node[1]) + ", Cost = " + str(node[0]))
break
  
cost = node[0]
for neighbor in graph[current]:
temp = node[1][:]
temp.append(neighbor)
queue.put((cost + graph[current][neighbor], temp))
  
def readGraph():
lines = int( input() )
graph = {}
  
for line in range(lines):
line = input()
  
tokens = line.split()
node = tokens[0]
graph[node] = {}
  
for i in range(1, len(tokens) - 1, 2):
# print(node, tokens[i], tokens[i + 1])
# graph.addEdge(node, tokens[i], int(tokens[i + 1]))
graph[node][tokens[i]] = int(tokens[i + 1])
return graph

def main():
graph = readGraph()
search(graph, 'Arad', 'Bucharest')
  
if __name__ == "__main__":
main()

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question 38 A simple connected graph with 7 vertices has 3 vertices of degree 1, 3...
Question 38 A simple connected graph with 7 vertices has 3 vertices of degree 1, 3 vertices of degree 2 and 1 vertex of degree 3. How many edges does the graph have? Question 29 Use two of the following sets for each part below. Let X = {a, b, c}, Y = {1, 2, 3, 4} and Z = {s, t}. a) Using ordered pairs define a function that is one-to-one but not onto. b) Using ordered pairs define...
Question 3 has two parts, Parts A and B. Both are compulsory. Part A – Weighted...
Question 3 has two parts, Parts A and B. Both are compulsory. Part A – Weighted Average Cool Fizzy Ltd’s division A produces soft drinks in standard sizes. Set out below is information for the month of January: Work-in-process inventory, 1 January = 4,000 units Direct materials: 100% complete = $6,000 Conversion: 50% complete = $12,000 Units started during January = 8,000 units Units completed and transferred out during January = 8000 units Work-in-process inventory, 31 January Direct materials: 100%...
Question 3: The Specic Factors Model a.) How does international trade aect the income distribution in...
Question 3: The Specic Factors Model a.) How does international trade aect the income distribution in Ricardo's model? b.) Via which channel could international trade in uence the income-distribution in the short-run? A small open economy produces cheese using land and labor and cars using capital and labor. Land and capital are specic factors of production, labor can move freely between both industries. The economy initially exports cheese and imports cars. The price of cheese on world markets increases suddenly...
QUESTION 3 Suppose a study is conducted to find out how the cost of flying between...
QUESTION 3 Suppose a study is conducted to find out how the cost of flying between Brisbane and Sydney using Boeing 737 varies with the number of passengers. The table below presents the number of passengers and associated costs for a random sample of 15 flights between Brisbane and Sydney using Boeing 737. Number of passengers 71 63 66 68 48 73 50 81 50 92 56 80 55 67 84 Cost ($) 4480 4020 4420 4150 3800 4300 4090...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events that took place at Enron and how the situation could have been dealt with differently to prevent further damage? THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies,...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
Please answer this question in short essay form (2-4 paragraphs) Considering that cultures as complicated and...
Please answer this question in short essay form (2-4 paragraphs) Considering that cultures as complicated and socially constructed through the communicative interaction of organizational members. Briefly describe how the organizational concepts of complicated, emergent, unitary, and ambiguous apply to the sample auto-ethnography. Sample Auto-ethnography: Required Reading Auto-ethnography of College X Joe Student Organizational Culture and Diversity 223-58000 “The organization’s culture has both a direct and an indirect impact on the allocation of power among diverse groups. The values and ideologies...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...