Exemplo n.º 1
0
class simulation:
    def __init__(self, duration):
        self._eventq = PrioQueue()
        self._time = 0
        self._duration = duration

    def run(self):
        while not self._eventq.is_empty():
            event = self.eventq.dequeue()
            self._time = event.time()
            if self._time > self._duration:
                break
            event.run()
            
    def add_event(self, event):
        self._eventq.enqueue(event)
        
    def cur_time(self)
Exemplo n.º 2
0
def dijkstra_shortest_paths_0(graph, v0):
    vnum = graph.vertex_num()
    assert 0 <= v0 < vnum
    count, paths = 0, [None]*vnum
    cands = PrioQueue([(0, v0, v0)])
    while count < vnum and not cands.is_empty():
        plen, u, vmin = cands.dequeue()
        if paths[vmin]:
            continue
        paths[vmin] = (u, plen)
        for v, w in graph.out_edges(vmin):
            if not paths[v]:
                cands.enqueue((plen + w, vmin, v))
        count += 1
    return paths
def Prim(graph):
    vnum = graph.vertex_num()
    mst = [None] * vnum
    cands = PrioQueue([(0, 0, 0)]) # 记录侯选边(w, vi, vj)
    count = 0
    while count < vnum and not cands.is_empty():
        w, u, v = cands.dequeue()  # 取当前最短边
        if mst[v]:
            continue
        mst[v] = ((u, v), w) # 记录新的边
        count += 1
        for vi, w in graph.out_edges(v):
            if not mst[vi]: # 如果vi不在mst中则作为侯选边
                cands.enqueue((w, v, vi))
    return mst
Exemplo n.º 4
0
def Prim(graph):
    """ Assume that graph is a network, a connected undirect
graph. This function implements Prim algorithm to build its
minimal span tree. A list mst to store the resulting
span tree, where each element takes the form ((i, j), w).
A representing array reps is used to record the representive
vertics of each of the connective parts.
"""
    vnum = graph.vertex_num()
    mst = [None]*vnum
    cands = PrioQueue([(0, 0, 0)])    # record cand-edges (w, vi, wj)
    count = 0
    while count < vnum and not cands.is_empty():
        w, u, v = cands.dequeue()  # take minimal candidate edge
        if mst[v]:   # vmin is already in mst
            continue
        mst[v] = ((u, v), w)    # record new MST edge and vertex
        count += 1
        for vi, w in graph.out_edges(v):  # for adjacents of vmin
            if not mst[vi]:  # when v is not in mst yet
                cands.enqueue((w, v, vi))
    return mst
Exemplo n.º 5
0
 def __init__(self, duration):
     self._eventq = PrioQueue()
     self._time = 0
     self._duration = duration