Subgraph ComposedRule::CreateSubgraph()
{
    std::set<const Node *> leaves;
    const std::set<const Node *> &baseLeaves = m_baseRule.GetLeaves();
    size_t i = 0;
    for (std::set<const Node *>::const_iterator p = baseLeaves.begin();
            p != baseLeaves.end(); ++p) {
        const Node *baseLeaf = *p;
        if (baseLeaf->GetType() == TREE && i < m_attachedRules.size()) {
            const Subgraph *attachedRule = m_attachedRules[i++];
            if (attachedRule) {
                leaves.insert(attachedRule->GetLeaves().begin(),
                              attachedRule->GetLeaves().end());
                continue;
            }
        }
        leaves.insert(baseLeaf);
    }
    return Subgraph(m_baseRule.GetRoot(), leaves);
}
Subgraph AlignmentGraph::ComputeMinimalFrontierGraphFragment(
    Node *root,
    const std::set<Node *> &frontierSet)
{
  std::stack<Node *> expandableNodes;
  std::set<const Node *> expandedNodes;

  if (root->IsSink()) {
    expandedNodes.insert(root);
  } else {
    expandableNodes.push(root);
  }

  while (!expandableNodes.empty()) {
    Node *n = expandableNodes.top();
    expandableNodes.pop();

    const std::vector<Node *> &children = n->GetChildren();

    for (std::vector<Node *>::const_iterator p(children.begin());
         p != children.end(); ++p) {
      Node *child = *p;
      if (child->IsSink()) {
        expandedNodes.insert(child);
        continue;
      }
      std::set<Node *>::const_iterator q = frontierSet.find(child);
      if (q == frontierSet.end()) { //child is not from the frontier set
        expandableNodes.push(child);
      } else if (child->GetType() == TARGET) { // still need source word
        expandableNodes.push(child);
      } else {
        expandedNodes.insert(child);
      }
    }
  }

  return Subgraph(root, expandedNodes);
}