Python 从列表中,图像#2也变得独一无二。因此,删除图像时,我们可以得到一个列表,其中所有剩余的图像都是唯一的,对吗?正如@Sergey所指出的,如果您将例如image\u 0与image\u 4交换,使用您的解决方案的结果将是[image\u 0,image\u

Python 从列表中,图像#2也变得独一无二。因此,删除图像时,我们可以得到一个列表,其中所有剩余的图像都是唯一的,对吗?正如@Sergey所指出的,如果您将例如image\u 0与image\u 4交换,使用您的解决方案的结果将是[image\u 0,image\u,python,numpy,igraph,graph-theory,independent-set,Python,Numpy,Igraph,Graph Theory,Independent Set,从列表中,图像#2也变得独一无二。因此,删除图像时,我们可以得到一个列表,其中所有剩余的图像都是唯一的,对吗?正如@Sergey所指出的,如果您将例如image\u 0与image\u 4交换,使用您的解决方案的结果将是[image\u 0,image\u 3],但我们仍然应该得到最大长度子集,即[image\u 2,image\u 3,image\u 4]或[image\u 1,image\u 2,image\u 3]。是的,我更新了答案以确保正确(我希望,考虑所有特殊情况和可能性是相当棘手的


从列表中,图像#2也变得独一无二。因此,删除图像时,我们可以得到一个列表,其中所有剩余的图像都是唯一的,对吗?正如@Sergey所指出的,如果您将例如
image\u 0
image\u 4
交换,使用您的解决方案的结果将是
[image\u 0,image\u 3]
,但我们仍然应该得到最大长度子集,即
[image\u 2,image\u 3,image\u 4]
[image\u 1,image\u 2,image\u 3]
。是的,我更新了答案以确保正确(我希望,考虑所有特殊情况和可能性是相当棘手的)。我恐怕这不是正确的解决方案,请参阅我的答案。无论如何谢谢你!谢尔盖:谢谢你的回答!但事实证明,您更新的解决方案对我来说太慢了(请参阅我的答案)。@AndreasK。谢谢你提供的信息。我已经评论了你的答案。我的祝贺!!!你们找到了数学解,找到了Python模块。真的很酷。Python不是解决NP难问题的最佳工具。Python模块大多使用C代码来加速执行。如果您找到使用GPU执行此任务的模块,它将更快。例如,使用GPU时,使用ffmpeg的视频编码在我的计算机上运行速度大约快12倍。
images = ['image_0', 'image_1', 'image_2', 'image_3', 'image_4']

sm = np.array([[1, 1, 1, 0, 1],
               [1, 1, 0, 0, 1],
               [1, 0, 1, 0, 0],
               [0, 0, 0, 1, 0],
               [1, 1, 0, 0, 1]])
np.array(images)[np.tril(sm).sum(0) == 1]
sm = np.array([[1, 1, 0, 0, 0],
               [1, 1, 0, 0, 0],
               [0, 0, 1, 1, 0],
               [0, 0, 1, 1, 1],
               [0, 0, 0, 1, 1]])
[images[i] for i in np.where(sm.sum(0) == 1)[0]]
images = ['image_0', 'image_1', 'image_2', 'image_3', 'image_4']

sm = np.array([[1, 1, 1, 0, 1],
               [1, 1, 0, 0, 1],
               [1, 0, 1, 0, 0],
               [0, 0, 0, 1, 0],
               [1, 1, 0, 0, 1]])

ix = list(range(len(images)))

while sm[ix].T[ix].sum() != len(ix): # exit if we got the identity matrix
  va = sm[ix].T[ix].sum(0)           # count similar images
  jx = np.argmax(va)                 # get the index of the worst image
  del ix[jx]                         # delete index of the worst image

print([images[i] for i in ix])
['image_2', 'image_3', 'image_4']
res = []

def get_wres(sm, ix):
  if sm[ix].T[ix].sum() == len(ix):
    res.append(list(ix))
    return
  va = sm[ix].T[ix].sum(0) # count similar images
  vx = np.max(va)          # get the value of the worst
  for i in range(len(ix)): # check every image
    if va[i] == vx:        # for the worst value
      ixn = list(ix)       # isolate one worst
      del ixn[i]           # image and
      get_wres(sm, ixn)    # try without it

get_wres(sm, ix)
print(res)
[[2, 3, 4], [1, 2, 3]]
results = [images[i] for i in range(len(images)) if sum(sm[i][i:]) == 1]
def put_zeros_to_image_with_most_similarities(arr: np.array):
    index = np.sum(arr, axis=1).argmax()
    if np.sum(arr[index], axis=0) == 1:
        return
    arr[index] = 0
    arr[:, index] = 0
for _ in sm:
    put_zeros_to_image_with_most_similarities(sm)
results = [images[i] for i in range(len(images)) if sum(sm[i][i:]) == 1]
images = ['image_0', 'image_1', 'image_2', 'image_3', 'image_4']

sm = np.array([[1, 1, 1, 0, 1],
               [1, 1, 0, 0, 1],
               [1, 0, 1, 0, 0],
               [0, 0, 0, 1, 0],
               [1, 1, 0, 0, 1]])

# Adjacency matrix
adj = sm.copy()
np.fill_diagonal(adj, 0)

# Create the graph
import igraph
g = igraph.Graph.Adjacency(adj.tolist(), mode='UNDIRECTED')
# Find the maximum independent sets
g.largest_independent_vertex_sets()
[(1, 2, 3), (2, 3, 4)]
sm = np.array([[1, 1, 0, 0, 0, 1],
               [1, 1, 0, 1, 0, 0],
               [0, 0, 1, 1, 1, 0],
               [0, 1, 1, 1, 0, 0],
               [0, 0, 1, 0, 1, 1],
               [1, 0, 0, 0, 1, 1]])
a = np.random.randint(2, size=(100, 100))

# create a symmetric similarity matrix
sm = np.tril(a) + np.tril(a, -1).T  
np.fill_diagonal(sm, 1)  

# create adjacency matrix for igraph
adj = sm.copy()
np.fill_diagonal(adj, 0)
def independent_set(adj):
    ''' 
    Given adjacency matrix, returns an independent set
    of size >= np.sum(1/(1 + adj.sum(0)))
    '''
    adj = np.array(adj, dtype=bool).astype(np.uint8)
    np.fill_diagonal(adj, 1)  # for the purposes of algorithm

    indep_set = set(range(len(adj)))
    # Loop until no edges remain
    while adj.sum(0).max() > 1: 
        degrees = adj.sum(0)
        # Randomly pick a vertex v of max degree
        v = random.choice(np.where(degrees == degrees.max())[0])
        # "Remove" the vertex v and the edges to its neigbours
        adj[v, :], adj[:, v] = 0, 0      
        # Update the maximal independent set
        indep_set.difference_update({v})
    return indep_set
def maximal_independent_set(adj):  
    adj = np.array(adj, dtype=bool).astype(np.uint8)
    degrees = adj.sum(0)
    V = set(range(len(adj)))  # vertices of the graph
    mis = set()  # maximal independent set
    while V:
        # Randomly pick a vertex of min degree
        v = random.choice(np.where(degrees == degrees.min())[0])
        # Add it to the mis and remove it and its neighbours from V
        mis.add(v)
        Nv_c = set(np.nonzero(adj[v])[0]).union({v})  # closed neighbourhood of v
        V.difference_update(Nv_c)
        degrees[list(Nv_c)] = len(adj) + 1
    return mis