def test_solve_square(self):
        """Solve problem with square

        start ---1--> A
          |           |
          1           1
          |           |
          V           V
          B ---1--> finish

        Right answer is (1*1 + 1*1)
        """

        start = Vertex('start')
        finish = Vertex('finish')
        a = Vertex('a')
        b = Vertex('b')

        for S in semirings:
            start_a = Edge(start, a, S.unity())
            start_b = Edge(start, b, S.unity())

            a_finish = Edge(a, finish, S.unity())
            b_finish = Edge(b, finish, S.unity())

            g = DynamicProgramming([start, a, b, finish], [start_a, start_b,
                                                        a_finish, b_finish])
            g.set_start(start)
            g.set_finish(finish)

            self.assertEqual(g.solve(S), S.unity()+S.unity())
    def test_solve_simple(self):
        """Solve simple problem

        start --1--> finish

        Right answer is 1
        """
        start = Vertex('start')
        finish = Vertex('finish')
        for S in semirings:
            e = Edge(start, finish, S.unity())
            g = DynamicProgramming([start, finish], [e])
            g.set_start(start)
            g.set_finish(finish)
            self.assertEqual(g.solve(S), S.unity())
    def test_solve_crossed_square(self):
        """Find length of the shortest path (min,+)

        start -5-> A ---1----> B --3--> C
          |        |                    |
          1        0                    9
          |        |                    |
          V        V                    V
          x --5--> y --2--> z --1--> finish

        Right answer is 8
        """

        start = Vertex('start')
        finish = Vertex('finish')

        a = Vertex('a')
        b = Vertex('b')
        c = Vertex('c')

        x = Vertex('x')
        y = Vertex('y')
        z = Vertex('z')

        start_a = Edge(start, a, SemiringMinPlusElement(5))
        a_b = Edge(a, b, SemiringMinPlusElement(1))
        b_c = Edge(b, c, SemiringMinPlusElement(3))
        c_finish = Edge(c, finish, SemiringMinPlusElement(9))

        start_x = Edge(start, x, SemiringMinPlusElement(1))
        x_y = Edge(x, y, SemiringMinPlusElement(5))
        y_z = Edge(y, z, SemiringMinPlusElement(2))
        z_finish = Edge(z, finish, SemiringMinPlusElement(1))

        a_y = Edge(a, y, SemiringMinPlusElement(0))

        g = DynamicProgramming([start, finish, a, b, c, x, y, z],
                               [start_a, a_b, b_c, c_finish,
                                start_x, x_y, y_z, z_finish,
                                a_y])
        g.set_start(start)
        g.set_finish(finish)

        self.assertEqual(g.solve(SemiringMinPlusElement),
                         SemiringMinPlusElement(8))
 def test_solve_incorrect_finish(self):
     vertex = Vertex()
     g = DynamicProgramming([], [])
     with self.assertRaises(ValueError):
         g.set_finish(vertex)