<commit_before>import { alg } from 'graphlib';

export default function initialOrdering(G) {
  let order = [];

  const start = G.nodes().filter(u => G.node(u).rank === 0)
          .concat(G.sources());
  if (start.length === 0) {
    throw Error('No sources or nodes with rank 0');
  }

  alg.preorder(G, start).forEach(u => {
    const rank = G.node(u).rank;
    while (rank >= order.length) order.push([]);
    order[rank].push(u);
  });

  return order;
}<commit_msg>don't raise error when there are no source nodes<commit_after>import { alg } from 'graphlib';

export default function initialOrdering(G) {
  let order = [];

  let start = G.nodes()
        .filter(u => G.node(u).rank === 0)
        .concat(G.sources());
  if (start.length === 0) {
    // start with first node even if it's not a source or rank 0
    start = G.nodes().slice(0, 1);
  }

  alg.preorder(G, start).forEach(u => {
    const rank = G.node(u).rank;
    while (rank >= order.length) order.push([]);
    order[rank].push(u);
  });

  return order;
}
{'fraction_non_alphanumeric': 0.17829457364341086, 'fraction_numerical': 0.007751937984496124, 'mean_word_length': 2.973076923076923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1869602', 'n_tokens_mistral': 362, 'n_tokens_neox': 345, 'n_words': 95}