コード例 #1
0
 def test(cls, fileName):
     from edge_weighted_graph import EdgeWeightedGraph
     with open(fileName, 'r') as f:
         g = EdgeWeightedGraph(f)
         mst = cls(g)
         for e in mst.edges():
             print(e)
         print(mst.weight())
コード例 #2
0
 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))
コード例 #4
0
        while k > 1 and self.__more(k / 2, k):
            self.__exch(k / 2, k)
            k = k / 2

    def __sink(self, k):
        N = self.size()
        while 2 * k <= N:
            j = 2 * k
            if j < N and self.__more(j, j + 1):
                j += 1
            if not self.__more(k, j):
                break
            self.__exch(k, j)
            k = j

    def __more(self, i, j):
        return self.__pq[i].get_weight() > self.__pq[j].get_weight()

    def __exch(self, i, j):
        self.__pq[i], self.__pq[j] = self.__pq[j], self.__pq[i]


if __name__ == '__main__':
    from edge_weighted_graph import EdgeWeightedGraph
    ewg = EdgeWeightedGraph(8)
    ewg.read_file("data/tinyEWG.txt")
    mst = LazyPrimMST(ewg)
    for each in mst.edges():
        print each.v, each.w, each.weight
    print mst.total_weight()
 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())
コード例 #6
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])
コード例 #7
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