This was intentional since the graphs were assumed to be directed, however, there are duplicate back-edges. You could avoid this by giving an "order" to the nodes you visited, where the first node is "0", the second node you visit is "1", and so on. If you detect a back-edge, it should be 2 -> (any value less than 2) which indicates that it is going backward. If it is 0 -> (Any value more than 0), it indicates that it is going forward and should not be a back-edge.