Exemplo n.º 1
0
    def get_dijkstra3(self, G, target):
        Gk = G.reverse()
        inf = float('inf')
        D = {target: 0}
        Que = PQDict(D)
        P = {}
        nodes = Gk.nodes
        U = set(nodes)
        while U:
            #print('len U %d'%len(U))
            #print('len Q %d'%len(Que))
            if len(Que) == 0: break
            (v, d) = Que.popitem()
            D[v] = d
            U.remove(v)
            #if v == target: break
            neigh = list(Gk.successors(v))
            for u in neigh:
                if u in U:
                    d = D[v] + Gk[v][u]['weight']
                    if d < Que.get(u, inf):
                        Que[u] = d
                        P[u] = v

        return P
Exemplo n.º 2
0
def djikstra(G, start):
    '''
    Djikstra's algorithm determines the length from `start` to every other 
    vertex in the graph.

    The graph argument `G` should be a dict indexed by nodes.  The value 
    of each item `G[v]` should also a dict indexed by predecessor nodes.
    In other words, for any node `v`, `G[v]` is itself a dict, indexed 
    by the predecessors of `v`.  For any directed edge `w -> v`, `G[v][w]` 
    is the length of the edge from `w` to `v`.

    '''
    inf = float('inf')
    dist = {start: 0}       # track shortest path distances from `start`
    E = set([start])        # explored
    U = set(G.keys()) - E   # unexplored

    while U:                                        # unexplored nodes
        D = PQDict()                                # frontier candidates
        for u in U:                                 # unexplored nodes
            for v in G[u]:                          # neighbors of u
                if v in E:                          # then u is a frontier node
                    l = dist[v] + G[u][v]           # start -> v -> u
                    D[u] = min(l, D.get(u, inf))    # choose minimum for u

        (x, d) = D.popitem()                        # node w/ min dist on frontier
        dist[x] = d                                 # assign djikstra greedy score
        U.remove(x)                                 # remove from unexplored
        E.add(x)                                    # add to explored

    return dist                                     # shortest path distances
Exemplo n.º 3
0
def djikstra(G, start):
    '''
    Djikstra's algorithm determines the length from `start` to every other 
    vertex in the graph.

    The graph argument `G` should be a dict indexed by nodes.  The value 
    of each item `G[v]` should also a dict indexed by predecessor nodes.
    In other words, for any node `v`, `G[v]` is itself a dict, indexed 
    by the predecessors of `v`.  For any directed edge `w -> v`, `G[v][w]` 
    is the length of the edge from `w` to `v`.

    '''
    inf = float('inf')
    dist = {start: 0}  # track shortest path distances from `start`
    E = set([start])  # explored
    U = set(G.keys()) - E  # unexplored

    while U:  # unexplored nodes
        D = PQDict()  # frontier candidates
        for u in U:  # unexplored nodes
            for v in G[u]:  # neighbors of u
                if v in E:  # then u is a frontier node
                    l = dist[v] + G[u][v]  # start -> v -> u
                    D[u] = min(l, D.get(u, inf))  # choose minimum for u

        (x, d) = D.popitem()  # node w/ min dist on frontier
        dist[x] = d  # assign djikstra greedy score
        U.remove(x)  # remove from unexplored
        E.add(x)  # add to explored

    return dist  # shortest path distances
Exemplo n.º 4
0
def dijkstra(graph, start, end):
    inf = float('inf')
    shortest_distances = {start: 0}                 # mapping of nodes to their dist from start
    queue_sd = PQDict(shortest_distances)           # priority queue for tracking min shortest path
    predecessors = {}                               # mapping of nodes to their direct predecessors
    unexplored = set(graph.keys())                  # unexplored nodes
    path = []

    while unexplored:                                           # nodes yet to explore
        (minNode, minDistance) = queue_sd.popitem()             # node w/ min dist d on frontier
        shortest_distances[minNode] = minDistance               # est dijkstra greedy score
        unexplored.remove(minNode)                              # remove from unexplored
        if minNode == end: break                                # end if goal already reached

        # now consider the edges from minNode with an unexplored head -
        # we may need to update the dist of unexplored successors
        for neighbor in graph[minNode]:                               # successors to v
            if neighbor in unexplored:                          # then neighbor is a frontier node
                minDistance = shortest_distances[minNode] + graph[minNode][neighbor]
                if minDistance < queue_sd.get(neighbor, inf):
                    queue_sd[neighbor] = minDistance
                    predecessors[neighbor] = minNode                   # set/update predecessor

    currentNode = end
    while currentNode != start:
        try:
            path.insert(0,currentNode)
            currentNode = predecessors[currentNode]
        except KeyError:
            print('Path not reachable')
            break
    path.insert(0,start)
    if shortest_distances[end] != inf:
        return shortest_distances[end], path
Exemplo n.º 5
0
def dijkstra(g, s):
    vis = [False for i in range(len(g))]
    dist = [float('inf') for i in range(len(g))]
    prev = [None for i in range(len(g))]
    dist[s] = 0

    ipq = PQDict()
    ipq.additem(s, 0)
    while len(ipq) != 0:
        index, minValue = ipq.popitem()
        vis[index] = True
        # mica optimizare, daca deja avem distanda mai buna pe alt drum fata de drumul direct dintre 2 noduri, pass
        if dist[index] < minValue:
            continue
        for edge in g[index]:
            # ia toate nodurile vecine nevizitate
            if vis[edge[0]]:
                continue
            # le calculaeza distanta
            newDist = dist[index] + edge[1]
            # si o modifica doar daca este mai mica decat cea care era deja
            if newDist < dist[edge[0]]:
                dist[edge[0]] = newDist
                # apoi modifica si in indexed priority queue, daca nu e, il adauga, daca e, ii updateaza valoarea
                if ipq.get(edge[0]) is None:
                    ipq.additem(edge[0], newDist)
                else:
                    ipq[edge[0]] = newDist
                # si seteaza si tatal de fiecare data pentru a ramane ultimul adica cel cu costul cel mai mic
                prev[edge[0]] = index
    return dist, prev
Exemplo n.º 6
0
def dijkstra(G, start, end=None):
    '''
    dijkstra's algorithm determines the length from `start` to every other 
    vertex in the graph.

    The graph argument `G` should be a dict indexed by nodes.  The value 
    of each item `G[v]` should also a dict indexed by successor nodes.
    In other words, for any node `v`, `G[v]` is itself a dict, indexed 
    by the successors of `v`.  For any directed edge `v -> w`, `G[v][w]` 
    is the length of the edge from `v` to `w`.

        graph = {'a': {'b': 1}, 
                 'b': {'c': 2, 'b': 5}, 
                 'c': {'d': 1},
                 'd': {}}

    Returns two dicts, `dist` and `pred`:

        dist, pred = dijkstra(graph, start='a') 
    
    `dist` is a dict mapping each node to its shortest distance from the
    specified starting node:

        assert dist == {'a': 0, 'c': 3, 'b': 1, 'd': 4}

    `pred` is a dict mapping each node to its predecessor node on the
    shortest path from the specified starting node:

        assert pred == {'b': 'a', 'c': 'b', 'd': 'c'}
    
    '''
    inf = float('inf')
    D = {start: 0}  # mapping of nodes to their dist from start           p*
    Q = PQDict(
        D)  # priority queue for tracking min shortest path      Cin ????
    P = {}  # mapping of nodes to their direct predecessors      存边
    U = set(G.keys())  # unexplored nodes

    while U:  # nodes yet to explore
        (v,
         d) = Q.popitem()  # node w/ min dist d on frontier           找到Cln最小的l
        D[v] = d  # est dijkstra greedy score                pl*
        U.remove(v)  # remove from unexplored                  从U中删除l
        if v == end: break  #如果集合为空,则算法终止

        # now consider the edges from v with an unexplored head -
        # we may need to update the dist of unexplored successors
        for w in G[v]:  # successors to v
            if w in U:  # then w is a frontier node
                d = D[v] + G[v][w]  # dgs: dist of start -> v -> w
                if d < Q.get(w, inf):
                    Q[w] = d  # set/update dgs
                    P[w] = v  # set/update predecessor

    return D, P
Exemplo n.º 7
0
def dijkstra(G, start, end=None):
    '''
    dijkstra's algorithm determines the length from `start` to every other 
    vertex in the graph.

    The graph argument `G` should be a dict indexed by nodes.  The value 
    of each item `G[v]` should also a dict indexed by successor nodes.
    In other words, for any node `v`, `G[v]` is itself a dict, indexed 
    by the successors of `v`.  For any directed edge `v -> w`, `G[v][w]` 
    is the length of the edge from `v` to `w`.

        graph = {'a': {'b': 1}, 
                 'b': {'c': 2, 'b': 5}, 
                 'c': {'d': 1},
                 'd': {}}

    Returns two dicts, `dist` and `pred`:

        dist, pred = dijkstra(graph, start='a') 
    
    `dist` is a dict mapping each node to its shortest distance from the
    specified starting node:

        assert dist == {'a': 0, 'c': 3, 'b': 1, 'd': 4}

    `pred` is a dict mapping each node to its predecessor node on the
    shortest path from the specified starting node:

        assert pred == {'b': 'a', 'c': 'b', 'd': 'c'}
    
    '''
    inf = float('inf')
    D = {start: 0}          # mapping of nodes to their dist from start
    Q = PQDict(D)           # priority queue for tracking min shortest path
    P = {}                  # mapping of nodes to their direct predecessors
    U = set(G.keys())       # unexplored nodes

    while U:                                    # nodes yet to explore
        (v, d) = Q.popitem()                    # node w/ min dist d on frontier
        D[v] = d                                # est dijkstra greedy score
        U.remove(v)                             # remove from unexplored
        if v == end: break

        # now consider the edges from v with an unexplored head -
        # we may need to update the dist of unexplored successors 
        for w in G[v]:                          # successors to v
            if w in U:                          # then w is a frontier node
                d = D[v] + G[v][w]              # dgs: dist of start -> v -> w
                if d < Q.get(w, inf):
                    Q[w] = d                    # set/update dgs
                    P[w] = v                    # set/update predecessor

    return D, P
Exemplo n.º 8
0
def dijkstra(G, src, dst=None):
    inf = float('inf')
    D = {src: 0}  # distance
    Q = PQDict(D)  # priority queue
    P = {}  # predecessor
    U = set(G.keys())  # unexplored nodes

    while U:  # still have unexplored
        (v, d) = Q.popitem()  # get node with least d
        D[v] = d  # add to D dict
        U.remove(v)  # node now explored
        if v == dst:  # reached destination
            break

        # now edges FROM v
        for w in G[v]:
            if w in U:  # unvisited neighbour
                tmpd = D[v] + G[v][w]
                if tmpd < Q.get(w, inf):  # return inf if not found
                    Q[w] = tmpd  # update distance
                    P[w] = v  # set predecessor as current
    return D, P
Exemplo n.º 9
0
def dijkstra(G, start, end=None):
    inf = float('inf')
    D = {start: 0}  # mapping of nodes to their dist from start
    Q = PQDict(D)  # priority queue for tracking min shortest path
    P = {}  # mapping of nodes to their direct predecessors
    U = set(G.keys())  # unexplored nodes

    while U:  # nodes yet to explore
        (v, d) = Q.popitem()  # node w/ min dist d on frontier
        D[v] = d  # est dijkstra greedy score
        U.remove(v)  # remove from unexplored
        if v == end: break

        # now consider the edges from v with an unexplored head -
        # we may need to update the dist of unexplored successors
        for w in G[v]:  # successors to v
            if w in U:  # then w is a frontier node
                d = int(D[v]) + int(G[v][w])  # dgs: dist of start -> v -> w
                if d < Q.get(w, inf):
                    Q[w] = d  # set/update dgs
                    P[w] = v  # set/update predecessor

    return D, P
Exemplo n.º 10
0
    def dijkstra(self, src, dst):
        """dijkstras algorithm used to calculate the shortest path between two nodes a Priority queue is used in this implementation"""
        inf = float('inf')
        D = {src: 0}
        Q = PQDict(D)
        P = {}
        U = set(self.router_list)

        while U:
            (v, d) = Q.popitem()
            D[v] = d
            U.remove(v)
            if str(v) == dst:
                break

            for w in self.router_list[v]:
                if w in U:

                    d = D[v] + self.router_list[v][w]
                    if d < Q.get(w, inf):
                        Q[w] = d
                        P[w] = v

        return D, P
Exemplo n.º 11
0
class Storage:
    """
    Kademlia storage implementation.

    Three responsibilities:
    - Storing data
    - Listing old keys to refresh
    - Keeping a record of data popularity and evicting unpopular data when
      a storage limit is reached.
    """
    implements(kademlia.storage.IStorage)

    max_len = 5000

    def __init__(self, args, ttl=604800, time=time):
        self.args = args
        self.time = time

        # linked
        self.popularity_queue = PQDict()
        self.age_dict = OrderedDict()

        # separate
        self.future_popularity_queue = PQDict()

        self.step = ttl

    def cull(self):
        if len(self.popularity_queue) > self.max_len:
            key = self.popularity_queue.pop()
            if self.args.verbose:
                log_info('Dropping key {} (over count {})'.format(binascii.hexlify(key), self.max_len))
            del self.age_dict[key]
        if len(self.future_popularity_queue) > self.max_len:
            key = self.future_popularity_queue.pop()
            if self.args.verbose:
                log_info('Dropping future key {} (over count {})'.format(binascii.hexlify(key), self.max_len))

    def inc_popularity(self, key):
        current = self.popularity_queue.get(key)
        if current is not None:
            self.popularity_queue[key] = current + self.step
        else:
            current = self.future_popularity_queue.get(key, self.time.time())
            self.future_popularity_queue[key] = current + self.step

    def _tripleIterable(self):
        ikeys = self.age_dict.iterkeys()
        ibirthday = imap(operator.itemgetter(0), self.age_dict.itervalues())
        ivalues = imap(operator.itemgetter(1), self.age_dict.itervalues())
        return izip(ikeys, ibirthday, ivalues)

    # interface methods below
    def __setitem__(self, key, value):
        age, oldvalue = self.age_dict.get(key) or self.time.time(), None
        if not validate(self.args, key, value, oldvalue)[0]:
            return
        if oldvalue is not None:
            self.age_dict[key] = (age, value)
        else:
            age = self.future_popularity_queue.pop(key, self.time.time())
            self.age_dict[key] = (self.time.time(), value)
            self.popularity_queue[key] = age
        self.cull()

    def __getitem__(self, key):
        self.inc_popularity(key)
        self.cull()
        return self.age_dict[key][1]

    def get(self, key, default=None):
        self.inc_popularity(key)
        self.cull()
        if key in self.age_dict:
            return self.age_dict[key][1]
        return default

    def iteritemsOlderThan(self, secondsOld):
        minBirthday = self.time.time() - secondsOld
        zipped = self._tripleIterable()
        matches = takewhile(lambda r: minBirthday >= r[1], zipped)
        return imap(operator.itemgetter(0, 2), matches)

    def iteritems(self):
        self.cull()
        return self.age_dict.iteritems()
 def test_get(self):
     pq = PQDict(self.items)
     self.assertEqual(pq.get('A'), 5)
     self.assertEqual(pq.get('A', None), 5)
     self.assertIs(pq.get('does_not_exist', None), None)
     self.assertRaises(KeyError, pq.get('does_not_exist'))