乙個有趣的一一配對問題

2021-08-11 03:32:59 字數 3538 閱讀 4139

問題描述:有兩個集合$\boldsymbol$,$\boldsymbol$,且$\boldsymbol$和$\boldsymbol$中元素的個數均為$d$。現給定$n$個由集合$\boldsymbol$中某元素到集合$\boldsymbol$中某元素的對映,期望找出所有可能的雙射的情形。

**:

import copy

import numpy as np

import matplotlib.pyplot as plt

def find_solutions(x_to_y_matrix, index_vectors,

connection_list, connection_index_list,

solution_set, solution_index_set):

""""""

start_pos = [-1, -1]

for i in range(x_to_y_matrix.shape[1]):

if x_to_y_matrix[0][i] == 1:

start_pos = [0, i]

reduced_matrix = np.zeros([x_to_y_matrix.shape[0] - 1, x_to_y_matrix.shape[1] - 1])

if reduced_matrix.shape[0] == 0 and reduced_matrix.shape[1] == 0:

connection_list.pop()

break

n = 0

for i in range(x_to_y_matrix.shape[0]):

if i != start_pos[0]:

current_row = np.array()

for j in range(x_to_y_matrix.shape[1]):

if j != start_pos[1]:

reduced_matrix[n, :] = current_row

n += 1

index_vectors[0][ : start_pos[0]], index_vectors[0][start_pos[0] + 1: ])

index_vectors[1][ : start_pos[1]], index_vectors[1][start_pos[1] + 1: ])

reduced_index_vectors = np.array([index_row, index_col])

find_solutions(reduced_matrix, reduced_index_vectors, \

connection_list, connection_index_list, \

solution_set, solution_index_set)

if len(connection_list) != 0:

connection_list.pop()

if len(connection_index_list) != 0:

connection_index_list.pop()

return none

def test():

""""""

dim_x_to_y_matrix = 5

x_to_y_connections = np.array([[0, 0],

[0, 2],

[0, 4],

[1, 0],

[1, 2],

[2, 1],

[2, 3],

[3, 1],

[3, 3],

[4, 0],

[4, 4]])

x_to_y_matrix = np.zeros([dim_x_to_y_matrix, dim_x_to_y_matrix])

for i in range(x_to_y_connections.shape[0]):

x_to_y_matrix[int(x_to_y_connections[i][0])][int(x_to_y_connections[i][1])] = 1

index_vectors = np.empty([2, dim_x_to_y_matrix])

index_vectors[0, :] = np.arange(0, dim_x_to_y_matrix, 1)

index_vectors[1, :] = np.arange(0, dim_x_to_y_matrix, 1)

connection_list =

connection_index_list =

solution_set =

solution_index_set =

find_solutions(x_to_y_matrix, index_vectors,

connection_list, connection_index_list,

solution_set, solution_index_set)

if len(solution_set) % 2 == 0:

plot_offset = 200 + (len(solution_set) + 2)/2 * 10

elif len(solution_set) % 2 == 1:

plot_offset = 200 + (len(solution_set) + 1)/2 * 10

plt.figure()

plt.subplot(plot_offset + 1)

for j in range(x_to_y_connections.shape[0]):

plt.plot([1, 2], x_to_y_connections[j, :], color='grey')

plt.grid()

plt.box()

plt.show()

for i in range(len(solution_set)):

x_to_y_list =

for j in range(len(solution_set[i])):

if j == 0:

elif j >= 1:

[int(solution_index_set[i][j - 1][0][int(solution_set[i][j][0])]), \

int(solution_index_set[i][j - 1][1][int(solution_set[i][j][1])])])

print(x_to_y_list)

plt.subplot(plot_offset + i + 2)

for j in range(len(x_to_y_list)):

plt.plot([1, 2], x_to_y_list[j], 'o-', linewidth=1)

plt.grid()

plt.box()

plt.show()

if __name__ == "__main__":

test()

測試用例執行結果:

乙個有趣的問題

今早朋友圈某人以100軟妹幣求助這樣乙個問題 概率論是學的一塌糊塗,但是突然想起類似用蒙特卡洛方法可以模擬出來概率。於是向著這100軟妹幣出發了。但是首先遇到了第乙個問題。陣列b的亂序排列感覺有點棘手。首先的第一反應是 迴圈隨機產生1 100的隨機數,判斷陣列中是否已經有該數,若已存在,則重新生成隨...

乙個有趣的指標問題

是從網上看到的乙個例子 struct s int i int p void main struct s s int p s.i p 0 4 p 1 3 s.p p s.p 1 1 s.p 0 2 問程式會在哪一行死掉 解答 程式執行到最後一行就會報出異常,死掉.具體解答為 首先需要說明的是結構體s,...

乙個有趣的SQL問題。

有朋友近來要我幫忙解決乙個 問題,問題描述 有表,表有3個字段 f1,f2,f3,其中,每個欄位中都可能出現1 9之間的9個數字,現要統計出整個表中1 9各出現的次數。如 f1 f2f311 2123 212在上例中 1 出現了4次,2 出現了4次,3 出現了1次。當然,這個 問題是要求盡可能的用 ...