Ejemplo n.º 1
0
    def routing(self):

        self.D = {}  # dictionary of node -> final Costs
        self.G = {}  # dictionary of node -> Gateway
        self.L = {}  # dictionary of gateway -> Load

        self.Q = priorityDictionary()  # est.dist. of non-final vert.

        self.H = {}  # dictionary of node -> true Hop Counts
        self.P = {}  # dictionary of node -> Predecessor

        for gateway in range(0, self.numGateways):
            self.Q[gateway] = 0
            self.G[gateway] = gateway
            self.L[self.G[gateway]] = 0
            self.H[gateway] = 0

        for v in self.Q:
            self.D[v] = self.Q[v]
            if len(self.D) == self.number_of_nodes():
                break
            #if v in self.cxns:
            neigh_v = self.neighbors(v)
            if len(neigh_v) > 0:
                #for w in self.cxns[v]:
                for w in neigh_v:
                    #Check that it is connected
                    if self.cxns[v][w]:
                        gatewayLoad = self.L[self.G[v]]

                        cost = self.D[v] + self.cxns[v][
                            w]  # old method to calculate using D table.
                        cost = cost + self.loadFactor * (gatewayLoad) / (200.)
                        if w in self.D:
                            if cost < self.D[w]:
                                raise ValueError(
                                    "Error Dijkstra: Better path to already-final vertex"
                                )
                        elif w not in self.Q or cost < self.Q[w]:
                            self.Q[w] = cost  # assign new cost to Q dictionary
                            self.P[w] = v  # assign predecessor for Parent

                            if self.G.get(w, -1) != -1:
                                self.L[self.G[w]] = self.L[self.G[w]] - 1

                            self.G[w] = self.G[
                                v]  # assign same Gateway as Parent
                            self.L[self.G[v]] = self.L[
                                self.G[v]] + 1  # increment Gateway load
                            self.H[w] = self.H[
                                v] + 1  # assign true Hop Count as increment from Parent
Ejemplo n.º 2
0
    def routing(self):

        self.D = {}  # dictionary of node -> final Costs
        self.G = {}  # dictionary of node -> Gateway
        self.L = {}  # dictionary of gateway -> Load

        self.Q = priorityDictionary()   # est.dist. of non-final vert.

        self.H = {}  # dictionary of node -> true Hop Counts
        self.P = {}  # dictionary of node -> Predecessor

        for gateway in range(0,self.numGateways):
            self.Q[gateway] = 0
            self.G[gateway] = gateway
            self.L[self.G[gateway]] = 0
            self.H[gateway] = 0

        for v in self.Q:
            self.D[v] = self.Q[v]
            if len(self.D) == self.number_of_nodes():
                break
            #if v in self.cxns:
            neigh_v = self.neighbors(v)
            if len(neigh_v) >0:
                #for w in self.cxns[v]:
                for w in neigh_v:
                    #Check that it is connected
                    if self.cxns[v][w]:
                        gatewayLoad = self.L[self.G[v]]

                        cost = self.D[v] + self.cxns[v][w] # old method to calculate using D table.
                        cost = cost + self.loadFactor * (gatewayLoad) / (200.)
                        if w in self.D:
                            if cost < self.D[w]:
                                raise ValueError("Error Dijkstra: Better path to already-final vertex")
                        elif w not in self.Q or cost < self.Q[w]:
                            self.Q[w] = cost  # assign new cost to Q dictionary
                            self.P[w] = v  # assign predecessor for Parent

                            if self.G.get(w, -1) != -1:
                                self.L[self.G[w]] = self.L[self.G[w]] - 1

                            self.G[w] = self.G[v]  # assign same Gateway as Parent
                            self.L[self.G[v]] = self.L[self.G[v]] + 1  # increment Gateway load
                            self.H[w] = self.H[v] + 1  # assign true Hop Count as increment from Parent
def dijkstra_algorithm(graph,start,target=None):
	start_time = datetime.datetime.now()
	final_dist_dict = {} # a dictionary to  maintain final distances
	dict_predecessor = {} # a dictionary to maintain predessor of nodes  
	PD = priorityDictionary() # a priority dictionary using binary heaps
	PD[start] = 0 # initialize 0 to the start vertex

	for u in PD:
		final_dist_dict[u] = PD[u]
		if u == target: break
		for v in graph[u]:
			uvLength = final_dist_dict[u] + graph[u][v]['weight']
			if v in final_dist_dict:
				if uvLength < final_dist_dict[v]: # raise an error if a shorter path exist for an already finalized node
					raise ValueError 
			elif v not in PD or uvLength < PD[v]:
				PD[v] = uvLength
				dict_predecessor[v] = u
	end_time = datetime.datetime.now()
	t = (end_time - start_time)
	print "Time taken by dijkstra_algorithm in seconds is", t.total_seconds()
	return (final_dist_dict,dict_predecessor)