CSE 373: Data Structures and Algorithms Topological Sort Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ... 1
<latexit sha1_base64="YVFLN63fwlLlrJ6AzC10IuRh9Y=">AEznicbVPBbtNAEHWbACVQaEGCA5cRTaVURFSCVEJKlUqUHqolArSVoqjarOeJEvWa+PdQmWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVPu61exf1oFc9ykvfi61ibiKXJUcTCERzV4Qwjg98hv0UK7mHg+6hMckUF4KR9mlbcz4YZBKGEUyKHwgekWGKowYTQNUVamAm1XpRo5FYR+aM6DPojEwKYEub0SGsiqMAjI0erdUp6bANDwhYIwEhmXCXyzaPEtM8r+sD0hvM4tTsfCYmQZHihQUG0A/NZM7A0q0o2BiORGqtkPCBEkyPgI0Bsi1Gw93gGJLBZqCJaAxwNIbM7oFVzl8eq7KsRTX5EQF5T351rMW2bthNJEvBbXYaESxDt0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyW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mWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVPu61exf1oFc9ykvfi61ibiKXJUcTCERzV4Qwjg98hv0UK7mHg+6hMckUF4KR9mlbcz4YZBKGEUyKHwgekWGKowYTQNUVamAm1XpRo5FYR+aM6DPojEwKYEub0SGsiqMAjI0erdUp6bANDwhYIwEhmXCXyzaPEtM8r+sD0hvM4tTsfCYmQZHihQUG0A/NZM7A0q0o2BiORGqtkPCBEkyPgI0Bsi1Gw93gGJLBZqCJaAxwNIbM7oFVzl8eq7KsRTX5EQF5T351rMW2bthNJEvBbXYaESxDt0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit> Review Dijkstra’s algorithm Vertex Distance Predecessor Processed 1: function Dijkstra(Graph G, Vertex source) . with MPQ initialize distances to ∞ , source.dist = 0 2: mark all vertices unprocessed 3: initialize MPQ as a min priority queue; add source with priority 0 4: while MPQ is not empty do 5: u = MPQ.getMin() 6: for each edge (u,v) leaving u do 7: if u.dist + w(u,v) < v.dist then 8: if v.dist == ∞ then 9: MPQ.insert(v, u.dist + w(u, v)) 10: else 11: MPQ.decreasePriority(v, u.dist + w(u,v)) 12: end if 13: B 5 v.dist = u.dist + w(u,v) 14: 1 v.predecessor = u 15: T end if 16: S 1 end for 17: 6 mark u as processed 18: 1 end while 19: E 20: end function C 1 CSE 373 AU 18 2
<latexit sha1_base64="YVFLN63fwlLlrJ6AzC10IuRh9Y=">AEznicbVPBbtNAEHWbACVQaEGCA5cRTaVURFSCVEJKlUqUHqolArSVoqjarOeJEvWa+PdQmWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVPu61exf1oFc9ykvfi61ibiKXJUcTCERzV4Qwjg98hv0UK7mHg+6hMckUF4KR9mlbcz4YZBKGEUyKHwgekWGKowYTQNUVamAm1XpRo5FYR+aM6DPojEwKYEub0SGsiqMAjI0erdUp6bANDwhYIwEhmXCXyzaPEtM8r+sD0hvM4tTsfCYmQZHihQUG0A/NZM7A0q0o2BiORGqtkPCBEkyPgI0Bsi1Gw93gGJLBZqCJaAxwNIbM7oFVzl8eq7KsRTX5EQF5T351rMW2bthNJEvBbXYaESxDt0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit> <latexit sha1_base64="Y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37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVPu61exf1oFc9ykvfi61ibiKXJUcTCERzV4Qwjg98hv0UK7mHg+6hMckUF4KR9mlbcz4YZBKGEUyKHwgekWGKowYTQNUVamAm1XpRo5FYR+aM6DPojEwKYEub0SGsiqMAjI0erdUp6bANDwhYIwEhmXCXyzaPEtM8r+sD0hvM4tTsfCYmQZHihQUG0A/NZM7A0q0o2BiORGqtkPCBEkyPgI0Bsi1Gw93gGJLBZqCJaAxwNIbM7oFVzl8eq7KsRTX5EQF5T351rMW2bthNJEvBbXYaESxDt0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit> Review Dijkstra’s algorithm Vertex Distance Predecessor Processed S 0 -- Yes 1: function Dijkstra(Graph G, Vertex source) . with MPQ initialize distances to ∞ , source.dist = 0 2: B 1 S Yes mark all vertices unprocessed 3: initialize MPQ as a min priority queue; add source with priority 0 4: C 6 S Yes while MPQ is not empty do 5: u = MPQ.getMin() E 2 B Yes 6: for each edge (u,v) leaving u do 7: T 3 E Yes if u.dist + w(u,v) < v.dist then 8: if v.dist == ∞ then 9: MPQ.insert(v, u.dist + w(u, v)) 10: else 11: MPQ.decreasePriority(v, u.dist + w(u,v)) 12: end if 13: B 5 v.dist = u.dist + w(u,v) 14: 1 v.predecessor = u 15: T end if 16: S 1 end for 17: 6 mark u as processed 18: 1 end while 19: E 20: end function C 1 CSE 373 AU 18 3
Recommend
More recommend