def _rank_cycle_function(self, cycle, function, ranks):
import heapq<EOL>Q = []<EOL>Qd = {}<EOL>p = {}<EOL>visited = set([function])<EOL>ranks[function] = <NUM_LIT:0><EOL>for call in compat_itervalues(function.calls):<EOL><INDENT>if call.callee_id != function.id:<EOL><INDENT>callee = self.functions[call.callee_id]<EOL>if callee.cycle is cycle:<EOL><INDENT>ranks[callee] = <NUM_LIT:1><EOL>item = [ranks[callee], function, callee]<EOL>heapq.heappush(Q, item)<EOL>Qd[callee] = item<EOL><DEDENT><DEDENT><DEDENT>while Q:<EOL><INDENT>cost, parent, member = heapq.heappop(Q)<EOL>if member not in visited:<EOL><INDENT>p[member]= parent<EOL>visited.add(member)<EOL>for call in compat_itervalues(member.calls):<EOL><INDENT>if call.callee_id != member.id:<EOL><INDENT>callee = self.functions[call.callee_id]<EOL>if callee.cycle is cycle:<EOL><INDENT>member_rank = ranks[member]<EOL>rank = ranks.get(callee)<EOL>if rank is not None:<EOL><INDENT>if rank > <NUM_LIT:1> + member_rank:<EOL><INDENT>rank = <NUM_LIT:1> + member_rank<EOL>ranks[callee] = rank<EOL>Qd_callee = Qd[callee]<EOL>Qd_callee[<NUM_LIT:0>] = rank<EOL>Qd_callee[<NUM_LIT:1>] = member<EOL>heapq._siftdown(Q, <NUM_LIT:0>, Q.index(Qd_callee))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>rank = <NUM_LIT:1> + member_rank<EOL>ranks[callee] = rank<EOL>item = [rank, member, callee]<EOL>heapq.heappush(Q, item)<EOL>Qd[callee] = item<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>
Dijkstra's shortest paths algorithm.

        See also:
        - http://en.wikipedia.org/wiki/Dijkstra's_algorithm