MIT OpenCourseWare: Free Courses from the institute

14. APSP and Johnson

MIT OpenCourseWare: Free Courses from the institute

Summary

The final lecture on graph algorithms focuses on the all pairs shortest paths problem, a significant extension of the single source shortest paths problem. The discussion begins with a review of previously covered algorithms, including BFS, Dijkstra's, and Bellman-Ford, highlighting their efficiency and applicability to different types of graphs. The lecture emphasizes that while single source shortest paths can be computed efficiently, the all pairs shortest paths problem inherently requires a quadratic output size, making it impossible to achieve better than quadratic time complexity.

To solve the all pairs shortest paths problem, the lecturer introduces Johnson's algorithm, which cleverly transforms a graph with potentially negative edge weights into one with non-negative weights while preserving shortest paths. This transformation involves adding a new vertex connected to all other vertices with zero-weight edges and using the Bellman-Ford algorithm to compute shortest path distances from this new vertex. If no negative weight cycles are detected, the graph can be reweighted, allowing the use of Dijkstra's algorithm to efficiently compute shortest paths.

The lecture concludes by summarizing the steps of Johnson's algorithm and its efficiency, noting that it can solve the all pairs shortest paths problem in nearly linear time for sparse graphs. The session wraps up the unit on graphs, preparing students for the next topic on algorithm design in dynamic programming.

Show Notes

MIT 6.006 Introduction to Algorithms, Spring 2020Instructor: Jason KuView the complete course: https://ocw.mit.edu/6-006S20YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP63EdVPNLG3ToM6LaEUuStEYUploaded 13-09-21This lecture focuses on solving any all-pairs shortest paths (APSP) in weighted graphs. Johnson's algorithm is introduced.License: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

More from MIT OpenCourseWare: Free Courses from the institute

Dec 6, 2026

MIT 14.12 Economic Applications of Game Theory, Fall 2025 Instructor: Ian Ball View the complete course: https://ocw.mit.edu/courses/14-12-economic-applications-of-game-theory-fall-2025/YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP63quuKvMHCt3cmTmt0O2qpvUploaded 18-05-26This lecture explores how companies compete and decide what prices to charge. Using real-world examples like oil production and cell phone companies, the instructor explains why some markets have one dominant player (like a local monopoly) while others have many competitors. License: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

Dec 6, 2026

MIT 8.701 Introduction to Nuclear and Particle Physics, Fall 2020Instructor: Markus Klute View the complete course: https://ocw.mit.edu/8-701F20YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP60Do91PdN978llIsvjKW0auUploaded 24-06-21Discussion of the early history and people in nuclear and particle physics from the 1820s to 1939.License: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

Dec 6, 2026

MIT 18.404J Theory of Computation, Fall 2020Instructor: Michael SipserView the complete course: https://ocw.mit.edu/18-404JF20YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP60_JNv2MmK3wkOt9syvfQWYUploaded 06-10-21Quickly reviewed last lecture. Discussed a connection between games and quantifiers. Described the formula game and showed that generalized geography is PSPACE-complete. Introduced log space: L and NL. Defined the configuration graph to prove NL is a subset of P.License: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

Dec 6, 2026

MIT 2.997 Direct Solar/Thermal To Electrical Energy Conversion Technologies, Fall 2009Instructor: Prof. Gang ChenUploaded 26-06-24View the complete course: https://ocw.mit.edu/courses/2-997-direct-solar-thermal-to-electrical-energy-conversion-technologies-fall-2009/YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP62sv7_wYRKqvf1HsL4p54KjRichardson formulaThermionic engines: vacuum, solid-stateSchottky barrier and diodepn junction and diodeLicense: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

Dec 6, 2026

MIT 14.12 Economic Applications of Game Theory, Fall 2025 Uploaded 18-05-26Instructor: Ian Ball View the complete course: https://ocw.mit.edu/courses/14-12-economic-applications-of-game-theory-fall-2025/YouTube Playlist: https://www.youtube.com/playlist?list=PLUl4u3cNGP63quuKvMHCt3cmTmt0O2qpvIn this lecture, Ian Ball discusses infinitely repeated games, which are scenarios where players engage in the same stage game indefinitely, allowing for cooperation that wouldn't exist in one-shot games. License: Creative Commons BY-NC-SAMore information at https://ocw.mit.edu/termsMore courses at https://ocw.mit.eduSupport OCW at http://ow.ly/a1If50zVRlQWe encourage constructive comments and discussion on OCW’s YouTube and other social media channels. Personal attacks, hate speech, trolling, and inappropriate comments are not allowed and may be removed. More details at https://ocw.mit.edu/comments. Learn more about your ad choices. Visit megaphone.fm/adchoices Learn more about your ad choices. Visit megaphone.fm/adchoices

See all episodes →