<commit_before># -*- test-case-name: spreadflow_core.test.test_graph -*-
"""
Graph utilities.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from collections import defaultdict

def contract(g, f):
    """
    Returns a directed graph with all vertices from the input graph for which
    f() returns True while maintaining reachability.
    """
    vertices = [v for v in g.keys() if f(v)]
    result = defaultdict(set, ((v, set()) for v in vertices))
    visited = set()

    stack = list(zip(vertices, vertices))
    while len(stack):
        pair = stack.pop()
        if pair not in visited:
            visited.add(pair)
            v, base = pair
            for w in g.get(v, set()):
                if f(w):
                    result[base].add(w)
                else:
                    stack.append((w, base))

    return result

def vertices(g, f=lambda v: True):
    result = set([v for v in g.keys() if f(v)])
    result.update([w for arcs in g.values() for w in arcs if f(w)])
    return result

def reverse(g):
    result = defaultdict(set)
    for v, arcs in g.items():
        for w in arcs:
            result[w].add(v)

    return result<commit_msg>Do not use/return defaultdict<commit_after># -*- test-case-name: spreadflow_core.test.test_graph -*-
"""
Graph utilities.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

def contract(g, f):
    """
    Returns a directed graph with all vertices from the input graph for which
    f() returns True while maintaining reachability.
    """
    vertices = [v for v in g.keys() if f(v)]
    result = {v: set() for v in vertices}
    visited = set()

    stack = list(zip(vertices, vertices))
    while len(stack):
        pair = stack.pop()
        if pair not in visited:
            visited.add(pair)
            v, base = pair
            for w in g.get(v, set()):
                if f(w):
                    result.setdefault(base, set()).add(w)
                else:
                    stack.append((w, base))

    return result

def vertices(g, f=lambda v: True):
    result = set([v for v in g.keys() if f(v)])
    result.update([w for arcs in g.values() for w in arcs if f(w)])
    return result

def reverse(g):
    result = {}
    for v, arcs in g.items():
        for w in arcs:
            result.setdefault(w, set()).add(v)

    return result
{'fraction_non_alphanumeric': 0.11630744204961366, 'fraction_numerical': 0.0, 'mean_word_length': 2.215686274509804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5685841', 'n_tokens_mistral': 751, 'n_tokens_neox': 730, 'n_words': 262}