-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass4.txt~
607 lines (308 loc) · 9.53 KB
/
class4.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
1.
IMPORTANCE RANK
# animal speed weight lifespan brain
# (mph) (kg) (years) mass (g)
animals = [("dog", 46, 35, 13, 280 ),
("elephant", 30, 3500, 50, 6250 ),
("frog", 5, 0.5, 8, 3 ),
("hippopotamus", 45, 1600, 45, 573 ),
("horse", 40, 385, 30, 642 ),
("human", 27, 80, 78, 2000 ),
("lion", 50, 250, 30, 454 ),
("mouse", 8, 0.025, 2, 0.625),
("rabbit", 25, 4, 12, 40 ),
("shark", 26, 230, 20, 92 ),
("sparrow", 16, 0.024, 7, 2 )]
def importance_rank(items, weights):
names = [item[0] for item in items]
scores = [sum([a*b for (a,b) in zip(item[1:], weights)]) for item in items]
results = zip(scores,names)
res2 = sorted(results)
return res2
answer = importance_rank(animals, (2,3,7,1))
for i in range(len(answer)):
print i, answer[i][1], "(", answer[i][0], ")"
SOCIAL NETWORK ANALYSIS
-betweenness centrality (a node is central if most of the shortest paths between arbitrarly chosen pairs in the network end up having to pass through the node)
-closeness centrality (a given node is central if it has short paths to other nodes in the network)
-eigenvector centrality (Eigenvector centrality is a measure of the influence of a node in a network. It assigns relative scores to all nodes in the network based on the concept that connections to high-scoring nodes contribute more to the score of the node in question than equal connections to low-scoring nodes. Google's PageRank is a variant of the Eigenvector centrality measure.[17] Another closely related centrality measure is Katz centrality.)
-node centrality (just has a lot of edges, well connected) (or degree centrality?)
SOME STATISTICS (given list L):
-maximum value in L
-minimum value in L
-midpoint (average of max and min)
-mean (average of values in L)
-mode: most common value in L
-median: "middlest" value in L (half bigger, half smaller)
if L is sorted:
minimum: L[0]
max: L[-1]
midpoint: (L[0]+L[-1])/2
median : L[n/2]
8.
#
# Write partition to return a new array with
# all values less then `v` to the left
# and all values greater then `v` to the right
#
def partition(L, v):
P = [v]
for e in L:
if e<v:
P.insert(0, e)
else:
P.append(e)
return P
print partition([8, 3, 5, 1, 9, 4, 1], 5)
USING PARTITION (changed a bit) FUNCTION TO GET AN UNORDERED TOP K in Big Theta of N (4N)
import random
L = [31, 45, 91, 51, 66, 82, 28, 33, 11, 89, 84, 27, 36]
def partition(L, v):
smaller= []
bigger = []
for val in L:
if val < v: smaller += [val]
if val > v: bigger += [val]
return (smaller, [v], bigger)
def top_k(L, k):
v = L[random.randrange(len(L))]
(left, middle, right) = partition(L, v)
if len(left) == k: return left
if lef(left)+1 == k: return left+[v]
if len(left) > k: return top_k(left, k)
return left+[v]+top_k(right, k-len(left)-1)
print top_k(L, 5)
SUMMARY OF TOP K
sort: nlogn
selection/insertion: n*k
partition: n (little practical use though)
selection/insertion with heap instead of list: n*logk
HEAPS
-insert
-find min
-remove min
16.
#
# Implement remove_min
#
def remove_min(L):
L[0] = L.pop()
down_heapify(L, 0)
return L
def parent(i):
return (i-1)/2
def left_child(i):
return 2*i+1
def right_child(i):
return 2*i+2
def is_leaf(L,i):
return (left_child(i) >= len(L)) and (right_child(i) >= len(L))
def one_child(L,i):
return (left_child(i) < len(L)) and (right_child(i) >= len(L))
# Call this routine if the heap rooted at i satisfies the heap property
# *except* perhaps i to its immediate children
def down_heapify(L, i):
# If i is a leaf, heap property holds
if is_leaf(L, i):
return
# If i has one child...
if one_child(L, i):
# check heap property
if L[i] > L[left_child(i)]:
# If it fails, swap, fixing i and its child (a leaf)
(L[i], L[left_child(i)]) = (L[left_child(i)], L[i])
return
# If i has two children...
# check heap property
if min(L[left_child(i)], L[right_child(i)]) >= L[i]:
return
# If it fails, see which child is the smaller
# and swap i's value into that child
# Afterwards, recurse into that child, which might violate
if L[left_child(i)] < L[right_child(i)]:
# Swap into left child
(L[i], L[left_child(i)]) = (L[left_child(i)], L[i])
down_heapify(L, left_child(i))
return
else:
(L[i], L[right_child(i)]) = (L[right_child(i)], L[i])
down_heapify(L, right_child(i))
return
#########
# Testing Code
#
# build_heap
def build_heap(L):
for i in range(len(L)-1, -1, -1):
down_heapify(L, i)
return L
def test():
L = range(10)
build_heap(L)
remove_min(L)
# now, the new minimum should be 1
print L
assert L[0] == 1
test()
INSERT IN A HEAP
def insert_heap(L, v):
L.append(v)
up_heapify(L, len(L)-1)
NLOGN complexity
HOMEWORK
#2
#
# Given a list of numbers, L, find a number, x, that
# minimizes the sum of the absolute value of the difference
# between each element in L and x: SUM_{i=0}^{n-1} |L[i] - x|
#
# Your code should run in Theta(n) time
#
def minimize_absolute(L):
results = []
for x in L:
sumL = (sum([abs(e-x) for e in L]), x)
results.append(sumL)
return min(results, key=lambda x: x[0])[1]
L = [45, 9, 12, 78, 12, 3, 21, 0]
print minimize_absolute(L)
OR
#
# Given a list of numbers, L, find a number, x, that
# minimizes the sum of the absolute value of the difference
# between each element in L and x: SUM_{i=0}^{n-1} |L[i] - x|
#
# Your code should run in Theta(n) time
#
def minimize_absolute(L):
minValue = None
xValue = 0
results = []
for x in L:
sumL = (sum([abs(e-x) for e in L]), x)
if minValue == None or sumL < minValue:
minValue = sumL
xValue = x
return xValue
L = [45, 9, 12, 78, 12, 3, 21, 0]
print minimize_absolute(L)
BEST VERSION
#
# Given a list of numbers, L, find a number, x, that
# minimizes the sum of the absolute value of the difference
# between each element in L and x: SUM_{i=0}^{n-1} |L[i] - x|
#
# Your code should run in Theta(n) time
#
def median(l):
l = sorted(l)
n = len(l)/2
if len(l) % 2 == 0:
return (l[n]+l[n-1])/2
else:
return l[n]
def minimize_absolute(L):
xValue = median(L)
return xValue
L = [45, 9, 12, 78, 99, 3, 21, 0]
print minimize_absolute(L)
#3
#
# Given a list of numbers, L, find a number, x, that
# minimizes the sum of the square of the difference
# between each element in L and x: SUM_{i=0}^{n-1} (L[i] - x)^2
#
# Your code should run in Theta(n) time
#
import math
def mean(L):
mean = sum(L)/float(len(L))
return mean
def minimize_square(L):
x = mean(L)
return x
L = [2, 2, 3, 4]
print minimize_square(L)
#4
#
# Given a list L of n numbers, find the mode
# (the number that appears the most times).
# Your algorithm should run in Theta(n).
# If there are ties - just pick one value to return
#
from operator import itemgetter
def mode(L):
count = {}
maxValue = 0
maxElement = None
for e in L:
if count.get(e):
count[e] += 1
else:
count[e] = 1
if count[e] > maxValue:
maxValue = count[e]
maxElement = e
return maxElement
#print mode([1, 2, 3, 3, 4, 4, 5, 6, 4])
####
# Test
#
import time
from random import randint
def test():
assert 5 == mode([1, 5, 2, 5, 3, 5])
iterations = (10, 20, 30, 100, 200, 300, 1000, 5000, 10000, 20000, 30000)
times = []
for i in iterations:
L = []
for j in range(i):
L.append(randint(1, 10))
start = time.clock()
for j in range(500):
mode(L)
end = time.clock()
print start, end
times.append(float(end - start))
slopes = []
for (x1, x2), (y1, y2) in zip(zip(iterations[:-1], iterations[1:]), zip(times[:-1], times[1:])):
print (x1, x2), (y1, y2)
slopes.append((y2 - y1) / (x2 - x1))
# if mode runs in linear time,
# these factors should be close (kind of)
print slopes
test()
#5
#
# write up_heapify, an algorithm that checks if
# node i and its parent satisfy the heap
# property, swapping and recursing if they don't
#
# L should be a heap when up_heapify is done
#
def parent(i):
return (i-1)/2
def left_child(i):
return 2*i+1
def right_child(i):
return 2*i+2
def is_leaf(L,i):
return (left_child(i) >= len(L)) and (right_child(i) >= len(L))
def one_child(L,i):
return (left_child(i) < len(L)) and (right_child(i) >= len(L))
def up_heapify(L, i):
if parent(i) >= 0:
if L[parent(i)] > L[i]:
L[parent(i)], L[i] = L[i], L[parent(i)]
up_heapify(L, parent(i))
else:
return
return L
def test():
L = [2, 4, 3, 5, 9, 7, 7]
L.append(1)
up_heapify(L, 7)
assert 1 == L[0]
assert 2 == L[1]
test()
#6