예제 #1
0
파일: network.py 프로젝트: gbrouwer/Tensor
def Dijkstra(G,start,end=None):


	#Init	
	D = {}	#Dctionary of final distances
	P = {}	#Dictionary of predecessors
	Q = priodict.priorityDictionary()
	Q[start] = 0



	#While there are elements in the priority queue	
	for v in Q:
		D[v] = Q[v]
		if v == end: 
			break
		for w in G[v]:
			vwLength = D[v] + G[v][w]
			if w in D:
				if vwLength < D[w]:
					raise ValueError
 			elif w not in Q or vwLength < Q[w]:
				Q[w] = vwLength
				P[w] = v
	
	return (D,Q,P)
예제 #2
0
def dijkstra(G, start, end=None):
    """
        Find shortest paths from the  start vertex to all vertices nearer than or equal to the end.
        For any vertex v, G[v] is itself a dictionary, indexed by the neighbors of v.  For any edge v->w, G[v][w] is the length of the edge.
        The output is a pair (D,P) where D[v] is the distance from start to v and P[v] is the predecessor of v along the shortest path from s to v.
        """

    D = {}  # dictionary of final distances.
    P = {}  # dictionary of predecessors.
    Q = priorityDictionary()  # estimated distances of non-final vertices.
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end: break
        for w in G[v]:
            vwLength = D[v] + G[v][w][0]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = (v, G[v][w][1])

    return (D, P)
예제 #3
0
	def __DijkstraFromSource(graph, sourceNode):
		utils.Utils.isStr(sourceNode)
		if sourceNode not in graph:
			raise ValueError('the source node is not in the graph')

		D = {}	# dictionary of final distances
		P = {}	# dictionary of predecessors
		Q = priorityDictionary()	# estimated distances of non-final vertices
		Q[sourceNode] = 0
	
		for anode in Q:
			D[anode] = Q[anode]
			graph[sourceNode].routingPath[anode]=node.Path([],0.0)
		
			for neighbor in graph[anode].connections:
				sourceToNeighborLenght = D[anode] + graph[anode].connections[neighbor]
				if neighbor in D:
					if sourceToNeighborLenght < D[neighbor]:
						raise ValueError, "Dijkstra: found better path to already-final vertex"
				elif neighbor not in Q or sourceToNeighborLenght < Q[neighbor]:
					Q[neighbor] = sourceToNeighborLenght
					P[neighbor] = anode
			graph[sourceNode].routingPath[anode].value = Q[anode]	
			end = anode;
			while 1:
				graph[sourceNode].routingPath[anode].path.append(end)
				if end == sourceNode: break
				end = P[end]				
			graph[sourceNode].routingPath[anode].path.reverse()
예제 #4
0
	def djikstraExtended(self,s,t):
		D = {}	# dictionary of final distances
		P = {}	# dictionary of predecessors
		Q = priorityDictionary()   # est.dist. of non-final vert.
		Q[s] = 0
		Path = []
		
		for v in Q:
			D[v] = Q[v]
			if v == t:
				while 1:
					if t == s: break
					Path.append(P[t][1])
					t = P[t][0]
				Path.reverse()
				return Path,Q
				
			
			for edgew in self.adj(v):
				w=edgew.v
				vwLength = D[v] + edgew.length
				if w in D:
					if vwLength < D[w]:
						raise ValueError, "Dijkstra: found better path to already-final vertex"
				elif w not in Q or vwLength < Q[w]:
					Q[w] = vwLength
					P[w] = [v, edgew]
		return Path,Q			
예제 #5
0
def astar(start, goal, data):
    closedset = set()
    openset = set()
    openset.add(start)

    came_from = {}

    g_score = {}
    g_score[start] = 0

    f_score = priodict.priorityDictionary()
    f_score[start] = g_score[start] + heuristic_cost_estimate(start, goal)

    while len(openset) != 0:
        current = f_score.smallest()
        del f_score[current]

        if current == goal:
            return reconstruct_path(came_from, goal)

        openset.remove(current)
        closedset.add(current)
        for neighbor in neighbor_nodes(current, data):
            tentative_g_score = g_score[current] + dist_between(current, neighbor)
            if neighbor in closedset and tentative_g_score >= g_score[neighbor]:
                continue

            if neighbor not in openset or tentative_g_score < g_score[neighbor]:
                came_from[neighbor] = current
                g_score[neighbor] = tentative_g_score
                f_score[neighbor] = g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
                if neighbor not in openset:
                    openset.add(neighbor)
    return None
def astar (start,end,forbidden):
  D = {}    # dictionary of final distances
  P = {}    # dictionary of predecessors
  Q = priorityDictionary()   # est.dist. of non-final vert.
  Q[start] = 0
  G={}
  for v in Q:
      D[v] = Q[v]
      if v == end: 
          break
      if not G.has_key(v):
          values= state_trans_fuction(v,Ux,sX)
          hold_v = copy.deepcopy(values)
          for x in values:
              if G.has_key(x) :  
                  #hold_v.remove(x)
                  continue
              if x in forbidden:
                   hold_v.remove(x)
                   print x,"forbidden"
              #if x in hold_v and x in forbidden:
               #   hold_v.remove(x)
          print hold_v
          G[v]=hold_v
          

          for w in G[v]:
              if w in forbidden:
                  pass
              vwLength = D[v] + 1 + abs((end[0]-w[0]))+abs((end[1]-w[1]))
              if w not in D or vwLength < D[w]:
                  Q[w] = vwLength
                  P[w] = v
  
  return (D,P)
예제 #7
0
def dijkstra(G,start,end=None):
    """
    Taken from http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/
    """

    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()   # est.dist. of non-final vert.
    Q[start] = 0
    
    for v in Q:
        D[v] = Q[v]
        if v == end: break
        
        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v
    
    return (D,P)
def Dijkstra(graph, start, end=None):
    final_distances = {}    # dictionary of final distances
    predecessors = {}   
    estimated_distances = priorityDictionary()   # heap binary tree initialization
    
    estimated_distances[start] = 0
   
    oneHop = False
    hops = 0
    
    for vertex in estimated_distances:
        final_distances[vertex] = estimated_distances[vertex]
        if (vertex == end and oneHop): 
            break
        oneHop = True
        hops  = hops+1
        
        for edge in graph[vertex]:
            path_distance = final_distances[vertex] + graph[vertex][edge]
            if edge in final_distances:
                if path_distance < final_distances[edge]:
                    raise ValueError, \
                        "Dijkstra: found better path to already-final vertex"
                else:
                    if (final_distances[edge] == 0): #scenario node - node
                        final_distances[edge] = path_distance
            if edge not in estimated_distances or path_distance < estimated_distances[edge]:
                estimated_distances[edge] = path_distance
                predecessors[edge] = vertex

    return (final_distances,predecessors)
예제 #9
0
def dijkstra(map, start, end):
    if start == end:
        return 0
    if not (map.has_key(start)) or not (map.has_key(end)):
        return -1

    distances = {}
    nodes = {}
    prelim_distances = priorityDictionary()
    prelim_distances[start] = 0
    for vertex in prelim_distances:
        distances[vertex] = prelim_distances[vertex]
        if vertex == end:
            break

        for edge in map[vertex]:
            dist = distances[vertex] + map[vertex][edge]
            if edge in distances:
                if dist < distances[edge]:
                    return Error
            elif edge not in prelim_distances or dist < prelim_distances[edge]:
                prelim_distances[edge] = dist
                nodes[edge] = vertex

    return (distances, nodes)
예제 #10
0
파일: graph.py 프로젝트: ckmetto/iw
    def shortest_path(self, start, end, parameter):
        """
        Find a single shortest path from the given start vertex
        to the given end vertex using Dijkstra's algorithm.
        Operates on this Network's graph, constructed using getGraph()
        parameter = one of ["latency" | "cost"]
        """

        # Run Dijkstra.
        D = {}  # dictionary of final distances
        P = {}  # dictionary of predecessors
        E = {}
        Q = priorityDictionary()  # est.dist. of non-final vert.
        Q[start] = 0

        for v in Q:
            D[v] = Q[v]
            if v == end:
                break

            edges = self.get_edges(v)
            for edge in edges:
                w = edge.sink
                if parameter == "latency":
                    vwLength = D[v] + edge.latency
                elif parameter == "cost":
                    vwLength = D[v] + edge.cost
                if w in D:
                    if vwLength < D[w]:
                        raise ValueError("Dijkstra: found better path to already-final vertex")
                elif w not in Q or vwLength < Q[w]:
                    Q[w] = vwLength
                    P[w] = v
                    E[w] = edge

        # Get the shortest path.
        Path = []
        walker = end
        while 1:
            Path.append(walker)
            if walker == start: break
            try:
                walker = P[walker]
            except KeyError:
                return None
        Path.reverse()

        # check sanity.
        if Path[0] != start:
            dprint("Path[0] is %s, start is %s", (Path[0], start))
            raise InvalidPath
        if Path[-1] != end:
            dprint("Path[-1] is %s, end is %s", (Path[-1], end))
            raise InvalidPath

        # return Path
        E[start] = None
        return [E[s] for s in Path]
예제 #11
0
def Dijkstra(G,start,end=None,option="length"):
    """
    Modified from David Eppstein's codes.
    G: graph object form snap.py
    start: nid of the start node (int)
    end: nid of the end node (int)
    option can be "length", "time" and "prob"
    
    Find shortest paths from the start vertex to all
    vertices nearer than or equal to the end.

    The output is a pair (D,P) where D[v] is the distance
    from start to v and P[v] is the predecessor of v along
    the shortest path from s to v.
    
    Dijkstra's algorithm is only guaranteed to work correctly
    when all edge lengths are positive. This code does not
    verify this property for all edges (only the edges seen
    before the end vertex is reached), but will correctly
    compute shortest paths even for some graphs with negative
    edges, and will raise an exception if it discovers that
    a negative edge has caused it to make a mistake.
    """

    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()   # est.dist. of non-final vert.
    Q[start] = 0
    
    for v in Q:
        D[v] = Q[v]
        if v == end: break
        NI = G.GetNI(v)
        for Id in NI.GetOutEdges():#w in G[v]:
            eid = G.GetEId(v, Id)
            #if eid == -1: raise
            if option == "length":
                edgeweight = 1
            elif option == "time":
                edgeweight = G.GetIntAttrDatE(eid, "AveTime")
            elif option == "prob":
                prob = G.GetFltAttrDatE(eid, "Prob")
                if prob <= 0.001:
                    prob = 0.001
                edgeweight = -  log(prob)
            vwLength = D[v] +  edgeweight#G[v][w]
            if Id in D:
                if vwLength < D[Id]:
                    raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
            elif Id not in Q or vwLength < Q[Id]:
                Q[Id] = vwLength
                P[Id] = v

    return (D,P)
예제 #12
0
def Dijkstra(G,start,end=None):
	"""
	Find shortest paths from the  start vertex to all vertices nearer than or equal to the end.

	The input graph G is assumed to have the following representation:
	A vertex can be any object that can be used as an index into a dictionary.
	G is a dictionary, indexed by vertices.  For any vertex v, G[v] is itself a dictionary,
	indexed by the neighbors of v.  For any edge v->w, G[v][w] is the length of the edge.
	This is related to the representation in <http://www.python.org/doc/essays/graphs.html>
	where Guido van Rossum suggests representing graphs as dictionaries mapping vertices
	to lists of outgoing edges, however dictionaries of edges have many advantages over lists:
	they can store extra information (here, the lengths), they support fast existence tests,
	and they allow easy modification of the graph structure by edge insertion and removal.
	Such modifications are not needed here but are important in many other graph algorithms.
	Since dictionaries obey iterator protocol, a graph represented as described here could
	be handed without modification to an algorithm expecting Guido's graph representation.

	Of course, G and G[v] need not be actual Python dict objects, they can be any other
	type of object that obeys dict protocol, for instance one could use a wrapper in which vertices
	are URLs of web pages and a call to G[v] loads the web page and finds its outgoing links.
	
	The output is a pair (D,P) where D[v] is the distance from start to v and P[v] is the
	predecessor of v along the shortest path from s to v.
	
	Dijkstra's algorithm is only guaranteed to work correctly when all edge lengths are positive.
	This code does not verify this property for all edges (only the edges examined until the end
	vertex is reached), but will correctly compute shortest paths even for some graphs with negative
	edges, and will raise an exception if it discovers that a negative edge has caused it to make a mistake.
	"""

	D = {}	# dictionary of final distances
	P = {}	# dictionary of predecessors
	Q = priorityDictionary()	# estimated distances of non-final vertices
	Q[start] = 0
	
	for v in Q:
		D[v] = Q[v]
		if v == end: break		
		try:		    	
		    for w in G[v]:
                        if w == None:
                                print v, G[v]
                                assert(0)
			vwLength = D[v] + G[v][w]
			if w in D:
				if vwLength < D[w]:
					raise ValueError, "Dijkstra: found better path to already-final vertex"
			elif w not in Q or vwLength < Q[w]:
				Q[w] = vwLength
				P[w] = v
	 	except: 
			#print len(Q), v, D[v]	    
			pass
	
	return (D,P)
예제 #13
0
def compute_optimal_steiner(G, in_arcs, out_arcs, U, max_dim):

    M = {}
    W = []
    W_it = powerset(U)
    
    for el in W_it:
        W.append(set(el))

    #Each subset is identified by its index in W
    for i in range(G.shape[0]):
        for w in W:
            M[(i, W.index(w))] = float('inf')
        M[(i, W.index(set([])))] = 0
    
    for u in U:
           M[(u, W.index(set([u])))] = 0

    for w in W:
        Q_w = priorityDictionary()
        for i in range(G.shape[0]):
            M[(i, W.index(w))] = min(map(lambda x: M[(i, W.index(set(x)))] + M[(i, W.index(w - set(x)))], powerset(w)))

            Q_w[i] = M[(i, W.index(w))]

        for v_min in Q_w:
            for arc in in_arcs[v_min]:
                v = arc[0]
                if(M[(v, W.index(w))] > M[(v_min, W.index(w))] + 1):
                    M[(v, W.index(w))] = M[(v_min, W.index(w))] + 1
                    Q_w[v] = M[(v, W.index(w))]

        #I can prune the search here if, for a given subset, the size of the minimum tree exceeds max_dim
        below_bound = False
        for i in range(G.shape[0]):
            if(M[(i, W.index(w))] <= max_dim):
                below_bound = True
                break

        if(not(below_bound)):
            return []

    root = 0
    for v in range(1, G.shape[0]):
        if (M[(v, W.index(set(U)))] < M[(root, W.index(set(U)))]):
          root = v


    solution = construct_tree(root, M, out_arcs, W, set(U))
    #print "W:"
    #print W
    #print "M:"
    #print M
    return solution
예제 #14
0
    def _shortest_path_to(self, dest):
        """Returns the next exit to the shortest path from self to dest
        and the distance of the shortest path from self to dest."""
        # TODO: remove the duplicate exits in the graph
        if dest is self:
            return None, 0
        ##        if not dest.exits: # small optimization
        ##            return None, None # no path exists

        # add start and end to the graph
        G = self.world.g
        for v in (self, dest):
            G[v] = {}
            for e in v.exits:
                G[v][e] = G[e][v] = int_distance(v.x, v.y, e.x, e.y)
        start = self
        end = dest

        # apply Dijkstra's algorithm (with priority list)
        D = {}  # dictionary of final distances
        P = {}  # dictionary of predecessors
        Q = priorityDictionary()  # est.dist. of non-final vert.
        Q[start] = (0,)

        for v in Q:
            D[v] = Q[v][0]
            if v == end:
                break

            for w in G[v]:
                vwLength = D[v] + G[v][w]
                if w in D:
                    pass
                elif w not in Q or vwLength < Q[w][0]:
                    Q[w] = (vwLength, int(w.id))  # the additional value makes the result "cross-machine deterministic"
                    P[w] = v

        # restore the graph
        for v in (start, end):
            del G[v]
            for e in v.exits:
                del G[e][v]

        # exploit the results
        if end not in P:
            return None, None  # no path exists
        Path = []
        while 1:
            Path.append(end)
            if end == start:
                break
            end = P[end]
        Path.reverse()
        return Path[1], D[dest]
예제 #15
0
def Dijkstra(G,start,end=None):
    """
    Find shortest paths from the  start vertex to all vertices nearer than or equal to the end.

    The input graph G is assumed to have the following representation:
    A vertex can be any object that can be used as an index into a dictionary.
    G is a dictionary, indexed by vertices.  For any vertex v, G[v] is itself a dictionary,
    indexed by the neighbors of v.  For any edge v->w, G[v][w] is the length of the edge.
    This is related to the representation in <http://www.python.org/doc/essays/graphs.html>
    where Guido van Rossum suggests representing graphs as dictionaries mapping vertices
    to lists of outgoing edges, however dictionaries of edges have many advantages over lists:
    they can store extra information (here, the lengths), they support fast existence tests,
    and they allow easy modification of the graph structure by edge insertion and removal.
    Such modifications are not needed here but are important in many other graph algorithms.
    Since dictionaries obey iterator protocol, a graph represented as described here could
    be handed without modification to an algorithm expecting Guido's graph representation.

    Of course, G and G[v] need not be actual Python dict objects, they can be any other
    type of object that obeys dict protocol, for instance one could use a wrapper in which vertices
    are URLs of web pages and a call to G[v] loads the web page and finds its outgoing links.

    The output is a pair (D,P) where D[v] is the distance from start to v and P[v] is the
    predecessor of v along the shortest path from s to v.

    Dijkstra's algorithm is only guaranteed to work correctly when all edge lengths are positive.
    This code does not verify this property for all edges (only the edges examined until the end
    vertex is reached), but will correctly compute shortest paths even for some graphs with negative
    edges, and will raise an exception if it discovers that a negative edge has caused it to make a mistake.
    """

    D = {} # dictionary of final distances
    P = {} # dictionary of predecessors
    Q = priorityDictionary() # estimated distances of non-final vertices
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end:
            break
        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D,P)
예제 #16
0
def MST_Kruskal(graph):
  """
  An implementation of Kruskal and Prim's Minimum Spanning tree algorithm - implemented after the description in Cormen
  """

  result_graph = {}
  set_container = {}

  # Initially the nodes only know themselves
  for node in graph:
    set_container[node] = [node]
    result_graph[node] = {}
  
  # we make the priority queue
  Q = priorityDictionary()

  # We make sure the edge is only there once
  already_there = {}
  for outer_node in graph:
    inner_nodes = graph[outer_node]
    
    for inner_node in inner_nodes:
      if already_there.get((inner_node, outer_node)):
        continue
     
      already_there[(inner_node, outer_node)] = 1
      already_there[(outer_node, inner_node)] = 1

      Q[(outer_node, inner_node)] = inner_nodes[inner_node]
          
  for edge in Q:
    p1 = edge[0]
    p2 = edge[1]
    
    if set_container[p1] != set_container[p2]:
      result_graph[p1][p2] = Q[edge]
      result_graph[p2][p1] = Q[edge]
      
      set1 = set_container[p1]
      set2 = set_container[p2]

      for node in set2:
        if not node in set1:
          set1.append(node)

      for node in set1:
        set_container[node] = set1

  return (result_graph, set_container) 
예제 #17
0
def MST_Kruskal(graph):
    """
  An implementation of Kruskal and Prim's Minimum Spanning tree algorithm - implemented after the description in Cormen
  """

    result_graph = {}
    set_container = {}

    # Initially the nodes only know themselves
    for node in graph:
        set_container[node] = [node]
        result_graph[node] = {}

    # we make the priority queue
    Q = priorityDictionary()

    # We make sure the edge is only there once
    already_there = {}
    for outer_node in graph:
        inner_nodes = graph[outer_node]

        for inner_node in inner_nodes:
            if already_there.get((inner_node, outer_node)):
                continue

            already_there[(inner_node, outer_node)] = 1
            already_there[(outer_node, inner_node)] = 1

            Q[(outer_node, inner_node)] = inner_nodes[inner_node]

    for edge in Q:
        p1 = edge[0]
        p2 = edge[1]

        if set_container[p1] != set_container[p2]:
            result_graph[p1][p2] = Q[edge]
            result_graph[p2][p1] = Q[edge]

            set1 = set_container[p1]
            set2 = set_container[p2]

            for node in set2:
                if not node in set1:
                    set1.append(node)

            for node in set1:
                set_container[node] = set1

    return (result_graph, set_container)
예제 #18
0
 def priorityGraph(self, graph=None):
     """
     return a priority graph. Each sub dictionnary of the graph is a
     priority dictionnary as defined in priorityDictionary
     """
     if graph == None:
         G = self.graph
     else:
         G = graph
     Gp = {}
     for n1 in G.keys():
         d = priorityDictionary()
         for n2 in G[n1].keys():
             d[n2] = G[n1][n2]
         Gp[n1] = d
     return Gp
예제 #19
0
 def priorityGraph(self, graph=None):
     """
     return a priority graph. Each sub dictionnary of the graph is a
     priority dictionnary as defined in priorityDictionary
     """
     if graph == None:
         G = self.graph
     else:
         G = graph
     Gp = {}
     for n1 in G.keys():
         d = priorityDictionary()
         for n2 in G[n1].keys():
             d[n2] = G[n1][n2]
         Gp[n1] = d
     return Gp
예제 #20
0
 def __init__(self,start,goal,state_trans,forbidden=set()):
     self.OPEN = priorityDictionary()
     self.INCONS= set() 
     self.CLOSED = set()
     self.s_start = State(start,start,goal)
     self.s_start.set_rhs(constants.INF)
     self.s_start.set_g(constants.INF)
     self.s_goal = State(goal,start,goal)
     self.s_goal.set_g(constants.INF)
     self.s_goal.set_rhs(0)
     self.eps = 2.5 # TODO make it change during iterations. 
     self.PREC = {}
     self.G = {} 
     self.path_= {}
     self.G[self.s_start] = self.s_start
     self.forbidden = forbidden#TODO add "obstacles" 
     self.OPEN[self.s_goal] = self.keys(self.s_goal)
     self.state_trans= state_trans
예제 #21
0
def Dijkstra(G, start, end=None):
    D = {}
    P = {}
    Q = priorityDictionary()
    Q[start] = G[start[0]][start[1]][0]
    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for w in G[v[0]][v[1]][1]:
            vwLength = D[v] + G[w[0]][w[1]][0]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return D[v]
예제 #22
0
def dijkstra(G, source):
    Q = priorityDictionary()
    prev = {}
    distances = {}
    Q[source] = 0.0
    for v in Q:
        distances[v] = Q[v]
        if v == None:
            break
        for w in G.matrix[v]:
            temp = distances[v] + G.matrix[v][w]
            if w in distances:
                if temp < distances[w]:
                    raise ValueError("Error")
            elif w not in Q or temp < Q[w]:
                Q[w] = temp
                prev[w] = v

    return (distances, prev)
예제 #23
0
def Dijkstra(G, start, end=None):
    """Find shortest paths from the  start vertex to all vertices nearer than or equal to the end.

    The input graph G is assumed to have the following representation:
    A vertex can be any object that can be used as an index into a dictionary.
    G is a dictionary, indexed by vertices. For any vertex v, G[v] is itself a dictionary, indexed by
    the neighbors of v. For any edge v->w, G[v][w] is the length of the edge.

    Of course, G and G[v] need not be actual Python dict objects, they can be any other type of object
    that obeys dict protocol, for instance one could use a wrapper in which vertices are URLs of web pages
    and a call to G[v] loads the web page and finds its outgoing links.

    The output is a pair (D, P) where D[v] is the distance from start to v and P[v] is the predecessor
    of v along the shortest path from s to v.

    Dijkstra's algorithm is only guaranteed to work correctly when all edge lengths are positive.
    This code does not verify this property for all edges (only the edges examined until the end vertex
    is reached), but will correctly compute shortest paths even for some graphs with negative edges, and
    will raise an exception if it discovers that a negative edge has caused it to make a mistake.
    """
    D = {}  # dictionary of real shortest distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary(
    )  # estimated distances from "unsettled" or "unprocessed" vertices
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end:
            break

        for w in G[v]:
            vw_dist = D[v] + G[v][w]
            if w in D:
                if vw_dist < D[w]:
                    raise ValueError(
                        "Dijkstra: found better path to already-processed vertex"
                    )
            elif w not in Q or vw_dist < Q[w]:
                Q[w] = vw_dist
                P[w] = v

    return D, P
예제 #24
0
def Dijkstra(G, start, end=None):
    D = {}
    P = {}
    Q = priorityDictionary()
    Q[start] = 0
    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D, P)
예제 #25
0
def Dijkstra(G,s,e=None):
    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()  # estimated distances of non-final vertices
    Q[s] = 0

    for v in Q:
        D[v] = Q[v]
        if v == e: break
        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D, P)
예제 #26
0
def Dijkstra(G,start,end=None):
        D = {}
        P = {}
        Q = priorityDictionary()
        Q[start] = G[start[0]][start[1]][0]
        for v in Q:
            D[v] = Q[v]
            if v == end: break

            for w in G[v[0]][v[1]][1]:
                vwLength = D[v] + G[w[0]][w[1]][0]
                if w in D:
                    if vwLength < D[w]:
                        raise ValueError, "Dijkstra: found better path to already-final vertex"
                elif w not in Q or vwLength < Q[w]:
                    Q[w] = vwLength
                    P[w] = v

        return D[v]
def Dijkstra(G,start,end=None):
	D = {}	# dictionary of final distances
	P = {}	# dictionary of predecessors
	Q = priorityDictionary()   # est.dist. of non-final vert.
	Q[start] = 0
	
	for v in Q:
		D[v] = Q[v]
		if v == end: break
		print(G)
		for w in G[v]:
			vwLength = D[v] + G[v][w]
			if w in D:
				if vwLength < D[w]:
					print("valueerror")
			elif w not in Q or vwLength < Q[w]:
				Q[w] = vwLength
				P[w] = v
	
	return (D,P)
예제 #28
0
def Dijkstra(G,start,end=None):
  D = {}  # dictionary of final distances
  P = {}  # dictionary of predecessors
  Q = priorityDictionary()   # est.dist. of non-final vert.
  Q[start] = 0
  
  for v in Q:
    D[v] = Q[v]
    if v == end: break
    
    for w in G[v]:
      vwLength = D[v] + G[v][w]
      if w in D:
        if vwLength < D[w]:
          raise ValueError, "Dijkstra: found better path to already-final vertex"
      elif w not in Q or vwLength < Q[w]:
        Q[w] = vwLength
        P[w] = v
  
  return (D,P)
예제 #29
0
def djsktra(G, start, end=None):
    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v
    return (D, P)
예제 #30
0
파일: route.py 프로젝트: zl2/pygeolib
def route(G,starts,ends=[]):
  """
  Build the shortest path tree in G from starts. Stops when any vertex in 'ends' is met. Uses Dijkstra's algorithm.

  @param G: graph
  @param starts: singleton or list of starting node ids
  @param ends: singleton or list of ending node ids

  @returns: a tuple (D,P,v)
         D: dictionary of distances (from starts)
         P: dictionary of predecessors (encoding the tree)
         v: final vertex (useful to determine which vertex of ends we reached)

  Code modified from the PADS library.
  http://www.ics.uci.edu/~eppstein/PADS/
  """
  D = {}	# dictionary of final distances
  P = {}	# dictionary of predecessors
  Q = priorityDictionary()
  if type(starts)==list:
    for s in starts:
      Q[s]=0
  else:
    Q[starts]=0

  if not type(ends)==list: ends=[ends]

  for v in Q:
    D[v] = Q[v]
    if v in ends: break

    for w in G[v]:
      vwLength = D[v] + G[v][w]
      if w in D:
        if vwLength < D[w]:
          print v,w,vwLength,D[v],G[v][w],D[w]
          raise ValueError, "Dijkstra: found better path to already-final vertex"
      elif w not in Q or vwLength < Q[w]:
        Q[w] = vwLength
        P[w] = v
  return (D,P,v)
예제 #31
0
def Dijkstra(Graph,ilk,son=None):
    
    uzaklik={}#son uzakliklari tutacak
    gelis={}#noktaya gelinen yeri tutar.
    nodedist = priorityDictionary()#nodların yaklasik hesaplamasini tutar
    
    nodedist[ilk]=0
    for node in nodedist:
        uzaklik[node] = nodedist[node]
        if node == son:
            break
        for node2 in Graph[node][1]:
            sonrasininUzakligi = uzaklik[node] + Graph[node][1][node2]
            if node2 in uzaklik:
                if sonrasininUzakligi < uzaklik[node2]:
                    raise ValueError 
            elif node2 not in nodedist or sonrasininUzakligi < nodedist[node2]:
                nodedist[node2] = sonrasininUzakligi
                gelis[node2] = node 
    
    return (uzaklik,gelis)
예제 #32
0
def Dijkstra(graph, start_node, end_node=None):
    distances = {}  # dictionary of final distances
    predecessors = {}  # dictionary of predecessors
    priority_dictionary = priorityDictionary(
    )  # estimated distances of non-final vertices
    priority_dictionary[start_node] = 0.0
    for vertex in priority_dictionary:
        distances[vertex] = priority_dictionary[vertex]
        if vertex == end_node:
            break
        for w in graph[vertex]:
            vwLength = float(
                format(distances[vertex] + graph[vertex][w], ".2f"))
            if w in distances:
                if vwLength < distances[w]:
                    pass
            elif w not in priority_dictionary or vwLength < priority_dictionary[
                    w]:
                priority_dictionary[w] = vwLength
                predecessors[w] = vertex
    return (distances, predecessors)
예제 #33
0
def Dijkstra(g,start,end=None):

    d = {}  # dictionary of final distances
    p = {}  # dictionary of predecessors
    q = priorityDictionary()   # est.dist. of non-final vert.
    q[start] = 0
    for v in q:
        d[v] = q[v]
        if v == end: break      
        for w in g[v]:
            vwLength = d[v] + g[v][w]
            if w in d:
                if vwLength < d[w]:
                    raise ValueError
            elif w not in q or vwLength < q[w]:
                q[w] = vwLength
                p[w] = [v]
            elif  w not in q or vwLength == q[w]:
                q[w] = vwLength
                p[w] += [v] 
    return (d,p)        
예제 #34
0
    def Dijkstra(self, start, end=None):

        D = {}  # dictionary of final distances
        P = {}  # dictionary of predecessors
        Q = priorityDictionary()  # est.dist. of non-final vert.
        Q[start] = 0
        for v in Q:
            D[v] = Q[v]
            if v == end:
                break
            for w in self.neighbours[v]:
                vwLength = D[v] + self.edge_weights[(v, w)]
                if w in D:
                    if vwLength < D[w]:
                        error = "found, better path to already-final vertex"
                        raise ValueError, error
                elif w not in Q or vwLength < Q[w]:
                    Q[w] = vwLength
                    P[w] = v

        return (D, P)
예제 #35
0
def Dijkstra(G,start,end=None):#TODO test that if there is no way between start and end???
    
    D = {}	# dictionary of final distances
    P = {}	# dictionary of predecessors
    Q = priorityDictionary()   # est.dist. of non-final vert.
    Q[start] = 0
	
    for v in Q:
        D[v] = Q[v]
        if v == end: break
		
        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v
	
    return (D,P)
예제 #36
0
def shortestPaths(nodes, edges, startDist, initialNodes):
  '''simple shortest paths algorithm, using advanced data structure. calculates
  all distances from the starting set of nodes to all other nodes. returns 
  nodes to distance mapping. nodes is a list, edges is a dict from nodes to 
  other nodes with distance as a tuple, startdist is a float, initialnodes 
  is a list.'''
  currentNodes = priorityDictionary() #holds data on nodes left to process
  nodeDist = {}
  for initialNode in initialNodes:
    currentNodes[initialNode] = startDist
  while len(currentNodes) > 0:
    currentNode = currentNodes.smallest()
    lastDist = currentNodes.pop(currentNodes.smallest())
    if currentNode not in nodeDist or lastDist < nodeDist[currentNode]:
      #update the dist, add neighbors to heap
      nodeDist[currentNode] = lastDist
      for neighborNode,nbDist in edges[currentNode]:
        newDist = lastDist + nbDist
        if neighborNode not in currentNodes or \
                     newDist <= currentNodes[neighborNode]:
          currentNodes[neighborNode] = newDist #updates prio dict
  return nodeDist
예제 #37
0
def Dijkstra(G, start, end=None):

    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()  # est.dist. of non-final vert.
    Q[start] = 0
    n = len(G)

    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for w in range(n):
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D, P)
예제 #38
0
def Dijkstra(G,start,end=None):
	D = {}	# dictionary of final distances
	P = {}	# dictionary of predecessors
	Q = priorityDictionary()   # est.dist. of non-final vert.
	Q[start] = 0

	vwLength=0
	for v in Q:
		D[v] = Q[v]
		if v == end: break
		for w in G[v]:
			vwLength = D[v] + G[v][w]
			if w in D:
				if vwLength < D[w]:
					print("wielgachny wynik")
					raise ValueError
			elif w not in Q or vwLength < Q[w]:
				Q[w] = vwLength
				P[w] = v
#	print('Dijkstra:')
#	print(D)
	return D,P
예제 #39
0
def UnitDijkstra(G,start,end=None):
  """
  Same as the other, now just with unit distances
  """
  D = {}	# dictionary of final distances
  P = {}	# dictionary of predecessors
  Q = priorityDictionary()   # est.dist. of non-final vert.
  Q[start] = 0
	
  for v in Q:
    D[v] = Q[v]
    if v == end: break
		
    for w in G[v]:
      vwLength = D[v] + 1
      if w in D:
        if vwLength < D[w]:
	  raise ValueError, "Dijkstra: found better path to already-final vertex"
      elif w not in Q or vwLength < Q[w]: 
        Q[w] = vwLength
	P[w] = v

  return (D,P)
예제 #40
0
def Dijkstra(graph,start,end=None):
	

	final_distances = {}	# dictionary of final distances
	predecessors = {}	# dictionary of predecessors
	estimated_distances = priorityDictionary()   # est.dist. of non-final vert.
	estimated_distances[start] = 0

	for vertex in estimated_distances:
		final_distances[vertex] = estimated_distances[vertex]
		if vertex == end: break

		for edge in graph[vertex]:
			path_distance = final_distances[vertex] + graph[vertex][edge]
			if edge in final_distances:
				if path_distance < final_distances[edge]:
					raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
			elif edge not in estimated_distances or path_distance < estimated_distances[edge]:
				estimated_distances[edge] = path_distance
				predecessors[edge] = vertex

	return (final_distances,predecessors)
예제 #41
0
def Dijkstra(G, start, end=None):
    min_max = float('inf')
    for s in start:
        D = {}
        P = {}
        Q = priorityDictionary()
        Q[s] = G[s[0]][s[1]][0]
        for v in Q:
            D[v] = Q[v]
            if v in end: break

            for w in G[v[0]][v[1]][1]:
                vwLength = D[v] + G[w[0]][w[1]][0]
                if w in D:
                    if vwLength < D[w]:
                        raise ValueError, "Dijkstra: found better path to already-final vertex"
                elif w not in Q or vwLength < Q[w]:
                    Q[w] = vwLength
                    P[w] = v

        if D[v] < min_max:
            min_max = D[v]
    return min_max
예제 #42
0
파일: dijkstra.py 프로젝트: NeshMx/Dijkstra
def Dijkstra(G, start, end=None):
    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()  # estimated distances of non-final vertices
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end:
            break

        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError(
                        "Dijkstra: se encotro un mejor camino al nodo destino."
                    )
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D, P)
예제 #43
0
def Dijkstra(G, start, end=None):

    #Init
    D = {}  #Dctionary of final distances
    P = {}  #Dictionary of predecessors
    Q = priodict.priorityDictionary()
    Q[start] = 0

    #While there are elements in the priority queue
    for v in Q:
        D[v] = Q[v]
        if v == end:
            break
        for w in G[v]:
            vwLength = D[v] + G[v][w]
            if w in D:
                if vwLength < D[w]:
                    raise ValueError
            elif w not in Q or vwLength < Q[w]:
                Q[w] = vwLength
                P[w] = v

    return (D, P)
예제 #44
0
def Dijkstra(G,start,end=None):
    # dict of final distances
    D = {}
    # dict of predecessors
    P = {}
    # estimated distances of non-final vertices
    estimates = priorityDictionary()
    estimates[start] = 0

    for vert in estimates:
        D[vert] = estimates[vert]
        if vert == end:
            break
        for w in G[vert]:
            length = D[vert] + G[vert][w]
            if w in D:
                if length < D[w]:
                    raise ValueError("Dijkstra: found better path to already-final vertex")
            elif w not in estimates or length < estimates[w]:
                estimates[w] = length
                P[w] = vert

    return (D,P)
예제 #45
0
def Dijkstra(G,start,end=None):
    min_max = float('inf')
    for s in start:
        D = {}
        P = {}
        Q = priorityDictionary()
        Q[s] = G[s[0]][s[1]][0]
        for v in Q:
            D[v] = Q[v]
            if v in end: break
            
            for w in G[v[0]][v[1]][1]:
                vwLength = D[v] + G[w[0]][w[1]][0]
                if w in D:
                    if vwLength < D[w]:
                        raise ValueError, "Dijkstra: found better path to already-final vertex"
                elif w not in Q or vwLength < Q[w]:
                    Q[w] = vwLength
                    P[w] = v

        if D[v] < min_max:
            min_max = D[v]
    return min_max
예제 #46
0
def dijkstra(map, start, end):
    if start == end:
        return 0
    if not (map.has_key(start)) or not (map.has_key(end)):
        return -1

    distances = {}
    nodes = {}
    prelim_distances = priorityDictionary()
    prelim_distances[start] = 0
    for vertex in prelim_distances:
        distances[vertex] = prelim_distances[vertex]
        if vertex == end: break

        for edge in map[vertex]:
            dist = distances[vertex] + map[vertex][edge]
            if edge in distances:
                if dist < distances[edge]:
                    return Error
            elif edge not in prelim_distances or dist < prelim_distances[edge]:
                prelim_distances[edge] = dist
                nodes[edge] = vertex

    return (distances, nodes)
예제 #47
0
	def correctgraph(self,conn):
		cur=conn.cursor()
		nodeDict=priorityDictionary()
		for node in self.edges:
			if len(self.edges[node])<=2:
				nodeDict[node]=min(map(lambda x:x.sectId, self.edges[node]))
		
		print len(nodeDict)
		count=0
		visited=[]
		for node in nodeDict:
			count+=1
			#~ print count
			cur.execute("update network_dumppoints_copy set split_id=%s \
				where st_startpoint(geomline)='%s' or st_endpoint(geomline)='%s'" % (nodeDict[node],node.geom,node.geom))			
			for nbr in self.adj(node):
				if nbr.u==node:
					if nbr.v not in visited and nbr.v in nodeDict:
						nodeDict[nbr.v]=nodeDict[node]
				else:
					if nbr.u not in visited and nbr.u in nodeDict:
						nodeDict[nbr.u]=nodeDict[node]
			visited.append(node)				
		conn.commit()	
예제 #48
0
def Dijkstra(graph,start,end=None):
	distances = {}	# dictionary of final distances
	predec = {}	# dictionary of predecessors
	queue = priorityDictionary()   # est.dist. of non-final vert.
	queue[start] = 0
def shortest_path_from_pts_to_intensity_dijk(point_0_s, point_1_int, dat, dist_func=lambda x:1.0/x, min_thr=0, max_thr=None, dist_diff=None, thr_distance=100000, zero_distance=None):
    '''
    find shortest path between point_0_s and points having intensity point_1_int in a 3d array dat.
    shortest_path_serial_dijk(point_0_s, point_1_int, dat, dist_func=lambda x:1.0/x, min_thr=0, max_thr=None, dist_diff=None, thr_distance=100000, zero_distance=None):
      point_0_s: list of starting points [(x, y, z), ...]
      point_1_int: ending point intensity intensity
      dat: numpy 3d array
      dist_func = lambda x:1.0/x: distance between a and b := euclidean_distance(a, b) * dist_func(b)
      min_thr = 0: ignore voxels having intensity < min_thr
      max_thr = 0: ignore voxels having intensity > max_thr
      dist_diff = None: if not None, distance between a and b := euclidean_distance(a, b) * dist_diff(a, b)
      thr_distance = 100000: the distance from/to the thresholded intensity voxels
      zero_distance = None: if not None, voxels having intensity zero_distance having distance 0 to their neighbors
    '''
    nx, ny, nz = dat.shape
    d1 = 1.0
    d2 = np.sqrt(2)
    d3 = np.sqrt(3)
    nbd_sets = [ (nbd_d1, d1), (nbd_d2, d2), (nbd_d3, d3) ]
    
    D = {}	# dictionary of final distances
    P = {}	# dictionary of predecessors
    Q = priorityDictionary()   # est.dist. of non-final vert.
    for point_0 in point_0_s:
        Q[point_0] = 0

    for v in Q:
        D[v] = Q[v]
        if dat[v] == point_1_int:
            point_1 = v
            break

        for nbd_d, dist_base in nbd_sets:
            for nbd in get_nbds_bdchk(v, nbd_d, nx, ny, nz):
                if dat[nbd] == point_1_int:
                    #print 'found point_2_int=%s' % point_1_int
                    #print len(Q)
                    distance = 0.01
                elif (zero_distance is not None and dat[nbd] == zero_distance):
                    distance = 0.01
                elif (min_thr is not None and dat[nbd] <= min_thr) or (max_thr is not None and max_thr <= dat[nbd]):
                    distance = thr_distance
                elif dist_diff is not None:
                    distance = dist_base * dist_diff(dat[nbd], dat[v])
                else:
                    distance = dist_base * dist_func(dat[nbd])
                if distance < 0.0:
                    print 'distance < 0'
                    return []

                vwLength = D[v] + distance
                
                if nbd in D:
                    if vwLength < D[nbd]:
                        raise ValueError, "Dijkstra: found better path to already-final vertex"
                elif nbd not in Q or vwLength < Q[nbd]:
                #if nbd not in Q or vwLength < Q[nbd]:
                    Q[nbd] = vwLength
                    P[nbd] = v
    Path = []
    end = point_1
    start = point_0_s
    while 1:
        Path.append(end)
        if end in start: break
        end = P[end]
        if len(Path) > 40:
            break
    Path.reverse()
    return Path
def a_star(position,angle, map, previous_start, previous_policy, depth=float('inf')):
	# start:(int, int),map:MATRIX, 
	# goals:coordinates of all goals
	#position of current position(start), and apple (goal)
	
	#TODO: 
	#	Check if the action of start state has been calculated

	# return 0
	
	
	goals_location = helper.object_position(Constants.GOAL_TYPE)
	goals_states = [helper.positionTOstate(*p) for p in goals_location]
	# for i in range(len(map)):
	# 	for j in range(len(map[i])):
	# 		if map[i][j] == 'g':
	# 			goals_states.append((i, j))

	# print "goals_location", goals_location
	# print 'goals_states', goals_states


	start = helper.positionTOstate(position[0],position[1])
	if start in previous_policy:
		at = previous_policy.index(start)
		if at != len(previous_policy):
			previous_policy = previous_policy[at:]
			print 'previous policy:', previous_policy
			print 'next step', start, 'to', previous_policy[1]
			return angle_between_position(position, 
					helper.stateTOposition(*previous_policy[1]))
	else:
		previous_policy = []


	unvisted = priorityDictionary(sort_by = lambda x: x[1])
	unvisted[start] = 0
	
	gScores = defaultdict(lambda : float('inf')) # cost from start to state
	fScores = defaultdict(None)  # ~cost from state to goal

	pred = defaultdict(lambda : (float('inf'), float('inf')))
	visited = set()
	gScores[start] = 0


	for curr in unvisted:
		visited.add(curr)
		# fScores[curr] = heuristic(curr, goals_states, map, angle) + gScores[curr]
		# if curr in goals_states:
			
			
		# 	break

		# add positions arround curr that are not visited
		for sur in get_surrounding(curr, map):
			if distance(sur, start, angle, map) > depth:
				continue

			if curr == start:
				dist = distance(curr, sur, angle, map)
			else:
				dist = distance(curr, sur, 
				angle_between_state(pred[curr], curr, map),
					map)
			
			if sur in visited and\
				 gScores[sur] < gScores[curr] + dist:
				 continue
			
			# calculate f values for new-added blocks
			# if gScores[sur]+heuristic(sur, goals_states, map, angle) > \
			# 	gScores[curr]+heuristic(curr, goals_states, map, angle):
			else:
				pred[sur] = curr
				gScores[sur] = gScores[curr] + dist
				fScores[sur] = heuristic(sur, goals_states, map, angle) + gScores[sur]
				


			if sur not in visited:
				unvisted[sur] = fScores[sur]
			

	# helper.print_dict(gScores, name='gScore')
	# helper.print_dict(fScores, name='fScore')
		


	#find the angle of close_list
	dest = start
	minF = float('inf')
	for state, f in fScores.items():
		if f < minF and state != start:
			minF = f
			dest = state


	previous_policy = retreive_path(start, dest, pred)

	next_block = previous_policy[1]


	print 'facing', angle, start, 'to', previous_policy[-1], ':', previous_policy
	print 'next step', start, 'to', next_block, '==', position, 'to', helper.stateTOposition(*next_block)
	return angle_between_position(position, 
			helper.stateTOposition(*next_block))
예제 #51
0
    w = [[0 for i in xrange(M)] for j in xrange(N)]
    t = [[0 for i in xrange(M)] for j in xrange(N)]
    for i in xrange(N):
        inp = map(int, raw_input().split())
        for j in xrange(M):
            s[i][j] = inp[3 * j]
            w[i][j] = inp[3 * j + 1]
            t[i][j] = inp[3 * j + 2] % (s[i][j] + w[i][j])
    s.reverse()
    w.reverse()
    t.reverse()
    start = (0, 0)
    end = (2 * N - 1, 2 * M - 1)
    D = {}  # dictionary of final distances
    P = {}  # dictionary of predecessors
    Q = priorityDictionary()  # est.dist. of non-final vert.
    Q[start] = 0

    for v in Q:
        D[v] = Q[v]
        if v == end: break

        for u, l in getnhd(v):
            ##print l,u
            vuLength = D[v] + l
            if u in D:
                if vuLength < D[u]:
                    raise ValueError, \
"Dijkstra: found better path to already-final vertex"
            elif u not in Q or vuLength < Q[u]:
                Q[u] = vuLength
예제 #52
0
	The output is a pair (D,P) where D[v] is the distance
	from start to v and P[v] is the predecessor of v along
	the shortest path from s to v.
	
	Dijkstra's algorithm is only guaranteed to work correctly
	when all edge lengths are positive. This code does not
	verify this property for all edges (only the edges seen
 	before the end vertex is reached), but will correctly
	compute shortest paths even for some graphs with negative
	edges, and will raise an exception if it discovers that
	a negative edge has caused it to make a mistake.
	"""

	D = {}	# dictionary of final distances
	P = {}	# dictionary of predecessors
	Q = priorityDictionary()   # est.dist. of non-final vert.
	Q[start] = 0
	
	for v in Q:
		D[v] = Q[v]
		if v == end: break
        curnode = ncollection.find_one({'name':v})
		availablenodes = curnode['availableNodes']
		for node in availablenodes:
            nnode = ncollection.find_one({'name':node})
            
			vwLength = D[v] + nodeDist(curnode, nnode, time.time()+D[node]
			if node in D:
				if vwLength < D[node]:
					raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
예제 #53
0
def Dijkstra(graph,start,end=None):
	"""
	Find shortest paths from the start vertex to all
	vertices nearer than or equal to the end.

	The input graph G is assumed to have the following
	representation: A vertex can be any object that can
	be used as an index into a dictionary.  G is a
	dictionary, indexed by vertices.  For any vertex v,
	G[v] is itself a dictionary, indexed by the neighbors
	of v.  For any edge v->w, G[v][w] is the length of
	the edge.  This is related to the representation in
	<http://www.python.org/doc/essays/graphs.html>
	where Guido van Rossum suggests representing graphs
	as dictionaries mapping vertices to lists of neighbors,
	however dictionaries of edges have many advantages
	over lists: they can store extra information (here,
	the lengths), they support fast existence tests,
	and they allow easy modification of the graph by edge
	insertion and removal.  Such modifications are not
	needed here but are important in other graph algorithms.
	Since dictionaries obey iterator protocol, a graph
	represented as described here could be handed without
	modification to an algorithm using Guido's representation.

	Of course, G and G[v] need not be Python dict objects;
	they can be any other object that obeys dict protocol,
	for instance a wrapper in which vertices are URLs
	and a call to G[v] loads the web page and finds its links.

	The output is a pair (D,P) where D[v] is the distance
	from start to v and P[v] is the predecessor of v along
	the shortest path from s to v.

	Dijkstra's algorithm is only guaranteed to work correctly
	when all edge lengths are positive. This code does not
	verify this property for all edges (only the edges seen
 	before the end vertex is reached), but will correctly
	compute shortest paths even for some graphs with negative
	edges, and will raise an exception if it discovers that
	a negative edge has caused it to make a mistake.
	"""

	final_distances = {}	# dictionary of final distances
	predecessors = {}	# dictionary of predecessors
	estimated_distances = priorityDictionary()   # est.dist. of non-final vert.
	estimated_distances[start] = 0

	for vertex in estimated_distances:
		final_distances[vertex] = estimated_distances[vertex]
		if vertex == end: break

		for edge in graph[vertex]:
			path_distance = final_distances[vertex] + graph[vertex][edge]
			if edge in final_distances:
				if path_distance < final_distances[edge]:
					raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
			elif edge not in estimated_distances or path_distance < estimated_distances[edge]:
				estimated_distances[edge] = path_distance
				predecessors[edge] = vertex

	return (final_distances,predecessors)
예제 #54
0
	def getRoute(self, startEdge, endEdge, algorithm):

		# if startEdge is internal
		if startEdge[0] == ':':
			return None

		# applies dijkstra's algorithm
		# to find the lightest route (list of edges) between the startEdge to the endEdge
		# in the self.__graph network
		D = {}
		P = {}
		Q = priorityDictionary()
		Q[startEdge] = 0
		
		# alters the edge weights if the error insertion is the chosen algorithm
		alteredEdgeWeights = {}
		if algorithm == 3:
			for edge, weight in self.__edgeWeights.iteritems():
				newWeight = weight * WEIGHT_VARIATION
				alteredEdgeWeights[edge] = weight + random.uniform(-newWeight, newWeight)
				

		for v in Q:
			D[v] = Q[v]
			if v == endEdge:
				break
			for w in self.__graph[v]:
				
				vwValue = 0
				# DistanceDijkstra - algorithm = 0
				if algorithm == 0:
					vwValue = D[v] + self.__graph[v][w]
				# OccupancyDijsktra - algorithm = 1
				elif algorithm == 1:
					vwValue = D[v] + self.__graph[v][w] + pow((self.__graph[v][w] * self.__edgeWeights[w]), 2)
				# RoadBlock - algorithm = 2
				elif algorithm == 2:
					if(self.__edgeWeights[w]>OCCUPANCY_LIMIT):
						break
					vwValue = D[v] + self.__graph[v][w]
				# ErrorInsertion - algorithm = 3
				elif algorithm == 3:
					vwValue = D[v] + self.__graph[v][w] + pow((self.__graph[v][w] * alteredEdgeWeights[w]), 2)
				
				if w in D:
					if vwValue < D[w]:
						raise ValueError, "Dijkstra: found better path to already-final vertex"
				elif w not in Q or vwValue < Q[w]:
					Q[w] = vwValue
					P[w] = v

		# computes the route from the dictionary with the preceding edges (P)
		try:
			route = []
			toEdge = endEdge
			while endEdge != startEdge:
				endEdge = P[endEdge]
				route.insert(0, toEdge)
				toEdge = endEdge
			route.insert(0, toEdge)
		except:
			# catches the no route found exception
			# for example, caused by road blocks when algorithm = 2
			return []

		return route
예제 #55
0
    def a_star(self, sources, old_target):
        '''
        Implementation of multisource A* as mentioned in
        practical heuristic for minor embedding paper.

        multisource A*
        Input: graph G, edge weights , heuristic costs , sources

        Output: vertex cv such that maxk i=1 d(cv, s(i)) is
        minimal among all vertices
        '''

        # Initializing variables:
        d = {}
        est = {}
        reached = {}
        min_est = priorityDictionary()
        min_src = {}
        path = []
        path_cs = []
        inf = len(self.original_nodes_list )**2*\
        len(self.edges)*10**4 # a very large number

        for v in self.nodes:
            for i in range(len(sources)):

                # d[(v,i)] = inf      # best known distance from v to i
                # est[(v,i)] = inf    # heuristic distance
                # reached[(v,i)] = False  # node v reached from source i?

                d[(v, sources[i])] = inf  # best known distance from v to i
                est[(v, sources[i])] = inf  # heuristic distance
                reached[(v,
                         sources[i])] = False  # node v reached from source i?

        for v in self.nodes:
            min_est[v] = inf  # min. dist. to v among all i

        for i in range(len(sources)):

            # This is dangereous:
            d[(sources[i], sources[i])] = 0

            min_est[sources[i]] = 0

            #min_src[sources[i]] = i     #index of source for min_est
            # I am trying to use the correct indecies for sources:
            min_src[sources[i]] = sources[i]  #index of source for min_est

        while True:

            #current node calculation:
            if path != []:
                cv = self.neighbours[path[-1]][0]
            else:
                cv = self.neighbours[sources[0]][0]

            for v in self.nodes:
                if min_est[v] < min_est[cv]:
                    cv = v

            #current source calculation:
            cs = min_src[cv]

            reached[(cv, cs)] = True

            # Check if all sources have reached cv:
            check_counter = 0
            for i in range(len(sources)):
                if (cv, sources[i]) in reached.keys():
                    if reached[(cv, sources[i])] == True:
                        if cv not in sources:
                            if cv not in self.neighbours[sources[i]]:
                                check_counter += 1

            if check_counter == len(sources):
                return cv  # all sources have reached cv

            # Find new best source for cv:
            found_minsrc = sources[0]

            for i in range(len(sources)):
                if reached[(cv, sources[i])] == False:

                    if est[(cv, sources[i])] < est[(cv, found_minsrc)]:
                        found_minsrc = sources[i]

            min_src[cv] = found_minsrc

            # Find new best distance for cv:
            min_est[cv] = est[(v, min_src[cv])]

            # update neighbour distances:
            for v in self.neighbours[cv]:
                # alternate distance to cs
                alt = d[(cv, cs)] + self.edge_weights[(v, cv)]

                if alt < d[(v, cs)]:

                    d[(v, cs)] = alt  #distance improved

                    # calculate heuristic cost for vertex v to old target g*:
                    h_cost_v = len(self.shortestPath(v, old_target))

                    #est[(v,cs)] = d[(v,cs)] + h_cost[v]  #new heuristic distance
                    est[(v, cs)] = d[(v, cs)] + h_cost_v

                    if est[(v, cs)] < min_est[v]:
                        min_est[v] = est[(v, cs)]  #improved best distance
                        min_src[v] = cs
예제 #56
0
 def __init__(self, maxsize, origitems=None):
     self.count = 0
     self.maxsize = maxsize
     self.priority = priorityDictionary()
     self.drop_listeners = []
     OrderedSet.__init__(self, origitems)
예제 #57
0
def Dijkstra(graph, start, end=None):
    """
	Find shortest paths from the start vertex to all
	vertices nearer than or equal to the end.

	The input graph G is assumed to have the following
	representation: A vertex can be any object that can
	be used as an index into a dictionary.  G is a
	dictionary, indexed by vertices.  For any vertex v,
	G[v] is itself a dictionary, indexed by the neighbors
	of v.  For any edge v->w, G[v][w] is the length of
	the edge.  This is related to the representation in
	<http://www.python.org/doc/essays/graphs.html>
	where Guido van Rossum suggests representing graphs
	as dictionaries mapping vertices to lists of neighbors,
	however dictionaries of edges have many advantages
	over lists: they can store extra information (here,
	the lengths), they support fast existence tests,
	and they allow easy modification of the graph by edge
	insertion and removal.  Such modifications are not
	needed here but are important in other graph algorithms.
	Since dictionaries obey iterator protocol, a graph
	represented as described here could be handed without
	modification to an algorithm using Guido's representation.

	Of course, G and G[v] need not be Python dict objects;
	they can be any other object that obeys dict protocol,
	for instance a wrapper in which vertices are URLs
	and a call to G[v] loads the web page and finds its links.

	The output is a pair (D,P) where D[v] is the distance
	from start to v and P[v] is the predecessor of v along
	the shortest path from s to v.

	Dijkstra's algorithm is only guaranteed to work correctly
	when all edge lengths are positive. This code does not
	verify this property for all edges (only the edges seen
 	before the end vertex is reached), but will correctly
	compute shortest paths even for some graphs with negative
	edges, and will raise an exception if it discovers that
	a negative edge has caused it to make a mistake.
	"""

    final_distances = {}  # dictionary of final distances
    predecessors = {}  # dictionary of predecessors
    estimated_distances = priorityDictionary()  # est.dist. of non-final vert.
    estimated_distances[start] = 0

    for vertex in estimated_distances:
        final_distances[vertex] = estimated_distances[vertex]
        if vertex == end: break

        for edge in graph[vertex]:
            path_distance = final_distances[vertex] + graph[vertex][edge]
            if edge in final_distances:
                if path_distance < final_distances[edge]:
                    raise ValueError, \
                 "Dijkstra: found better path to already-final vertex"
            elif edge not in estimated_distances or path_distance < estimated_distances[
                    edge]:
                estimated_distances[edge] = path_distance
                predecessors[edge] = vertex

    return (final_distances, predecessors)