Question

USING PYTHON ONLY : I need to implement this pseudocode of a star (A*) searching algorithm...

USING PYTHON ONLY : I need to implement this pseudocode of a star (A*) searching algorithm :

# Let pq be an empty min priority queue'

# g(start) = 0

# f(start) = h(start)

# path(start) = []

# pq.push(start, f(start))

# while not pq.empty():

# top = pq.pop()

# if isGoal(top):

# return f(top), path(top)

# foreach next in succ(top):

# g(next) = g(top) + cost(top, next)

# f(next) = g(next) + h(next)

# path(next) = path(top).append(next)

# pq.push(next, f(next))

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import heapq def calculate_distances(graph, starting_vertex): distances = {vertex: float('infinity') for vertex in graph} distances[starting_vertex] = 0 pq = [(0, starting_vertex)] while len(pq) > 0: current_distance, current_vertex = heapq.heappop(pq) # Nodes can get added to the priority queue multiple times. We only # process a vertex the first time we remove it from the priority queue. if current_distance > distances[current_vertex]: continue for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight # Only consider this new path if it's better than any path we've # already found. if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) return distances example_graph = { 'U': {'V': 2, 'W': 5, 'X': 1}, 'V': {'U': 2, 'X': 2, 'W': 3}, 'W': {'V': 3, 'U': 5, 'X': 3, 'Y': 1, 'Z': 5}, 'X': {'U': 1, 'V': 2, 'W': 3, 'Y': 1}, 'Y': {'X': 1, 'W': 1, 'Z': 1}, 'Z': {'W': 5, 'Y': 1}, } print(calculate_distances(example_graph, 'X')) # => {'U': 1, 'W': 2, 'V': 2, 'Y': 1, 'X': 0, 'Z': 2}
Add a comment
Know the answer?
Add Answer to:
USING PYTHON ONLY : I need to implement this pseudocode of a star (A*) searching algorithm...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers...

    10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers (b) Implement the algorithm and use it to solve instances of size 6, 7, (c) Compare the performance of this algorithm to that of Algorithm all possible tours 8, 9, 10, 15, and 20 6.3 using the instances developed in (b) Algorithm 6.3 The Best-First Search with Branch-and-Bound Pruning Algorithm for the Traveling Salesperson problem Problem: Determine an optimal tour in a weighted, directed...

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span...

    Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span Problem using the ADT for a Stack in ython. The implementation of the ADT of a Stack (10 points) Implementation of computeSpans1 (20 points) Implementation of computeSpan2 (30 points) Provide the derived computational complexity of both computeSpans1 and computeSpans2 (40 points) Algorithm computeSpans1 (P) Input: an n-element list P of numbers such that P[i] is the prices of the stock on day I Output:...

  • Run the Dijkstra’s algorithm on the directed graph of the following figure 24.6, using vertex t...

    Run the Dijkstra’s algorithm on the directed graph of the following figure 24.6, using vertex t as the source. In the style of Figure 24.6, show the d and ? values and the vertices in set S after each iteration of the while loop. 1 8 10 I 10 14 4 6 4 6 2 3 2 3 4 6 5 5 2 (a) (c) 1 10 13 4 6 (d) (e) Figure 24.6 The execution of Dijkstra's algorithm. The...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT