more efficient graph girth calculation