Topological Sort - Hong Kong University of Science and ...€¦ · Topological Sort Algorithm...

32
Topological Sort Version of September 23, 2016 Version of September 23, 2016 Topological Sort 1 / 14

Transcript of Topological Sort - Hong Kong University of Science and ...€¦ · Topological Sort Algorithm...

  • Topological Sort

    Version of September 23, 2016

    Version of September 23, 2016 Topological Sort 1 / 14

  • Directed Graph

    In a directed graph, we distinguish between edge (u, v) and edge(v , u)

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    out-degree of a vertex is the number of edges leaving it

    in-degree of a vertex is the number of edges entering it

    Each edge (u, v) contributes one to the out-degree of u andone to the in-degree of v∑

    v∈Vout-degree(v) =

    ∑v∈V

    in-degree(v) = |E |

    Version of September 23, 2016 Topological Sort 2 / 14

  • Directed Graph

    In a directed graph, we distinguish between edge (u, v) and edge(v , u)

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    out-degree of a vertex is the number of edges leaving it

    in-degree of a vertex is the number of edges entering it

    Each edge (u, v) contributes one to the out-degree of u andone to the in-degree of v∑

    v∈Vout-degree(v) =

    ∑v∈V

    in-degree(v) = |E |

    Version of September 23, 2016 Topological Sort 2 / 14

  • Directed Graph

    In a directed graph, we distinguish between edge (u, v) and edge(v , u)

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    out-degree of a vertex is the number of edges leaving it

    in-degree of a vertex is the number of edges entering it

    Each edge (u, v) contributes one to the out-degree of u andone to the in-degree of v∑

    v∈Vout-degree(v) =

    ∑v∈V

    in-degree(v) = |E |

    Version of September 23, 2016 Topological Sort 2 / 14

  • Directed Graph

    In a directed graph, we distinguish between edge (u, v) and edge(v , u)

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    out-degree of a vertex is the number of edges leaving it

    in-degree of a vertex is the number of edges entering it

    Each edge (u, v) contributes one to the out-degree of u andone to the in-degree of v

    ∑v∈V

    out-degree(v) =∑v∈V

    in-degree(v) = |E |

    Version of September 23, 2016 Topological Sort 2 / 14

  • Directed Graph

    In a directed graph, we distinguish between edge (u, v) and edge(v , u)

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    out-degree of a vertex is the number of edges leaving it

    in-degree of a vertex is the number of edges entering it

    Each edge (u, v) contributes one to the out-degree of u andone to the in-degree of v∑

    v∈Vout-degree(v) =

    ∑v∈V

    in-degree(v) = |E |

    Version of September 23, 2016 Topological Sort 2 / 14

  • Usage of Directed Graph

    Directed graphs are often used to represent order-dependenttasks

    That is, we cannot start a task before another task finishes

    Edge (u, v) denotes that task v cannot start until task u isfinished

    u v

    Clearly, for the system not to hang, the graph must be acyclic

    It must be a directed acyclic graph (or DAG)

    Version of September 23, 2016 Topological Sort 3 / 14

  • Usage of Directed Graph

    Directed graphs are often used to represent order-dependenttasks

    That is, we cannot start a task before another task finishes

    Edge (u, v) denotes that task v cannot start until task u isfinished

    u v

    Clearly, for the system not to hang, the graph must be acyclic

    It must be a directed acyclic graph (or DAG)

    Version of September 23, 2016 Topological Sort 3 / 14

  • Usage of Directed Graph

    Directed graphs are often used to represent order-dependenttasks

    That is, we cannot start a task before another task finishes

    Edge (u, v) denotes that task v cannot start until task u isfinished

    u v

    Clearly, for the system not to hang, the graph must be acyclic

    It must be a directed acyclic graph (or DAG)

    Version of September 23, 2016 Topological Sort 3 / 14

  • Usage of Directed Graph

    Directed graphs are often used to represent order-dependenttasks

    That is, we cannot start a task before another task finishes

    Edge (u, v) denotes that task v cannot start until task u isfinished

    u v

    Clearly, for the system not to hang, the graph must be acyclic

    It must be a directed acyclic graph (or DAG)

    Version of September 23, 2016 Topological Sort 3 / 14

  • Usage of Directed Graph

    Directed graphs are often used to represent order-dependenttasks

    That is, we cannot start a task before another task finishes

    Edge (u, v) denotes that task v cannot start until task u isfinished

    u v

    Clearly, for the system not to hang, the graph must be acyclic

    It must be a directed acyclic graph (or DAG)

    Version of September 23, 2016 Topological Sort 3 / 14

  • Course dependence chart 09/10

    Red: COMP/CSIE CoreGreen: COMP/CSIE RequiredPurple: CSIE (NW) RequiredBlue: CSIE (MC) Required

    104 (F)

    180 (S)

    211 (F,S) 251 (F)

    272 (S)

    271 (F,S)252 (F)

    M113 (F,S)

    231 (S)

    201 (sum,S) 221

    361 (F,S)

    362 (F)

    381

    303

    336 341 (s), 343 (F,S),344 (F),

    342

    332, 334

    364 (F)

    170 (F)

    152 (F,S)

    E211 (Sum,F)

    E214 (S)

    E314 (F)F and S means offered in Fall and Spring respectively.Course offering schedule shown here is for reference only; the actual offering schedule may vary slightly from year to year.

    M100/M150 (F,S)E101/E102 (F,S)

    M144 (F,S)/E210 (F)

    327, 328

    355

    111

    M021 (F,S)

    Version of September 23, 2016 Topological Sort 4 / 14

  • Topological Sort

    A Topological ordering of a graph is a linear ordering of thevertices of a DAG such that if (u, v) is in the graph, u appearsbefore v in the linear ordering

    e.g., order in which classes can be taken

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Topological ordering may not be unique as there are many“equal” elements!E.G., there are several topological orderings

    0, 6, 1, 4, 3, 2, 5, 7, 8, 90, 4, 1, 6, 2, 5, 3, 7, 8, 9. . .

    Version of September 23, 2016 Topological Sort 5 / 14

  • Topological Sort

    A Topological ordering of a graph is a linear ordering of thevertices of a DAG such that if (u, v) is in the graph, u appearsbefore v in the linear ordering

    e.g., order in which classes can be taken

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Topological ordering may not be unique as there are many“equal” elements!E.G., there are several topological orderings

    0, 6, 1, 4, 3, 2, 5, 7, 8, 90, 4, 1, 6, 2, 5, 3, 7, 8, 9. . .

    Version of September 23, 2016 Topological Sort 5 / 14

  • Topological Sort

    A Topological ordering of a graph is a linear ordering of thevertices of a DAG such that if (u, v) is in the graph, u appearsbefore v in the linear ordering

    e.g., order in which classes can be taken

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Topological ordering may not be unique as there are many“equal” elements!E.G., there are several topological orderings

    0, 6, 1, 4, 3, 2, 5, 7, 8, 90, 4, 1, 6, 2, 5, 3, 7, 8, 9. . .

    Version of September 23, 2016 Topological Sort 5 / 14

  • Topological Sort

    A Topological ordering of a graph is a linear ordering of thevertices of a DAG such that if (u, v) is in the graph, u appearsbefore v in the linear ordering

    e.g., order in which classes can be taken

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Topological ordering may not be unique as there are many“equal” elements!

    E.G., there are several topological orderings0, 6, 1, 4, 3, 2, 5, 7, 8, 90, 4, 1, 6, 2, 5, 3, 7, 8, 9. . .

    Version of September 23, 2016 Topological Sort 5 / 14

  • Topological Sort

    A Topological ordering of a graph is a linear ordering of thevertices of a DAG such that if (u, v) is in the graph, u appearsbefore v in the linear ordering

    e.g., order in which classes can be taken

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Topological ordering may not be unique as there are many“equal” elements!E.G., there are several topological orderings

    0, 6, 1, 4, 3, 2, 5, 7, 8, 90, 4, 1, 6, 2, 5, 3, 7, 8, 9. . .

    Version of September 23, 2016 Topological Sort 5 / 14

  • Topological Sort Algorithm

    ObservationsA DAG must contain at least one vertex with in-degree zero(why?)

    Algorithm: Topological Sort1 Output a vertex u with in-degree zero in current graph.

    2 Remove u and all edges (u, v) from current graph.

    3 If graph is not empty, goto step 1.

    CorrectnessAt every stage, current graph is a DAG (why?)Because current graph is always a DAG, algorithm can alwaysoutput some vertex. So algorithm outputs all vertices.Suppose order output was not a topological order. Then thereis some edge (u, v) such that v appears before u in the order.This is impossible, though, because v can not be output untiledge (u, v) is removed!

    Version of September 23, 2016 Topological Sort 6 / 14

  • Topological Sort Algorithm

    ObservationsA DAG must contain at least one vertex with in-degree zero(why?)

    Algorithm: Topological Sort1 Output a vertex u with in-degree zero in current graph.

    2 Remove u and all edges (u, v) from current graph.

    3 If graph is not empty, goto step 1.

    CorrectnessAt every stage, current graph is a DAG (why?)Because current graph is always a DAG, algorithm can alwaysoutput some vertex. So algorithm outputs all vertices.Suppose order output was not a topological order. Then thereis some edge (u, v) such that v appears before u in the order.This is impossible, though, because v can not be output untiledge (u, v) is removed!

    Version of September 23, 2016 Topological Sort 6 / 14

  • Topological Sort Algorithm

    ObservationsA DAG must contain at least one vertex with in-degree zero(why?)

    Algorithm: Topological Sort1 Output a vertex u with in-degree zero in current graph.

    2 Remove u and all edges (u, v) from current graph.

    3 If graph is not empty, goto step 1.

    Correctness

    At every stage, current graph is a DAG (why?)Because current graph is always a DAG, algorithm can alwaysoutput some vertex. So algorithm outputs all vertices.Suppose order output was not a topological order. Then thereis some edge (u, v) such that v appears before u in the order.This is impossible, though, because v can not be output untiledge (u, v) is removed!

    Version of September 23, 2016 Topological Sort 6 / 14

  • Topological Sort Algorithm

    ObservationsA DAG must contain at least one vertex with in-degree zero(why?)

    Algorithm: Topological Sort1 Output a vertex u with in-degree zero in current graph.

    2 Remove u and all edges (u, v) from current graph.

    3 If graph is not empty, goto step 1.

    CorrectnessAt every stage, current graph is a DAG (why?)Because current graph is always a DAG, algorithm can alwaysoutput some vertex. So algorithm outputs all vertices.Suppose order output was not a topological order. Then thereis some edge (u, v) such that v appears before u in the order.This is impossible, though, because v can not be output untiledge (u, v) is removed!

    Version of September 23, 2016 Topological Sort 6 / 14

  • Topological Sort Algorithm

    Topological sort(G )

    Initialize Q to be an empty queue;foreach u in V do

    if in-degree(u) = 0 then// Find all starting vertices

    Enqueue(Q, u);

    end

    endwhile Q is not empty do

    u = Dequeue(Q);Output u;foreach v in Adj(u) do

    // remove u’s outgoing edgesin-degree(v) = in-degree(v)− 1;if in-degree(v) = 0 then

    Enqueue(Q, v);end

    end

    end

    Version of September 23, 2016 Topological Sort 7 / 14

  • Example

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9 Q= {0}

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9 Q = {}

    Version of September 23, 2016 Topological Sort 8 / 14

  • Example

    1

    4

    2

    3

    7

    5

    8

    9 Q= {1, 4, 3}

    6

    1

    4

    2

    3

    7

    5

    8

    9 Q = {6, 1, 4}

    Output: 0

    Output: 0, 6

    Version of September 23, 2016 Topological Sort 9 / 14

  • Example

    2

    3

    7

    5

    8

    9 Q = {3, 2}

    4

    2

    3

    7

    5

    8

    9 Q = {4, 3, 2}

    Output: 0, 6, 1

    Output: 0, 6, 1, 4

    Version of September 23, 2016 Topological Sort 10 / 14

  • Example

    7

    5

    8

    9 Q = {7, 5}

    2 7

    5

    8

    9 Q = {2}

    Output: 0, 6, 1, 4, 3

    Output: 0, 6, 1, 4, 3, 2

    Version of September 23, 2016 Topological Sort 11 / 14

  • Example

    8

    9 Q = {8}

    5

    8

    9 Q = {5, 8}

    Output: 0, 6, 1, 4, 3, 2, 7

    Output: 0, 6, 1, 4, 3, 2, 7, 5

    Version of September 23, 2016 Topological Sort 12 / 14

  • Example

    Q = {}

    9 Q = {9}

    Output: 0, 6, 1, 4, 3, 2, 7, 5, 8

    Output: 0, 6, 1, 4, 3, 2, 7, 5, 8, 9

    Done!

    0

    6

    1

    4

    2

    3

    7

    5

    8

    9

    Version of September 23, 2016 Topological Sort 13 / 14

  • Topological Sort: Complexity

    We never visit a vertex more than once

    For each vertex, we examine all outgoing edges∑v∈V out-degree(v) = E

    Therefore, the running time is O(V + E )

    Question

    Can we use DFS to implement topological sort?

    Version of September 23, 2016 Topological Sort 14 / 14

  • Topological Sort: Complexity

    We never visit a vertex more than once

    For each vertex, we examine all outgoing edges∑v∈V out-degree(v) = E

    Therefore, the running time is O(V + E )

    Question

    Can we use DFS to implement topological sort?

    Version of September 23, 2016 Topological Sort 14 / 14

  • Topological Sort: Complexity

    We never visit a vertex more than once

    For each vertex, we examine all outgoing edges∑v∈V out-degree(v) = E

    Therefore, the running time is O(V + E )

    Question

    Can we use DFS to implement topological sort?

    Version of September 23, 2016 Topological Sort 14 / 14

  • Topological Sort: Complexity

    We never visit a vertex more than once

    For each vertex, we examine all outgoing edges∑v∈V out-degree(v) = E

    Therefore, the running time is O(V + E )

    Question

    Can we use DFS to implement topological sort?

    Version of September 23, 2016 Topological Sort 14 / 14