Esempio n. 1
0
def has_converter(source, target=Validator(type=None, format=None)):
    """Determines if any converters exist from a given type, and optionally format.

    Underneath, this just traverses the edges until it finds one which matches
    the arguments.

    :param source: ``Validator`` tuple indicating the type/format being
        converted `from`.
    :param target: ``Validator`` tuple indicating the type/format being
        converted `to`.
    :returns: ``True`` if it can converter from ``source`` to ``target``,
        ``False`` otherwise.
    """
    sources = []

    for node in conv_graph.nodes():
        if ((source.type is None) or (source.type == node.type)) and \
           ((source.format is None) or (source.format == node.format)):
            sources.append(node)

    for u in sources:
        reachable = single_source_shortest_path(conv_graph, u)
        del reachable[u]  # Ignore path to self, since there are no self loops

        for v in reachable:
            if ((target.type is None) or (target.type == v.type)) and \
               ((target.format is None) or (target.format == v.format)):
                return True

    return False
Esempio n. 2
0
def has_converter(source, target=Validator(type=None, format=None)):
    """Determines if any converters exist from a given type, and optionally format.

    Underneath, this just traverses the edges until it finds one which matches
    the arguments.

    :param source: ``Validator`` tuple indicating the type/format being
        converted `from`.
    :param target: ``Validator`` tuple indicating the type/format being
        converted `to`.
    :returns: ``True`` if it can converter from ``source`` to ``target``,
        ``False`` otherwise.
    """
    sources = []

    for node in conv_graph.nodes():
        if ((source.type is None) or (source.type == node.type)) and \
           ((source.format is None) or (source.format == node.format)):
            sources.append(node)

    for u in sources:
        reachable = single_source_shortest_path(conv_graph, u)
        del reachable[u]  # Ignore path to self, since there are no self loops

        for v in reachable:
            if ((target.type is None) or (target.type == v.type)) and \
               ((target.format is None) or (target.format == v.format)):
                return True

    return False
Esempio n. 3
0
def print_conversion_table():
    """
    Print a table of supported conversion paths in CSV format with ``'from'``
    and ``'to'`` columns to standard output.
    """

    print 'from,to'

    for node in conv_graph.nodes():
        paths = single_source_shortest_path(conv_graph, node)
        reachable_conversions = [p for p in paths.keys() if p != node]

        for dest in reachable_conversions:
            print '%s:%s,%s:%s' % (node[0], node[1], dest[0], dest[1])
Esempio n. 4
0
def print_conversion_table():
    """
    Print a table of supported conversion paths in CSV format with ``'from'``
    and ``'to'`` columns to standard output.
    """

    print 'from,to'

    for node in conv_graph.nodes():
        paths = single_source_shortest_path(conv_graph, node)
        reachable_conversions = [p for p in paths.keys() if p != node]

        for dest in reachable_conversions:
            print '%s:%s,%s:%s' % (node[0], node[1], dest[0], dest[1])
Esempio n. 5
0
def print_conversion_graph():
    """
    Print a graph of supported conversion paths in DOT format to standard
    output.
    """

    print 'digraph g {'

    for node in conv_graph.nodes():
        paths = single_source_shortest_path(conv_graph, node)
        reachable_conversions = [p for p in paths.keys() if p != node]

        for dest in reachable_conversions:
            print '"%s:%s" -> "%s:%s"' % (node[0], node[1], dest[0], dest[1])

    print '}'
Esempio n. 6
0
def print_conversion_graph():
    """
    Print a graph of supported conversion paths in DOT format to standard
    output.
    """

    print 'digraph g {'

    for node in conv_graph.nodes():
        paths = single_source_shortest_path(conv_graph, node)
        reachable_conversions = [p for p in paths.keys() if p != node]

        for dest in reachable_conversions:
            print '"%s:%s" -> "%s:%s"' % (node[0], node[1], dest[0], dest[1])

    print '}'
Esempio n. 7
0
def shortestPathLength(source, target, seed, part2=False):
    PT = PathTimer()
    CM = CubicleMaze(seed)
    cutoff = sum(target)

    if part2:
        paths = single_source_shortest_path(CM, source, cutoff=cutoff)
        path = paths.keys()
    else:
        manhattan = lambda c1, c2: abs(c1[0] - c2[0]) + abs(c1[1] - c2[1])
        path = astar_path(CM,
                          source,
                          target,
                          heuristic=manhattan,
                          weight="weight")

    CM.plotPath(cutoff, cutoff, path)
    return len(path) if part2 else len(path) - 1
Esempio n. 8
0
def find_shortest_paths_from_source(sentence, start_token):
    g = build_networkXGraph_from_spaCy_depGraph(sentence)
    shortest_paths_from_source = single_source_shortest_path(g, start_token)
    return shortest_paths_from_source
Esempio n. 9
0
            # we've hit everything
            complete = True
            break
        direction, newpos = breadcrumbs.pop()
        # print(f"attempting to backtrack to {newpos}")
        output = droid.run([direction])
        if direction > 0:
            pos = newpos

    # print(grid)

gridpoints = grid.keys()
minrow = min(x[0] for x in gridpoints)
maxrow = max(x[0] for x in gridpoints)
mincol = min(x[1] for x in gridpoints)
maxcol = max(x[1] for x in gridpoints)

grid[target] = "^"

for row in range(minrow, maxrow + 1):
    for col in range(mincol, maxcol + 1):
        print(grid[(row, col)], end="")
    print()

print(target)

print(shortest_path(gridnet, (0, 0), target))
print(len(shortest_path(gridnet, (0, 0), target)))
paths = single_source_shortest_path(gridnet, target)
print(max(len(x) for x in paths.values()))