tools/android/dependency_analysis/count_cycles.py
def find_cycles(base_graph: graph.Graph,
                max_cycle_length: int) -> List[List[Cycle]]:
    """Finds all cycles in the graph within a certain length.

    The algorithm is as such: Number the nodes arbitrarily. For i from 0 to
    the number of nodes, find all cycles starting and ending at node i using
    only nodes with numbers >= i (see find_cycles_from_node). Taking the union
    of the results will give all relevant cycles in the graph.

    Returns:
        A list |cycles| of length |max_cycle_length| + 1, where cycles[i]
          contains all cycles of length i.
    """
    sorted_base_graph_nodes = sorted(base_graph.nodes)
    # Some preliminary setup: map between the graph nodes' unique keys and a
    # unique number, since the algorithm needs some way to decide when a node is
    # 'bigger'. Nodes with a lower number will be processed first, which
    # influences the output cycles. For example, the cycle abca is also valid as
    # the cycle bcab or cabc. By numbering node a lower than b and c, it is
    # guaranteed that the cycle will be output as abca.
    node_to_id = {}
    for generated_node_id, node in enumerate(sorted_base_graph_nodes):
        node_to_id[node] = generated_node_id

    num_nodes = base_graph.num_nodes
    cycles = [[] for _ in range(max_cycle_length + 1)]

    for start_node in sorted_base_graph_nodes:
        start_node_cycles = find_cycles_from_node(start_node, max_cycle_length,
                                                  node_to_id)
        for cycle_length, cycle_list in enumerate(start_node_cycles):
            cycles[cycle_length].extend(cycle_list)

    # Convert cycles to be tuples of nodes, so the cycles are hashable and
    # immutable.
    immutable_cycles = []
    for cycle_list in cycles:
        immutable_cycles.append([tuple(cycle) for cycle in cycle_list])
    return immutable_cycles
Finds all cycles in the graph within a certain length.

    The algorithm is as such: Number the nodes arbitrarily. For i from 0 to
    the number of nodes, find all cycles starting and ending at node i using
    only nodes with numbers >= i (see find_cycles_from_node). Taking the union
    of the results will give all relevant cycles in the graph.

    Returns:
        A list |cycles| of length |max_cycle_length| + 1, where cycles[i]
          contains all cycles of length i.
    
Finds all cycles in the graph within a certain length.
The algorithm is as such: Number the nodes arbitrarily. For i from 0 to
the number of nodes, find all cycles starting and ending at node i using
only nodes with numbers >= i . Taking the union
of the results will give all relevant cycles in the graph.
Finds all cycles in the graph within a certain length . The algorithm is as such : Number the nodes arbitrarily . For i from 0 to the number of nodes find all cycles starting and ending at node i using only nodes with numbers > = i . Taking the union of the results will give all relevant cycles in the graph .
def find_cycles(base_graph: graph.Graph,
                max_cycle_length: int) -> List[List[Cycle]]:
    sorted_base_graph_nodes = sorted(base_graph.nodes)
    node_to_id = {}
    for generated_node_id, node in enumerate(sorted_base_graph_nodes):
        node_to_id[node] = generated_node_id
    num_nodes = base_graph.num_nodes
    cycles = [[] for _ in range(max_cycle_length + 1)]
    for start_node in sorted_base_graph_nodes:
        start_node_cycles = find_cycles_from_node(start_node, max_cycle_length,
                                                  node_to_id)
        for cycle_length, cycle_list in enumerate(start_node_cycles):
            cycles[cycle_length].extend(cycle_list)
    immutable_cycles = []
    for cycle_list in cycles:
        immutable_cycles.append([tuple(cycle) for cycle in cycle_list])
    return immutable_cycles
def find_cycles ( base_graph : graph . Graph , max_cycle_length : int ) -> List [ List [ Cycle ] ] : sorted_base_graph_nodes = sorted ( base_graph . nodes ) node_to_id = { } for generated_node_id , node in enumerate ( sorted_base_graph_nodes ) : node_to_id [ node ] = generated_node_id num_nodes = base_graph . num_nodes cycles = [ [ ] for _ in range ( max_cycle_length + 1 ) ] for start_node in sorted_base_graph_nodes : start_node_cycles = find_cycles_from_node ( start_node , max_cycle_length , node_to_id ) for cycle_length , cycle_list in enumerate ( start_node_cycles ) : cycles [ cycle_length ] . extend ( cycle_list ) immutable_cycles = [ ] for cycle_list in cycles : immutable_cycles . append ( [ tuple ( cycle ) for cycle in cycle_list ] ) return immutable_cycles
Finds all cycles in the graph within a certain length.
Finds all cycles in the graph within a certain length .
"""Finds all cycles in the graph within a certain length.

    The algorithm is as such: Number the nodes arbitrarily. For i from 0 to
    the number of nodes, find all cycles starting and ending at node i using
    only nodes with numbers >= i (see find_cycles_from_node). Taking the union
    of the results will give all relevant cycles in the graph.

    Returns:
        A list |cycles| of length |max_cycle_length| + 1, where cycles[i]
          contains all cycles of length i.
    """ # Some preliminary setup: map between the graph nodes' unique keys and a # unique number, since the algorithm needs some way to decide when a node is # 'bigger'. Nodes with a lower number will be processed first, which # influences the output cycles. For example, the cycle abca is also valid as # the cycle bcab or cabc. By numbering node a lower than b and c, it is # guaranteed that the cycle will be output as abca. # Convert cycles to be tuples of nodes, so the cycles are hashable and # immutable.
A list |cycles| of length |max_cycle_length| + 1, where cycles[i]
contains all cycles of length i.
A list |cycles| of length |max_cycle_length| + 1 where cycles [ i ] contains all cycles of length i .
base_graph
graph.Graph
max_cycle_length
int