void C_DirectedGraph::depthFirstTopologicalSort (TC_UniqueArray <uint32_t> & outSortedNodes,
                                                 TC_UniqueArray <uint32_t> & outUnsortedNodes) const {
  outSortedNodes.setCountToZero () ;
  outUnsortedNodes.setCountToZero () ;
//--- Get working copies
  TC_UniqueArray <bool> nodes ; getNodeBoolArray (nodes) ;
  TC_UniqueArray <uint32_t> dependencyCount (mReverseEdges.count (), 0 COMMA_HERE) ;
  for (int32_t i=0 ; i<mReverseEdges.count () ; i++) {
    TC_UniqueArray <uint32_t> s ; mReverseEdges (i COMMA_HERE).getValueArray (s) ;
    for (int32_t j=0 ; j<s.count () ; j++) {
      dependencyCount.incrementAtIndex ((int32_t) s (j COMMA_HERE) COMMA_HERE) ;
    }
  }
//--- Loop
  TC_UniqueArray <uint32_t> workingArray ;
  TC_UniqueArray <uint32_t> s ;
  bool loop = true ;
  while (loop) {
  //--- Find a node without any dependence  
    for (int32_t i=0 ; (i<dependencyCount.count ()) && (workingArray.count () == 0) ; i++) {
      if (nodes (i COMMA_HERE) && (dependencyCount (i COMMA_HERE) == 0)) {
        nodes.setObjectAtIndex (false, i COMMA_HERE) ;
        workingArray.appendObject ((uint32_t) i) ;
      }
    }
    loop = workingArray.count () > 0 ;
    if (loop) {
      const uint32_t node = workingArray.lastObject (HERE) ;
      workingArray.removeLastObject (HERE) ;
      outSortedNodes.appendObject (node) ;
      mReverseEdges ((int32_t) node COMMA_HERE).getValueArray (s) ;
      for (int32_t j=0 ; j<s.count () ; j++) {
        const uint32_t candidate = s (j COMMA_HERE) ;
        dependencyCount.decrementAtIndex ((int32_t) candidate COMMA_HERE) ;
        if (dependencyCount ((int32_t) candidate COMMA_HERE) == 0) {
          workingArray.appendObject (candidate) ;
          nodes.setObjectAtIndex (false, (int32_t) candidate COMMA_HERE) ;
        }
      }
    }
  }
//--- Add unusorted nodes
  for (int32_t i=0 ; i<nodes.count () ; i++) {
    if (nodes (i COMMA_HERE)) {
      outUnsortedNodes.appendObject ((uint32_t) i) ;
    }
  }
}
C_DirectedGraph C_DirectedGraph::subGraphFromNodes (const C_UIntSet & inStartNodes,
                                                    #ifdef USE_NODE_NAMES_WITH_SUBGRAPH_COMPUTATION
                                                      const TC_UniqueArray <C_String> & inNodeNames,
                                                    #endif
                                                    const C_UIntSet & inNodesToExclude) const {
  TC_UniqueArray <bool> nodeBoolArray ; mNodes.getBoolValueArray (nodeBoolArray) ;
  C_DirectedGraph result ;
  { C_UIntSet nodeSet = inStartNodes ;
    nodeSet -= inNodesToExclude ;
    result.addNodes (nodeSet) ;
  }
  #ifdef USE_NODE_NAMES_WITH_SUBGRAPH_COMPUTATION
    { TC_UniqueArray <uint32_t> sourceNodeArray ; result.getNodeValueArray (sourceNodeArray) ;
      printf ("START NODES (%d):\n", sourceNodeArray.count ()) ;
      for (int32_t i=0 ; i<sourceNodeArray.count () ; i++) {
        printf ("  - %d (%s)\n", sourceNodeArray (i COMMA_HERE), inNodeNames ((int32_t) sourceNodeArray (i COMMA_HERE) COMMA_HERE).cString (HERE)) ;
      }
    }
  #endif
  bool loop = true ;
  while (loop) {
    loop = false ;
    TC_UniqueArray <uint32_t> sourceNodeArray ; result.getNodeValueArray (sourceNodeArray) ;
    #ifdef USE_NODE_NAMES_WITH_SUBGRAPH_COMPUTATION
      printf ("********************* NODE COUNT %d\n", sourceNodeArray.count ()) ;
    #endif
    for (int32_t i=0 ; i<sourceNodeArray.count () ; i++) {
      const uint32_t sourceNodeIndex = sourceNodeArray (i COMMA_HERE) ;
      if (nodeBoolArray ((int32_t) sourceNodeIndex COMMA_HERE)) {
        #ifdef USE_NODE_NAMES_WITH_SUBGRAPH_COMPUTATION
          printf ("NEW NODE %d (%s):\n", sourceNodeIndex, inNodeNames ((int32_t) sourceNodeIndex COMMA_HERE).cString (HERE)) ;
        #endif
        loop = true ;
        nodeBoolArray.setObjectAtIndex (false, (int32_t) sourceNodeIndex COMMA_HERE) ;
        C_UIntSet s = mEdges ((int32_t) sourceNodeIndex COMMA_HERE) ;
        s -= inNodesToExclude ;
        TC_UniqueArray <uint32_t> targetNodeArray ; s.getValueArray (targetNodeArray) ;
        for (int32_t j=0 ; j<targetNodeArray.count () ; j++) {
          result.addEdge (sourceNodeIndex, targetNodeArray (j COMMA_HERE)) ;
          #ifdef USE_NODE_NAMES_WITH_SUBGRAPH_COMPUTATION
            printf ("  edge %d (%s)\n", targetNodeArray (j COMMA_HERE), inNodeNames ((int32_t) targetNodeArray (j COMMA_HERE) COMMA_HERE).cString (HERE)) ;
          #endif
        }
      }
    }
  }
  return result ;
}
void C_DirectedGraph::getNodesInvolvedInCircularities (TC_UniqueArray <uint32_t> & outNodes) const {
  outNodes.setCountToZero () ;
//--- Get working copies
  TC_UniqueArray <bool> nodes ; getNodeBoolArray (nodes) ;
  TC_UniqueArray <uint32_t> successorCount ;
  TC_UniqueArray <uint32_t> predecessorCount ;
  for (int32_t i=0 ; i<mEdges.count () ; i++) {
    successorCount.appendObject (mEdges (i COMMA_HERE).count ()) ;
  }
  for (int32_t i=0 ; i<mReverseEdges.count () ; i++) {
    predecessorCount.appendObject (mReverseEdges (i COMMA_HERE).count ()) ;
  }
//--- Eliminate nodes that have no successor or no predecessor
  bool loop = true ;
  while (loop) {
    loop = false ;
    for (int32_t i=0 ; i<nodes.count () ; i++) {
      if (nodes (i COMMA_HERE) && ((successorCount (i COMMA_HERE) == 0) || (predecessorCount (i COMMA_HERE) == 0))) {
        loop = true ;
        nodes.setObjectAtIndex (false, i COMMA_HERE) ;
        TC_UniqueArray <uint32_t> s ; mEdges (i COMMA_HERE).getValueArray (s) ;
        for (int32_t j=0 ; j<s.count () ; j++) {
          predecessorCount.decrementAtIndex ((int32_t) s (j COMMA_HERE) COMMA_HERE) ;
        }
        mReverseEdges (i COMMA_HERE).getValueArray (s) ;
        for (int32_t j=0 ; j<s.count () ; j++) {
          successorCount.decrementAtIndex ((int32_t) s (j COMMA_HERE) COMMA_HERE) ;
        }
      }
    }
  }

//--- Add circular nodes
  for (int32_t i=0 ; i<nodes.count () ; i++) {
    if (nodes (i COMMA_HERE)) {
      outNodes.appendObject ((uint32_t) i) ;
    }
  }
}
void C_DirectedGraph::topologicalSort (TC_UniqueArray <uint32_t> & outSortedNodes,
                                       TC_UniqueArray <uint32_t> & outUnsortedNodes) const {
  outSortedNodes.setCountToZero () ;
  outUnsortedNodes.setCountToZero () ;
//--- Get working copies
  TC_UniqueArray <bool> nodes ; getNodeBoolArray (nodes) ;
  TC_UniqueArray <uint32_t> dependencyCount (mReverseEdges.count (), 0 COMMA_HERE) ;
  for (int32_t i=0 ; i<mReverseEdges.count () ; i++) {
    TC_UniqueArray <uint32_t> s ; mReverseEdges (i COMMA_HERE).getValueArray (s) ;
    for (int32_t j=0 ; j<s.count () ; j++) {
      dependencyCount.incrementAtIndex ((int32_t) s (j COMMA_HERE) COMMA_HERE) ;
    }
  }
//--- Loop
  bool loop = true ;
  TC_UniqueArray <uint32_t> s ;
  while (loop) {
    loop = false ;
    for (int32_t i=0 ; i<nodes.count () ; i++) {
      if (nodes (i COMMA_HERE) && (dependencyCount (i COMMA_HERE) == 0)) {
        loop = true ;
        outSortedNodes.appendObject ((uint32_t) i) ;
        nodes.setObjectAtIndex (false, i COMMA_HERE) ;
        mReverseEdges (i COMMA_HERE).getValueArray (s) ;
        for (int32_t j=0 ; j<s.count () ; j++) {
          dependencyCount.decrementAtIndex ((int32_t) s (j COMMA_HERE) COMMA_HERE) ;
        }
      }
    }
  }
//--- Add unusorted nodes
  for (int32_t i=0 ; i<nodes.count () ; i++) {
    if (nodes (i COMMA_HERE)) {
      outUnsortedNodes.appendObject ((uint32_t) i) ;
    }
  }
}