Skip to content

Commit a3a1d3d

Browse files
committed
graph island
1 parent 90eef08 commit a3a1d3d

File tree

6 files changed

+223
-78
lines changed

6 files changed

+223
-78
lines changed

algorithms/heapsort.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def main():
2626

2727
# 堆的构造方法就是树的遍历过程的逆过程
2828
# 左边,右边,下一层,左边,右边
29-
# 数据结构,队列,
29+
# 数据结构,队列
30+
# 构造一个队列,初始头节点
31+
# 进一个节点,后面加该节点的左右子节点
32+
link_queue = LinkQueue()
33+
for i in range(10):
34+
link_queue.push(i)
35+
link_queue.traverse()
3036

3137

3238
if __name__ == "__main__":

datastructure/LinkedList.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,57 @@
1-
class Node:
2-
def __init__(self, val=None, next=None):
3-
self.val = val
1+
class SLinkedList:
2+
"""
3+
单链表
4+
本身即指针
5+
既是节点,也是指针,Python风格
6+
"""
7+
8+
def __init__(self, data=None, next=None):
9+
self.data = data
410
self.next = next
511

6-
class SLinkedList:
7-
def __init__(self):
8-
self.head = None
912
def traverse(self):
10-
ptr = self.head
11-
# print(ptr.val)
13+
ptr = self
14+
# print(ptr.data)
1215
while ptr:
13-
print(ptr.val)
16+
# print(ptr.data)
17+
yield ptr.data
1418
ptr = ptr.next
1519

1620
# 一种是非遍历的
1721
def reverse(self):
18-
ptr = self.head
22+
ptr = self
1923
while ptr:
2024
ptr = ptr.next
2125
ptr.next.next = ptr
2226

2327
def recursiveReverse(self):
2428
pass
2529

26-
li = SLinkedList()
27-
n1 = Node("Mon")
28-
n2 = Node("Tue")
29-
n3 = Node("Wed")
30-
n4 = Node("Thur")
31-
n5 = Node("Fri")
32-
n6 = Node("Sat")
33-
n7 = Node("Sun")
34-
35-
# linking
36-
n6.next = n7
37-
n5.next = n6
38-
n4.next = n5
39-
n3.next = n4
40-
n2.next = n3
41-
n1.next = n2
42-
li.head = n1
43-
44-
'''
45-
print(e2.next)
46-
#结果为e3内存地址<__main__.Node object at 0x0000001A0F9644BE0>
47-
print(e2.next.val)
48-
#结果为e3所代表的值Wed
49-
'''
50-
51-
li.traverse()
52-
# 翻转链表
5330

31+
if __name__ == "__main__":
32+
33+
n1 = SLinkedList("Mon")
34+
n2 = SLinkedList("Tue")
35+
n3 = SLinkedList("Wed")
36+
n4 = SLinkedList("Thur")
37+
n5 = SLinkedList("Fri")
38+
n6 = SLinkedList("Sat")
39+
n7 = SLinkedList("Sun")
40+
41+
# linking
42+
n6.next = n7
43+
n5.next = n6
44+
n4.next = n5
45+
n3.next = n4
46+
n2.next = n3
47+
n1.next = n2
48+
49+
'''
50+
print(e2.next)
51+
# 结果为e3内存地址<__main__.Node object at 0x0000001A0F9644BE0>
52+
print(e2.next.data)
53+
# 结果为e3所代表的值Wed
54+
'''
55+
56+
n1.traverse()
57+
# 翻转链表 TODO

datastructure/graph.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2019-06-24 20:20:50
4+
# @Author : Your Name ([email protected])
5+
# @Link : https://zh.wikipedia.org/wiki/%E5%9B%BE_(%E6%95%B0%E5%AD%A6)
6+
# @Version : $Id$
7+
8+
import os
9+
import itertools
10+
from linkedlist import SLinkedList
11+
12+
13+
class GraphNode(object):
14+
"""
15+
图的节点,C风格写法
16+
"""
17+
18+
def __init__(self, data=None, adjacent: SLinkedList = None):
19+
self.data = data
20+
self.adjacent = adjacent
21+
self.adjacent_set = None # TODO 使用Python的set数据结构
22+
23+
24+
if __name__ == "__main__":
25+
test = [[1, 2, 3, 4],
26+
[5, 6, 7, 8],
27+
[9, 10, 11, 12]]
28+
# 矩阵转化为图
29+
l = []
30+
h = len(test)
31+
w = len(test[0])
32+
coords = list(itertools.product(list(range(h)), list(range(w))))
33+
for c in coords:
34+
data = (c, test[c[0]][c[1]]) # 坐标元组 和 数据 # 这样数据存了两份,但好处是原来的矩阵不需要
35+
# 双指针法构造邻接链表
36+
adj = None # SLinkedList()
37+
ptr = None
38+
# 邻接表存的是索引,不是指针,索引可以用hash
39+
# 但这里用顺序表
40+
# 上下左右,相邻
41+
for c2 in [(c[0]+1, c[1]), (c[0]-1, c[1]), (c[0], c[1]+1), (c[0], c[1]-1)]:
42+
if (0 <= c2[0] < h) and (0 <= c2[1] < w):
43+
new = SLinkedList(data=c2, next=None)
44+
if not ptr:
45+
ptr = new
46+
adj = ptr
47+
else:
48+
ptr.next = new
49+
ptr = ptr.next
50+
51+
node = GraphNode(data=data, adjacent=adj)
52+
l.append(node) # l中存入元素
53+
54+
print(l[0].adjacent)
55+
# traverse
56+
for _ in l[7].adjacent.traverse():
57+
print("travers coord:", _)
58+
print("travers data:", test[_[0]][_[1]])

heapsort.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

leetcode/num-of-island-graph.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2019-06-24 21:11:06
4+
# @Author : Your Name ([email protected])
5+
# @Link : http://example.org
6+
# @Version : $Id$
7+
8+
import os
9+
import itertools
10+
from linkedlist import SLinkedList
11+
12+
13+
class GraphNode(object):
14+
"""
15+
图的节点,C风格写法
16+
"""
17+
18+
def __init__(self, data=None, adjacent: SLinkedList = None):
19+
self.data = data
20+
self.adjacent = adjacent
21+
self.adjacent_set = None # TODO 使用Python的set数据结构
22+
23+
24+
if __name__ == "__main__":
25+
island_grids = [["1", "1", "1", "1", "0"],
26+
["1", "1", "0", "1", "0"],
27+
["1", "1", "0", "0", "0"],
28+
["0", "0", "0", "0", "0"]]
29+
# 矩阵转化为图
30+
l = []
31+
h = len(test)
32+
w = len(test[0])
33+
coords = list(itertools.product(list(range(h)), list(range(w))))
34+
for c in coords:
35+
data = (c, test[c[0]][c[1]]) # 坐标元组 和 数据 # 这样数据存了两份,但好处是原来的矩阵不需要了
36+
# 双指针法构造邻接链表
37+
adj = None # SLinkedList()
38+
ptr = None
39+
# 邻接表存的是索引,不是指针,索引可以用hash
40+
# 但这里用顺序表
41+
# 上下左右,相邻
42+
for c2 in [(c[0]+1, c[1]), (c[0]-1, c[1]), (c[0], c[1]+1), (c[0], c[1]-1)]:
43+
if (0 <= c2[0] < h) and (0 <= c2[1] < w):
44+
new = SLinkedList(data=c2, next=None)
45+
if not ptr:
46+
ptr = new
47+
adj = ptr
48+
else:
49+
ptr.next = new
50+
ptr = ptr.next
51+
52+
node = GraphNode(data=data, adjacent=adj)
53+
l.append(node) # l中存入元素
54+
55+
print(l[0].adjacent)
56+
# traverse
57+
l[7].adjacent.traverse()

leetcode/num-of-islands-1.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2019-06-24 20:24:23
4+
# @Author : Your Name ([email protected])
5+
# @Link : https://leetcode.com/problems/number-of-islands
6+
# @Version : $Id$
7+
8+
import os
9+
import itertools
10+
11+
12+
def belongTo(c: tuple, island: list):
13+
for _ in island:
14+
if c in [(_[0]+1, _[1]), (_[0]-1, _[1]), (_[0], _[1]+1), (_[0], _[1]-1)]:
15+
return True
16+
return False
17+
18+
19+
class Solution:
20+
def numIslands(self, grid: list) -> int:
21+
if grid == []:
22+
return 0
23+
h = len(grid)
24+
w = len(grid[0])
25+
coords = list(itertools.product(list(range(h)), list(range(w))))
26+
# print(coords)
27+
one_coords = []
28+
for c in coords:
29+
if grid[c[0]][c[1]] == '1':
30+
one_coords.append(c)
31+
# print(one_coords)
32+
islands = []
33+
island = []
34+
for c in one_coords:
35+
if island == []:
36+
island.append(c)
37+
elif belongTo(c, island):
38+
island.append(c)
39+
# print(island)
40+
else:
41+
islands.append(island)
42+
# print(islands)
43+
island = []
44+
if islands == []:
45+
islands.append(island)
46+
# print(len(islands))
47+
return len(islands)
48+
49+
50+
def main():
51+
test = [["1", "1", "1", "1", "0"],
52+
["1", "1", "0", "1", "0"],
53+
["1", "1", "0", "0", "0"],
54+
["0", "0", "0", "0", "0"]]
55+
sol = Solution()
56+
print(sol.numIslands(test))
57+
58+
59+
if __name__ == "__main__":
60+
main()

0 commit comments

Comments
 (0)