algorithm - BFS and correctness on the term "VISITED" -
mark x visited list l = x tree t = x while l nonempty choose vertex v front of list process v each unmarked neighbor w mark w visited add end of list add edge vw t
most of code choose mark adjacent node visited before visiting them. won't technically correct add neighbor first , visit them later?
list l = x tree t = x while l nonempty choose vertex v front of list if (v not yet visitd) mark v visited here each unmarked neighbor w add end of list add edge vw t
why every bfs seems mark node visited when did not visit them yet? trying find theoretically correct code bfs. 1 correct?
both algorithms work, second version might add same node list l twice. doesn't affect correctness because of additional check whether node visited, increases memory consumption , requires check. that's why you'll typically see first algorithm in text books.
Comments
Post a Comment