Skip to content

Tag: breadth-first-search

Time Limit Exceed for BFS algorithm

It is leetcode #200, number of islands. My code is It works fine. But when I change the bfs function into It gives me the Time Limit Exceeded error, what could the reasons be? I think these two codes are nearly identical? Answer You are visiting the same state many times: Change to the following:

BFS algorithm in Python

Guys! Can someone help me with this bfs code? I can’t understand how to solve this queue line. Answer To extend your queue with all nodes not yet seen on the path, use set operations: or use a generator expression: Lists don’t support subtraction. You don’t really need to filter the nodes, h…