fixed the bug to handle directed graphs