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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

binary matrix, find number of islands

A

bfs/dfs, keep visited

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

dfs

A

adsgsdgdfg

How well did you know this?
1
Not at all
2
3
4
5
Perfectly