Algorithm to find AP on a given graph