示例#1
0
#!/usr/bin/env python3
from common import IO
from graph import algorithm, Graph

io = IO()
G = Graph.fromfile(io.filein)

# Part 1: graph planatity
io.section('Graph planarity')
if algorithm.is_planar(G):
    io.print('Graph is planar')
else:
    io.print('Graph is not planar!')

# Part 2: graph coloring
io.section('Graph coloring')
k, colors = algorithm.coloring(G)
io.print('Graph can be colored using {} colors, for example like this:'
  .format(k))
io.print_numbered_list(colors)

示例#2
0
G = Graph.fromfile(io.filein)

s = userio.readvertex(G.V(), 'source')
t = userio.readvertex(G.V(), 'destination')

# dijkstra part
io.section('Dijkstra\'s algorithm')
if G.has_negative():
    io.print('Cannot find distance using dijkstra - negative edges are '
             'present')
else:
    # print distances from s to other vertexes
    search_results = algorithm.dijkstra(G, s)
    io.print('Distances from source vertex (#{}) using Dijkstra\'s '
             'algorithm: '.format(s + 1))
    io.print_numbered_list(search_results.distances(), 'Distance')

    # print path from s to t
    path = algorithm.backtrace_path(search_results, t)
    if path is None:
        io.print('There is no path from vertex #{} to #{}'.format(s + 1,
                                                                  t + 1))
    else:
        io.print('Path from #{} to #{}:'.format(s + 1, t + 1))
        io.print_path(path)

# bellman-ford part
# print distances from s to other vertexes
io.section('Bellman-Ford\'s algorithm')
search_results = algorithm.bellman_ford(G, s)
if search_results is None: