Example #1
0
    def visitBFS(self, node):
        """
        this is a Breadth First Search starting from a vertex. Please note that all the operations are done on the leaves
        to let the algorithm be more modular (it doesn't seems be affecting the computational time for it remains proportional
        to the dimension of the graph)
        :param node: Node, the starting vertex
        :return: Tree, representing the visit path
        """
        #initializing some useful constants (funny constants too)
        unexplored = 0
        explored = 21
        closed = 42 #So long and thanks for all the fish!
        #validating all the nodes as unexplored and starting from the vertex
        self.validateNodes(unexplored)
        node.set_token(explored)
        #initializing the tree containing the only vertex
        T_root = Leaf(node)
        T_root.setDistance(0.0) #using the float - it is not a counter value
        T = Tree(T_root)
        #initializing the fringe of the visit
        F = Queue()
        F.enqueue(T_root)

        while not F.isEmpty():
            u = F.dequeue()
            n = u.getElem()
            n.set_token(closed)
            for v in self._adjacency[n.get_index()].getLastAddedList():
                if v.get_token() == unexplored:
                    v.set_token(explored)
                    l = Leaf(v)
                    F.enqueue(l)
                    T.insertLeaf(l, u)
        return T
Example #2
0
    def Dijkstra(self, node):
        """
        this is a Dijstra shortest path algorithm implementation starting from a vertex
        :param node: Node, the starting vertex
        :return: Tree, the shortest paths tree
        """
        INF = float('inf')
        self.validateNodes(INF)
        #we will use the nodes' tokens to store the distance info!
        node.set_token(0.0) #0-distance from itself!
        #initializing the tree
        T_root = Leaf(node)
        T_root.setDistance(node.get_token())
        T = Tree(T_root)
        #initializing a dictionary to keep trace of the leaves
        leaves = dict()
        leaves[node] = T_root
        #initializing the priority queue to mantain the fringe
        PQ = PriorityQueue()
        PQ.insert(T_root, node.get_token())

        while not PQ.isEmpty():
            u = PQ.deleteMin() #retrieving the min node from the leaf
            n = u.getElem()
            for v in self._adjacency[n.get_index()].getLastAddedList():
                if v.get_token() == INF:
                    l = Leaf(v)
                    leaves[v] = l #updating the leaves' dictionary
                    PQ.insert(l, n.get_token() + 1.0) #each edge will be unitary-cost
                    v.set_token(n.get_token() + 1.0)
                    T.insertLeaf(l, u)
                elif n.get_token() + 1.0 < v.get_token():
                    relaxed = n.get_token() + 1.0
                    leaves[v].setDistance(relaxed)
                    #updating the tree... (we are now saving in the priority queue the leaves)
                    leaves[v].setFather(u)
                    leaves[n].addSon(leaves[v])
                    #updating the priority queue
                    PQ.decreaseKey(leaves[v], relaxed)
                    v.set_token(relaxed)
        return T