def test_results_one_connected_component(self):
        points = np.array([(0., 3.), (3., 4.), (9., 10.), (7., 8.),
            (5., 6.), (2., 1.)])
        graph = create_graph(points, 6, 2, "foo")
        edges = [(1, 0),
                  (5, 1),
                  (1, 4),
                  (3, 4),
                  (2, 3)]
        for (i, j) in edges:
            graph.solution.update(i, j, True)
        graph.compute_connected_components()
        ccs = graph.connected_components
        self.assertEqual(len(ccs), 1)
        cc = ccs.get_connected_component(3)
        points_indices = range(0, 6)
        self.assertItemsEqual(cc, points_indices)

        graph.compute_spanning_tree_on_ccs()
        cc_edges = graph.solution
        for (i, j) in edges:
            if i < j:
                edge = (i, j)
            else:
                edge = (j, i)
            self.assertTrue(edge in cc_edges,
                    "%s not in %s" % (edge, cc_edges))
 def test_preprocessing_lines_omits_duplicates(self):
     points = np.array([(2., 2.), (6., 4.), (3., 6.), (5., 7.), (4.25, 5.)])
     graph = create_graph(points, 5, 2, "foo")
     l1 = HighDimLine(np.array([(2., 6.), (3., 2.)]))  # y = -4x + 14
     l2 = HighDimLine(np.array([(2., 3.), (6., 5.)]))  # y = 0.5x + 2
     l3 = HighDimLine(np.array([(3., 5.5), (5., 6.5)]))  # y = 0.5x + 4
     # duplicates part
     l4 = HighDimLine(np.array(((2.5, 6.), (3.5, 2.))))  # y = -4x + 16
     l5 = HighDimLine(np.array(((2., 2.5), (6., 4.5))))  # y = 0.5x + 1.5
     l6 = HighDimLine(np.array(((3., 5.), (5., 6.))))  # y = 0.5x + 3.5
     # lines outside of point set
     # above
     l7 = HighDimLine(np.array([(0., 7.), (1., 10.)]))  # y = 3 x + 7
     # below
     l8 = HighDimLine(np.array([(0., -1.), (6., 1.)]))  # y = 1/3 x - 1
     lines = [l1, l2, l3, l4, l5, l6, l7, l8]
     graph.lines = lines
     graph.preprocess_lines()
     result = graph.lines
     self.assertEqual(len(result), 3)
     self.assertFalse(l7 in result)
     self.assertFalse(l8 in result)
     self.assertTrue(l1 in result or l4 in result)
     self.assertTrue(l2 in result or l5 in result)
     self.assertTrue(l3 in result or l6 in result)
    def setUp(self):
        points = np.array([(2., 2.), (6., 4.), (3., 6.), (5., 7.), (4.25, 5.)])
        self.graph = create_graph(points, 5, 2, "foo")

        l1 = HighDimLine(np.array([(2., 6.), (3., 2.)]))  # y = -4x + 14
        l2 = HighDimLine(np.array([(2., 3.), (6., 5.)]))  # y = 0.5x + 2
        l3 = HighDimLine(np.array([(3., 5.5), (5., 6.5)]))  # y = 0.5x + 4
        self.graph.lines = [l1, l2, l3]
 def setUp(self):
     points = np.array([(3., 3.), (4.5, 2.), (8.5, 3.5), (6.5, 6.5)])
     graph = create_graph(points, 4, 2, "example")
     graph.solution.update(0, 3, True)
     # self crossing edges between (0,2) and (1,3)
     graph.solution.update(0, 2, True)
     graph.solution.update(1, 3, True)
     self.graph = graph
    def test_compute_spanning_tree_on_ccs(self):
        points = np.array([(0., 3.), (3., 4.), (9., 10.), (7., 8.)])
        graph = create_graph(points, 4, 2, "foo")
        edges = [(1, 0),
                  (0, 2),
                  (2, 3),
                  (1, 2)]
        for (i, j) in edges:
            graph.solution.update(i, j, True)
        graph.compute_connected_components()
        ccs = graph.connected_components
        self.assertEqual(len(ccs), 1)
        cc = ccs.get_connected_component(2)
        points_indices = range(0, 4)
        self.assertItemsEqual(cc, points_indices)

        graph.compute_spanning_tree_on_ccs()
        sol_edges = [(0, 2), (0, 1), (2, 3)]
        self.assertItemsEqual(sol_edges, graph.solution)
 def setUp(self):
     # setting up example graph
     points = np.array([(8.5, 2), (10.5, 3.5), (4., 4.), (5., 5.), (8.5,
         5.5), (9., 7.), (10.5, 6.5)])
     graph = create_graph(points, 7, 2, "example")
     # first connected component
     graph.solution.update(0, 1, True)
     graph.edges.update(0, 1, False)
     # second connected component
     graph.solution.update(2, 3, True)
     graph.edges.update(2, 3, False)
     # third connected component
     graph.solution.update(4, 5, True)
     graph.solution.update(5, 6, True)
     graph.edges.update(4, 5, False)
     graph.edges.update(5, 6, False)
     graph.edges.update(4, 6, False)
     # update information in graph about connected components
     graph.compute_connected_components()
     self.graph = graph
def main():
    # minimal example to find optimal spanning tree
    import numpy as np
    points = np.array([(2., 2.), (6., 4.), (3., 6.), (5., 7.), (4.25, 5.)])
    import spanningtree.highdimgraph.factories as factories
    graph = factories.create_graph(points, 5, 2, 'custom')
    from spanningtree.highdimgraph.lines import HighDimLine
    # y = -4x + 14
    l1 = HighDimLine(np.array([(2., 6.), (3., 2.)]))
    # y = 0.5x + 2
    l2 = HighDimLine(np.array([(2., 3.), (6., 5.)]))
    # y = 0.5x + 4
    l3 = HighDimLine(np.array([(3., 5.5), (5., 6.5)]))
    lines = [l1, l2, l3]
    graph.lines = lines
    graph.preprocess_lines()
    compute_spanning_tree(graph)
    print "crossing number = %s" % graph.crossing_number()
    import spanningtree.plotting as plotting
    plotting.plot(graph)
 def test_results_two_connected_components(self):
     points = np.array([(0., 3.), (3., 4.), (9., 10.), (7., 8.),
         (5., 6.), (2., 1.)])
     graph = create_graph(points, 6, 2, "foo")
     edges = [(1, 0),
               (5, 1),
               # (1, 4), now two connected components
               (3, 4),
               (2, 3)]
     for (i, j) in edges:
         graph.solution.update(i, j, True)
     graph.compute_connected_components()
     ccs = graph.connected_components
     self.assertEqual(len(ccs), 2)
     c1 = set([0, 1, 5])
     self.assertItemsEqual(c1, ccs.get_connected_component(0))
     c1_edges = [(0, 1), (1, 5)]
     c2 = set([2, 3, 4])
     self.assertItemsEqual(c2, ccs.get_connected_component(3))
     self.assertNotEqual(ccs.get_connected_component(0),
             ccs.get_connected_component(3))
     c2_edges = [(3, 4), (2, 3)]
     expected_sol = c1_edges + c2_edges
     self.assertItemsEqual(expected_sol, graph.solution)
 def setUp(self):
     point_set = np.array([[0], [1], [2], [3]])
     self.graph = create_graph(point_set, 4, 1, "foo")