Graph Flashcards
Algorithms for graph
1
Q
breath-first-search
A
elegant bfs using list comprehension, also can be used in tree traverse (row by row)
bfs = [target]
visited = set(bfs)
for i in range(K):
bfs = [y for x in bfs for y in links[x] if y not in visited]
visited |= set(bfs)
2
Q
binary matrix, find number of islands
A
bfs/dfs, keep visited
3
Q
binary matrix, find 0s that are surrounded by all 1s
A
bfs start from boundaries, if 0 are connected to boundaries, set it to 2
4
Q
dfs
A
adsgsdgdfg