-
Notifications
You must be signed in to change notification settings - Fork 0
/
tu.py
160 lines (157 loc) · 4.88 KB
/
tu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import json
class Stack(object):
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
class Queue(object):
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def peek(self):
return self.items[0]
def size(self):
return len(self.items)
def add(self, item):
self.items.append(item)
def pop(self):
return self.items.pop(0)
class ArcNode(object):
adjvex = 0
next = None
def __init__(self,adjvex = 0,next = None):
self.adjvex = adjvex
self.next = next
class VNode(object):
data = 0
firstarc = None
def __init__(self,data = 0,firstarc = None):
self.data = data
self.firstarc = firstarc
class ALGraph(object):
Adjlist = []
vexnum = 0
arcnum = 0
def __init__(self,Adjlist = []):
sum = 0
self.Adjlist = Adjlist
for v in Adjlist:
t = v.firstarc
while(t != None):
sum += 1
t = t.next
self.vexnum = len(Adjlist) - 1
self.arcnum = int(sum/2)
def outputAdjlist(l = ALGraph.Adjlist):
for i in range(1,len(l)):
print(l[i].data,end="-->")
t = l[i].firstarc
while(t != None):
if(t.next != None): print(t.adjvex,end="->")
else: print(t.adjvex)
t = t.next
def dfs(ALGraph = ALGraph,data = 1):
vStack = Stack()
arcset = []
vexset = []
flag = [0 for _ in range(ALGraph.vexnum+1)]
vStack.push(ALGraph.Adjlist[data])
temp = ALGraph.Adjlist[data].firstarc
while(True):
if(temp != None):
if(flag[temp.adjvex] == 0):
#flag[temp.adjvex] = 1
if(flag[vStack.peek().data] == 0):
flag[vStack.peek().data] = 1
#print(vStack.peek().data)
vexset.append(vStack.peek().data)
vStack.push(ALGraph.Adjlist[temp.adjvex])
temp = vStack.peek().firstarc
P = vStack.pop()
arcset.append((vStack.peek().data, P.data))
vStack.push(P)
else:
if (flag[vStack.peek().data] == 0):
flag[vStack.peek().data] = 1
#print(vStack.peek().data)
vexset.append(vStack.peek().data)
temp = temp.next
else:
vStack.pop()
if(vStack.is_empty() == True): break
temp = vStack.peek().firstarc.next
return vexset,arcset
def initArc(str=str):
l = str.split(",")
h = ArcNode
c = ArcNode
h = c
for i in range(len(l)):
t = ArcNode(int(l[i]))
c.next = t
c = c.next
return h.next
def initimg(imgfile):
initjson = open(imgfile,encoding='UTF-8')
init = json.load(initjson)
imgV = [VNode for i in range(len(init) + 1)]
for key in init.keys():
#print(key)
imgV[int(key)] = VNode(int(key))
imgV[int(key)].firstarc = initArc(init[key])
return ALGraph(imgV)
def bfs(ALGraph = ALGraph,data = 1):
vQueue = Queue()
arcset = []
vexset = []
flag = [0 for _ in range(ALGraph.vexnum + 1)]
for i in range(data,len(ALGraph.Adjlist)):
if(flag[ALGraph.Adjlist[i].data] == 0):
flag[ALGraph.Adjlist[i].data] = 1
#print("1,,",ALGraph.Adjlist[i].data)
vexset.append(ALGraph.Adjlist[i].data)
vQueue.add(ALGraph.Adjlist[i])
while(vQueue.is_empty() != True):
u = vQueue.pop()
temp = u.firstarc
while(temp != None):
if(flag[temp.adjvex] == 0):
flag[temp.adjvex] = 1
#print("2,,",temp.adjvex)
vexset.append(temp.adjvex)
arcset.append((u.data,temp.adjvex))
vQueue.add(ALGraph.Adjlist[temp.adjvex])
temp = temp.next
return vexset,arcset
if __name__ == "__main__":
V = []
V.append(VNode)
for i in range(1, 9):
V.append(VNode(i))
V[1].firstarc = ArcNode(2, ArcNode(3))
V[2].firstarc = ArcNode(4, ArcNode(5, ArcNode(1)))
V[3].firstarc = ArcNode(6, ArcNode(7, ArcNode(1)))
V[4].firstarc = ArcNode(2, ArcNode(8))
V[5].firstarc = ArcNode(8, ArcNode(2))
V[6].firstarc = ArcNode(7, ArcNode(3))
V[7].firstarc = ArcNode(6, ArcNode(3))
V[8].firstarc = ArcNode(5, ArcNode(4))
num = 0
img = ALGraph(V)
#bfs(img,4)
c,d = bfs(img, 2)
#outputAdjlist(V)
print(c,d)
test = initimg("image.json")
vexset, arcset = bfs(test, 1)
#bfs(test,1)
print(vexset, arcset)