Question

Following algorithm belongs to Dijsktra's Shortest path algorithm: PLEASE SHOW THE RUNNING TIME OF THE EACH...

Following algorithm belongs to Dijsktra's Shortest path algorithm: PLEASE SHOW THE RUNNING TIME OF THE EACH LINE OF THE ALGORITHM AND DETERMINE THE RUNNING TIME OF THE ALGORITHM BY SUMMING UP THEM

while current_node != end:

visited.add(current_node)

destinations = graph.edges[current_node]

weight_to_current_node = shortest_paths[current_node][1]

for next_node in destinations:

weight = graph.weights[(current_node, next_node)] + weight_to_current_node

if next_node not in shortest_paths:

shortest_paths[next_node] = (current_node, weight)

else:

current_shortest_weight = shortest_paths[next_node][1]

if current_shortest_weight > weight:

shortest_paths[next_node] = (current_node, weight)

next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}

if not next_destinations:

return "Route Not Possible"

# next node is the destination with the lowest weight

current_node = min(next_destinations, key=lambda k: next_destinations[k][1])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The while loop here goes to every node there it iterates N times.

In djikstra basically we check each node and its corresponding node and the path between them to see if that's the shortest path or not.
So we iterate for all the nodes that are connected to current node
That is what for next_node in destinations defines as you can see destination is equivalent graph.edges(current_node) which means all the nodes connected to current node
weight is basically the the weight till that next_node if the path was passing through that current_node.
current_shortest_weight says the weight of the path from soure i.e. 1 till the destination i.e. next node

All the lines in between the loops automatically take in N complexity.

in the FOR LOOP everything is on N*N complexity

overall complexity of this code is O(N^2)

Add a comment
Know the answer?
Add Answer to:
Following algorithm belongs to Dijsktra's Shortest path algorithm: PLEASE SHOW THE RUNNING TIME OF THE EACH...
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
  • can you please solve this CORRECTLY? Exercise 4 - Shortest path (25 pts) Using Dijkstra's algorithm,...

    can you please solve this CORRECTLY? Exercise 4 - Shortest path (25 pts) Using Dijkstra's algorithm, find the shortest path from A to E in the following weighted graph: a- Once done, indicate the sequence (min distance, previous node) for nodes D and E. (15pts) b- Below is a high-level code for Dijkstra's algorithm. The variables used in the code are self-explanatory. Clearly explain why its running time (when we use a min-heap to store the values min distance of...

  • please answer one of the two 1. (25) [Single-source shortest-path: algorithm tracing] Show the tracing of...

    please answer one of the two 1. (25) [Single-source shortest-path: algorithm tracing] Show the tracing of Dijkstra's shortest path search algorithm on the weighted directed graph shown below. Do the tracing it twice, first starting with the nodes and, second, starting with the node z. For each tracing, each time the shortest path to a new node is determined, show the graph with the shortest path to the node clearly marked and show inside the node the shortest distance to...

  • Q1: Here we consider finding the length of the shortest path between all pairs of nodes...

    Q1: Here we consider finding the length of the shortest path between all pairs of nodes in an undirected, weighted graph G. For simplicity, assume that the n nodes are labeled 1; 2; : : : ; n, that the weight wij of any edge e = (i; j) is positive and that there is an edge between every pair of nodes. In this question, the goal is to solve this via dynamic programming. Note that the algorithm you will...

  • Please provide the big oh notation for running time and space complexity for the following methods...

    Please provide the big oh notation for running time and space complexity for the following methods (splay, findSplay, putSplay, eraseSplay: void SplayTreeMap::splay(Node* x) {    if(x!=NULL){ while (x -> parent != NULL ) { Node *p = x -> parent; Node *g = p -> parent; if (g == NULL) zig(x); else if (g -> left == p && p -> left == x) zigZig(x); else if (g -> right == p && p -> right == x) zigZig(x); else...

  • In this question, we will think about how to answer shortest path problems where we have more than just a single sourc...

    In this question, we will think about how to answer shortest path problems where we have more than just a single source and destination. Answer each of the following in English (not code or pseudocode). Each subpart requires at most a few sentences to answer. Answers significantly longer than required will not receive full credit You are in charge of routing ambulances to emergency calls. You have k ambulances in your fleet that are parked at different locations, and you...

  • Problem 1: Shortest Path-ish Suppose that you want to get from vertex s to vertex t in an...

    Problem 1: Shortest Path-ish Suppose that you want to get from vertex s to vertex t in an unweighted graph G = (V, E), but you would like to stop by vertex u if it is possible to do so without increasing the length of your path by more than a factor of a. Describe an efficient algorithm that would determine an optimal s-t path given your preference for stopping at u along the way if doing so is not prohibitively costly....

  • In this question, we will think about how to answer shortest path problems where we have more than just a single sourc...

    In this question, we will think about how to answer shortest path problems where we have more than just a single source and destination. Answer each of the following in English (not code or pseudocode). Each subpart requires at most a few sentences to answer. Answers significantly longer than required will not receive full credit You are in charge of routing ambulances to emergency calls. You have k ambulances in your fleet that are parked at different locations, and you...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • Please provide the big oh notation for running time and space complexity for the following functions...

    Please provide the big oh notation for running time and space complexity for the following functions (available, into, out, size, and printAll): int SplayTreeInventory::available(string id){   Node* temp=stmap->findSplay(id); if(temp!=NULL) return temp->value; else return -1;   } void SplayTreeInventory::into(string id, int amount) { Node* temp=stmap->findSplay(id); if(temp==NULL){ stmap->putSplay(id, amount); } else{ temp->key+=amount; } } void SplayTreeInventory::out(string id, int amount) { Node* temp=stmap->findSplay(id); if(temp==NULL){ } else{ if(temp->value<=amount) stmap->erase(id); else temp->value-=amount; } } int SplayTreeInventory::size() { return stmap->size(); } void SplayTreeInventory::printAll() { printAllHelper(this->stmap->root); }

  • Please compute the time complexity of the algorithm. (ex: O(N) or O(N log N)) void Set::unite(const...

    Please compute the time complexity of the algorithm. (ex: O(N) or O(N log N)) void Set::unite(const Set& seti, const Set& set2) { const Set* sp = &set2; if (this == &seti) { if (this == &set2) return; } else if (this == &set2) sp = &setl; else { *this = seti; if (&setl == &set2) return; } Node* pl = m_head->m_next; Node* p 2 = sp->m_head->m_next; while (pl != m_head & & p2 != sp->m_head) { if (pl->m_value < p2->m_value)...

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