Saturday, July 25, 2009

Ex-3.19

Method-1:
We basically keep on visiting next pair in the list. Whenever we visit a pair, we tag it saying that it was visited before. Moreover when we visit a pair, we check if it is already visited and if we find an already visited pair before reaching the list end then it is cyclic. The issue with this algorithm is that it is destructive in nature that is it will change the input list.
(define (visit-car list)
(set-car! list (cons '*visited* (car list))))
(define (visited? x)
(and (pair? x) (eq? (car x) '*visited*)))
(define (has-cycle? list)
(cond
((null? list) #f)
((visited? (car list)) #t)
(else (visit-car list)
(has-cycle? (cdr list)))))

Method-2:
This is a partial implementation of idea of Floyd's cycle finding algorithm aka tortoise and the hare algorithm, where we maintain two pointers(tortoise, hare), second(hare) moving twice as fast the first(tortoise) one and keep comparing them in each iteration. List has cycles if pointers become equal before encountering end of list.
(define (has-cycle? list)
(define (has-cycle-aux? l1 l2)
(cond
((eq? l1 l2) #t)
(else
(if (not (pair? (cdr l2))) #f
(has-cycle-aux? (cdr l1) (cddr l2))))))
(if (null? list) #f
(has-cycle-aux? list (cdr list))))

No comments:

Post a Comment