Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
22 January 2021

Topological Sort

by mervyn

Motivation

Given a set of tasks with dependencies,

Restriction of Paths in DAGs

In a DAG, given two different vertices \(v_{j}\) and \(v_{k}\), there cannot be both be a path from \(v_{j}\) to \(v_{k}\) and a path from \(v_{k}\) to \(v_{j}\)

Proof: Assume otherwise; thus there exists two paths \((v_{j}, v_{1.1}, v_{1.2}, v_{1.3}, ..., v_{k})\) \((v_{k}, v_{2.1}, v_{2.2}, v_{2.3}, ..., v_{j})\) From this, we can construct the path \((v_{j}, v_{1.1}, v_{1.2}, v_{1.3}, ..., v_{k}, v_{2.1}, v_{2.2}, v_{2.3}, ..., v_{j})\) This path is a cycle, but this is an acyclic graph \(\therefore\) contradiction

Definition of Topological Sorting

A topological sorting of the vertices in a DAG is an ordering \(v_{1}, v_{2}, v_{3}, ..., v_{\left|V\right|}\) such that \(v_{j}\) appears before \(v_{k}\) if there is a path from \(v_{j}\) to \(v_{k}\)

Given this DAG, a topological sort is topo

H, C have no in-degree, no dependencies

There are paths from H, C, I, D and J to F, so all these must come before F in a topological sort This sorting need not to be unique

Applications

Consider the course instructor getting ready for a dinner out wear: jacket, shirt, briefs, socks, tie, etc. constraints: pants after the briefs, socks before shoes

taskgraph

One topological sort: briefs, pants, wallets, keys, belt, socks, shoes, shirt, tie, jacket, iPod, watch A more reasonable topological sort: briefs, socks, pants, shirt, belt, tie, jacket, wallet, keys, iPod, watch, shoes

C++ header and source files have #include statements

Curriculum prerequisite

Topological Sort

Idea:

Given a DAG \(V\), make a copy \(W\) and iterate:

On this graph, iterate the following \(|V|\) = 12 times

topo

We can start from C or H Having completed task C, only H can be completed we can choose from D or I …

Thus, one possible topological sort would be: C, H, D, A< B, I, J, F, G, E, K or H, I, J, C, D, F, G, K, L, A, B, E

Analysis

Tools necessary for a topological sort

Iterate at least \(|V|\) times, so the runtime must be : \(\Omega(|V|)\)

Tree traversals

Each value in the in-degree table is associated with an edge

Total runtime:

Implementation

To implement a topological sort:

While the queue is not empty:

Trick not to delete data from the queue and increasing the size of the queue in accordance with the number of the original vertices

Type array[vertex_size];
int ihead = 0, itail = -1;
ihead == itail +1
itail++;
array[itail] = next vertex;
Type current_top = array[ihead];
ihead++;

Place each vertex into the queue exactly once

Example

topo

  1. Initialize:
    • The array of in-degrees
    • The queue
  2. Stepping through the table, push all source vertices into the queue
  3. Pop the front of the queue
    • C has one neighbor: D
    • Decrement its in-degree
  4. Pop H
    • H has two neighbors: D and I
    • Decrement their in-degrees
      • Both are decremented to zero, so push them onto the queue
  5. Pop D
    • D has three neighbors: A, E and F
    • Decrement their in-degrees
      • A is decremented to zero, so push it onto the queue
  6. Iterate this process
    • E, L have no neighbor: they are sink
  7. When the queue is empty, we are done

The array stores the topological sorting

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 14-1 DAG 구조와 활용 분야 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: