def hillClimb(self): PQ = PriorityQueue() ricList = self.findRIC() for nn in range(len(ricList)): # Add left leftMap = self.moveLeft(ricList[nn]) if (leftMap != None): PQ.push(leftMap, UPGetScore.getMapScore(leftMap)) # Add right rightMap = self.moveRight(ricList[nn]) if (rightMap != None): PQ.push(rightMap, UPGetScore.getMapScore(rightMap)) # Add up upMap = self.moveUp(ricList[nn]) if (upMap != None): PQ.push(upMap, UPGetScore.getMapScore(upMap)) # Add down downMap = self.moveDown(ricList[nn]) if (downMap != None): PQ.push(downMap, UPGetScore.getMapScore(downMap)) return PQ
def color_most_constrained_first(graphs, color): global spills, unspillable, num_unused, used_registers #print 'starting color_most_constrained_first' for v in graphs.vertices(): used_registers[v] = set([]) num_unused[v] = len(registers) - len(used_registers[v]) left = set(graphs.vertices()) - set(reserved_registers) - set(registers) queue = PriorityQueue(left, avail_reg_then_unspill) while not queue.empty(): v = queue.pop() if debug: print 'next to color is ' + v if v not in color.keys(): c = choose_color(v, color, graphs,False) color[v] = c for u in graphs.interferes_with(v): #if u in graphs.vertices(): queue.update(u) used_registers[u] |= set([c]) num_unused[u] = len(registers) - len(used_registers[u]) if not is_reg(c): spills += 1 return color
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices # COMPLETE the Dijkstra code here return (d,pi)
def representativeNodes(G, k, metric=1): ''' Finds the most distinguishable (representative) nodes in graph G greedily. Takes the most furthest node to the already chosen nodes at each step. Input: G -- networkx object graph with weighted edges k -- number of nodes needed metric -- parameter for differentiating representative qualities metric == 1 trying to maximize total distance in the chosen set of k nodes metric == 2 trying to maximize minimal distance between a pair of k nodes Output: S -- chosen k nodes objv -- objective value according to the chosen metric and set of nodes ''' S = [] # set of chosen nodes S_dist = PQ() # distances from each node in G to set S according to metric # initialize S with furthest vertices try: u,v,d = max(G.edges(data=True), key=lambda (u, v, d): d['weight']) except KeyError: raise KeyError, 'Most likely you have no weight attribute' S.extend([u,v]) # compute distances from each node in G to S for v in G.nodes(): if v not in S: # calculate only for nodes in G if metric == 1: S_dist.add_task(v, - _sumDist(G, S, v)) # take minus to pop the maximum value from priority queue elif metric == 2: S_dist.add_task(v, - _minDist(G, S, v)) # take minus to pop the maximum value from priority queue # add new nodes to the set greedily while len(S) < k: u, priority = S_dist.pop_item() # find maximum value of distance to set S S.append(u) # append that node to S # only increase distance for nodes that are connected to u for v in G[u].keys(): if v not in S: # add only remained nodes [priority, count, task] = S_dist.entry_finder[v] # finds distance for the previous step try: if metric == 1: S_dist.add_task(v, priority-G[u][v]['weight']) # adds distance to the new member of S elif metric == 2: S_dist.add_task(v, max(priority, -G[u][v]['weight'])) # update min distance to the set S except: raise u,v, "These are vertices that caused the problem" # extract objective value of the chosen set if metric == 1: objv = 0 for u in S: objv += _sumDist(G, S, u) elif metric == 2: objv = float('Inf') for u in S: objv = min(objv, _minDist(G, S, u)) return S, objv
def solveUCS(self): #Initialize priority queue and fill it with the first connecting nodes pq = PriorityQueue() for edge in self.graph[self.start]: pq.push(edge,self.start,edge[1]) #Recurse path = [] self.rsolveUCS(pq,path) path.append(self.start) return path
def dijkstra(graphObj, startVertex): pq = PriorityQueue() for vertex in graphObj.getVertices(): if vertex == startVertex: pq.insert([0, vertex]) graphObj.verticesList[startVertex].distance = 0 else: pq.insert([INFINITY, vertex]) while(len(pq.pqueue)): currentVertex = pq.extractMin() if len(pq.pqueue) == 1: break # print(pq.pqueue, pq.lookup) for adjNode in graphObj.verticesList[currentVertex[1]].getConnections(): newDistance = graphObj.verticesList[currentVertex[1]].distance + graphObj.verticesList[currentVertex[1]].adjList[adjNode] if newDistance < graphObj.verticesList[adjNode].distance: graphObj.verticesList[adjNode].distance = newDistance graphObj.verticesList[adjNode].predecessor = currentVertex[1] index = pq.lookup[adjNode] pq.decreaseKey(index, newDistance) return graphObj
def degreeHeuristicSeed(G, k): S = [] d = PQ() for u in G: degree = sum([1 for v in G[u] if G[u][v]['weight']]) # degree = len(G[u]) d.add_task(u, -degree) for i in range(k): u, priority = d.pop_item() S.append(u) return S
def __init__(self, maze): #super already sets the start as the current cell super(Astar, self).__init__(maze, maze.start()) self.strategy = "Euclidean" #make it possible to choose an option which heuristic can be used #current position not necessary, stored in walker #open list containing all cells to explore self.open = PriorityQueue() self.closed = dict()# node:parent self.g = 10 #total costs the agent took so far self.last = None self.closed[self._cell] = None
def test_enqueue(self): priority_queue = PriorityQueue() priority_queue.enqueue(10, 6) priority_queue.enqueue(20, 5) priority_queue.enqueue(30, 4) priority_queue.enqueue(40, 3) priority_queue.enqueue(50, 2) priority_queue.enqueue(60, 1) priority_queue.enqueue(70, 0) self.assertEqual([{0: 70}, {3: 40}, {1: 60}, {6: 10}, {4: 30}, {5: 20}, {2: 50}], priority_queue.returnQueue())
def __init__(self): ''' Method for initializing an empty Network. ''' self._nodes = set() self._topLevelNodes = set() self._bottomLevelNodes = set() self._allLinks = set() self._topLevelLinks = set() self._cutLinks = set() self._sortedLinks = PriorityQueue() self._idDict = {} self._idCounter = 0
def init_central_trading_system(): global glob stockInfoList = StockInfo.objects.all() securityStockInfoList = SecurityStockInfo.objects.all() # this is accounts!!! for iStock in stockInfoList: glob.InstQueue[iStock.StockID] = (PriorityQueue(),PriorityQueue()) for iAccount in securityStockInfoList: # iSecStock glob.InstNotDealt[iAccount.SecurityID] = {} return True
def djisktra(self, a, b): distances = [float("inf") for _ in self.vertices] distances[a] = 0 predecessors = [None for _ in self.vertices] # queue = PriorityQueue() queue = PriorityQueue() queue.enqueue((distances[a], self.vertices[a].label)) while True: if queue.empty(): break # print(queue) _, current = queue.dequeue() if current is b: break # dequeue a node we already looked at # this may not be necessary because it will just not decrease any travel times anyway else: # traverse adj list, and see if any are shorter than current dist for v in self.vertices[current].adj: temp = distances[current] + weight(self.vertices[current], self.vertices[v]) if distances[v] > temp: distances[v] = temp predecessors[v] = current queue.enqueue((temp, v)) # shouldn't this be (distances[v], v) count = 0 for distance in distances: if (distance != float("inf")): count+=1 #print((distances[b], predecessors[b], count)) return (distances, predecessors, count)
def AStar(maze, x, y, fpoint): startPoint = Point(x, y, None) startPoint.f = startPoint.g = startPoint.h = 0 openList = PriorityQueue() openList.insert(startPoint) while (not (openList.isEmpty())): current_node = openList.delete() maze[current_node.x][current_node.y] = 3 if (current_node.isEqual(fpoint)): return current_node children = [] for pos in [(0, -1), (0, 1), (-1, 0), (1, 0)]: curr_x = current_node.x + pos[0] curr_y = current_node.y + pos[1] if (not (isFeasible(maze, curr_x, curr_y))): continue child = Point(curr_x, curr_y, current_node) children.append(child) for child in children: child.g = current_node.g + 1 child.h = manhattanDist(child, fpoint) child.f = child.g + child.h openList.insert(child)
def prims(graphObj, start): pq = PriorityQueue() for vertex in graphObj.verticesList: if vertex == start: g.verticesList[vertex].distance = 0 pq.insert([0, vertex]) continue g.verticesList[vertex].distance = INFINITY pq.insert([INFINITY, vertex]) while(len(pq.pqueue)): currentVertex = pq.extractMin() if len(pq.pqueue) == 1: return for adjNode in graphObj.verticesList[currentVertex[1]].getConnections(): if adjNode in pq.lookup(adjNode): newDistance = graphObj.verticesList[currentVertex[1]].getCost(adjNode) if newDistance < graphObj.verticesList[adjNode].distance: graphObj.verticesList[adjNode].distance = newDistance graphObj.verticesList[adjNode].predecessor = currentVertex[1] return graphObj
def read(G): # id to name & name to id dictionary id_to_name = dict() name_to_id = dict() # distance dictionary distance = dict() # predecessor dictionary predecessor = dict() # priority queue pq = PQ() # read nodes from movie_nodes.txt with open('movie_nodes.txt') as fn: # read data rows from file rows = fn.readlines() # for each row for row in rows: # string token tokens = row.strip('\n').split('\t') # dictionary for id -> name id_to_name[tokens[0]] = tokens[1] # dictionary for name -> id name_to_id[tokens[1]] = tokens[0] # graph add node G.add_node(tokens[0]) # initialize all nodes distance distance[tokens[0]] = MAXINT # initialize priority queue pq.add_task(tokens[0], MAXINT) # initialize all nodes predecessor predecessor[tokens[0]] = None # close file fn.close() # read edges from movie_edgesw.txt with open('movie_edgesw.txt') as fn: # read data rows from file rows = fn.readlines() # for each row for row in rows: # string token tokens = row.strip('\n').split('\t') # graph add edges G.add_edge(tokens[0], tokens[1], weight=float(tokens[2])) # close file fn.close() return id_to_name, name_to_id, distance, predecessor, pq, G
def test_priority_and_sort_order(): p = PriorityQueue() p.add(1, 1) p.add(2, 1) p.add(3, 2) p.add(4, 1) assert p.pop() == 3 assert p.pop() == 1 assert p.pop() == 2 assert p.pop() == 4 assert p.peek() is None
def baseFC(crawlParams): seedURLs = crawlParams['seedURLs'] t = [(-1, p, -1, "") for p in seedURLs] priorityQueue = PriorityQueue(t) classifierFileName = crawlParams['classifierFileName'] crawlParams["priorityQueue"] = priorityQueue try: classifierFile = open(classifierFileName, "rb") vsmClf = pickle.load(classifierFile) #self.classifier.error = 0.05 #self.classifier.relevanceth = 0.75 classifierFile.close() except: #vsmClf = VSMClassifier() vsmClf = VSM_CentroidClassifier() vsmClf.buildModel(crawlParams['model'], topK=10) #vsmClf.buildVSMClassifier(crawlParams['seedURLs'],classifierFileName,crawlParams['eventType'], crawlParams['minCollFreq']) print 'Saving model to file' classifierFile = open(classifierFileName, "wb") pickle.dump(vsmClf, classifierFile) classifierFile.close() crawlParams['scorer'] = vsmClf #keywordModel = KeywordModel() #keywordModel.buildModel(crawlParams['model'])#,crawlParams['No_Keywords']) #crawlParams['scorer']=keywordModel #crawler = Crawler(priorityQueue,scorer,options) crawler = Crawler(crawlParams) qu = crawler.crawl() quS = '\n'.join([str(-1 * s[0]) + "," + s[1] for s in qu]) with open('queueBase.txt', 'w') as fw: fw.write(quS.encode('utf8')) return crawler.relevantPages
def eventFC(scorer, url_scorer, options): # seedUrls = ["http://www.cnn.com/2013/09/27/world/africa/kenya-mall-attack/index.html", # "http://www.youtube.com/watch?v=oU9Oop892BQ", # "http://ifrc.org/en/news-and-media/press-releases/africa/kenya/kenya-red-cross-society-continues-to-provide-vital-support-to-victims-and-families-of-the-westgate-shopping-mall-attack/" # ] #keywords = ['demonstrations','protest','elections','egypt','revolution','uprising','arab','spring','tunisia','libya','military'] t = [(-1, p, -1) for p in options['seeds']] #t = [(-1,Url(p)) for p in seedUrls] priorityQueue = PriorityQueue(t) crawler = Crawler(priorityQueue, scorer, options) crawler.set_url_scorer(url_scorer) crawler.enhanced_crawl() print crawler.relevantPagesCount print crawler.pagesCount f = open("harverstRatioData.txt", "w") for r, p in crawler.harvestRatioData: f.write(str(r) + "," + str(p) + "\n") f.close() f = open("logData.txt", "w") furl = open("Output-URLs.txt", "w") for p in crawler.relevantPages: f.write(str(p.pageId) + "," + str(p.pageUrl[2]) + "\n") furl.write(p.pageUrl[1] + "\n") f.close() furl.close()
def __init__(self, app, length, fps, synchronous): self.__app = app self.__events = PriorityQueue() self.__spf = 1/fps # seconds per frame self.__frame = 0 self.__frameTime = -self.__spf self.__length = length self.__synchronous = synchronous
def __init__(self,newsSeedList,socialSeedList,govSeedList): self.turn = 0 ''' if newsSeedList is not None: self.newsQueue = PriorityQueue(newsSeedList) if socialSeedList is not None: self.socialQueue = PriorityQueue(socialSeedList) if govSeedList is not None: self.govQueue = PriorityQueue(govSeedList) ''' self.newsQueue = PriorityQueue(newsSeedList) self.socialQueue = PriorityQueue(socialSeedList) self.govQueue = PriorityQueue(govSeedList) heapq.heapify(self.newsQueue.queue) heapq.heapify(self.socialQueue.queue) heapq.heapify(self.govQueue.queue)
def buildTree(self, text): queue = PriorityQueue(cmp=lambda a,b: a.frequency > b.frequency) for v,f in dict(Counter(list(text))).items(): queue.enqueue(BinaryNode(v, f)) while queue.size() > 1: nodeL = queue.dequeue() nodeR = queue.dequeue() parent = BinaryNode(None, nodeL.frequency+nodeR.frequency) parent.left = nodeL parent.right = nodeR queue.enqueue(parent) self.root = queue.dequeue()
class TestPriorityQueue(unittest.TestCase): def setUp(self): self.pq = PriorityQueue() def tearDown(self): self.pq = None def test_basic(self): self.assertTrue(self.pq.isEmpty()) self.assertEqual(0, len(self.pq)) def test_fill(self): """Fill up the queue.""" data = knuth_shuffle(range(1, 10)) for d in data: self.pq.enqueue(d) sorted_data = sorted(data) self.assertEqual(sorted_data[0], self.pq.peekFront()) self.assertEqual(sorted_data[len(sorted_data) - 1], self.pq.peekRear()) self.assertEqual(len(data), len(self.pq)) for d in sorted_data: self.assertEqual(d, self.pq.dequeue()) self.assertTrue(self.pq.isEmpty())
def prim(aGraph, startVertex): # create a priority queue that uses distance as the value to determine priority and thus its position # use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance # decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue. pQueue = PriorityQueue() # initialize the state of the graph for vertex in aGraph: # initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately vertex.setDistance(sys.maxsize) vertex.setPred(None) # distance represents distance from the startVertex, trivially 0 for the startVertex startVertex.setDistance(0) # key value pair # key is distance and vertex is the value pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ]) while not pQueue.isEmpty(): currentVertex = pQueue.delMin() # iterate over the currentVertex's edges for nextVertex in currentVertex.getConnections(): # calculate the weight from currentVertex to nextVertex newCost = currentVertex.getWeight(nextVertex) # found a shorter path # node is not considered to be part of the spanning tree until it is removed from the priority queue. # nextVertex in pQueue means that vertex is not yet in the spanning tree so it is safe to add (ensures an acyclic graph) if nextVertex in pQueue and newCost< nextVertex.getDistance(): # assign the predecessor appropiately nextVertex.setPred(currentVertex) # set a new distance on the nextVertex nextVertex.setDistance(newCost) # update the priorityQueue with the correct values pQueue.decreaseKey(nextVertex, newCost)
def dijkstra(aGraph, startVertex): # create a priority queue that uses distance as the value to determine priority and thus its position # use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance # decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue. pQueue = PriorityQueue() # distance represents distance from the startVertex, trivially 0 for the startVertex # initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately startVertex.setDistance(0) # key value pair # key is distance and vertex is the value pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ]) while not pQueue.isEmpty(): currentVertex = pQueue.delMin() # iterate over the currentVertex's edges for nextVertex in currentVertex.getConnections(): # distance of current vertex and the weight of it's edges newDistance = currentVertex.getDistance() + currentVertex.getWeight(nextVertex) # found a shorter path if newDistance < nextVertex.getDistance(): # set a new distance on the nextVertex nextVertex.setDistance(newDistance) # assign the predecessor appropiately nextVertex.setPred(currentVertex) # update the priorityQueue with the correct values pQueue.decreaseKey(nextVertex, newDistance)
def getScores(G, Ep): '''Finds scores for GDD. Scores are degree for each node. ''' scores = PQ() # degree discount active = dict() inactive = dict() # initialize degree discount for u in G: active[u] = 1 # inactive[u] = sum([Ep[(u,v)]*G[u][v]['weight'] for v in G[u]]) inactive[u] = sum([1 - (1 - Ep[(u,v)])**G[u][v]["weight"] for v in G[u]]) priority = active[u]*(1 + inactive[u]) scores.add_task(u, -priority) # add degree of each node return scores, active, inactive
def degreeHeuristic(G, k, p=.01): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- chosen k nodes ''' S = [] d = PQ() for u in G: degree = sum([G[u][v]['weight'] for v in G[u]]) # degree = len(G[u]) d.add_task(u, -degree) for i in range(k): u, priority = d.pop_item() S.append(u) return S
def getScores(G, Ep): '''Finds scores for GDD. Scores are degree for each node. ''' scores = PQ() # degree discount active = dict() inactive = dict() # initialize degree discount for u in G: active[u] = 1 # inactive[u] = sum([Ep[(u,v)]*G[u][v]['weight'] for v in G[u]]) inactive[u] = sum( [1 - (1 - Ep[(u, v)])**G[u][v]["weight"] for v in G[u]]) priority = active[u] * (1 + inactive[u]) scores.add_task(u, -priority) # add degree of each node return scores, active, inactive
def solveUCS(self): # return ["A","B","C"] visited = [self.start] q = PriorityQueue() if self.start == self.end: print("Graph already solved! start and end nodes are the same!") else: #Build Queue startNode = self.findNode(self.start) for child in startNode.children: print("ADDING",child,"PARENT",startNode.name) q.put((child,startNode.name,startNode.children[child])) visited.append(child) #Recursively solve path = [] self.rsolveUCS(q,path,visited) path.append(self.start) return path
def solveUCS(self): # return ["A","B","C"] visited = [self.start] q = PriorityQueue() if self.start == self.end: print("Graph already solved! start and end nodes are the same!") else: #Build Queue startNode = self.findNode(self.start) for child in startNode.children: print("ADDING", child, "PARENT", startNode.name) q.put((child, startNode.name, startNode.children[child])) visited.append(child) #Recursively solve path = [] self.rsolveUCS(q, path, visited) path.append(self.start) return path
def dijkstra(aGraph, start): pq = PriorityQueue() start.setDistance(0) pq.buildHeap([(v.getDistance(), v) for v in aGraph]) while not pq.isEmpty(): currentVert = pq.delMin() for nextVert in currentVert.getConnections(): newDist = currentVert.getDistance() \ + currentVert.getWeight(nextVert) if newDist < nextVert.getDistance(): nextVert.setDistance(newDist) nextVert.setPred(currentVert) pq.decreaseKey(nextVert, newDist)
def Dijkstras(graph,start): pq=PriorityQueue() start.setDistance(0) pq.buildHeap([(v.getDistance(),v) for v in graph]) # distance is the key in the priority queue while not pq.isEmpty(): currentvertex=pq.delMin() for newvertex in currentvertex.getConnections(): newDist=currentvertex.getDistance()+currentvertex.getWeight(newvertex) if newDist<newvertex.getDistance(): # at the start the distance of all the vertices is set to maximum. That's why the if statement will be executed newvertex.setDistance(newDist) newvertex.setPredecessor(currentvertex) pq.decreaseKey(newvertex,newDist)
def run(layout, heuristic): layoutText = load_maze(layout) currentMaze = Maze(layoutText) aMaze = Maze(layoutText) aProblem = Problem(aMaze) numberOfMoves = 0 fringe = PriorityQueue() visitedNodes = set() goal = a_star_search(aProblem, heuristic, fringe, visitedNodes) path = list() while goal is not None: path.insert(0, goal) numberOfMoves += 1 goal = goal.get_parent() move = 0 print("For Heuristics: ", heuristic) if len(path) > 0: print("|------------- STARTING MAZE--------------|\n") currentMaze.update_maze(path[0].get_x_coordinate(), path[0].get_y_coordinate(), "S") currentMaze.printMaze() print( "\n|------------- STARTING DICE ORIENTATION--------------|\n") path[0].dice.display() for currentNode in path: print("\n|-------------------- MOVE: " + str(move) + " -------------------|\n") print("|------------- MAZE--------------|\n") currentMaze.update_maze(currentNode.get_x_coordinate(), currentNode.get_y_coordinate(), '#') currentMaze.printMaze() print("\n|------------- DICE--------------|\n") currentNode.dice.display() move += 1 print("\n|---------------- PERFORMANCE METRICS -----------------|\n") print("No. of moves in the solution : ", numberOfMoves - 1) print("No. of nodes put on the queue : ", fringe.nodesPutOnQueue) print("No. of nodes visited / removed from the queue : ", len(visitedNodes)) print("\n|------------------------------------------------------|\n") result = [ heuristic, numberOfMoves - 1, fringe.nodesPutOnQueue, len(visitedNodes) ] return result
def __init__(self, game, player, nom, init, goal, wallStates, goalStates): self.game = game self.player = player self.nom = nom self.position = init self.goal = goal self.graph = Graph((wallStates, goalStates), game.spriteBuilder.rowsize, game.spriteBuilder.colsize) self.chemin = [] self.frontier = PriorityQueue() self.frontier.put(init, 0) self.came_from = {} self.cost_so_far = {} self.came_from[init] = None self.cost_so_far[init] = 0 self.priority = 0 self.avoid = [] self.ID = Jeu.cpt Jeu.cpt += 1 Jeu.positions[self.nom] = init Jeu.caches["l1"][self.nom] = [] Jeu.references[self.nom] = self
def generalGreedy(G, k, p=.01): ''' Finds initial seed set S using general greedy heuristic Input: G -- networkx Graph object k -- number of initial nodes needed p -- propagation probability 传播概率 Output: S -- initial set of k nodes to propagate ''' import time start = time.time() R = 20 # number of times to run Random Cascade S = [] # set of selected nodes # add node to S if achieves maximum propagation for current chosen + this node for i in range(k): s = PQ() # priority queue for v in G.nodes(): # 遍历G中所有节点 if v not in S: s.add_task(v, 0) # initialize spread value 0为优先度 for j in range(R): # run R times Random Cascade 运行R次随机级联 [priority, count, task] = s.entry_finder[v] # 获取v的优先度 # runIC(G, S + [v], p) 表示把S+[v]看做种子集,p为传播概率 返回Influence Spread # priority - float(len(runIC(G, S + [v], p))) / R 为优先度 # v由于在上面已经加入pq, 所以会先执行remove_task 将v移出entry_finder 即把上面的task置为<removed-task> # 此时,该方法用于更新v的优先度 如果在IC模型中 S=[v]的影响越大,那么v的优先度越小 s.add_task(v, priority - float(len(runIC(G, S + [v], p))) / R) # add normalized spread value task, priority = s.pop_item() # 移除并返回最低优先度的节点 S.append(task) # 将优先度最低的节点加入S 优先度低是因为在IC模型中扩散的很快 print(i, k, time.time() - start) return S
def GDD(G, k, Ep): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed Ep -- propagation probabilities Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount active = dict() inactive = dict() # initialize degree discount for u in G: active[u] = 1 # inactive[u] = sum([Ep[(u,v)]*G[u][v]['weight'] for v in G[u]]) inactive[u] = sum([1 - (1 - Ep[(u,v)])**G[u][v]["weight"] for v in G[u]]) priority = active[u]*(1 + inactive[u]) dd.add_task(u, -priority) # add degree of each node # add vertices to S greedily for i in range(k): u, priority = dd.pop_item() # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: active[v] *= (1-Ep[(u,v)])**G[u][v]['weight'] inactive[v] -= 1 - (1 - Ep[(u,v)])**G[u][v]['weight'] priority = active[v]*(1 + inactive[v]) dd.add_task(v, -priority) return S
def GDD(G, k, Ep): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed Ep -- propagation probabilities Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount active = dict() inactive = dict() # initialize degree discount for u in G: active[u] = 1 # inactive[u] = sum([Ep[(u,v)]*G[u][v]['weight'] for v in G[u]]) inactive[u] = sum( [1 - (1 - Ep[(u, v)])**G[u][v]["weight"] for v in G[u]]) priority = active[u] * (1 + inactive[u]) dd.add_task(u, -priority) # add degree of each node # add vertices to S greedily for i in range(k): u, priority = dd.pop_item( ) # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: active[v] *= (1 - Ep[(u, v)])**G[u][v]['weight'] inactive[v] -= 1 - (1 - Ep[(u, v)])**G[u][v]['weight'] priority = active[v] * (1 + inactive[v]) dd.add_task(v, -priority) return S
def degreeDiscountIC(G, k, p=.01): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item() # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v]['weight'] # increase number of selected neighbors priority = d[v] - 2*t[v] - (d[v] - t[v])*t[v]*p # discount of degree dd.add_task(v, -priority) return S
def generalGreedy(G, k, p=0.01): """ Finds initial seed set S using general greedy heuristic Input: G -- networkx Graph object k -- number of initial nodes needed p -- propagation probability Output: S -- initial set of k nodes to propagate """ import time start = time.time() R = 20 # number of times to run Random Cascade S = [] # set of selected nodes # add node to S if achieves maximum propagation for current chosen + this node for i in range(k): s = PQ() # priority queue for v in G.nodes(): if v not in S: s.add_task(v, 0) # initialize spread value for j in range(R): # run R times Random Cascade [priority, count, task] = s.entry_finder[v] s.add_task(v, priority - float(len(runIC(G, S + [v], p))) / R) # add normalized spread value task, priority = s.pop_item() S.append(task) print i, k, time.time() - start return S
def generalGreedy(G, k, p=.01): ''' Finds initial seed set S using general greedy heuristic Input: G -- networkx Graph object k -- number of initial nodes needed p -- propagation probability Output: S -- initial set of k nodes to propagate ''' # import time # start = time.time() R = 200 # number of times to run Random Cascade S = [] # set of selected nodes # add node to S if achieves maximum propagation for current chosen + this node for i in range(k): # cannot parallellize s = PQ() # priority queue for v in G.nodes(): if v not in S: s.add_task(v, 0) # initialize spread value # [priority, count, task] = s.entry_finder[v] for j in range( R ): # run R times Random Cascade The gain of parallelizing isn't a lot as the one runIC is not very complex maybe for huge graphs [priority, count, task] = s.entry_finder[v] s.add_task(v, priority - float(len(runIC(G, S + [v], p))) / R) # add normalized spread value task, priority = s.pop_item() print(task, priority) S.append(task) # print(i, k, time.time() - start) return S
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0, self.n): # Iterate through all vertex ID if (i == srcID): # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices # COMPLETE the Dijkstra code here return (d, pi)
def color_most_constrained_first(graphs, color): global spills, unspillable, num_unused, used_registers #print 'starting color_most_constrained_first' for v in graphs.vertices(): used_registers[v] = set([]) num_unused[v] = len(registers) - len(used_registers[v]) left = set(graphs.vertices()) - set(reserved_registers) - set(registers) queue = PriorityQueue(left, avail_reg_then_unspill) while not queue.empty(): v = queue.pop() if debug: print 'next to color is ' + v if v not in color.keys(): c = choose_color(v, color, graphs, False) color[v] = c for u in graphs.interferes_with(v): #if u in graphs.vertices(): queue.update(u) used_registers[u] |= set([c]) num_unused[u] = len(registers) - len(used_registers[u]) if not is_reg(c): spills += 1 return color
def NewDiscount(G, k, p): S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.degree(): d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item( ) # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v][ 'weight'] # increase number of selected neighbors priority = d[v] - 2 * t[v] - ( d[v] - t[v]) * t[v] * p[u, v] # discount of degree dd.add_task(v, -priority) return S
def degreeDiscountIC(G, k, p=.01): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- chosen k nodes ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily for i in range(k): u, priority = dd.pop_item( ) # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v][ 'weight'] # increase number of selected neighbors priority = d[v] - 2 * t[v] - ( d[v] - t[v]) * t[v] * p # discount of degree dd.add_task(v, -priority) return S
class Sim: def __init__(self): self.q = PriorityQueue() self.time = 100 self.nodes = {} self.actors = [] self.done = False def add_actor(self, actor): actor.sim = self self.actors.append(actor) def at(self, event): if event.time < self.time: print "ERROR, time warp" else: self.q.put(event, event.time) def process(self): while not self.q.empty(): event = self.q.get() self.time = event.time try: (result, actor) = event.process(self) actor.send(result) except StopIteration: pass print "Sim done" def prime(self): for a in self.actors: a.prime() def go(self): self.prime() self.process()
def prim(graph,start): # it belongs to the family of greedy algorithms pq=PriorityQueue() for v in graph: v.setDistance(sys.maxsize) v.setPredecessor(None) start.setDistance(0) pq.buildHeap([(v.getDistance(),v) for v in graph]) while not pq.isEmpty() currentvertex=pq.delMin() for newvertex in currentvertex.getConnections(): newDist=currentvertex.getWeight(newvertex) if newDist<newvertex.getDistance() and newvertex in pq: newvertex.setDistance(newDist) newvertex.setPredecessor(currentvertex) pq.decreaseKey(newvertex,newDist)
def FIND_LDAG(G, v, t, Ew): ''' Compute local DAG for vertex v. Reference: W. Chen "Scalable Influence Maximization in Social Networks under LT model" Algorithm 3 INPUT: G -- networkx DiGraph object v -- vertex of G t -- parameter theta Ew -- influence weights of G NOTE: Since graph G can have multiple edges between u and v, total influence weight between u and v will be number of edges times influence weight of one edge. OUTPUT: D -- networkx DiGraph object that is also LDAG ''' # intialize Influence of nodes Inf = PQ() Inf.add_task(v, -1) x, priority = Inf.pop_item() M = -priority X = [x] D = nx.DiGraph() while M >= t: out_edges = G.out_edges([x], data=True) for (v1,v2,edata) in out_edges: if v2 in X: D.add_edge(v1, v2, edata) in_edges = G.in_edges([x]) for (u,_) in in_edges: if u not in X: try: [pr, _, _] = Inf.entry_finder[u] except KeyError: pr = 0 Inf.add_task(u, pr - G[u][x]['weight']*Ew[(u,x)]*M) try: x, priority = Inf.pop_item() except KeyError: return D M = -priority X.append(x) return D
def stopDegreeDiscount(G, tsize, ic_step=1, p=.01, iterations=200): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object tsize -- number of nodes necessary to reach ic_step -- step of change in k between 2 iterations of IC p -- propagation probability Output: S -- seed set Tspread -- spread values for different sizes of seed set ''' S = [] dd = PQ() # degree discount t = dict() # number of adjacent vertices that are in S d = dict() # degree of each vertex # initialize degree discount for u in G.nodes(): d[u] = sum([G[u][v]['weight'] for v in G[u]]) # each edge adds degree 1 # d[u] = len(G[u]) # each neighbor adds degree 1 dd.add_task(u, -d[u]) # add degree of each node t[u] = 0 # add vertices to S greedily # until necessary number of nodes can be reached Tspread = dict() # spread for different k k = 0 Tspread[k] = 0 stepk = 1 while Tspread[k] < tsize: u, priority = dd.pop_item() # extract node with maximal degree discount S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v]['weight'] # increase number of selected neighbors priority = d[v] - 2*t[v] - (d[v] - t[v])*t[v]*p # discount of degree dd.add_task(v, -priority) # calculate IC spread with ic_step if stepk == ic_step: k = len(S) Tspread[k] = avgSize(G, S, p, iterations) print k, Tspread[k] stepk = 0 stepk += 1 # search precise boundary if abs(int(math.ceil(float(ic_step)/2))) == 1: return S, Tspread else: return binarySearchBoundary(G, k, Tspread, tsize, ic_step, p, iterations)
def singleDiscount(G, k, p=.1): ''' Finds initial set of nodes to propagate in Independent Cascade model (with priority queue) Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- chosen k nodes ''' S = [] # set of activated nodes d = PQ() # degrees for u in G: degree = sum([G[u][v]['weight'] for v in G[u]]) d.add_task(u, -degree) for i in range(k): u, priority = d.pop_item() S.append(u) for v in G[u]: if v not in S: [priority, count, task] = d.entry_finder[v] d.add_task(v, priority + G[u][v]['weight']) # discount degree by the weight of the edge return S
def degreeDiscountStar(G,k,p=.01): S = [] scores = PQ() d = dict() t = dict() for u in G: d[u] = sum([G[u][v]['weight'] for v in G[u]]) t[u] = 0 score = -((1-p)**t[u])*(1+(d[u]-t[u])*p) scores.add_task(u, ) for iteration in range(k): u, priority = scores.pop_item() print iteration, -priority S.append(u) for v in G[u]: if v not in S: t[v] += G[u][v]['weight'] score = -((1-p)**t[u])*(1+(d[u]-t[u])*p) scores.add_task(v, score) return S
class Scheduler(): def __init__(self, aCpu): self.currentQueue = FifoQueue() self.cpu = aCpu def getNextPcb(self): return self.currentQueue.getMax() def addPcb(self, pcb): if(self.currentQueue.isEmpty() and not(self.cpu.havePcb())): pcb.toRunning() self.cpu.assignPcb(pcb) else: self.currentQueue.addPcb(pcb) def setFIFOMode(self): self.currentQueue = FifoQueue() def setPriorityMode(self): self.currentQueue = PriorityQueue(3,3) def removePid(self, aPid): self.currentQueue.removePid(aPid)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue cur=self for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices minKey, minDist=pq.extractMin() d[minKey]=minDist #set orignal source distance to 0 pi[minKey]=minKey #set sources to orginal source while(pq.isEmpty()==False):# COMPLETE the Dijkstra code here lst=self.adjList[minKey]#grap array of lists of format (vertices,weight) for node minKey for n in lst:#loop through said list ##grap the next node attached to this node somehow dist=n[1]+minDist#grab distance from list node=n[0] #grab node from list if(pq.hasKey(node)):#check if that nodes min dist has been found if(pq.get(node)>dist):#check if path to the node from minKey is less then current path pq.set(node,dist)#set the nodes distance in the queue d[node]=dist#set the nodes distance in the return array pi[node]=minKey#sets the nodes parents minKey, minDist=pq.extractMin()#extract next node dont need to extract last node as all the paths will be filled would probably work better in a do while loop return (d,pi)
def spreadNewGreedyIC(G, targeted_size, step=1, p=.01, S0=[], iterations = 200): ''' Finds initial set of nodes to propagate in Independent Cascade. Input: G -- networkx graph object k -- number of nodes needed p -- propagation probability Output: S -- set of k nodes chosen TODO: add step functionality ''' import time start = time.time() assert type(S0) == list, "S0 must be a list. %s provided instead" % type(S0) S = S0 # set of selected nodes tsize = 0 R = iterations for i in range(R): T = runIC(G, S, p) tsize += float(len(T))/R while tsize <= targeted_size: s = PQ() # number of additional nodes each remained mode will bring to the set S in R iterations Rv = dict() # number of reachable nodes for node v # initialize values of s for v in G.nodes(): if v not in S: s.add_task(v, 0) # calculate potential additional spread for each vertex not in S prg_idx = 1 idx = 1 prcnt = .1 # for progress to print R = iterations # number of iterations to run RanCas for j in range(R): # create new pruned graph E E = deepcopy(G) edge_rem = [] # edges to remove for (u,v) in E.edges(): w = G[u][v]['weight'] if random() < 1 - (1 - p)**w: edge_rem.append((u,v)) E.remove_edges_from(edge_rem) # find reachable vertices from S Rs = bfs(E, S) # find additional nodes each vertex would bring to the set S for v in G.nodes(): if v not in S + Rs: # if node has not chosen in S and has chosen by spread from S [priority, c, task] = s.entry_finder[v] s.add_task(v, priority - float(len(bfs(E, [v])))/R) if idx == int(prg_idx*prcnt*R): print '%s%%...' %(int(prg_idx*prcnt*100)) prg_idx += 1 idx += 1 # add vertex with maximum potential spread task, priority = s.pop_item() S.append(task) print i, len(S), task, -priority, time.time() - start tsize = 0 for j in range(R): T = runIC(G, S, p) tsize += float(len(T))/R return S
class Animation(Iterator): def __init__(self, app, length, fps, synchronous): self.__app = app self.__events = PriorityQueue() self.__spf = 1/fps # seconds per frame self.__frame = 0 self.__frameTime = -self.__spf self.__length = length self.__synchronous = synchronous def __dequeue(self): # increase frame data try: time, event = self.__events.pop() except IndexError: print "No events left." raise StopIteration if time < self.__frameTime: raise Exception("Cannot go back in time: %s requested %s after %s." % (repr(event.__name__), time, self.__frameTime)) return (time, event) def __updateQueue(self, event): # update the queue try: t = event.next() self.__events.push((self.__sync(t), event)) except StopIteration: pass def next(self): if self.__frameTime > self.__length: print "Reached end time." raise StopIteration if self.__frameTime == -self.__spf: time = 0 event = iter(()) else: time, event = self.__dequeue() if time == self.__frameTime: raise Exception("Double Frame: %s requested %s again." % (repr(event.__name__), time)) self.__frame += 1 self.__frameTime = time self.__updateQueue(event) while self.__events and self.__events[0][0] < self.nextTime: time, event = self.__dequeue() self.__updateQueue(event) return (self.__frame, time) def __sync(self, time): if self.__synchronous: return time - (time % self.__spf) else: return time def addEvent(self, eventFn): generator = eventFn(self, self.__app.camera, self.__app.canvas) time = generator.next() event = (self.__sync(time), generator) self.__events.push(event) @property def nextTime(self): return self.__frameTime + self.__spf @property def time(self): return self.__frameTime
def __init__(self): self.q = PriorityQueue() self.time = 100 self.nodes = {} self.actors = [] self.done = False
acceptCount = 0 rejectCount = 0 # Representation of a solution incumbent is a triple (score, revlist, result) where # revlist is a list of reversals, result is the permutation after applying the # reversals to the starting permutation, and score is the length of revlist # plus the reversalBound of the result. initialState = (reversalBound(alpha), [], alpha) print "Initial state: ", initialState # Build a list of all possible reversals. allrevs = [(i,j) for i in xrange(n-1) for j in xrange(i+1, n)] # The queue is implemented as a list of dictionaries. Each dictionary holds items of # the same score (0...n-1). queue = PriorityQueue(n) queue.insert(initialState) maxQueueLen = len(queue) while True: l = len(queue) if l % 1000 <= 5: print l incumbent = queue.pop() if incumbent == None: break #print "Popping ", incumbent if (incumbent[0] >= bestScore): #print "Rejecting ", incumbent continue
def LDAG_heuristic(G, Ew, k, t): ''' LDAG algorithm for seed selection. Reference: [1] Algorithm 5 Input: G -- directed graph (nx.DiGraph) Ew -- inlfuence weights of edges (eg. uniform, random) (dict) k -- size of seed set (int) t -- parameter theta for finding LDAG (0 <= t <= 1; typical value: 1/320) (int) Output: S -- seed set (list) ''' # define variables S = [] IncInf = PQ() for node in G: IncInf.add_task(node, 0) # IncInf = dict(zip(G.nodes(), [0]*len(G))) # in case of usage dict instead of PQ LDAGs = dict() InfSet = dict() ap = dict() A = dict() print 'Initialization phase' for v in G: LDAGs[v] = FIND_LDAG(G, v, t, Ew) # update influence set for each node in LDAGs[v] with its root for u in LDAGs[v]: InfSet.setdefault(u, []).append(v) alpha = computeAlpha(LDAGs[v], Ew, S, v) A.update(alpha) # add new linear coefficients to A # update incremental influence of all nodes in LDAGs[v] with alphas for u in LDAGs[v]: ap[(v, u)] = 0 # additionally set initial activation probability (line 7) priority, _, _ = IncInf.entry_finder[u] # find previous value of IncInf IncInf.add_task(u, priority - A[(v, u)]) # and add alpha # IncInf[u] += A[(v, u)] # in case of using dict instead of PQ print 'Main loop' for it in range(k): s, priority = IncInf.pop_item() # chose node with biggest incremental influence print it+1, s, -priority for v in InfSet[s]: # for all nodes that s can influence if v not in S: D = LDAGs[v] # update alpha_v_u for all u that can reach s in D (lines 17-22) alpha_v_s = A[(v,s)] dA = computeAlpha(D, Ew, S, s, val=-alpha_v_s) for (s,u) in dA: if u not in S + [s]: # don't update IncInf if it's already in S A[(v,u)] += dA[(s,u)] priority, _, _ = IncInf.entry_finder[u] # find previous value of incremental influence of u IncInf.add_task(u, priority - dA[(s,u)]*(1 - ap[(v,u)])) # and update it accordingly # update ap_v_u for all u reachable from s in D (liens 23-28) dap = computeActProb(D, Ew, S + [s], s, val=1-ap[(v,s)]) for (s,u) in dap: if u not in S + [s]: ap[(v,u)] += dap[(s,u)] priority, _, _ = IncInf.entry_finder[u] # find previous value of incremental influence of u IncInf.add_task(u, priority + A[(v,u)]*dap[(s,u)]) # and update it accordingly S.append(s) return S