def test_week1_lecture_print(): """Print.""" # -0 # -1 # -6 # --5 # -7 # -8 # -9 # --2 # --4 # ---3 print "\ntest_week1_lecture quick-union print 00:51" alg = QuickUnionUF(10) print alg alg.ID = [0, 1, 9, 4, 9, 6, 6, 7, 8, 9] print alg.ID
def __init__(self, G): # t ~ O(E log E), s ~ V self._weight = 0.0 # weight of MST. Edge weights can be +, 0, or -, need not be distinct. self._mst = [] # new Queue<Edge>(); # edges in MST # more efficient to build heap by passing array of edges pq = [] # new MinPQ<Edge>() for item in [(e.weight(), e) for e in G.edges()]: heappush(pq, item) # insert(e) # run greedy algorithm uf = QuickUnionUF(G.V()) while not pq and len(self._mst) < (G.V() - 1): edge_cur = heappop(pq) # .delMin() v, w = edge_cur.get_vw() if not uf.connected(v, w): # v-w does not create a cycle uf.union(v, w) # merge v and w components self._mst.append(edge_cur) # enqueue(e); # add edge e to mst self._weight += edge_cur.weight()
def _check(MST, G): """check optimality conditions (takes time proportional to E V lg* V)""" # check total weight total = 0.0 for e in MST.edges(): total += e.weight() if abs(total - MST.weight()) > MST.FLOATING_POINT_EPSILON: sys.stderr.write("Weight of edges does not equal weight(): {} vs. {}\n".format( total, MST.weight())) return False # check that it is acyclic uf = QuickUnionUF(G.V()) for e in MST.edges(): v, w = e.get_vw() if uf.connected(v, w): sys.stderr.write("Not a forest\n") return False uf.union(v, w) # check that it is a spanning forest for e in G.edges(): print v, w if not uf.connected(v, w): sys.stderr.write("Not a spanning forest\n") return False # check that it is a minimal spanning forest (cut optimality conditions) for e in edges(): # all edges in MST except e uf = QuickUnionUF(G.V()) for f in MST._mst: v, w = e.get_vw() if f != e: uf.union(x, y) # check that e is min weight edge in crossing cut for f in G.edges(): x, y = f.get_vw() if not uf.connected(x, y): if f.weight() < e.weight(): sys.stderr.write("Edge {} violates cut optimality conditions\n".format(f)) return False return True
def test_week1_exercise_Q2(): """Test 2.""" alg = QuickUnionUF(10) run_unions(alg, "1-2 7-9 0-4 8-0 4-6 1-9 3-4 7-0 0-5", "\ntest_week1_exercise_Q2") chk_arrays(alg.ID, [4, 2, 9, 6, 6, 5, 5, 9, 4, 6])
def test_week1_lecture(): """From Quick Union (7:50) Lecture Example.""" alg = QuickUnionUF(10) run_unions(alg, "4-3 3-8 6-5 9-4 2-1 8-9 5-0 7-2 6-1 7-3", "\nwk1_lec quick-union", "QU_demo") chk_arrays(alg.ID, [1, 8, 1, 8, 3, 0, 5, 1, 8, 8])
def _check(MST, G): """check optimality conditions (takes time proportional to E V lg* V)""" # check total weight total = 0.0 for e in MST.edges(): total += e.weight() if abs(total - MST.weight()) > MST.FLOATING_POINT_EPSILON: sys.stderr.write( "Weight of edges does not equal weight(): {} vs. {}\n".format( total, MST.weight())) return False # check that it is acyclic uf = QuickUnionUF(G.V()) for e in MST.edges(): v, w = e.get_vw() if uf.connected(v, w): sys.stderr.write("Not a forest\n") return False uf.union(v, w) # check that it is a spanning forest for e in G.edges(): print v, w if not uf.connected(v, w): sys.stderr.write("Not a spanning forest\n") return False # check that it is a minimal spanning forest (cut optimality conditions) for e in edges(): # all edges in MST except e uf = QuickUnionUF(G.V()) for f in MST._mst: v, w = e.get_vw() if f != e: uf.union(x, y) # check that e is min weight edge in crossing cut for f in G.edges(): x, y = f.get_vw() if not uf.connected(x, y): if f.weight() < e.weight(): sys.stderr.write( "Edge {} violates cut optimality conditions\n".format( f)) return False return True