Exemplo n.º 1
0
class GraphTest(unittest.TestCase):
    def setUp(self):
        self.g = Graph(flag_dag=True)
        self.g.add_vertex(0)
        self.g.add_vertex(1)
        self.g.add_vertex(2)
        self.g.add_edge(GraphEdge(2, 3, 1))
        self.g.add_vertex(3)
        self.g.add_edge(GraphEdge(3, 1, 1))
        self.g.add_vertex(4)
        self.g.add_edge(GraphEdge(4, 1, 1))
        self.g.add_edge(GraphEdge(4, 0, 1))
        self.g.add_vertex(5)
        self.g.add_edge(GraphEdge(5, 2, 1))
        self.g.add_edge(GraphEdge(5, 0, 1))

    def test_vertices(self):
        self.assertEqual(self.g.vertices(), [0, 1, 2, 3, 4, 5])

    def test_edges(self):
        edges = [
            GraphEdge(2, 3, 1),
            GraphEdge(3, 1, 1),
            GraphEdge(4, 1, 1),
            GraphEdge(4, 0, 1),
            GraphEdge(5, 2, 1),
            GraphEdge(5, 0, 1)
        ]
        self.assertEqual(self.g.edges(), edges)

    def test_topological_sorting(self):
        self.assertEqual(self.g.topological_sort(), deque([5, 4, 2, 3, 1, 0]))

    def tearDown(self):
        pass
class GraphTest(unittest.TestCase):

    def setUp(self):
        self.g = Graph(flag_dag=True)
        self.g.add_vertex(0)
        self.g.add_vertex(1)
        self.g.add_vertex(2)
        self.g.add_edge(GraphEdge(2, 3, 1))
        self.g.add_vertex(3)
        self.g.add_edge(GraphEdge(3, 1, 1))
        self.g.add_vertex(4)
        self.g.add_edge(GraphEdge(4, 1, 1))
        self.g.add_edge(GraphEdge(4, 0, 1))
        self.g.add_vertex(5)
        self.g.add_edge(GraphEdge(5, 2, 1))
        self.g.add_edge(GraphEdge(5, 0, 1))

    def test_vertices(self):
        self.assertEqual(self.g.vertices(), [0, 1, 2, 3, 4, 5])

    def test_edges(self):
        edges = [
            GraphEdge(2, 3, 1),
            GraphEdge(3, 1, 1),
            GraphEdge(4, 1, 1),
            GraphEdge(4, 0, 1),
            GraphEdge(5, 2, 1),
            GraphEdge(5, 0, 1)
        ]
        self.assertEqual(self.g.edges(), edges)

    def test_topological_sorting(self):
        self.assertEqual(self.g.topological_sort(), deque([5, 4, 2, 3, 1, 0]))

    def tearDown(self):
        pass
import sys
import math
import itertools

from codingame_solutions.utilities.graph import Graph


graph = Graph()

n = int(input())  # the number of relationships of influence
for i in range(n):
    # x: a relationship of influence between two people (x influences y)
    x, y = [int(j) for j in input().split()]

    graph.add_edge((x, y))

print(graph, file=sys.stderr)

vertices_pairs = itertools.permutations(graph.vertices(), 2)

paths = []
for vertices_pair in vertices_pairs:
    print("vertices_pair: " + str(vertices_pair), file=sys.stderr)
    new_paths = graph.find_all_paths(vertices_pair[0], vertices_pair[1])
    paths += new_paths

print("Paths: " + str(paths), file=sys.stderr)

longest_path_length = max([len(l) for l in paths])
import sys
import math
import itertools

from codingame_solutions.utilities.graph import Graph


graph = Graph()

n = int(input())  # the number of relationships of influence
for i in range(n):
    # x: a relationship of influence between two people (x influences y)
    x, y = [int(j) for j in input().split()]

    graph.add_edge((x, y))

print(graph, file=sys.stderr)

vertices_pairs = itertools.permutations(graph.vertices(), 2)

paths = []
for vertices_pair in vertices_pairs:
    print("vertices_pair: " + str(vertices_pair), file=sys.stderr)
    new_paths = graph.find_all_paths(vertices_pair[0], vertices_pair[1])
    paths += new_paths

print("Paths: " + str(paths), file=sys.stderr)

longest_path_length = max([len(l) for l in paths])
import sys
import math
from codingame_solutions.utilities.graph import Graph

# MAIN

persons = Graph()

n = int(input())  # the number of adjacency relations
for i in range(n):
    # xi: the ID of a person which is adjacent to yi
    # yi: the ID of a person which is adjacent to xi
    xi, yi = [int(j) for j in input().split()]

    persons.add_edge((xi, yi))
    persons.add_edge((yi, xi))

#print(persons, file=sys.stderr)

# lets start with first person that has one neighbour
persons_with_1_neighbour = persons.get_vertices_with_n_edges(1)
print("persons_with_1_neighbour: " + str(persons_with_1_neighbour), file=sys.stderr)

paths = persons.find_all_paths_from_vertex(persons_with_1_neighbour[0])
print("paths: " + str(paths), file=sys.stderr)

longest_path_length = max([len(l) for l in paths])
longest_paths = []
for path in paths:
    if len(path) == longest_path_length:
Exemplo n.º 6
0
import sys
import math
from codingame_solutions.utilities.graph import Graph

# MAIN

persons = Graph()

n = int(input())  # the number of adjacency relations
for i in range(n):
    # xi: the ID of a person which is adjacent to yi
    # yi: the ID of a person which is adjacent to xi
    xi, yi = [int(j) for j in input().split()]

    persons.add_edge((xi, yi))
    persons.add_edge((yi, xi))

#print(persons, file=sys.stderr)

# lets start with first person that has one neighbour
persons_with_1_neighbour = persons.get_vertices_with_n_edges(1)
print("persons_with_1_neighbour: " + str(persons_with_1_neighbour),
      file=sys.stderr)

paths = persons.find_all_paths_from_vertex(persons_with_1_neighbour[0])
print("paths: " + str(paths), file=sys.stderr)

longest_path_length = max([len(l) for l in paths])
longest_paths = []
for path in paths:
Exemplo n.º 7
0
        v_values.append(int(vertex_value))
        if vertex_neighbour_1 != "E":
            v_destination_1.append(int(vertex_neighbour_1))
        else:
            v_destination_1.append(-1)
        if vertex_neighbour_2 != "E":
            v_destination_2.append(int(vertex_neighbour_2))
        else:
            v_destination_2.append(-1)

    # create graph
    g = Graph(flag_dag=True)

    for name in v_names:
        g.add_vertex(name)
        if v_destination_1[name] != -1:
            g.add_edge(GraphEdge(name, v_destination_1[name], v_values[v_destination_1[name]]))
        if v_destination_2[name] != -1:
            g.add_edge(GraphEdge(name, v_destination_2[name], v_values[v_destination_2[name]]))

    #print(g.edges(), file=sys.stderr)
    #print(g.vertices(), file=sys.stderr)
    #print(g.find_longest_path(0), file=sys.stderr)

    max_dist = max([value for key, value in g.find_longest_path(0).items()]) + v_values[0]

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr)

    print(max_dist)