No implementation and analysis the initialization

Info icon This preview shows pages 68–79. Sign up to view the full content.

View Full Document Right Arrow Icon
No
Image of page 68

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Implementation and analysis The initialization requires (| V |) memory and run time We iterate | V | – 1 times, each time finding next closest vertex to the source Iterating through the table requires is (| V |) time Each time we find a vertex, we must check all of its neighbors With an adjacency matrix, the run time is (| V |(| V | + | V |)) = (| V | 2 ) With an adjacency list, the run time is (| V | 2 + | E |) = (| V | 2 ) as | E | = O(| V | 2 ) Can we do better? How about using a priority queue to find the closest vertex? Assume we are using a binary heap
Image of page 69
Implementation and analysis The initialization still requires (| V |) memory and run time The priority queue will also requires (| V |) memory We must use an adjacency list, not an adjacency matrix We iterate | V | times, each time finding the closest vertex to the source Place the distances into a priority queue The size of the priority queue is (| V |) Thus, the work required for this is O(| V | ln(| V |)) Is this all the work that is necessary? Recall that each edge visited may result in a distance being updated Thus, the work required for this is O(| E | ln(| V |)) Thus, the total run time is O(| V | ln(| V |) + | E | ln(| V |)) = O(| E | ln(| V |))
Image of page 70

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Implementation and analysis Here is an example of a worst-case scenario: Immediately, all of the vertices are placed into the queue Each time a vertex is visited, all the remaining vertices are checked, and in succession, each is pushed to the top of the binary heap
Image of page 71
Implementation and analysis We could use a different heap structure: A Fibonacci heap is a node-based heap Pop is still O(ln(| V |)) , but inserting and moving a key is (1) Thus, because we are only calling pop | V | – 1 times, the overall run-time reduces to O(| E | + | V | ln(| V |))
Image of page 72

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Implementation and analysis Thus, we have two run times when using A binary heap: O(| E | ln(| V |)) A Fibonacci heap: O(| E | + | V | ln(| V |)) Questions : Which is faster if | E | = (| V |) ? How about if | E | = (| V | 2 ) ?
Image of page 73
Summary We have seen an algorithm for finding single-source shortest paths Start with the initial vertex Continue finding the next vertex that is closest Dijkstra’s algorithm always finds the next closest vertex It solves the problem in O(| E | + | V | ln(| V |)) time
Image of page 74

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Triangle Inequality If the distances satisfy the triangle inequality, That is, the distance between a and b is less than the distance from a to c plus the distance from c to b , we can use the A* search which is faster than Dijkstra’s algorithm All Euclidean distances satisfy the triangle inequality
Image of page 75
Negative Weights If some of the edges have negative weight, so long as there are no cycles with negative weight, the Bellman-Ford algorithm will find the minimum distance It is slower than Dijkstra’s algorithm
Image of page 76

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Outline Definition and applications Dijkstra’s algorithm Floyd-Warshall algorithm
Image of page 77
Background Dijkstra’s algorithm finds the shortest path between two nodes Run time: O(| E | ln(| V |)) If we wanted to find the shortest path between all pairs of nodes, we could apply Dijkstra’s algorithm to each vertex: Run time: O(| V | | E | ln(| V |))
Image of page 78

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 79
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern