I have python lists like follows
>>>[[2, 0, 10], [2, 0, 11], [2, 1, 12], [2, 1, 13], [4, 3, 5], [4, 3, 7], [7, 6, 8], [7, 6, 10], [10, 9, 2], [10, 9, 11], [13, 14, 15]]
>>>[[0, 1, 3], [0, 1, 6], [3, 2, 0], [3, 2, 6], [3, 4, 5]]
I want to extract largest list set containing uncommon values. For example, for latter list, the largest independent set should be [[0, 1, 6], [3, 4, 5]], whereas for the former, it should be [[2, 0, 12], [4, 3, 5], [7, 6, 8], [10, 9, 11], [13, 14, 15]]. It may be similar with maximum independent set problem, but I have no idea about this as I know nothing about graphs. So any suggestion to solve this problem? Thanks in advance.
Related
I would like to achieve the following in an efficient way in numpy. Suppose I have a matrix
A = np.asarray([[1, 2], [3, 4]])
and a vector of the following form
B = np.asarray([7, 8, 9])
What I would like to achieve is the following: Take the first element in B and add it to all elements in A. Then we take the second element of B and so on. At the end I would like to get a matrix of the form (A.shape[0]*B.shape[0],A.shape[1]). The result in this case should be (hopefully on typo):
np.asarray([[8, 9], [10, 11], [9, 10], [11, 12], [10, 11], [12, 13]])
Out[142]:
array([[ 8, 9],
[10, 11],
[ 9, 10],
[11, 12],
[10, 11],
[12, 13]])
Add two new axes to B to its end and then perform addition, thus leveraging broadcasting and finally a reshape for 2D output such that the number of columns is same as in A -
In [396]: (A + B[:,None,None]).reshape(-1,A.shape[-1])
Out[396]:
array([[ 8, 9],
[10, 11],
[ 9, 10],
[11, 12],
[10, 11],
[12, 13]])
Also, this is basically outer addition, so with the builtin -
In [399]: np.add.outer(B,A).reshape(-1,A.shape[-1])
Out[399]:
array([[ 8, 9],
[10, 11],
[ 9, 10],
[11, 12],
[10, 11],
[12, 13]])
You can also achieve this with np.tile:
In [42]: (A + np.tile(B[:, None, None], A.shape)).reshape(
A.shape[0] * B.shape[0], A.shape[1]
)
Out[42]:
array([[ 8, 9],
[10, 11],
[ 9, 10],
[11, 12],
[10, 11],
[12, 13]])
I have this nested list:
list = [[1, 2, 3, 4],
[2, 7, 2, 1],
[3, 3, 7, 5],
[4, 4, 1, 7]]
And I'm trying to skip the first list of this nested list, and the first element of each list. I want it to become like this:
list = [[7, 2, 1],
[3, 7, 5],
[4, 1, 7]]
So far I have this:
% skip first list in list of lists
skip_first_list([_|Tail], Tail).
% attemping to skip first element in each of the lists
skip_first_list([[_ | HeadTail] | Tail], X) :-
skip_first_list(Tail, R),
append(R, [HeadTail], X).
Which does not produce the correct result:
?- skip_first_list([[1, 2, 3, 4], [2, 7, 2, 1], [3, 3, 7, 5], [4, 4, 1, 7]], X).
X = [[2, 7, 2, 1], [3, 3, 7, 5], [4, 4, 1, 7]] ;
X = [[3, 3, 7, 5], [4, 4, 1, 7], [2, 3, 4]] ;
X = [[4, 4, 1, 7], [7, 2, 1], [2, 3, 4]] ;
X = [[3, 7, 5], [7, 2, 1], [2, 3, 4]] ;
false.
Whereas I'm after this answer:
X = [[7, 2, 1], [3, 7, 5], [4, 1, 7]]
My result so far seems to be showing I'm appending in a reverse/incorrect order, How can I fix this? I don't really understand what order Prolog evaluates expressions. Any any would be appreciated.
Well the specification is that you provide a list of lists and that:
the first sublist is ignored (not part of the output); and
that for the remaining sublists, all heads are ignored as well.
So we better split this into two predicates:
remove_heads/2, which removes the heads of all sublists; and
remove_upper_left/2 which remove the first sublist, and then uses the above predicate to pop the heads of the sublists.
We can perform remove_heads/2 for instance with recursion:
remove_heads([],[]).
remove_heads([[_|H]|T],[H|T2]) :-
remove_heads(T,T2).
finally our remove_upper_left/2 simply ignores the head of the list, and makes a call to remove_heads with the tail:
remove_upper_left([_|T],T2) :-
remove_heads(T,T2).
Or in full:
remove_heads([],[]).
remove_heads([[_|H]|T],[H|T2]) :-
remove_heads(T,T2).
remove_upper_left([_|T],T2) :-
remove_heads(T,T2).
This then produces:
?- remove_upper_left([[1, 2, 3, 4], [2, 7, 2, 1], [3, 3, 7, 5], [4, 4, 1, 7]],X).
X = [[7, 2, 1], [3, 7, 5], [4, 1, 7]].
and works in the opposite direction as well:
?- remove_upper_left(X, [[1, 2, 3, 4], [2, 7, 2, 1], [3, 3, 7, 5], [4, 4, 1, 7]]).
X = [_G1364, [_G1370, 1, 2, 3, 4], [_G1376, 2, 7, 2, 1], [_G1382, 3, 3, 7, 5], [_G1388, 4, 4, 1|...]].
So here it prepends a variable to every list, and prepends a variable (possibly a sublist) to the output.
Furthermore we have here two predicates at the price of one: we can also use remove_heads/2 in the future if we want to pop the heads of all the sublists in a list of lists.
Why in the following example is appending to the big_l in the for loop changes also the last lists already added to the big_l?
l=[1,2,3,4,5]
big_l=[]
def f(ll):
x=ll.pop(0)
ll.append(x)
return ll
for i in range(4):
big_l.append(l)
print l,big_l
l=f(l)
It prints:
[1, 2, 3, 4, 5] - [[1, 2, 3, 4, 5]]
[2, 3, 4, 5, 1] - [[2, 3, 4, 5, 1], [2, 3, 4, 5, 1]]
[3, 4, 5, 1, 2] - [[3, 4, 5, 1, 2], [3, 4, 5, 1, 2], [3, 4, 5, 1, 2]]
[4, 5, 1, 2, 3] - [[4, 5, 1, 2, 3], [4, 5, 1, 2, 3], [4, 5, 1, 2, 3], [4, 5, 1, 2, 3]]
I have a large graph made in networkx, and I used the clique function to find the largest clique for each node:
thecliques = nx.node_clique_number(mygraph)
However, no matter how much I search, I am unable to find the members of the maximal clique for each node. Does anyone know how to do this?
May not be computationally optimal as it enumerates all cliques, but you can use:
G= nx.barabasi_albert_graph(20, 2, seed=1234)
dict_of_cliques=nx.cliques_containing_node(G,nodes=None,cliques=None)
print dict_of_cliques
Output is a dictionary {node:list of lists of nodes in clique, including node}. IE:
{0: [[0, 3, 2], [0, 3, 7], [0, 4, 8], [0, 5], [0, 9, 13], [0, 15], [0, 17], [0, 19]], 1: [[1, 2, 11], [1, 4], [1, 5]], 2: [[0, 3, 2], [1, 2, 11], [6, 2], [18, 2]], 3: [[0, 3, 2], [0, 3, 7], [12, 3]], 4: [[0, 4, 8], [1, 4], [6, 4], [12, 4], [18, 4]], 5: [[0, 5], [1, 5], [14, 5]], 6: [[6, 2], [6, 10, 16], [6, 10, 9], [6, 4]], 7: [[0, 3, 7]], 8: [[0, 4, 8]], 9: [[0, 9, 13], [6, 10, 9], [14, 9]], 10: [[6, 10, 16], [6, 10, 9], [10, 19]], 11: [[1, 2, 11], [11, 17]], 12: [[12, 3], [12, 4], [12, 15]], 13: [[0, 9, 13]], 14: [[14, 9], [14, 5]], 15: [[0, 15], [12, 15]], 16: [[6, 10, 16]], 17: [[0, 17], [11, 17]], 18: [[18, 2], [18, 4]], 19: [[0, 19], [10, 19]]}
Crucially you can see here that the maximal cliques may not be unique, so it is up to you how you will select the largest clique.
I have a number, x, and I wish to find all unique ways to write a*b*c. By unique I mean 2*3*5 is the same as 3*2*5 or 5*3*2.
I've got a working algorithm that takes the prime factorization of x and then divvies up factors into three bins but it's quite slow and brute and I have to remove duplicates later, so I am curious if there is a faster way to generate unique combinations here directly.
Consider the number 720.
[3, 5, 48]
[5, 9, 16]
[3, 15, 16]
[3, 3, 80]
[2, 5, 72]
[5, 6, 24]
[5, 8, 18]
[2, 15, 24]
[2, 3, 120]
[3, 10, 24]
[6, 8, 15]
[3, 8, 30]
[3, 6, 40]
[2, 8, 45]
[2, 9, 40]
[8, 9, 10]
[4, 5, 36]
[5, 12, 12]
[4, 12, 15]
[3, 4, 60]
[3, 12, 20]
[4, 4, 45]
[4, 9, 20]
[2, 2, 180]
[2, 10, 36]
[2, 12, 30]
[2, 6, 60]
[6, 10, 12]
[2, 4, 90]
[2, 18, 20]
[4, 10, 18]
[4, 6, 30]
[6, 6, 20]
In Python:
def trifactorgenerator(n):
return (((i,j,n/(i*j))
for i in range(1, int(n**.5)+1) if n%i==0
for j in range(i, int( (n/i)**.5)+1) if n%(i*j) == 0))
This function has the interesting effects:
It is a true generator -- the entire list is never in memory unless the caller creates such a list
Each tuple is sorted (e.g., (2,3,4) never (2,4,3)
It returns no duplicates
The tuples are returned in lexicographic order.
Ref: https://stackoverflow.com/a/6800214/8747