Ejemplo n.º 1
0
#!/usr/bin/env python3
from common import IO
from graph.directed import Graph, GraphMatrix
from graph import algorithm

io = IO()

G = Graph.fromfile(io.filein)
G_matrix = GraphMatrix.from_graph(G)

# Eulerian cycle, part 1
io.section('Eulerian cycle')
eulerian_cycle = algorithm.eulerian_cycle(G_matrix)
if eulerian_cycle is not None:
    io.print('Eulerian cycle found: ')
    io.print_path(eulerian_cycle)
else:
    io.print('Eulerian cycle not found!')

    # Eulerian path, part 2
    io.section('Eulerian path')
    eulerian_path = algorithm.eulerian_path(G_matrix)
    if eulerian_path is not None:
        io.print('Eulerian path found: ')
        io.print_path(eulerian_path)
    else:
        io.print('Eulerian path not found!')


# Hamiltonian cycle, part 1
io.section('Hamiltonian cycle')
Ejemplo n.º 2
0
#!/usr/bin/env python3
from math import sqrt
from common import IO
from graph.weighted import GraphMatrix, Edge
from graph import algorithm

io = IO()

N = int(io.readline())
coords = []
G = GraphMatrix(N, 0, [])
for i in range(N):
    x, y = map(float, io.readline().split())
    coords.append((x, y))
    for j, (x2, y2) in enumerate(coords):
        dist = sqrt((x2 - x) ** 2 + (y2 - y) ** 2)
        G.add_edge(Edge(i, j, dist))


io.section('TSP path')
weight, path = algorithm.TSP(G)
if path is not None:
    io.print('TSP path with weight {} found:'.format(weight))
    io.print_path(path)
else:
    io.print('TSP path not found')
Ejemplo n.º 3
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)

Ejemplo n.º 4
0
#!/usr/bin/env python3
from common import IO, UserIO
from graph import algorithm
from graph.weighted.directed import Graph

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

# later defined source and destination vertexes
s = 0
t = 0

# Floyd-Warshall's part
io.section('Floyd-Warshall\'s algorithm')
search_results = algorithm.floyd_warshall(G)
if search_results is None:
    io.print('Cannot find distance using Floyd-Warshall - negative cycles are '
             'present')
else:
    # print distance matrix
    io.print('Distance matrix: ')
    io.print_matrix(algorithm.distance_matrix(search_results), 's\\t')

    # ask user for source and destination vertexes
    s = userio.readvertex(G.V(), 'source')
    t = userio.readvertex(G.V(), 'destination')

    # print path from s to t
    path = algorithm.forwardtrace_path_from_all_to_all(search_results, s,
                                                       t)
Ejemplo n.º 5
0
#!/usr/bin/env python3
from common import IO
from graph import algorithm
from graph.weighted.directed import Graph

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

io.section('Max flow algorithm (Floyd-Fulkerson method, '
           'Edmonds-Karp algorithm)')
try:
    s = algorithm.get_source(G)
    t = algorithm.get_sink(G)
    flow_val, flow_graph = algorithm.edmonds_karp(G, s, t)
    io.print('Max flow value is {}\n'.format(flow_val))
    io.print('Flow on each edge: ')
    for edge in flow_graph.edges():
        io.print('\tfor edge ({}, {}) flow is {}'.format(edge.source,
                                                      edge.dest,
                                                      edge.weight))
except algorithm.ambigous_source:
    io.print('Source is ambigous (more than 2 vertexes with out-degree = 0')
except algorithm.no_source:
    io.print('Cannot find source - there are no vertexes with out-degree = 0')
except algorithm.ambigous_sink:
    io.print('Source is ambigous (more than 2 vertexes with in-degree = 0')
except algorithm.no_sink:
    io.print('Cannot find sink - there are no vertexes with in-degree = 0')
Ejemplo n.º 6
0
#!/usr/bin/env python3
from common import IO, UserIO
from graph import algorithm
from graph.weighted.directed import Graph


io = IO()
userio = UserIO()
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: