def test_path(self):
     g = EdgeWeightedGraph(4)
     e1 = Edge(0, 1, 2)
     e2 = Edge(0, 2, 5)
     e3 = Edge(1, 3, 10)
     e4 = Edge(2, 3, 3)
     g.add_edge(e1)
     g.add_edge(e2)
     g.add_edge(e3)
     g.add_edge(e4)
     self.assertListEqual(Dijkstra(g, 0).get_path(3), [0, 2, 3])
 def test_edge_in_mst(self):
     g = EdgeWeightedGraph(8)
     e01 = Edge(0, 1, 2)
     g.add_edge(e01)
     e02 = Edge(0, 2, 10)
     g.add_edge(e02)
     e16 = Edge(1, 6, 19)
     g.add_edge(e16)
     e13 = Edge(1, 3, 25)
     g.add_edge(e13)
     e24 = Edge(2, 4, 10)
     g.add_edge(e24)
     e27 = Edge(2, 7, 5)
     g.add_edge(e27)
     e23 = Edge(2, 3, 16)
     g.add_edge(e23)
     e36 = Edge(3, 6, 6)
     g.add_edge(e36)
     e35 = Edge(3, 5, 4)
     g.add_edge(e35)
     e47 = Edge(4, 7, 9)
     g.add_edge(e47)
     e45 = Edge(4, 5, 11)
     g.add_edge(e45)
     self.assertTrue(IsAnEdgeInAMST(g).is_edge_in_mst(e01))
     self.assertFalse(IsAnEdgeInAMST(g).is_edge_in_mst(e13))
     self.assertFalse(IsAnEdgeInAMST(g).is_edge_in_mst(e23))
     self.assertTrue(IsAnEdgeInAMST(g).is_edge_in_mst(e27))
Example #3
0
 def test_mst(self):
     g = EdgeWeightedGraph(8)
     e01 = Edge(0, 1, 2)
     g.add_edge(e01)
     e02 = Edge(0, 2, 10)
     g.add_edge(e02)
     e16 = Edge(1, 6, 19)
     g.add_edge(e16)
     e13 = Edge(1, 3, 25)
     g.add_edge(e13)
     e24 = Edge(2, 4, 10)
     g.add_edge(e24)
     e27 = Edge(2, 7, 5)
     g.add_edge(e27)
     e23 = Edge(2, 3, 16)
     g.add_edge(e23)
     e36 = Edge(3, 6, 6)
     g.add_edge(e36)
     e35 = Edge(3, 5, 4)
     g.add_edge(e35)
     e47 = Edge(4, 7, 9)
     g.add_edge(e47)
     e45 = Edge(4, 5, 11)
     g.add_edge(e45)
     self.assertListEqual(
         KruskalsMST(g).get_mst(), [e01, e35, e27, e36, e47, e02, e45])
 def test_mst(self):
     g = EdgeWeightedGraph(8)
     e01 = Edge(0, 1, 2) 
     g.add_edge(e01)
     e02 = Edge(0, 2, 10)
     g.add_edge(e02)
     e16 = Edge(1, 6, 19)
     g.add_edge(e16)
     e13 = Edge(1, 3, 25)
     g.add_edge(e13)
     e24 = Edge(2, 4, 10)
     g.add_edge(e24)
     e27 = Edge(2, 7, 5)
     g.add_edge(e27)
     e23 = Edge(2, 3, 16)
     g.add_edge(e23)
     e36 = Edge(3, 6, 6)
     g.add_edge(e36)
     e35 = Edge(3, 5, 4)
     g.add_edge(e35)
     e47 = Edge(4, 7, 9)
     g.add_edge(e47)
     e45 = Edge(4, 5, 11)
     g.add_edge(e45)
     print(MinWTFeedbackEdgeSet(g).get_mst())
Example #5
0
            if not self._marked[w]:
                self._scan(G, w)

    # add all edges e incident to v onto pq if the other endpoint has not yet been scanned
    def _scan(self, G, v):
        assert not self._marked[v]
        self._marked[v] = True
        for e in G.adj(v):
            if not self._marked[e.other(v)]:
                self._pq.insert(e)

    def weight(self):
        return self._weight

    def edges(self):
        return self._mst


if __name__ == "__main__":
    V = 3
    G = EdgeWeightedGraph(V)
    edge_1 = Edge(0, 1, 4.4)
    edge_2 = Edge(0, 2, 3.3)
    big_edge = Edge(1, 2, 10000)
    G.add_edge(edge_1)
    G.add_edge(edge_2)
    G.add_edge(big_edge)
    lazy_prim = LazyPrimMST(G)
    print(lazy_prim.edges())  # [(0, 2, 3.3), (0, 1, 4.4)]
    print(lazy_prim.weight())  # 7.7