BFS' shortest path unweighted directed graph The code I have is based on BFS and a little bit of Dijkstra and returns the shortest path of an unweighted directed graph as an integer. I was wondering if someone could take a look at my code too see if anything could be changed or improved:

public int bfsshortestpath (V source, V target) {
    Map&lt;V, Integer&gt; dist = new TreeMap&lt;V, Integer&gt;();
    Map&lt;V, V&gt; prev = new TreeMap&lt;V, V&gt;();

    for(V v: adjlist.keySet()){
        dist.put(v, null);
        prev.put(v, null);
    }
    dist.put(source, 0);

    Queue&lt;V&gt; q = new LinkedList&lt;V&gt;();
    q.offer(source);
    while(!q.isEmpty()){
        V u = q.poll();
        if(u == target)
            break;
        q.remove(u);
        for(V neighbor: adjlist.get(u)){
            int a = dist.get(u);
            if(dist.get(neighbor) != null)
                continue;
            //BFS distance
            dist.put(neighbor, a + 1);
            prev.put(neighbor, u);
            q.offer(neighbor);
        }
    }

    List&lt;V&gt; s = new LinkedList&lt;V&gt;();
    V u = target;
    while(prev.get(u) != null){
        s.add(u);
        u = prev.get(u);
    }
    return s.size();
}
Advice 1

for(V v: adjlist.keySet()){
    dist.put(v, null);
    prev.put(v, null);
}


This is \$\Theta(N)\$ and you don't really need it (see below). Also, TreeMap orders the keys (nodes) in a balanced binary search tree, which runs insertion/search/removal in worst case \$\Theta(\log n)\$ time, and you don't need it either: just use a HashMap with \$\Theta(1)\$ access, reading and writing.

Advice 2

In order to get rid of the initialization for in the above advice, you can do:

public int shortestPathLength(V source, V target) {
    Map&lt;V, Integer&gt; dist = new HashMap&lt;V, Integer&gt;();
    Map&lt;V, V&gt; prev = new HashMap&lt;V, V&gt;();
    Queue&lt;V&gt; q = new LinkedList&lt;&gt;();

    dist.put(source, 0);
    prev.put(source, null);
    q.offer(source);

    while(!q.isEmpty()){
        V u = q.poll();
        if(u == target)
            return tracebackPath(u, prev);
        q.remove(u);
        for(V neighbor: adjlist.get(u)){
            if (!dist.containsKey(neighbor)) {                        
                dist.put(neighbor, dist.get(u) + 1);
                prev.put(neighbor, u);
                q.offer(neighbor);
            }
        }
    }

    return 0; // Target not reachable from source.
}

List&lt;V&gt; tracebackPath(V target, Map&lt;V, V&gt; prev) {
    List&lt;V&gt; s = new LinkedList&lt;V&gt;();
    V u = target;
    while(prev.get(u) != null){
        s.add(u);
        u = prev.get(u);
    }
    return s.size();
}


Advice 3

if(u == target)


Beware. I suggest you implement equals for whatever node type you use and change the above to 

if (u.equals(target))


Advice 4

Since you need to map nodes to values in the HashMap s, you need to override a hashCode() as well in the graph node class.

Concluding

Suppose both target and source nodes are very close to each other. My implementation will return very fast because it does not have to run that nasty for loop.

Hope that helps.