I have a matrix in this form:
In [1]: df = pd.DataFrame([[0, 0, 1, 1, 1], [0, 0, 0, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 1], [1, 0, 0, 1, 0]], columns = list('ABCDE'), index = list('ABCDE'))
In [2]:df
Out[2]:
A B C D E
A 0 0 1 1 1
B 0 0 0 1 0
C 1 0 0 0 0
D 1 1 0 0 1
E 1 0 0 1 0
The number 1 represents the connection between two elements. In this case 'A' is connected to 'D' and 'E' and 'D' are connected to 'E', forming a closed connection formed by three elements. I look for the greatest number of elements connected to each other, in this case 'A', 'D', 'E'. I could use for loops but it gets too slow for a 300x300 matrix. How can I solve?
Update 1:
df = pd.DataFrame([[0, 0, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 0, 0]], columns = list('ABCDEF'), index = list('ABCDEF'))
In this case the solution is the longest cycle ['D', 'F', 'A', 'C', 'E'].
Is it possible to have only full connected elements such as, in the case presented in the example, 'C', 'D', 'E'?


