public boolean TopologicalSort(ArrayList<Vertex> graph) {

    Stack<Vertex> vertexStack = new Stack<Vertex>();
    //ArrayList<Vertex> vertexVisitedOrder = new ArrayList<Vertex>();

    // Add all vertices with an indegree of 0 to the stack.
    for (Vertex vertex : graph) {
      if (vertex.inCount == 0) {
        vertexStack.push(vertex);
      }
    }

    //initialise the vistied index.
    int i = 1;

    while (!vertexStack.empty()) {
      // get our next vertex from the stack, and add it to the visited array.
      Vertex currentVertex = vertexStack.pop();
      currentVertex.order = i++;
      //vertexVisitedOrder.add(currentVertex);
      
      // Process the dependencies immediately, rather than delaying processing
      // Based on a derived path.
      if (!currentVertex.adjList.isEmpty()) {
        // have dependencies, so calculate
        currentVertex.calculate();
      }
      // Get all edges from this vertex.
      for (Cell cell : currentVertex.adjList) {
        Vertex w = cells[cell.row][cell.column];
        w.inCount--;
        // Decrement the in degree count, and if 0, add to the processing list.
        if (w.inCount == 0) {
          vertexStack.add(w);
        }
      }
    }
    // If we have processed all edges, then return the visited order array.
    if (i > graph.size()) {
      return true; // We found a parh, and processed all edges 
    }
    return false; // return an error.
  }