-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
116 lines (111 loc) · 3.18 KB
/
image.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
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 imageNode(object):
def __init__(self,data,next = None):
self.data = data
self.next = next
self.marked = 0
def allmarked(self):
temp = self
while (temp != None):
if (temp.marked == 1):
temp = temp.next
else:
return 0
return 1
def dfs(image, i, j):
m = Stack()
m.push(image[i])
temp = image[i].next
#print("(", m.peek().data, end=",")
while (m.is_empty() != True):
#print(m.peek().data)
m.peek().marked = 1
if (temp != None):
print("(", m.peek().data, end=",")
if(temp.marked == 0):
m.push(image[temp.data])
m.peek().marked = 1
if (m.peek().data == j):
print(j,")")
return 1
else:
if (m.peek().next == None):
print(m.peek().data, ")", end=" -> ")
m.pop()
else:
print(m.peek().data,")",end=" -> ")
#return dfs(image, m.peek().data, j)
m.push(image[m.peek().data])
temp = image[m.peek().data]
temp = temp.next
else: m.pop()
#print("(",m.peek().data,end=",")
return 0
def bfs(image,i,j):
m = Queue()
m.add(image[i])
m.peek().marked = 1
temp = image[i].next
while (temp != None):
print(m.peek().data)
if (temp.marked == 0):
m.add(image[temp.data])
m.peek().marked = 1
if (temp.data == j): return 1
temp = temp.next
else:
temp = temp.next
m.pop()
while(m.is_empty() != True):
if (m.peek().next == None):
m.pop()
else:
return bfs(image, m.peek().data, j)
return 0
def dfsimg(V, i):
m = Stack()
temp = V[i]
temp.marked = 1
m.push(temp)
while(m.is_empty() != True):
#print(m.peek().data)
temp = V[temp.data]
temp = temp.next
if(temp != None):
if (V[temp.data].marked == 0):
temp.marked = 1
V[temp.data].marked = 1
print("(",m.peek().data,end=",")
m.push(V[temp.data])
print(m.peek().data,")")
else:
temp = temp.next
temp.marked = 1
if(m.peek().allmarked() == 1): m.pop()
#print("(",m.peek().data,end=",")
return 0