Topological Sort Autumn 2018 Shrirang (Shri) Mare - - PowerPoint PPT Presentation

topological sort
SMART_READER_LITE
LIVE PREVIEW

Topological Sort Autumn 2018 Shrirang (Shri) Mare - - PowerPoint PPT Presentation

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,


slide-1
SLIDE 1

Topological Sort

CSE 373: Data Structures and Algorithms

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 ...

Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu

1

slide-2
SLIDE 2

Review

Dijkstra’s algorithm

CSE 373 AU 18 2

Vertex Distance Predecessor Processed

1: function Dijkstra(Graph G, Vertex source)

. with MPQ

2:

initialize distances to ∞, source.dist = 0

3:

mark all vertices unprocessed

4:

initialize MPQ as a min priority queue; add source with priority 0

5:

while MPQ is not empty do

6:

u = MPQ.getMin()

7:

for each edge (u,v) leaving u do

8:

if u.dist + w(u,v) < v.dist then

9:

if v.dist == ∞ then

10:

MPQ.insert(v, u.dist + w(u, v))

11:

else

12:

MPQ.decreasePriority(v, u.dist + w(u,v))

13:

end if

14:

v.dist = u.dist + w(u,v)

15:

v.predecessor = u

16:

end if

17:

end for

18:

mark u as processed

19:

end while

20: end function

<latexit sha1_base64="YVFLN63fwlLlrJ6AzC10IuRh9Y=">AEznicbVPBbtNAEHWbACVQaEGCA5cRTaVURFSCVEJKlUqUHqolArSVoqjarOeJEvWa+PdQmWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVP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t0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit>

S B C T E 1 5 1 6 1 1

slide-3
SLIDE 3

Review

Dijkstra’s algorithm

CSE 373 AU 18 3

Vertex Distance Predecessor Processed S

  • Yes

B 1 S Yes C 6 S Yes E 2 B Yes T 3 E Yes

1: function Dijkstra(Graph G, Vertex source)

. with MPQ

2:

initialize distances to ∞, source.dist = 0

3:

mark all vertices unprocessed

4:

initialize MPQ as a min priority queue; add source with priority 0

5:

while MPQ is not empty do

6:

u = MPQ.getMin()

7:

for each edge (u,v) leaving u do

8:

if u.dist + w(u,v) < v.dist then

9:

if v.dist == ∞ then

10:

MPQ.insert(v, u.dist + w(u, v))

11:

else

12:

MPQ.decreasePriority(v, u.dist + w(u,v))

13:

end if

14:

v.dist = u.dist + w(u,v)

15:

v.predecessor = u

16:

end if

17:

end for

18:

mark u as processed

19:

end while

20: end function

<latexit sha1_base64="YVFLN63fwlLlrJ6AzC10IuRh9Y=">AEznicbVPBbtNAEHWbACVQaEGCA5cRTaVURFSCVEJKlUqUHqolArSVoqjarOeJEvWa+PdQmWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAc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EQF5T351rMW2bthNJEvBbXYaESxDt0gw9S4/VkDzm9ucZ2wesGLEcp73gwg83aL+f9j9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit>

S B C T E 1 5 1 6 1 1

slide-4
SLIDE 4

1: function Dijkstra(Graph G, Vertex source)

. with MPQ

2:

initialize distances to ∞, source.dist = 0

3:

mark all vertices unprocessed

4:

initialize MPQ as a min priority queue; add source with priority 0

5:

while MPQ is not empty do

6:

u = MPQ.getMin()

7:

for each edge (u,v) leaving u do

8:

if u.dist + w(u,v) < v.dist then

9:

if v.dist == ∞ then

10:

MPQ.insert(v, u.dist + w(u, v))

11:

else

12:

MPQ.decreasePriority(v, u.dist + w(u,v))

13:

end if

14:

v.dist = u.dist + w(u,v)

15:

v.predecessor = u

16:

end if

17:

end for

18:

mark u as processed

19:

end while

20: end function

<latexit sha1_base64="YVFLN63fwlLlrJ6AzC10IuRh9Y=">AEznicbVPBbtNAEHWbACVQaEGCA5cRTaVURFSCVEJKlUqUHqolArSVoqjarOeJEvWa+PdQmWBUd+kS/gNxjHTmjS7sXjmXkz+97O9EMptGk2/6yslsp37t5bu1958HD90eONzSdnOrARxw4PZBd9JlGKR2jDASL8Imd+XeN4fH2bx8xgjLQL1xUxC7PlsqMRAcGbIdbm5+svt41CohMlhEAkz8gVPu61exf1oFc9ykvfi61ibiKXJUcTCERzV4Qwjg98hv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9MYUV3Sm+hS2hxPH1Jg4Z1sJv216GUXNbpySHFNFVcVN7CsF1ubDUbzemBm0arMLac4rQvN1deu17AbTZxXDKtu61maHoJywZFIsloNYaMj9kQu2Qq5qPuJdNdSWGbPB4MiNQgUAam3uIhPlaT/w+ZfrMjPRyLHPeFutaM9jrJUKF1qDieaOBldm0Z4tH8x8hN3JCBuNEXnDgIxYxbmg9F7vM1EmX3aFGS9QDL2NJ/xEqvOK0fEx5Lm2kyR4hSbehk5Gkzn2EqV/zSoWEbi3LetM420yD7d3TrYKyRfc14L52a03LeOAfOJ6ftdBy+re0XnpWel5ul+NyWv6Zp6uFJinzsIp/4HDlCNTQ=</latexit>

Running time analysis

CSE 373 AU 18 4

slide-5
SLIDE 5

Algorithm/Author Time complexity Year Ford !( # $ % ) 1956 Bellman-Ford. Shimbel !( # % ) 1958 Dijkstra’s algorithm with binary heap '( ( )*+ , + , )*+ |,|) 1959 Dijkstra’s algorithm with Fibonacchi heap !( % + # log |#|) 1984 Gabow’s algorithm !( % + # log |#|) 1990 Thorup !( % + # log log |#|) 2004

History of shortest path algorithms

CSE 373 AU 18 5

History of shortest path algorithms. In this class, from this table, you are expected to know only Dijkstra’s algorithm with binary heap and its time complexity. You are not expected to know the other algorithms or their time complexities.

slide-6
SLIDE 6

Other applications of shortest paths

Shortest path algorithms are obviously useful for GoogleMaps. The wonderful thing about graphs is they can encode arbi bitr trary relationships among

  • bjects.

CSE 373 AU 18 6

I have a message I need to get from point s to point t. But the connections are unreliable. What path should I send the message along so it has the best chance

  • f arriving?

s u v t 0.6 0.8 0.97 0.7 0.2

Given: a directed graph G, where each edge weight is the probability of successfully transmitting a message across that edge Find: the path from s to t with maximum probability of message transmission Maximum Probability Path Problem

slide-7
SLIDE 7

Robot navigation Urban traffic planning Tramp steamer problem Optimal pipelining of VLSI chips Operator scheduling Subroutine in higher level algorithms Exploiting arbitrage opportunities in currency exchanges Open shortest path first routing protocol for IP Optimal truck routing through given traffic congestion

Other applications of shortest paths

CSE 373 AU 18 7

slide-8
SLIDE 8

Topological Sort

CSE 373 AU 18

slide-9
SLIDE 9

Problem 1: Ordering Dependencies

Given a bunch of courses with prerequisites, find an order to take the courses in.

CSE 373 AU 18 9

Math 126 CSE 142 CSE 143 CSE 373 CSE 374 CSE 417 CSE 142 CSE 143 CSE 373 CSE 374 CSE 417 Math 126

slide-10
SLIDE 10

Problem 1: Ordering Dependencies

Given a directed graph G, where we have an edge from u to v if u must happen before v. We can only do things one at a time, can we find an order that re respects dependencies?

CSE 373 AU 18 10

Given: a directed graph G Find: an ordering of the vertices so all edges go from left to right. Topological Sort (aka Topological Ordering) Uses: Compiling multiple files Graduating Manufacturing workflows (assembly lines)

slide-11
SLIDE 11

Topological Ordering

A course prerequisite chart and a possible topological ordering.

CSE 373 AU 18 11

Math 126 CSE 142 CSE 143 CSE 373 CSE 374 CSE 417 Math 126 CSE 142 CSE 143 CSE 373 CSE 374 CSE 417

slide-12
SLIDE 12

Can we always order a graph?

CSE 373 AU 18 12

A graph has a topological ordering if and only if it is a DAG. A directed graph without any cycles. Directed Acyclic Graph (DAG)

A B C

Can you topologically order this graph?

slide-13
SLIDE 13

Ordering a DAG

Does this graph have a topological ordering? If so find one.

CSE 373 AU 18 13

A B C E D

If a vertex doesn’t have any edges going into it, we can add it to the ordering. More generally, if the only incoming edges are from vertices already in the ordering, it’s safe to add.

slide-14
SLIDE 14

1: function TopologicalSort(Graph G, Vertex source) 2:

count how many incoming edges each vertex has

3:

Collection toProcess = new Collection()

4:

for each Vertex v in G do

5:

if v.edgesRemaining == 0 then

6:

toProcess.insert(v)

7:

end if

8:

end for

9:

topOrder = new List()

10:

while toProcess is not empty do

11:

u = toProcess.remove()

12:

topOrder.insert(u)

13:

for each edge (u,v) leaving u do

14:

v.edgesRemaining = v.edgesRemaining - 1

15:

if v.edgesRemaining == 0 then

16:

toProcess.insert(v)

17:

end if

18:

end for

19:

end while

20: end function

<latexit sha1_base64="VzLj61hA2ofpKTWC8I5FjIB1mo=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cqOBhDcYoL1b+Oj7+YAE3Vxyf6Hc2tIu40FBKz60aFBaUdYBS76RwuobxrSIORTjFnsFj8qpek5FZSy1lAM9lOt0AiS3MmybzuMsl12vw/8NAbo6EvgXr0lNpozFAFSxIp9E4X9tot9rFgWXDr4wNrzrd8/WVN4NA8yRC5bhk1vb9duyGTNOcJnrKrEYM37BJtgnU7EI7TArxD+DTfIEMCZVjDXJrvDezMhYZO0GlFkxFxob7/lzn+9RM3h1mQsWJQ8VLoHEiaWiQbxIEwpAq5ZQMxom+4MBDZh3tG+LKFfzmd12xYToq6DnCXdDZLQaF1ocYIBidvF9H+y2Sb0cpKEPEIo/JYXg/Zvj3XZONlp+WQf7mzs71YjX/VeK+8pud7b71976PX9Xoer/2o/ar9rv2pv6x36p/r3TK0tlLlPcWTv3bX1PjgZY=</latexit>

How Do We Find a Topological Ordering?

CSE 373 AU 18 14

slide-15
SLIDE 15

1: function TopologicalSort(Graph G, Vertex source) 2:

count how many incoming edges each vertex has

3:

Collection toProcess = new Collection()

4:

for each Vertex v in G do

5:

if v.edgesRemaining == 0 then

6:

toProcess.insert(v)

7:

end if

8:

end for

9:

topOrder = new List()

10:

while toProcess is not empty do

11:

u = toProcess.remove()

12:

topOrder.insert(u)

13:

for each edge (u,v) leaving u do

14:

v.edgesRemaining = v.edgesRemaining - 1

15:

if v.edgesRemaining == 0 then

16:

toProcess.insert(v)

17:

end if

18:

end for

19:

end while

20: end function

<latexit sha1_base64="VzLj61hA2ofpKTWC8I5FjIB1mo=">AEn3icrVNb9NAEHVDgBK+WjhyGagqpVKJ4kqIXipVQhAqIUhp0xYlUbVZT+JV17vW7tolsvK/+CtcuMLPYPzRtGk4shfPzs7Mmzd+M4qlsK7d/rlSu1O/e+/+6oPGw0ePnzxdW392YnViOPa4ltqcjZhFKRT2nHASz2KDLBpJPB1dvMvfT1M0Vmh17KYxDiM2UWIsOHPkOl+vHTYGI5wIlTE50Ua4MBJ81veHjcGHRPE8KDvWMdWZUI480sbNso5hc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8VLoHEiaWiQbxIEwpAq5ZQMxom+4MBDZh3tG+LKFfzmd12xYToq6DnCXdDZLQaF1ocYIBidvF9H+y2Sb0cpKEPEIo/JYXg/Zvj3XZONlp+WQf7mzs71YjX/VeK+8pud7b71976PX9Xoer/2o/ar9rv2pv6x36p/r3TK0tlLlPcWTv3bX1PjgZY=</latexit>

What’s the running time?

CSE 373 AU 18 15

slide-16
SLIDE 16

Strongly Connected Components

slide-17
SLIDE 17

Connected [Undirected] Graphs

Co Connected grap aph – a graph where every vertex is connected to every other vertex via some

  • path. It is not required for every vertex to have

an edge to every other vertex There exists some way to get from each vertex to every other vertex

CSE 373 AU 18 17

Sansa Robb Bran Arya Rickon Jon Dany

Co Connected Co Component – a subgraph in which any two vertices are connected via some path, but is connected to no additional vertices in the supergraph

  • There exists some way to get from each

vertex within the connected component to every other vertex in the connected component

  • A vertex with no edges is itself a connected

component

Viserys

slide-18
SLIDE 18

Strongly Connected Components

Note: the direction of the edges matters!

CSE 373 AU 18 18

A subgraph C such that every pair of vertices in C is connected via some path in both directions, and there is no other vertex which is connected to every vertex of C in both directions. Strongly Connected Component

D B C A E

slide-19
SLIDE 19

Strongly Connected Components Problem

CSE 373 AU 18 19

Given: A directed graph G Find: The strongly connected components of G Strongly Connected Components Problem

D C F B E A K J

{A}, {B}, {C,D,E,F}, {J,K}

slide-20
SLIDE 20

SCC Algorithm

  • Ok. How do we make a computer do this?

You could:

  • run a [B/D]FS from every vertex,
  • For each vertex record what other vertices it can get to
  • and figure it out from there.

But you can do better. There’s actually an O(|V|+|E|) algorithm! I only want you to remember two things about the algorithm:

  • It is an application of depth first search.
  • It runs in linear time

The problem with running a [B/D]FS from every vertex is you recompute a lot of information. The time you are popped off the stack in DFS contains a “smart” ordering to do a second DFS where you don’t need to recompute that information.

CSE 373 AU 18 20

slide-21
SLIDE 21

Why Find SCCs?

Graphs are useful because they encode relationships between arbitrary objects. We’ve found the strongly connected components of G. Let’s build a new graph out of them! Call it H

  • Have a vertex for each of the strongly connected components
  • Add an edge from component 1 to component 2 if there is an edge from a vertex inside 1 to one inside 2.

CSE 373 AU 18 21

D C F B E A K J

1 3 4 2

slide-22
SLIDE 22

Why Find SCCs?

That’s awful meta. Why? This new graph summarizes reachability information of the original graph.

  • I can get from A (of G) in 1 to F (of G) in 3 if and only if I can get from 1 to 3 in H.

CSE 373 AU 18 22

D C F B E A K J

1 3 4 2

slide-23
SLIDE 23

Why Must H Be a DAG?

H is always a DAG (do you see why?).

CSE 373 AU 18 23

slide-24
SLIDE 24

Takeaways

Finding SCCs lets you co collapse your graph to the meta-structure. If (and only if) your graph is a DAG, you can find a topological sort of your graph. Both of these algorithms run in linear time. Just about everything you could want to do with your graph will take at least as long. You should think of these as “almo most t free” pr prepr processing g of your graph.

  • Your other graph algorithms only need to work on
  • topologically sorted graphs and
  • strongly connected graphs.

CSE 373 AU 18 24

slide-25
SLIDE 25

Appendix: Strongly Connected Components Algorithm

slide-26
SLIDE 26

Efficient SCC

We’d like to find all the vertices in our strongly connected component in time corresponding to the size of the component, not for the whole graph. We can do that with a DFS (or BFS) as long as we don’t leave our connected component. If we’re a “sink” component, that’s guaranteed. I.e. a component whose vertex in the meta- graph has no outgoing edges. How do we find a sink component? We don’t have a meta-graph yet (we need to find the components first) DFS can find a vertex in a source component, i.e. a component whose vertex in the meta- graph has no incoming edges.

  • That vertex is the last one to be popped off the stack.

So if we run DFS in the reversed graph (where each edge points the opposite direction) we can find a sink component.

CSE 373 AU 18 26

slide-27
SLIDE 27

Efficient SCC

So from a DFS in the reversed graph, we can use the order vertices are popped off the stack to find a sink component (in the original graph). Run a DFS from that vertex to find the vertices in that component in size of that component time. Now we can delete the edges coming into that component. The last remaining vertex popped off the stack is a sink of the remaining graph, and now a DFS from them won’t leave the component. Iterate this process (grab a sink, start DFS, delete edges entering the component). In total we’ve run two DFSs. (since we never leave our component in the second DFS). More information, and pseudocode: https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm http://jeffe.cs.illinois.edu/teaching/algorithms/notes/19-dfs.pdf (mathier)

CSE 373 AU 18 27