Exemplo n.º 1
0
def primsAlgorithm(graph, start, surface):
    
    for v in graph:
        v.setStatus(0)
        v.setParent(None)
        
    pq = PriorityQueue()
    #heapq.heappush(pq, (0, start))
    pq.enQueue((0, start))
    while (not pq.isEmpty()):
        current = pq.deQueue()[1]
        current.setStatus(2)
        if(current.getParent() != None):
            graph.drawEdge(current.getParent(), current, surface, "yellow")
            time.sleep(0.5)
            #print(str(current.getParent().getId()) + "->" + str(current.getId()))
        for neighbour in current.getNeighbours():
                    if (neighbour.getStatus() == 0):        
                        neighbour.setStatus(1)
                        neighbour.setParent(current)
                        neighbour.setpqWeight(current.neighbours[neighbour])
                        #print(str(neighbour.getpqWeight()))
                        pq.enQueue((neighbour.getpqWeight(), neighbour))
                    elif (neighbour.getStatus() == 1):
                        if (neighbour.getpqWeight() > current.neighbours[neighbour]):
                            old = (neighbour.getpqWeight(), neighbour)
                            neighbour.setpqWeight(current.neighbours[neighbour])
                            pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
                            neighbour.setParent(current)    
    """
Exemplo n.º 2
0
 def prims(self, graph, start, canvas):
     for v in graph:
         v.setStatus(0)
         v.setParent(None)
             
     pq = PriorityQueue()
     pq.enQueue((0, start))
     while (not pq.isEmpty()):
         current = pq.deQueue()[1]
         current.setStatus(2)
         if(current.getParent() != None):
             graph.drawEdge(current.getParent(), current, canvas, "yellow")
             canvas.update()
             time.sleep(0.5)
         for neighbour in current.getNeighbours():
             if (neighbour.getStatus() == 0):  
                 # Encountered a new vertex
                 neighbour.setStatus(1)
                 neighbour.setParent(current)
                 neighbour.setpqWeight(current.neighbours[neighbour])
                 pq.enQueue((neighbour.getpqWeight(), neighbour))
             elif (neighbour.getStatus() == 1):
                 if (neighbour.getpqWeight() > current.neighbours[neighbour]):
                     # Found smaller weight, update accordingly
                     old = (neighbour.getpqWeight(), neighbour)
                     neighbour.setpqWeight(current.neighbours[neighbour])
                     pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
                     neighbour.setParent(current)
Exemplo n.º 3
0
def dijkstrasAlgorithm(graph, start):
    for v in graph:
        v.setStatus(0)
        
    #start.setStatus(2)
    start.setDistance(0)
    pq = PriorityQueue()
    #heapq.heappush(pq, (0, start))
    pq.enQueue((0, start))
    while (not pq.isEmpty()):
        current = pq.deQueue()[1]
        current.setStatus(2)
        #if(current.getParent() != None):
            #  print(str(current.getParent().getId()) + "->" + str(current.getId()))
        for neighbour in current.getNeighbours():
            if (neighbour.getStatus() == 0):        
                neighbour.setStatus(1)
                neighbour.setParent(current)
                neighbour.setpqWeight(current.neighbours[neighbour])
                neighbour.setDistance(current.getDistance() + current.neighbours[neighbour])
                pq.enQueue((neighbour.getpqWeight(), neighbour))
            elif (neighbour.getStatus() == 1):
                if (neighbour.getDistance()) > (current.getDistance() + current.neighbours[neighbour]):
                    #old = (neighbour.getpqWeight(), neighbour)
                    #neighbour.setpqWeight(current.neighbours[neighbour])
                    #pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
                    neighbour.setParent(current)
                    neighbour.setDistance(current.getDistance() + current.neighbours[neighbour])
Exemplo n.º 4
0
def dijkstrasAlgorithm(graph, start, surface):
    pq = PriorityQueue()
    for v in graph:
        v.setStatus(0)
        v.setDistance(float("inf"))
        #print((v.getDistance(), v))
        pq.enQueue((v.getDistance(), v))
            
        #start.setStatus(2)
    start.setDistance(0)
    pq.updatePriority((float("inf"), start), (0, start))
    #heapq.heappush(pq, (0, start))
    #pq.enQueue((0, start))
    #while (not pq.isEmpty()):
    for i in range(0, graph.order):
        current = pq.deQueue()[1]
        current.setStatus(2)
        if(current.getParent() != None):
            graph.drawEdge(current.getParent(), current, surface, "blue")
            time.sleep(0.5)            
            #if(current.getParent() != None):
                #  print(str(current.getParent().getId()) + "->" + str(current.getId()))
        for neighbour in current.getNeighbours():
            """
            if (neighbour.getStatus() == 0):        
                neighbour.setStatus(1)
                neighbour.setParent(current)
                #neighbour.setpqWeight(current.neighbours[neighbour])
                neighbour.setDistance(current.getDistance() + current.neighbours[neighbour])
                pq.enQueue((neighbour.getDistance(), neighbour))
            """
            #elif (neighbour.getStatus() == 1):
            if (neighbour.getDistance()) > (current.getDistance() + current.neighbours[neighbour]):
                old = (neighbour.getDistance(), neighbour)
                        #neighbour.setpqWeight(current.neighbours[neighbour])
                        #pq.updatePriority(old, (neighbour.getpqWeight(), neighbour))
                neighbour.setParent(current)
                neighbour.setDistance(current.getDistance() + current.neighbours[neighbour]) 
                pq.updatePriority(old, (neighbour.getDistance(), neighbour))
    
    """
Exemplo n.º 5
0
 def dijkstras(self, graph, start, canvas):
     pq = PriorityQueue()
     for v in graph:
         v.setStatus(0)
         v.setDistance(float("inf"))
         pq.enQueue((v.getDistance(), v))
                 
     start.setDistance(0)
     pq.updatePriority((float("inf"), start), (0, start))
     for i in range(0, graph.order):
         current = pq.deQueue()[1]
         current.setStatus(2)
         if(current.getParent() != None):
             graph.drawEdge(current.getParent(), current, canvas, "blue")
             canvas.update()
             time.sleep(0.5)            
         for neighbour in current.getNeighbours():
             if (neighbour.getDistance()) > (current.getDistance() + current.neighbours[neighbour]):
                 # Found smaller weight, update accordingly.
                 old = (neighbour.getDistance(), neighbour)
                 neighbour.setParent(current)
                 neighbour.setDistance(current.getDistance() + current.neighbours[neighbour]) 
                 pq.updatePriority(old, (neighbour.getDistance(), neighbour))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PriorityQueue import PriorityQueue

s = PriorityQueue()
s.enQueue(2,1)
s.enQueue(3,1)
s.enQueue("sxw1",3)
s.enQueue("sxw2",3)
s.enQueue("sxw3",3)
s.enQueue("sxw4",3)
s.enQueue(1.14,2)
s.enQueue(2.14,2)
s.enQueue(3.14,2)
s.enQueue(4.14,2)

while not s.isEmpty():
	item = s.deQueue()
	print("length:", len(s),"value:", item.value)