Exemplo n.º 1
0
def main():
    """ Make it so. """
    edges = None
    graph_name = setup_args()
    try:
        print(('Loading graph: {}'.format(graph_name)))
        edges = getattr(data.data, graph_name)
    except (AttributeError, TypeError):
        print(('\nInvalid graph name. Available graphs:\n\t{}\n'.format(
            '\n\t'.join([x for x in dir(data.data)
                         if not x.startswith('__')]))))
        sys.exit()

    original_graph = network.Graph(edges)

    print(('{} edges'.format(len(original_graph))))
    if not original_graph.is_eularian:
        print('Converting to Eularian path...')
        graph = eularian.make_eularian(original_graph)
        print('Conversion complete')
        print(('\tAdded {} edges'.format(len(graph) - len(original_graph))))
        print(('\tTotal cost is {}'.format(graph.total_cost)))
    else:
        graph = original_graph

    print('Attempting to solve Eularian Circuit...')
    route, attempts = eularian.eularian_path(graph, start=1)
    if not route:
        print(('\tGave up after {} attempts.'.format(attempts)))
    else:
        print(('\tSolved in {} attempts'.format(attempts, route)))
        print(('Solution: ({} edges)'.format(len(route) - 1)))
        print(('\t{}'.format(route)))
Exemplo n.º 2
0
def main():
    edges = None
    graph_name = setup_args()
    try:
        edges = getattr(data.data, graph_name)
    except (AttributeError, TypeError):
        print('Invalid graph name. Available graphs:\n\t{}\n'.format(
            '\n\t'.join([x for x in dir(data.data)
                         if not x.startswith('__')])))
        sys.exit()

    original_graph = network.Graph(edges)

    print('Number of edges: {}'.format(len(original_graph)))
    if not original_graph.is_eularian:
        graph, num_dead_ends = eularian.make_eularian(original_graph)
        # print('\tAdded {} edges'.format(len(graph) - len(original_graph) + num_dead_ends))
        print('Total cost: {}'.format(graph.total_cost))
    else:
        graph = original_graph

    route, attempts = eularian.eularian_path(graph, start=1)
    if not route:
        print('Gave up after <{}> attempts.'.format(attempts))
    else:
        print('Solution ({} edges):'.format(len(route) - 1))
        print('{}'.format(route))
Exemplo n.º 3
0
def main():
    edges = None
    graph_name = setup_args()
    try:
        edges = getattr(data.data, graph_name)
    except (AttributeError, TypeError):
        print('Графа з такою назвою не існує. Доступні графи: \n\t{}\n'.format(
            '\n\t'.join([x for x in dir(data.data)
            if not x.startswith('__')])))
        sys.exit()

    original_graph = network.Graph(edges)

    print('Кількість ребер: {}'.format(len(original_graph)))
    if not original_graph.is_eularian:
        graph, num_dead_ends = eularian.make_eularian(original_graph)
        #print('\tAdded {} edges'.format(len(graph) - len(original_graph) + num_dead_ends))
        print('Загальна вартість: {}'.format(graph.total_cost))
    else:
        graph = original_graph

    route, attempts = eularian.eularian_path(graph, start=1)
    if not route:
        print('Здався після <{}> спроб.'.format(attempts))
    else:
        print('Відповідь ({} вершини):'.format(len(route) - 1))
        print('{}'.format(route))
Exemplo n.º 4
0
 def test_make_eularian_simple_true(self):
     """ Test an obvious eularian circuit modification. """
     # A belted diamond, with no dead-ends
     graph = Graph([(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4, 4), (3, 4, 4)])
     new_graph, dead_ends = eularian.make_eularian(graph)
     expected = [
         Edge(*args)
         for args in [(1, 2, 4), (1, 3, 4), (2, 3, 1), (2, 4,
                                                        4), (3, 4,
                                                             4), (2, 3, 1)]
     ]
     self.assertCountEqual(new_graph.all_edges, expected)
     self.assertEqual(dead_ends, 0)