added diagram explaining algorithm