Related
Given a 1d vecotr x of size n, how can we construct an n-by-n matrix X consisting of all the rolled vectors of x in PyTorch?
For example
x = torch.tensor([1,2,3,4])
The expected output is
tensor([[1, 2, 3, 4],
[2, 3, 4, 1],
[3, 4, 1, 2],
[4, 1, 2, 3]])
Is there any better way than this?
N = x.shape[0]
A = torch.zeros(N, N)
for i in range(N):
A[i] = torch.roll(x, -i)
I have written the following code for the generation of all "triangulations" of a regular or convex polygon:
def getTriangles(points,i,j):
print('i={}, j={}'.format(i,j))
ee = []
if j-i<2:
return []
if j-i==2:
return [[[i,i+1,j]]]
for k in range(i+1,j):
print(' k={}'.format(k))
e1= getTriangles(points,i,k)
e2 = getTriangles(points,k,j)
for x in e1:
for y in e2:
e = [[i,k,j]]
e.extend(x)
e.extend(y)
ee.append(e)
if len(e1)==0:
for y in e2:
e = [[i,k,j]]
e.extend(y)
ee.append(e)
if len(e2)==0:
for a in e1:
e = [[i,k,j]]
e.extend(x)
ee.append(e)
print(' e1={}, e2={}, ee={}'.format(e1,e2,ee))
return ee
n=5
tr = getTriangles(range(1,n+1),0,n-1)
print()
print(tr)
print(len(tr))
For n=3,4 it is correct, and in general "navigates" through the right number of possible triangulations (that is the Catalan number) for n=3,4,5,6,7,8, but the triangulations are not unique. here the formatted output for n=5, consisting of a list of triangles (e.g. three vertices in [0,1,2,3,4]):
[[[0, 1, 4], [1, 2, 4], [2, 3, 4]],
[[0, 1, 4], [1, 3, 4], [1, 2, 3]],
[[0, 2, 4], [0, 1, 2], [2, 3, 4]],
[[0, 3, 4], [0, 2, 3], [0, 1, 2]],
[[0, 3, 4], [0, 2, 3], [0, 1, 2]]]
as you can see the last two are equal. Where is the error?
Intuitively the code is more complex than needed.
EDIT As you can see I'm not in bad company for this error: here is Robert Sedgewick, a computer science professor at Princeton University and in the background you see that the n=5 is correct but for n=6 there are double ;-)
The following code seams to work. The change is in the middle. The algorithm fix the edge [i,j] and k moves from i+1 to j-1. The triangle ijk is fixed and split the polygon in three sub-polygons: itself, the polygon i...k, and the polygon, k..j. If k=i+1 or k=j-1, one of the two polygon is degenerate (empty):
def getTriangles(points,i,j):
print('i={}, j={}'.format(i,j))
ee = []
if j-i<2:
return []
if j-i==2:
return [[[i,i+1,j]]]
for k in range(i+1,j): # k is the vertex such that the triangle ikj split the polygon 1j in 2 subspace plus the triangle ikj
print(' k={}'.format(k))
e1= getTriangles(points,i,k)
e2 = getTriangles(points,k,j)
if k==i+1:
for y in e2:
e = [[i,k,j]]
e.extend(y)
ee.append(e)
elif k==j-+1:
for x in e1:
e = [[i,k,j]]
e.extend(x)
ee.append(e)
else:
for x in e1:
for y in e2:
e = [[i,k,j]]
e.extend(x)
e.extend(y)
ee.append(e)
print(' e1={}, e2={}, ee={}'.format(e1,e2,ee))
return ee
n=5
tr = getTriangles(range(1,n+1),0,n-1)
print()
print(tr)
print(len(tr))
Here's a more Pythonic version of the above:
def genTriangles(i, j):
if j - i < 2:
yield []
return
if j - i == 2:
yield [(i, i+1, j)]
return
for k in range(i + 1, j):
for x in genTriangles(i, k):
for y in genTriangles(k, j):
yield x + y + [(i, k, j)]
n = 5
for k, tr in enumerate(genTriangles(0, n - 1), 1):
print(k, tr)
1 [(2, 3, 4), (1, 2, 4), (0, 1, 4)]
2 [(1, 2, 3), (1, 3, 4), (0, 1, 4)]
3 [(0, 1, 2), (2, 3, 4), (0, 2, 4)]
4 [(1, 2, 3), (0, 1, 3), (0, 3, 4)]
5 [(0, 1, 2), (0, 2, 3), (0, 3, 4)]
Here are two related SO questions 1 2 that helped me formulate my preliminary solution.
The reason for wanting to do this is to feed permutations by edit distance into a Damerau-Levenshtein NFA; the number of permutations grows fast, so it's a good idea to delay (N-C) cycle N permutations candidates until (N-C) iterations of the NFA.
I've only studied engineering math up to Differential Equations and Discrete Mathematics, so I lack the foundation to approach this task from a formal perspective. If anyone can provide reference materials to help me understand this problem properly, I would appreciate that!
Through brief empirical analysis, I've noticed that I can generate the swaps for all C cycle N permutations with this procedure:
Generate all 2-combinations of N elements (combs)
Subdivide combs into arrays where the smallest element of each 2-combination is the same (ncombs)
Generate the cartesian products of the (N-C)-combinations of ncombs (pcombs)
Sum pcombs to get a list of the swaps that will generate all C cycle N permutations (swaps)
The code is here.
My Python is a bit rusty, so helpful advice about the code is appreciated (I have the feeling that lines 17, 20, and 21 should be combined. I'm not sure if I should be making lists of the results of itertools.(combinations|product). I don't know why line 10 can't be ncombs += ... instead of ncombs.append(...)).
My primary question is how to solve this question properly. I did the rounds on my own due diligence by finding a solution, but I am sure there's a better way. I've also only verified my solution for N=3 and N=4, is it really correct?
The ideal solution would be functionally identical to heap's algorithm, except it would generate the permutations in decreasing cycle order (by the minimum number of swaps to generate the permutation, increasing).
This is far from Heap's efficiency, but it does produce only the necessary cycle combinations restricted by the desired number of cycles, k, in the permutation. We use the partitions of k to create all combinations of cycles for each partition. Enumerating the actual permutations is just a cartesian product of applying each cycle n-1 times, where n is the cycle length.
Recursive Python 3 code:
from math import ceil
def partitions(N, K, high=float('inf')):
if K == 1:
return [[N]]
result = []
low = ceil(N / K)
high = min(high, N-K+1)
for k in range(high, low - 1, -1):
for sfx in partitions(N-k, K - 1, k):
result.append([k] + sfx)
return result
print("partitions(10, 3):\n%s\n" % partitions(10, 3))
def combs(ns, subs):
def g(i, _subs):
if i == len(ns):
return [tuple(tuple(x) for x in _subs)]
res = []
cardinalities = set()
def h(j):
temp = [x[:] for x in _subs]
temp[j].append(ns[i])
res.extend(g(i + 1, temp))
for j in range(len(subs)):
if not _subs[j] and not subs[j] in cardinalities:
h(j)
cardinalities.add(subs[j])
elif _subs[j] and len(_subs[j]) < subs[j]:
h(j)
return res
_subs = [[] for x in subs]
return g(0, _subs)
A = [1,2,3,4]
ns = [2, 2]
print("combs(%s, %s):\n%s\n" % (A, ns, combs(A, ns)))
A = [0,1,2,3,4,5,6,7,8,9,10,11]
ns = [3, 3, 3, 3]
print("num combs(%s, %s):\n%s\n" % (A, ns, len(combs(A, ns))))
def apply_cycle(A, cycle):
n = len(cycle)
last = A[ cycle[n-1] ]
for i in range(n-1, 0, -1):
A[ cycle[i] ] = A[ cycle[i-1] ]
A[ cycle[0] ] = last
def permutations_by_cycle_count(n, num_cycles):
arr = [x for x in range(n)]
cycle_combs = []
for partition in partitions(n, num_cycles):
cycle_combs.extend(combs(arr, partition))
result = {}
def f(A, cycle_comb, i):
if i == len(cycle_comb):
result[cycle_comb].append(A)
return
if len(cycle_comb[i]) == 1:
f(A[:], cycle_comb, i+1)
for k in range(1, len(cycle_comb[i])):
apply_cycle(A, cycle_comb[i])
f(A[:], cycle_comb, i+1)
apply_cycle(A, cycle_comb[i])
for cycle_comb in cycle_combs:
result[cycle_comb] = []
f(arr, cycle_comb, 0)
return result
result = permutations_by_cycle_count(4, 2)
print("permutations_by_cycle_count(4, 2):\n")
for e in result:
print("%s: %s\n" % (e, result[e]))
Output:
partitions(10, 3):
[[8, 1, 1], [7, 2, 1], [6, 3, 1], [6, 2, 2], [5, 4, 1], [5, 3, 2], [4, 4, 2], [4, 3, 3]]
# These are the cycle combinations
combs([1, 2, 3, 4], [2, 2]):
[((1, 2), (3, 4)), ((1, 3), (2, 4)), ((1, 4), (2, 3))]
num combs([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [3, 3, 3, 3]):
15400
permutations_by_cycle_count(4, 2):
((0, 1, 2), (3,)): [[2, 0, 1, 3], [1, 2, 0, 3]]
((0, 1, 3), (2,)): [[3, 0, 2, 1], [1, 3, 2, 0]]
((0, 2, 3), (1,)): [[3, 1, 0, 2], [2, 1, 3, 0]]
((1, 2, 3), (0,)): [[0, 3, 1, 2], [0, 2, 3, 1]]
((0, 1), (2, 3)): [[1, 0, 3, 2]]
((0, 2), (1, 3)): [[2, 3, 0, 1]]
((0, 3), (1, 2)): [[3, 2, 1, 0]]
I'm stuck in this problem and I would need some help:
Given an array arr, in each step, 1, 2 or 5 units have to be incremented to all but one item of the array (same amount of units to all of them). The goal is to find the minimum number of steps to all items to be equal.
First example
arr = [1, 1, 5]
1) [1 (+2), 1 (+2), 5] = [3, 3, 5]
2) [3 (+2), 3 (+2), 5] = [5, 5, 5]
Solution: 2 steps
Second example
arr = [2, 2, 3, 7]
1) [2 (+1), 2 (+1), 3, 7 (+1)] = [3, 3, 3, 8]
2) [3 (+5), 3 (+5), 3 (+5), 8] = [8, 8, 8, 8]
Solution: 2 steps
I have tried some things but I'm really stuck.
I consider a base case when all items are already equal. In another case, I try to find all the possible solutions by incrementing 1, 2 and 5 to every item but one in the array:
def equal(arr):
if (allElementsIdentical(arr)):
return 0
min = sys.maxsize
for i in [1, 2, 5]:
for j in range(len(arr)):
#Increment all items by "i", except item at index "j"
newArr = arr.copy()
for k in range(j):
newArr[k] += i
for k in range(j + 1, len(newArr)):
newArr[k] += i
movements = 1 + equal(newArr)
if movements < min:
min = movements
return min
This solution doesn't work because recursion never ends. E.g.
[1, 1, 5] -> [1, 2, 6] -> [1, 3, 7] -> [1, 4, 8] -> [1, 5, 9] -> ...
Is it my initial approach correct? How can I break it down in subproblems properly? How can I get the recurrence relation?
(I'm learning Python, so any comment about the syntax is also appreciated)
To me adding 1, 2 or 5 to all but one element seems a lot easier to think about as subtracting 1, 2 or 5 from just one element.
[1, 1, 5] -> 5 - 2 -> 3 - 2
[2, 2, 3, 7] -> 3 - 1 -> 7 - 5
To construct a recurrence, we can use a histogram and consider that to shift any value would cost its frequency in operations. Since we are allowed to reduce by 1, we can easily set a lower-bound for the lowest target we might need to shift all values to. Since the lowest could be reached by any other value, shifting all values down to (lowest-5) (as the HackerRank editorial notes), would involve n more operations than shifting all elements down to the lowest, as we first shift all elements to the lowest, then apply (-5) to each one.
Also noted by the editorial is that the smallest number of operations, k, to shift x to target 0, can be found in O(1) by the greedy
k = x / 5 + (x % 5) / 2 + (x % 5) % 2
Since you've asked to rather try and form a recurrence, under these circumstances, we would be left with solving the coin change problem (coins [5, 2, 1]) for each value in the histogram to reach the target. These are independent: it makes no difference the order by which we apply coin_change to each value to find the number of operations needed, which we then multiply by the value's frequency. Tallying the total operations for all values to reach each target, we choose the least.
We want to replace this problem with one that produces the same answer but will be easier to evaluate.
The trick to making it easier to evaluate with a dynamic programming approach is to have the same results show up in lots of places. And therefore we have to replace equivalent versions of this problem with normalized ones.
For a start, the answer doesn't depend on the order that the elements of the array are in. So we can replace our arrays with arrays sorted from smallest to largest. The operation is now we add x to everything but one, then reorder to canonical form.
Next, the answer doesn't depend on the value of the smallest element. So we can subtract that value from all entries. The operation is now we add x to everything but one, then reorder to canonical form, then subtract the smallest from everything.
This greatly reduces our problem. Enough that a breadth first search has a shot. But we have one more trick to pull. And that trick is that it doesn't matter what order we apply the operations in. And therefore we can apply all of our 5 operations before our 2 operations before our 1 operations. With this trick, we can replace each normalized node with (node, last_operation) and a starting last_operation of 5. The reason why this is a win is that now we have an upper bound for the rest of an A* search. That bound is the current number of steps + sum of ceil(node[i] / last_operation).
And now this can be solved with A* search. Let's do your examples by hand. Using the notation, (total cost, normalized, last_operation, steps).
Example 1: [1, 1, 5]
We normalize to [0, 0, 4] and have a last_operation of 5 and a cost of 0+0+1 = 1. No steps taken. So we start with:
(1, [0, 0, 4], 5)
We take that out, and consider our operations. We get the following for operation 5:
[0, 0, 4] + [5, 5, 0] = [5, 5, 4] => [0, 1, 1] # cost 1 + 0+1+1 = 3
[0, 0, 4] + [5, 0, 5] = [5, 0, 9] => [0, 5, 9] # cost 1 + 0+1+2 = 4
[0, 0, 4] + [0, 5, 5] = [0, 5, 9] => [0, 5, 9] # cost 1 + 0+1+2 = 4 DUP
And for operation 2 we get:
[0, 0, 4] + [2, 2, 0] = [2, 2, 4] => [0, 0, 2] # cost 1 + 0+0+1 = 2
[0, 0, 4] + [2, 0, 2] = [2, 0, 4] => [0, 2, 4] # cost 1 + 0+1+2 = 4
[0, 0, 4] + [0, 2, 2] = [0, 2, 4] => [0, 2, 4] # cost 1 + 0+1+2 = 4 DUP
And for operation 1 we get:
[0, 0, 4] + [1, 1, 0] = [1, 1, 4] => [0, 0, 3] # cost 1 + 0+0+3 = 4
[0, 0, 4] + [1, 0, 1] = [1, 0, 4] => [0, 1, 4] # cost 1 + 0+1+5 = 6
[0, 0, 4] + [0, 1, 1] = [0, 1, 4] => [0, 1, 4] # cost 1 + 0+1+5 = 6 DUP
We stick the 7 non-dups into our priority queue, and the best that comes out looks like this:
(total cost, normalized, last_operation, steps)
( 2, [0,0,2], 2, 1)
We then try operations 2 and 1 on this, and of course find that one of the outcomes is [0, 0, 0] after 2 steps.
Suppose I have an array, namely Map. Map[i][j] means the distance between area i and area j. Under this definition, we get:
a) Map[i][i] always equals 0.
b) Map[i][k] <= Map[i][j] + Map[j][k] for all i,j,k
I want to build a function func(Map,k) returning a metric D, while D[i][j] is the shortest distance of a route from area i to area j, and this route should pass through at least k different area.
This is my python code to do so:
def func(Map,k):
n=len(Map)
D_temp = [list(x) for x in Map]
D = [list(x) for x in Map]
for m in range(k - 1):
for i in range(n):
for j in range(n):
tmp = [D[i][x] + Map[x][j] for x in range(n) if x != i and x != j]
D_temp[i][j] = min(tmp)
D = [list(x) for x in D_temp]
return D
func([[0, 2, 3], [2, 0, 1], [3, 1, 0]],2)
return a distance metric D which equals [[4, 4, 3], [4, 2, 5], [3, 5, 2]]
D[0][0] equals 4, because the shortest route from area0 to area0 which pass through at least 2 area is {area0-->area1-->area0}, and the distance of the route is Map[0][1]+Map[1][0]=2+2=4
Wanted to know what would be the best way to do that?
You can use the A* algorithm for this, using Map[i][j] as the heuristic for the minimum remaining distance to the target node (assuming that, as you said, Map[i][j] <= Map[i][x] + Map[x][j] for all i,j,x). The only difference to a regular A* would be that you only accept paths if they have a minimum length of k.
import heapq
def min_path(Map, k, i, j):
heap = [(0, 0, i, [])]
while heap:
_, cost, cur, path = heapq.heappop(heap)
if cur == j and len(path) >= k:
return cost
for other in range(len(Map)):
if other != cur:
c = cost + Map[cur][other]
heapq.heappush(heap, (c + Map[other][j], c, other, path + [other]))
Change your func to return a list comprehension using this min_path accordingly.
def func(Map, k):
n = len(Map)
return [[min_path(Map, k, i, j) for i in range(n)] for j in range(n)]
res = func([[0, 2, 3], [2, 0, 1], [3, 1, 0]], 2)
This gives me the result [[4, 4, 3], [4, 2, 3], [3, 3, 2]] for len(path) >= k, or [[4, 4, 3], [4, 2, 5], [3, 5, 2]] for len(path) == k.