Пример #1
0
class LazyPrimMST:
    def __init__(self, g):
        self.pq = MinPQ()
        self.marked = []
        self.mst = Queue()

        for i in range(g.v):
            self.marked.append(False)
        self.visit(g, 0)
        while not self.pq.is_empty():
            e = self.pq.del_min()
            v = e.either()
            w = e.other(v)
            if self.marked[v] and self.marked[w]:
                continue
            self.mst.enqueue(e)
            if not self.marked[v]:
                self.visit(g, v)
            if not self.marked[w]:
                self.visit(g, w)

    def visit(self, g, v):
        self.marked[v] = True
        for e in g.adj[v]:
            if not self.marked[e.other(v)]:
                self.pq.insert(e)
Пример #2
0
    def __init__(self, g):
        self.marked = []
        self.pre = Queue()
        self.post = Queue()
        self.reverse_post = Stack()
        for i in range(g.get_v()):
            self.marked.append(False)

        for v in range(g.get_v()):
            if not self.marked[v]:
                self.dfs(g, v)
Пример #3
0
    def __init__(self, g):
        self.mst = Queue()
        self.pq = MinPQ()
        self.uf = UF(g.v)

        for e in g.edges:
            self.pq.insert(e)

        while not self.pq.is_empty() and self.mst.size() < g.v - 1:
            e = self.pq.del_min()
            v = e.either()
            w = e.other(v)
            if self.uf.connected(v, w):
                continue
            self.mst.enqueue(e)
            self.uf.union(v, w)
Пример #4
0
 def __init__(self, g, s):
     self.dist_to = []
     self.edge_to = []
     self.on_q = []
     self.queue = Queue()
     self.cost = 0
     for i in range(g.v):
         self.dist_to.append(float('inf'))
         self.edge_to.append(None)
         self.on_q.append(None)
     self.queue.enqueue(s)
     self.on_q[s] = True
     while not self.queue.is_empty() and not self.has_nagative_cycle():
         v = self.queue.dequeue()
         self.on_q[v] = False
         self.relax(g, v)
Пример #5
0
 def bfs(self, g, s):
     q = Queue()
     q.enqueue(s)
     self.marked[s] = True
     while not q.is_empty():
         v = q.dequeue()
         for w in g.get_adj(v):
             if not self.marked[w]:
                 self.edge_to[w] = v
                 self.marked[w] = True
                 q.enqueue(w)
Пример #6
0
    def __init__(self, g):
        self.pq = MinPQ()
        self.marked = []
        self.mst = Queue()

        for i in range(g.v):
            self.marked.append(False)
        self.visit(g, 0)
        while not self.pq.is_empty():
            e = self.pq.del_min()
            v = e.either()
            w = e.other(v)
            if self.marked[v] and self.marked[w]:
                continue
            self.mst.enqueue(e)
            if not self.marked[v]:
                self.visit(g, v)
            if not self.marked[w]:
                self.visit(g, w)
Пример #7
0
class DepthFirstOrder:
    def __init__(self, g):
        self.marked = []
        self.pre = Queue()
        self.post = Queue()
        self.reverse_post = Stack()
        for i in range(g.get_v()):
            self.marked.append(False)

        for v in range(g.get_v()):
            if not self.marked[v]:
                self.dfs(g, v)

    def dfs(self, g, v):
        self.marked[v] = True
        self.pre.enqueue(v)
        for w in g.get_adj(v):
            if not self.marked[w]:
                self.dfs(g, w)
        self.post.enqueue(v)
        self.reverse_post.push(v)
Пример #8
0
class BellmanFordSP:
    def __init__(self, g, s):
        self.dist_to = []
        self.edge_to = []
        self.on_q = []
        self.queue = Queue()
        self.cost = 0
        for i in range(g.v):
            self.dist_to.append(float('inf'))
            self.edge_to.append(None)
            self.on_q.append(None)
        self.queue.enqueue(s)
        self.on_q[s] = True
        while not self.queue.is_empty() and not self.has_nagative_cycle():
            v = self.queue.dequeue()
            self.on_q[v] = False
            self.relax(g, v)

    def relax(self, g, v):
        for e in g.adj[v]:
            w = e.v_to
            if self.dist_to[w] > self.dist_to[v] + e.weight:
                self.dist_to[w] = self.dist_to[v] + e.weight
                self.edge_to[w] = e
                if not self.on_q[w]:
                    self.on_q[w] = True
                    self.queue.enqueue(w)
Пример #9
0
class DigraphOrder:
    def __init__(self, g):
        self.pre = Queue()
        self.post = Queue()
        self.reverse_post = Stack()
        self.marked = []

        for i in range(g.v):
            self.marked.append(False)

        for v in range(g.v):
            if not self.marked[v]:
                self.dfs(g, v)

    def dfs(self, g, v):
        self.marked[v] = True
        self.pre.enqueue(v)
        for e in g.adj[v]:
            w = e.v_to()
            if not self.marked[w]:
                self.dfs(g, w)
        self.post.enqueue(v)
        self.reverse_post.push(v)
Пример #10
0
 def keys_with_prefix(self, pre):
     x = self.recurse_get(self.root, pre, 0)
     q = Queue()
     self.collection(x, pre, q)
     return q
Пример #11
0
 def keys(self, lo, hi):
     queue = Queue()
     self.recurse_keys(queue, self.root, lo, hi)
     return queue