Uploaded by Paulina Siemińska

Algorithm Exercises Chapter 1 - Intro t...jhallock7 Algorithm Exercises · GitHub.pdf

advertisement
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
1417 lines (1417 loc) · 56.1 KB
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
1/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
2/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
3/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
4/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
5/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
6/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
for
for
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
7/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
i
i
i
i
i
i
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
i
8/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
j
j
j
i
i
i
i
i
i
i
i+k
i+k
i
i
i
i
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
9/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
10/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
11/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
12/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
13/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
14/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
15/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
16/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
17/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
In [1]:
import numpy as np
import random
P = [(1,2),(2,3),(3,1)]
def distance(a, b):
xdist = a[0] - b[0]
ydist = a[1] - b[1]
return (xdist**2 + ydist**2)**.5
def NearestNeighbor(P):
## P is an iterable of tuples.
unvisited = set(P) # List of points not yet visited
n = len(unvisited)
## Number of points
distances = list()
## Initialize with a random point
p0, = random.sample(unvisited, 1) ## comma needed for unpackin
pstart = p0 ## Will need to return to this point later
unvisited.remove(p0)
i = 0
while len(unvisited) > 0:
p1, = random.sample(unvisited, 1)
unvisited.remove(p1)
distances.append(distance(p0, p1))
p0 = p1
i = i + 1
distances.append(distance(pstart, p1))
return sum(distances)
print(NearestNeighbor(P))
5.8863495173726745
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
18/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
In [2]:
def divide1(x, y):
result = 0
while (x - y) >= 0:
x -= y
result += 1
return result
%time divide1(101, 2)
CPU times: user 10 µs, sys: 0 ns, total: 10 µs
Wall time: 11.9 µs
Out[2]: 50
In [3]:
%time divide1(10**7, 2)
CPU times: user 899 ms, sys: 7.21 ms, total: 906 ms
Wall time: 915 ms
Out[3]: 5000000
In [4]:
def divide2(x, y):
result = 0
y2 = y + y
while (x - y2) >= 0:
x -= y2
result += 2
while (x - y) >= 0:
x -= y
result += 1
return result
%time divide2(10**7, 2)
CPU times: user 454 ms, sys: 4.07 ms, total: 458 ms
Wall time: 460 ms
Out[4]: 5000000
In [5]:
%time divide2(10**8, 2)
CPU times: user 4.76 s, sys: 73.8 ms, total: 4.83 s
Wall time: 4.98 s
Out[5]: 50000000
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
19/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
In [6]:
def divide3(x, y):
y = [y]
result = 0
factor = [1]
while True:
if (x - y[-1]) >= 0:
x -= y[-1]
result += factor[-1]
## Try doubling y.
if (x - y[-1] - y[-1]) >= 0:
y.append(y[-1] + y[-1])
factor.append(factor[-1] + factor[-1])
## If y can't be doubled, check and see
## if it can just stay the same.
elif (x - y[-1]) >= 0:
continue
## If not, divide y by 2
## by returning to the previous y.
## If y is len 1, break.
else:
if len(y) == 1:
break
else:
y.pop()
factor.pop()
return result
In [7]:
%time divide3(10**8, 2)
CPU times: user 113 µs, sys: 1 µs, total: 114 µs
Wall time: 123 µs
Out[7]: 50000000
In [8]:
%time divide3(10**1000, 2)
CPU times: user 12.2 ms, sys: 1.32 ms, total: 13.5 ms
Wall time: 14 ms
Out[8]: 500000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
20/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
0000000000
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
21/22
14/08/2023, 11:49
Algorithm_Exercises/Chapter 1 - Intro to Algorithm Design.ipynb at master · jhallock7/Algorithm_Exercises · GitHub
https://github.com/jhallock7/Algorithm_Exercises/blob/master/Chapter 1 - Intro to Algorithm Design.ipynb
22/22
Download