function topoSort (vertexs, edges) {
  const sorted = []
  let cursor = vertexs.length
  const visited = {}
  let i = cursor
  while (i--) {
    if (!visited[i]) visit(vertexs[i], i, [])
  }

  return sorted.reverse()

  function visit (vertex, i, predecessors) {
    if (predecessors.indexOf(vertex) >= 0) {
      throw new Error('Cyclic dependency:' + JSON.stringify(vertex))
    }

    if (visited[i]) return
    visited[i] = true

    const outgoing = edges.filter(function (edge) {
      return edge.to() === vertex
    })

    let preds = []
    if (outgoing.length > 0) {
      preds = predecessors.concat(vertex)
    }
    let from
    outgoing.forEach(function (edge) {
      from = edge.from()
      visit(from, vertexs.indexOf(from), preds)
    })

    sorted[--cursor] = vertex
  }
}
@param {Array} all vertex in digraph
@param {Object} all edges in the digraph