Exemplo n.º 1
0
    def test_edge_orders(self):
        with self.assertRaises(Exception) as cm:
            self.graph.setEdgeOrder(tlp.node(NB_NODES), [])
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(tlp.node(NB_NODES),
                                     tlp.edge(), tlp.edge())
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(self.graph.getRandomNode(),
                                     tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for n in self.graph.getNodes():
            edges = list(self.graph.getInOutEdges(n))
            edges = list(reversed(edges))
            self.graph.setEdgeOrder(n, edges)
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.allEdges(n), edges)

            self.graph.swapEdgeOrder(n, edges[0], edges[-1])
            edges[0], edges[-1] = edges[-1], edges[0]
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.allEdges(n), edges)
Exemplo n.º 2
0
  def test_edge_orders(self):
    with self.assertRaises(Exception) as cm:
      self.graph.setEdgeOrder(tlp.node(NB_NODES), [])
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.swapEdgeOrder(tlp.node(NB_NODES), tlp.edge(), tlp.edge())
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.swapEdgeOrder(self.graph.getRandomNode(), tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1))
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    for n in self.graph.getNodes():
      edges = list(self.graph.getInOutEdges(n))
      edges = list(reversed(edges))
      self.graph.setEdgeOrder(n, edges)
      self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
      self.assertEqual(self.graph.allEdges(n), edges)

      self.graph.swapEdgeOrder(n, edges[0], edges[-1])
      edges[0], edges[-1] = edges[-1], edges[0]
      self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
      self.assertEqual(self.graph.allEdges(n), edges)
Exemplo n.º 3
0
    def test_del_edges_from_list(self):
        invalid_edges = [tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1)]
        with self.assertRaises(Exception) as cm:
            self.graph.delEdges(invalid_edges)
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        self.graph.delEdges(self.edges)
        self.assertEqual(self.graph.numberOfEdges(), 0)
Exemplo n.º 4
0
  def test_del_edges_from_list(self):

    invalid_edges=[tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1)]

    with self.assertRaises(Exception) as cm:
      self.graph.delEdges(invalid_edges)
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    self.graph.delEdges(self.edges)
    self.assertEqual(self.graph.numberOfEdges(), 0)
Exemplo n.º 5
0
    def test_del_elements(self):

        with self.assertRaises(Exception) as cm:
            self.graph.delNode(tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.delEdge(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for i, e in enumerate(self.edges):
            self.graph.delEdge(e)
            self.assertEqual(self.graph.numberOfEdges(), NB_EDGES - (i + 1))
            self.assertFalse(self.graph.isElement(e))

        for i, n in enumerate(self.nodes):
            self.graph.delNode(n)
            self.assertEqual(self.graph.numberOfNodes(), NB_NODES - (i + 1))
            self.assertFalse(self.graph.isElement(n))
Exemplo n.º 6
0
    def has_assignment(self, G, res, forced_arc=tlp.edge()):
        ## get augmented bipartite graph
        [bipart_g, map_oends, s,
         t] = self.bipartite_transformation(G, forced_arc)

        ## add edge in opposite direction and set edge capacities
        capacity = bipart_g.getDoubleProperty("capacity")
        capacity.setAllEdgeValue(0.)
        for e in bipart_g.getEdges():
            eo = bipart_g.addEdge(bipart_g.target(e), bipart_g.source(e))
            capacity[e] = 1.

        ## init flow
        flow = bipart_g.getDoubleProperty("flow")
        flow.setAllEdgeValue(0.)

        ## compute the maximum matching in the bipartite graph using a maximum flow algorithm
        maximumFlow.getMaxFlow(bipart_g, s, t, capacity, flow)

        ## get the results
        bipart_g.delNode(s)
        bipart_g.delNode(t)
        nb_assignment = 0.
        for e in bipart_g.getEdges():
            if flow[e] > 0:
                if e not in map_oends.keys():
                    print "Error: unknown edge in maximum matching."
                    return False
                res[map_oends[e]] = True
                nb_assignment += 1
        return nb_assignment == G.numberOfNodes()
Exemplo n.º 7
0
    def test_is_element(self):
        for n in self.nodes:
            self.assertTrue(self.graph.isElement(n))
            self.assertTrue(n in self.graph)

        for e in self.edges:
            self.assertTrue(self.graph.isElement(e))
            self.assertTrue(e in self.graph)

        for n in self.graph.getNodes():
            self.assertIn(n, self.nodes)

        for n in self.graph.nodes():
            self.assertIn(n, self.nodes)

        for e in self.graph.getEdges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)
            ends = self.graph.ends(e)
            self.assertIn(ends[0], self.nodes)
            self.assertIn(ends[1], self.nodes)

        for e in self.graph.edges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)

        for i in range(NB_NODES - 1):
            self.assertTrue(
                self.graph.hasEdge(self.nodes[i], self.nodes[i + 1]))
            self.assertIn(
                self.graph.existEdge(self.nodes[i], self.nodes[i + 1]),
                self.edges)
            self.assertTrue((self.nodes[i], self.nodes[i + 1]) in self.graph)
            self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[-1]))
            self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[-1]),
                          self.edges)
            self.assertTrue((self.nodes[i], self.nodes[-1]) in self.graph)

        self.assertFalse(self.graph.isElement(tlp.node(NB_NODES)))
        self.assertTrue(tlp.node(NB_NODES) not in self.graph)
        self.assertFalse(self.graph.isElement(tlp.edge(NB_EDGES)))
        self.assertTrue(tlp.edge(NB_EDGES) not in self.graph)
Exemplo n.º 8
0
  def test_add_invalid_node_or_edge_to_root_graph(self):
    with self.assertRaises(Exception) as cm:
      self.graph.addNode(tlp.node(NB_NODES))
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.addEdge(tlp.edge(NB_EDGES))
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))
Exemplo n.º 9
0
  def test_is_element(self):
    for n in self.nodes:
      self.assertTrue(self.graph.isElement(n))
      self.assertTrue(n in self.graph)

    for e in self.edges:
      self.assertTrue(self.graph.isElement(e))
      self.assertTrue(e in self.graph)

    for n in self.graph.getNodes():
      self.assertIn(n, self.nodes)

    for n in self.graph.nodes():
      self.assertIn(n, self.nodes)

    for e in self.graph.getEdges():
      self.assertIn(e, self.edges)
      self.assertIn(self.graph.source(e), self.nodes)
      self.assertIn(self.graph.target(e), self.nodes)
      ends = self.graph.ends(e)
      self.assertIn(ends[0], self.nodes)
      self.assertIn(ends[1], self.nodes)

    for e in self.graph.edges():
      self.assertIn(e, self.edges)
      self.assertIn(self.graph.source(e), self.nodes)
      self.assertIn(self.graph.target(e), self.nodes)

    for i in range(NB_NODES - 1):
      self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[i+1]))
      self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[i+1]), self.edges)
      self.assertTrue((self.nodes[i], self.nodes[i+1]) in self.graph)
      self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[-1]))
      self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[-1]), self.edges)
      self.assertTrue((self.nodes[i], self.nodes[-1]) in self.graph)

    self.assertFalse(self.graph.isElement(tlp.node(NB_NODES)))
    self.assertTrue(tlp.node(NB_NODES) not in self.graph)
    self.assertFalse(self.graph.isElement(tlp.edge(NB_EDGES)))
    self.assertTrue(tlp.edge(NB_EDGES) not in self.graph)
Exemplo n.º 10
0
    def test_add_invalid_node_or_edge_to_root_graph(self):
        with self.assertRaises(Exception) as cm:
            self.graph.addNode(tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.addEdge(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))
Exemplo n.º 11
0
  def test_del_elements(self):

    with self.assertRaises(Exception) as cm:
      self.graph.delNode(tlp.node(NB_NODES))
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.delEdge(tlp.edge(NB_EDGES))
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    for i, e in enumerate(self.edges):
      self.graph.delEdge(e)
      self.assertEqual(self.graph.numberOfEdges(), NB_EDGES - (i+1))
      self.assertFalse(self.graph.isElement(e))

    for i, n in enumerate(self.nodes):
      self.graph.delNode(n)
      self.assertEqual(self.graph.numberOfNodes(), NB_NODES - (i+1))
      self.assertFalse(self.graph.isElement(n))
Exemplo n.º 12
0
    def bipartite_transformation(self, G, forced_arc=tlp.edge()):
        map_in_nodes = {
        }  ## vertices in self.graph -> corresponding duplicated vertices
        map_out_nodes = {
        }  ## vertices in self.graph -> corresponding duplicated vertices
        map_oends = {}
        ## arcs in bipartite graph -> edge in G

        edges = []
        for e in G.getEdges():
            if e != forced_arc:
                edges.append(e)
                # forced_arc is assumed to be part of the assignment
        random.shuffle(edges)
        # shuffling arcs

        bipart_g = tlp.newGraph()
        ## duplicate vertices
        for e in edges:
            n1, n2 = G.ends(e)
            if n1 not in map_out_nodes.keys():
                n_out = bipart_g.addNode()
                map_out_nodes[n1] = n_out
            if n2 not in map_in_nodes.keys():
                n_in = bipart_g.addNode()
                map_in_nodes[n2] = n_in
            a = bipart_g.addEdge(map_out_nodes[n1], map_in_nodes[n2])
            map_oends[a] = e

        ## connected the new nodes to source and sink nodes
        s = bipart_g.addNode()
        for n in map_out_nodes.values():
            bipart_g.addEdge(s, n)
        t = bipart_g.addNode()
        for n in map_in_nodes.values():
            bipart_g.addEdge(n, t)
        return [bipart_g, map_oends, s, t]
Exemplo n.º 13
0
    def test_edge_extremities_modification(self):

        with self.assertRaises(Exception) as cm:
            self.graph.ends(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.reverse(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(tlp.edge(NB_EDGES), tlp.node(), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(self.graph.getRandomEdge(), tlp.node(NB_NODES),
                               tlp.node(NB_NODES + 1))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        for e in self.graph.edges():
            ends = self.graph.ends(e)
            self.graph.reverse(e)
            self.assertEqual(self.graph.source(e), ends[1])
            self.assertEqual(self.graph.target(e), ends[0])

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setSource(e, new_src)
            self.graph.setTarget(e, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setEnds(e, new_src, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))
Exemplo n.º 14
0
  def test_edge_extremities_modification(self):

    with self.assertRaises(Exception) as cm:
      self.graph.ends(tlp.edge(NB_EDGES))
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.reverse(tlp.edge(NB_EDGES))
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setSource(tlp.edge(NB_EDGES), tlp.node())
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setTarget(tlp.edge(NB_EDGES), tlp.node())
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setSource(self.graph.getRandomEdge(), tlp.node(NB_NODES))
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setTarget(self.graph.getRandomEdge(), tlp.node(NB_NODES))
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setEnds(tlp.edge(NB_EDGES), tlp.node(), tlp.node())
      self.assertEquals(cm.exception.args[0],
                        'Edge with id %s does not belong to graph "%s" (id %s)' %
                        (NB_EDGES, self.graph.getName(), self.graph.getId()))

    with self.assertRaises(Exception) as cm:
      self.graph.setEnds(self.graph.getRandomEdge(), tlp.node(NB_NODES), tlp.node(NB_NODES+1))
      self.assertEquals(cm.exception.args[0],
                        'Node with id %s does not belong to graph "%s" (id %s)' %
                        (NB_NODES, self.graph.getName(), self.graph.getId()))

    for e in self.graph.edges():
      ends = self.graph.ends(e)
      self.graph.reverse(e)
      self.assertEqual(self.graph.source(e), ends[1])
      self.assertEqual(self.graph.target(e), ends[0])

      new_src = self.graph.getRandomNode()
      new_tgt = self.graph.getRandomNode()
      self.graph.setSource(e, new_src)
      self.graph.setTarget(e, new_tgt)
      self.assertEqual(self.graph.source(e), new_src)
      self.assertEqual(self.graph.target(e), new_tgt)
      self.assertEqual(self.graph.ends(e), (new_src, new_tgt))

      new_src = self.graph.getRandomNode()
      new_tgt = self.graph.getRandomNode()
      self.graph.setEnds(e, new_src, new_tgt)
      self.assertEqual(self.graph.source(e), new_src)
      self.assertEqual(self.graph.target(e), new_tgt)
      self.assertEqual(self.graph.ends(e), (new_src, new_tgt))