Exemplo n.º 1
0
 def __init__(self, g):
     self.G = g
     self.marked = [False] * self.G.V
     self.post = [0] * self.G.V
     self.pre = [0] * self.G.V  # 记录顶点在preorder这个排序中排名第几
     self.postorder = Queue1.Queue()  # postorder是顶点遍历完成的顺序
     self.preorder = Queue1.Queue()  # preorder是顶点调用的顺序
     self.preCounter = 0  # preCounter是用来统计pre数组中的个数的
     self.postCounter = 0
     for v in range(self.G.V):  # 就是按照顶点标号进行遍历
         if not self.marked[v]:
             self.dfs(v)
Exemplo n.º 2
0
 def edges(self):
     mst = Queue1()
     for v in range(len(self.edgeTo)):
         e = self.edgeTo[v]
         if e != None:
             mst.enqueue(e)
     return mst
Exemplo n.º 3
0
 def bfs(self, s):  # 现将原点放到队列中然后进行对她相邻带你的寻找
     #再一一对相邻点进行排查标记路径也进行时最短的路径
     q = Queue1.Queue()
     self.marked[s] = True
     self.distTo[s] = 0
     q.enqueue(s)
     while not q.isEmpty():
         v = q.dequeue()
         for w in self.G.adj(v):
             if not self.marked[w]:
                 self.edgeTo[w] = v
                 self.distTo[w] = self.distTo[v] + 1
                 self.marked[w] = True
                 q.enqueue(w)
Exemplo n.º 4
0
 def bfs(self):  #用广度优先的算法对各个顶点进行遍历
     q = Queue1.Queue()
     for v in range(self.G.V()):
         self.distTo[v] = sys.maxint
     self.distTo[self.source] = 0
     self.marked[self.source] = True
     q.enqueue(self.source)
     while not q.isEmpty():
         l = q.dequeue()
         for w in self.G.adj(l):
             if not self.marked[w]:
                 self.edgeTo[w] = l
                 self.distTo[w] = self.distTo[l] + 1
                 self.marked[w] = True
                 q.enqueue(w)
Exemplo n.º 5
0
 def __init__(self, G, s):
     self.distTo = [0.0] * G.V()
     self.edgeTo = [0] * G.V()
     self.onQueue = [False] * G.V()
     for v in range(G.V()):
         self.distTo[v] = float("inf")
     self.distTo[0] = 0.0
     self.queue = Queue1()
     self.queue.enqueue(s)
     self.onQueue[s] = True
     self.cost = 0
     self.cycle = Stack1()
     while (not self.queue.isEmpty() and not self.hasNegativeCycle()):
         v = self.queue.dequeue()
         self.onQueue[v] = False
         self.relax(G, v)
Exemplo n.º 6
0
 def __init__(self, G):
     self.weight = 0.0  # 最小生成树的总权重
     self.mst = Queue1()
     self.pq = MinPQ()  #将所有边按权值从小到达的顺序排列
     #为什么不用序列,因为权值的大小是边这一对象上的附带品
     for e in G.edges():
         self.pq.insert(e)
     self.uf = Wq()  #用一个连通域判断是不是会形成环
     #运行贪心算需要遍历最小优先队列中的所有边
     while (not self.pq.isEmpty() and self.mst.size() < G.V() - 1):
         e = self.pq.delMin()
         v = e.either()
         w = e.other(v)  #找到这天边的两个端点
         if not self.uf.connected(v, w):  #判断v,w是不是在一个联通域内
             self.uf.union(v, w)
             self.mst.enqueue(e)
             self.weight += e.weight
Exemplo n.º 7
0
 def hasAugmentingPath(self,G,s,t):
     edgeTo = [0]*G.V()
     marked = [False]*G.V()
     queue = Queue1()
     queue.enqueue(s)
     marked[s] = True
     while not queue.isEmpty() and not marked[t]:
         v = queue.dequeue() #  检查队列中的每一个点可达的边
         #  是不是仍然存在剩余容量大于0的边也就是说仍有增益边
         for e in G.adj(v):
             w = e.other(v)
             if e.residualCapacityTo(w)>0:
                 if not marked[w]:
                     edgeTo[w] = e
                     marked[w] = True
                     queue.enqueue(w)
     return marked[t]
Exemplo n.º 8
0
 g.addEdge(3, 4)
 g.addEdge(3, 5)
 g.addEdge(5, 6)
 g.addEdge(7, 8)
 g.addEdge(7, 9)
 g.addEdge(8, 12)
 g.addEdge(9, 12)
 g.addEdge(9, 10)
 g.addEdge(12, 11)
 g.addEdge(10, 11)
 g.addEdge(13, 14)
 g.addEdge(13, 17)
 g.addEdge(13, 18)
 g.addEdge(14, 15)
 g.addEdge(14, 16)
 g.addEdge(15, 19)
 g.addEdge(16, 19)
 #    g.addEdge(16,20)
 g.addEdge(15, 18)
 #    g.addEdge(18,20)
 cc = CC(g)
 m = cc.count()
 print m + "components"
 components = Queue1.Queue()
 for i in range(m):
     components[i] = Queue1.Queue()
 for v in range(g.V()):
     components[cc.id(v)].enqueue(v)
 for i in range(m):
     for v in components[i]:
         print v + ' '