Minimum number of flight segments using breadth-first search
1. Review


There's no docstring. What does this function do? What arguments does it take? What does it return?
The code uses a list to represent the queue of nodes to be visited. But if you look at the TimeComplexity page on the Python wiki, you'll see that list.pop(0) takes time proportional to the length of the list, and that means that the algorithm takes time that's quadratic in the number of nodes in the graph.

Instead of keeping the queue of nodes in a list, you need a collections.deque, which has an efficient popleft method.
Each time the code reaches a new node, it copies the path to reach that node, and puts the path on the queue. But this is wasteful of memory (because paths might become long and have to be copied out many times) and leads to quadratic runtime.

Since the code only needs to return the distance to the target (not the path), it only needs to store the distance to each node.
The variable n is not used.
Instead of:

while (len(queue) &gt; 0):


write:

while queue:

It would be faster to check whether a node has been visited before adding it to the queue (rather than after removing it from the queue). This avoids unnecessary append and pop operations on visited nodes.
It would be faster to check whether the target has been reached before adding it to the queue (rather than after removing it from the queue). This avoids having to wait for the target to move all the way up the queue.
distance returns -1 if there was no path from source to target. But it is risky to return an exceptional value like this: it would be all too easy for the caller to forget to check the result. It is better to raise an exception for exceptional cases.


2. Revised code

from collections import deque

class Unreachable(Exception):
    """Exception raised when there is no path between nodes in a graph."""

def distance(graph, source, target):
    """Return the number of edges in the shortest path from source to
    target in a directed graph. The graph must be represented as a
    mapping from a node to a collection of its neighbours. If there is
    no path from source to target, raise Unreachable.

    &gt;&gt;&gt; distance({1: [2, 3], 2: [3, 4], 3: [4, 5]}, 1, 5)
    2

    """
    if source == target:
        return 0
    queue = deque([(source, 0)]) # Queue of pairs (node, distance).
    visited = set()              # Set of visited nodes.
    while queue:
        node, dist = queue.popleft()
        visited.add(node)
        for neighbour in graph[node]:
            if neighbour in visited:
                continue
            elif neighbour == target:
                return dist + 1
            else:
                queue.append((neighbour, dist + 1))
    else:
        raise Unreachable("No path from {!r} to {!r}".format(source, target))