Esempio n. 1
0
def Dijkstraheap(g, s, t):
    n = g.v
    status = {}  #unseen, intree, fringe
    wt = {}
    dad = {}
    heap = Heap.MaxHeap()
    for v in range(n):
        status[v] = "unseen"
    status[s] = "intree"
    for edge in g.adjlist[s]:
        status[edge[0]] = "fringe"
        wt[edge[0]] = edge[1]
        dad[edge[0]] = s
        heap.Insert(edge[0], edge[1])
    while status[t] != "intree":
        #pick the max wt[v]
        maxv = heap.Max()[0]
        status[maxv] = "intree"
        #print(maxv)
        heap.Delete(maxv)
        edges = g.adjlist[maxv]
        for edge in edges:
            w = edge[0]
            if status[w] == "unseen":
                status[w] = "fringe"
                dad[w] = maxv
                wt[w] = min(wt[maxv], edge[1])
                heap.Insert(w, wt[w])
            elif status[w] == "fringe" and wt[w] < min(wt[maxv], edge[1]):
                dad[w] = maxv
                wt[w] = min(wt[maxv], edge[1])
                heap.Delete(w)
                heap.Insert(w, wt[w])
    print("using Dijkstra with heap")
    print("the max bandwidth path from vertex%d to vertex%d is : %s" %
          (s, t, wt[t]))
    return s, t, wt[t]
Esempio n. 2
0
 def __init__(self):
     # self._heap = Heap.MinHeap(key=lambda x: -x.priority)
     self._heap = Heap.MaxHeap(key=lambda x: x.priority)
Esempio n. 3
0
def Kruskal(g, s, t):
    rank = {}
    dad = {}

    def Union(u, v):
        if rank[u] > rank[v]:
            dad[v] = u
        elif rank[u] < rank[v]:
            dad[u] = v
        else:
            dad[v] = u
            rank[u] += 1

    def Find(v):
        w = v
        while dad[w] != -1:
            w = dad[w]
        return w

    def MakeSet(v):
        dad[v] = -1
        rank[v] = 0

    #Build heap
    heap = Heap.MaxHeap()
    edges = g.alledges.items()
    #print(edges)
    #start_time = time.time()
    for edge, w in edges:
        heap.H.append(edge)
        heap.D.append(w)
        heap.n += 1
    #end_time = time.time()
    #print("finish init%f"%(end_time - start_time))
    #sort
    sorted_edge = []
    #start_time = time.time()
    heap.HeapSort()
    #end_time = time.time()
    #print("finish heapsort%f" % (end_time - start_time))
    for i in range(1, heap.n + 1):
        sorted_edge.append((heap.H[i], heap.D[i]))
    #print(sorted_edge)
    for i in range(g.v):
        MakeSet(i)
    #print("finish heap sort")
    MST = Graph.Graph(g.v)
    while (len(sorted_edge)):  # and t not in path(MST,s,t)[0]:
        edge = sorted_edge.pop(-1)
        u = edge[0][0]
        v = edge[0][1]
        w = edge[1]
        ru = Find(u)
        rv = Find(v)
        if ru != rv:
            Union(ru, rv)
            MST.addedge(u, v, w)
    #print(MST.adjlist)
    Path = path(MST, s, t)
    print("using Kruskal")
    print("the max bandwidth path from vertex%d to vertex%d is : %s" %
          (s, t, Path[1]))
    return s, t, Path[1]
Esempio n. 4
0
#Source: https://github.com/careermonk/DataStructureAndAlgorithmicThinkingWithPython/blob/master/src/chapter07priorityqueues/KthSmallestWithExtraHeap.py

import sys
sys.path.append("./mylib")
import Heap  #custom Heap module
from heapq_showtree import show_tree #custom module for printing Heap

#Initialize Max Heap
Something = Heap.MaxHeap()

#Insert into Heap
Something.Insert(13)
Something.Insert(5)
Something.Insert(6)
Something.Insert(1)
Something.Insert(2)
Something.Insert(4)
Something.Insert(17)

#Print Heap - linear
Something.printHeap()
#Print Heap - tree type
show_tree(Something.printHeap2())

#Delete node
for x in range (1,Something.size+1):
    print("Delete node")
    Something.Delete()
    show_tree(Something.printHeap2())