def ksort(inputlist, k ):
    indx =1
    heap = MinHeap()
    res = []
    heap.insertKey(inputlist[0])
    while len(heap.heap)>0:
        for i in range(k+1):
            if indx < len(inputlist):
                heap.insertKey(inputlist[indx])
                indx+=1
        minValue = heap.extractMin()
        res.append(minValue)
           
    return res 
Example #2
0
def Dijkstra(graph, start):# pylint: disable=invalid-name
    """ Main implementation using Priority Queue"""
    priorityqueue = MinHeap()
    priorityqueue.insertKey(start)

    distance = [-1 for i in range(graph.num_of_nodes)]
    path = [-1 for i in range(graph.num_of_nodes)]
    while not priorityqueue.empty():
        v = priorityqueue.extractMin() # pylint: disable=invalid-name
        print(graph.graph.items(),"...",v)
        for adj, weight in graph.graph.get(v):
            d = distance[v] + weight # pylint: disable=invalid-name
            if distance[adj] == -1:
                distance[adj] = d
                priorityqueue.insertKey(adj)
                priorityqueue.decreaseKey(adj, d)
            elif distance[adj] > d:
                distance[adj] = d
                priorityqueue.decreaseKey(adj, d)
            
            path[adj] = v

    return distance, path
Example #3
0
def Dijkstra(graph, start):  # pylint: disable=invalid-name
    """ Main implementation using Priority Queue"""
    priorityqueue = MinHeap()
    priorityqueue.insertKey(start)

    distance = [-1 for i in range(graph.num_of_nodes)]
    path = [-1 for i in range(graph.num_of_nodes)]
    while not priorityqueue.empty():
        v = priorityqueue.extractMin()  # pylint: disable=invalid-name
        print(graph.graph.items(), "...", v)
        for adj, weight in graph.graph.get(v):
            d = distance[v] + weight  # pylint: disable=invalid-name
            if distance[adj] == -1:
                distance[adj] = d
                priorityqueue.insertKey(adj)
                priorityqueue.decreaseKey(adj, d)
            elif distance[adj] > d:
                distance[adj] = d
                priorityqueue.decreaseKey(adj, d)

            path[adj] = v

    return distance, path