added BFS to calculate APL