-
Notifications
You must be signed in to change notification settings - Fork 0
/
class1.txt~
281 lines (175 loc) · 5.02 KB
/
class1.txt~
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
Eulerian path:
hitting every edge in a graph exactly once.
Every node that is not a start or ending node has to have even degree (even number of edges)
Every start and end node has to have odd degree
If a graph has only even degree nodes, it will also have a eulerian path.
3. NAIVE IMPLEMENTATION OF MULTIPLICATION (linear time)
def naive(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
5. RUSSIAN PEASANTS ALGORITHM (logarithmic)
def russian(a,b):
x = a; y = b
z = 0
while x > 0:
if x % 2 == 1:
z = z + y
y = y << 1
x = x >> 1
return z
8.
import math
def time(n):
""" Return the number of steps
necessary to calculate
`print countdown(n)`"""
return 3 + (2 * math.ceil(n/5.0))
def countdown(x):
y = 0
while x > 0:
x = x - 5
y = y + 1
print y
countdown(50)
print time(6)
9.
# counting steps in naive as a function of a
def naive(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
def time(a):
# The number of steps it takes to execute naive(a, b)
# as a function of a
return (2*a +3)
RUSSIAN ALGO RECURSIVE
def rec_russian(a, b):
if a == 0:
return 0
if a % 2 == 0:
return 2*rec_russian(a/2, b)
return b + 2*rec_russian((a-1)/2, b)
HOMEWORK
3.
# Eulerian Tour Ver 1
#
# Write a function, `create_tour` that takes as
# input a list of nodes
# and outputs a list of tuples representing
# edges between nodes that have an Eulerian tour.
#
def create_tour(nodes):
# your code here
return [(node, nodes[(e+1)%len(nodes)]) for e, node in enumerate(nodes)]
#########
print create_tour([0, 1, 2, 3, 4, 5])
def get_degree(tour):
degree = {}
for x, y in tour:
degree[x] = degree.get(x, 0) + 1
degree[y] = degree.get(y, 0) + 1
return degree
def check_edge(t, b, nodes):
"""
t: tuple representing an edge
b: origin node
nodes: set of nodes already visited
if we can get to a new node from `b` following `t`
then return that node, else return None
"""
if t[0] == b:
if t[1] not in nodes:
return t[1]
elif t[1] == b:
if t[0] not in nodes:
return t[0]
return None
def connected_nodes(tour):
"""return the set of nodes reachable from
the first node in `tour`"""
a = tour[0][0]
nodes = set([a])
explore = set([a])
while len(explore) > 0:
# see what other nodes we can reach
b = explore.pop()
for t in tour:
node = check_edge(t, b, nodes)
if node is None:
continue
nodes.add(node)
explore.add(node)
return nodes
def is_eulerian_tour(nodes, tour):
# all nodes must be even degree
# and every node must be in graph
degree = get_degree(tour)
for node in nodes:
try:
d = degree[node]
if d % 2 == 1:
print "Node %s has odd degree" % node
return False
except KeyError:
print "Node %s was not in your tour" % node
return False
connected = connected_nodes(tour)
if len(connected) == len(nodes):
return True
else:
print "Your graph wasn't connected"
return False
def test():
nodes = [20, 21, 22, 23, 24, 25]
tour = create_tour(nodes)
return is_eulerian_tour(nodes, tour)
9.
# Write a function, `count`
# that returns the units of time
# where each print statement is one unit of time
# and each evaluation of range also takes one unit of time
def count(n):
# Your code here to count the units of time
# it takes to execute clique
return 2 + n + (n*(n-1))/2
print count(4)
def clique(n):
print "in a clique..."
for j in range(n):
for i in range(j):
print i, "is friends with", j
10.
# Find Eulerian Tour
#
# Write a function that takes in a graph
# represented as a list of tuples
# and return a list of nodes that
# you would follow on an Eulerian Tour
#
# For example, if the input graph was
# [(1, 2), (2, 3), (3, 1)]
# A possible Eulerian tour would be [1, 2, 3, 1]
def find_eulerian_tour(graph):
tour=[]
startNode = graph[0][0]
find_tour(startNode,graph,tour)
return tour
def find_tour(node, graph, tour):
for (a,b) in graph:
if a==node:
graph.remove((a,b))
find_tour(b,graph,tour)
elif b==node:
graph.remove((a,b))
find_tour(a,graph,tour)
tour.insert(0,node)
print find_eulerian_tour([(1, 2), (2, 3), (3, 1)])