并查集:
并查集的初始化:
自己的root先初始化为自己。
如果需要合并ab, 则把b的老大指向a
并查集优化1:
需要多创建一个数据结构rank去查看深度
将短链的root接在长链上,降低查找深度
xRoot.rank yRoot.rank rank是一个变量,用来表示查找深度
优化2:
200. 岛屿问题:
自己的解法1:DFS
注意问题:
如果发现回溯超过最大限制,一定是位置置0存在问题。
1.调用函数: 函数名(变量) 一定是小括号
2.岛屿问题这里是引号“ ” 的字符,不是0和1
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
self.row = len(grid)
self.col = len(grid[0])
self.count = 0
self.grid =grid
for i in range(self.row):
for j in range(self.col):
if self.grid[i][j] == "1":
self.grid[i][j] = "0"
self.count += 1
self.put_zero(i,j)
return self.count
def put_zero(self,i,j):
if i+1<self.row:
if self.grid[i+1][j] == "1":
self.grid[i+1][j] = "0"
self.put_zero(i+1,j)
if j+1<self.col:
if self.grid[i][j+1] == "1":
self.grid[i][j+1] = "0"
self.put_zero(i,j+1)
if i-1>=0:
if self.grid[i-1][j] == "1":
self.grid[i-1][j] = "0"
self.put_zero(i-1,j)
if j-1>=0:
if self.grid[i][j-1] == "1":
self.grid[i][j-1] = "0"
self.put_zero(i,j-1)
return
解法2:
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
self.row = len(grid)
self.col = len(grid[0])
self.count = 0
self.grid =grid
for i in range(self.row):
for j in range(self.col):
if self.grid[i][j] == "1":
self.put_zero(i,j)
self.count += 1
return self.count
def put_zero(self,i,j):
if not 0<=i<self.row or not 0<=j<self.col or self.grid[i][j] == '0':return
self.grid[i][j] = '0'
self.put_zero(i+1,j)
self.put_zero(i-1,j)
self.put_zero(i,j+1)
self.put_zero(i,j-1)
return
如何遍历上下左右:
self.dx = [-1,1,0,0]
self.dy = [0,0,-1,1]
for k in range(4):
self.dfs(x+self.dx[k],y+self.dy[k])
放入这道题中:
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
self.row = len(grid)
self.col = len(grid[0])
self.count = 0
self.grid =grid
self.dx = [-1,1,0,0]
self.dy = [0,0,-1,1]
for i in range(self.row):
for j in range(self.col):
if self.grid[i][j] == "1":
self.count += 1
self.put_zero(i,j)
return self.count
def put_zero(self,i,j):
if (not 0 <= i <self.row) or (not 0<=j<self.col ) or self.grid[i][j] == "0": return
else:
self.grid[i][j] = "0"
for k in range(4):
self.put_zero(i+self.dx[k],j+self.dy[k])
return
这道题的并查集class代码:
class UnionFind(object): #并查集
def __init__(self,grid):
m,n = len(grid), len(grid[0])
self.count = 0
self.parent = [-1] *(m*n) #root
self.rank = [0]*(m*n) #深度
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
self.parent[i*n+j] = i*n +j
self.count += 1
def find(self,i):
if self.parent[i] != i:
self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def union(self,x,y):
rootx = self.find(x)
rooty = self.find(y)
if rootx != rooty:
if self.rank[rootx] >self.rank[rooty]:
self.parent[rooty] = rootx
elif self.rank[rooty] >self.rank[rootx]:
self.parent[rootx] = rooty
else:
self.parent[rooty] = rootx
self.rank[rootx] += 1
self.count -= 1
用并查集的思想去处理这道题目:
这里我们把二维grid的parent给平面化,用一维数组去表示它的parent
并查集思路:
init: 里面,将每一个元素的parent指向自身。 遍历每一个元素的时候增加集合数量。此时集合的总数量为元素总数量
find: 用递归去找这个元素的头
union:用第一种优化方式,引入rank,从而在合并的时候将短链接在长链的root上面。如果两条链相等,接在哪一条都行。若是y接在x的头上,则x的rank要+1。每次union合并的时候,集合总数减1.
遍历还有一个办法:用
directions = [(0,1),(0,-1),(1,0),(-1,0)]
for d in directions:
row = i+d[0]
col = j+d[1]
这样去判断上下左右的方向也是可以的
回到这道题目本身:
将grid初始化为uf的对象。
循环每个为1的节点,若它上下左右的节点不为1,则将他们合并
最后数有多少个集合,则为多少个岛屿
class UnionFind(object): #并查集
def __init__(self,grid):
m,n = len(grid), len(grid[0])
self.count = 0
self.parent = [-1] *(m*n) #root
self.rank = [0]*(m*n) #深度
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
self.parent[i*n+j] = i*n +j
self.count += 1
def find(self,i):
if self.parent[i] != i:
self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def union(self,x,y):
rootx = self.find(x)
rooty = self.find(y)
if rootx != rooty:
if self.rank[rootx] >self.rank[rooty]:
self.parent[rooty] = rootx
elif self.rank[rooty] >self.rank[rootx]:
self.parent[rootx] = rooty
else:
self.parent[rooty] = rootx
self.rank[rootx] += 1
self.count -= 1
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if not grid or not grid[0]:
return 0
uf = UnionFind(grid)
directions = [(0,1),(0,-1),(1,0),(-1,0)]
m,n = len(grid),len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == '0':
continue
for d in directions:
row = i+d[0]
col = j+d[1]
if 0<=row<m and 0<=col<n and grid[row][col] != '0':
uf.union(i*n+j,row*n+col)
return uf.count
547.朋友圈问题。不用DFS,用并查集解决:
class Unionfind(object):
def __init__(self,isConnected):
self.r = len(isConnected)
self.count = 0
self.root = [-1]*(self.r)
self.rank = [0]*(self.r)
for i in range(self.r):
self.root[i] = i
self.count += 1
def find(self,x):
while self.root[x] != x:
x = self.root[x]
return self.root[x]
def union(self,x,y):
if x == y:
return
rootx = self.find(x)
rooty = self.find(y)
if rootx == rooty:
return
if self.rank[rootx] > self.rank[rooty]:
self.root[rooty] = rootx
elif self.rank[rootx] < self.rank[rooty]:
self.root[rootx] = rooty
else:
self.root[rooty] = rootx
self.rank[rootx] += 1
self.count -= 1
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
row = len(isConnected)
uf = Unionfind(isConnected)
for i in range(row):
for j in range(i,row):
if isConnected[i][j] == 1 :
uf.union(i,j)
return uf.count