``````

In [1]:

import numpy as np
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix

``````
``````

In [2]:

l = [[0, 8, 0, 3],
[0, 0, 2, 5],
[0, 0, 0, 6],
[0, 0, 0, 0]]

``````
``````

In [3]:

mst = minimum_spanning_tree(l)
print(mst)

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [4]:

print(type(mst))

``````
``````

<class 'scipy.sparse.csr.csr_matrix'>

``````
``````

In [5]:

print(mst.toarray())

``````
``````

[[0. 0. 0. 3.]
[0. 0. 2. 5.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

``````
``````

In [6]:

print(mst.toarray().astype(int))

``````
``````

[[0 0 0 3]
[0 0 2 5]
[0 0 0 0]
[0 0 0 0]]

``````
``````

In [7]:

print(mst.astype(int).toarray())

``````
``````

[[0 0 0 3]
[0 0 2 5]
[0 0 0 0]
[0 0 0 0]]

``````
``````

In [8]:

a = np.array(l)
print(a)

``````
``````

[[0 8 0 3]
[0 0 2 5]
[0 0 0 6]
[0 0 0 0]]

``````
``````

In [9]:

print(type(a))

``````
``````

<class 'numpy.ndarray'>

``````
``````

In [10]:

print(minimum_spanning_tree(a))

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [11]:

csr = csr_matrix(l)
print(csr)

``````
``````

(0, 1)	8
(0, 3)	3
(1, 2)	2
(1, 3)	5
(2, 3)	6

``````
``````

In [12]:

print(type(csr))

``````
``````

<class 'scipy.sparse.csr.csr_matrix'>

``````
``````

In [13]:

print(minimum_spanning_tree(csr))

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [14]:

print(minimum_spanning_tree(coo_matrix(l)))

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [15]:

print(minimum_spanning_tree(lil_matrix(l)))

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [16]:

n = 4
d = [8, 3, 2, 5, 6]
i = [0, 0, 1, 1, 2]
j = [1, 3, 2, 3, 3]

``````
``````

In [17]:

csr_ = csr_matrix((d, (i, j)), shape=(n, n))
print(csr_)

``````
``````

(0, 1)	8
(0, 3)	3
(1, 2)	2
(1, 3)	5
(2, 3)	6

``````
``````

In [18]:

print(csr_.toarray())

``````
``````

[[0 8 0 3]
[0 0 2 5]
[0 0 0 6]
[0 0 0 0]]

``````
``````

In [19]:

print(minimum_spanning_tree(csr))

``````
``````

(0, 3)	3.0
(1, 2)	2.0
(1, 3)	5.0

``````
``````

In [20]:

print(mst.toarray().astype(int).tolist())

``````
``````

[[0, 0, 0, 3], [0, 0, 2, 5], [0, 0, 0, 0], [0, 0, 0, 0]]

``````
``````

In [21]:

print(type(mst.toarray().astype(int).tolist()))

``````
``````

<class 'list'>

``````
``````

In [22]:

print(mst.sum())

``````
``````

10.0

``````
``````

In [23]:

print(int(mst.sum()))

``````
``````

10

``````
``````

In [24]:

r, c = mst.nonzero()
print(r, c)

``````
``````

[0 1 1] [3 2 3]

``````
``````

In [25]:

print(list(zip(*mst.nonzero())))

``````
``````

[(0, 3), (1, 2), (1, 3)]

``````
``````

In [26]:

print(mst.data)

``````
``````

[3. 2. 5.]

``````
``````

In [27]:

print(list(zip(*mst.nonzero(), mst.data.astype(int))))

``````
``````

[(0, 3, 3), (1, 2, 2), (1, 3, 5)]

``````
``````

In [28]:

l = [[0, 8, 0, 3],
[8, 0, 2, 5],
[0, 2, 0, 6],
[3, 5, 6, 0]]

``````
``````

In [29]:

print(minimum_spanning_tree(l).toarray().astype(int))

``````
``````

[[0 0 0 3]
[0 0 2 5]
[0 0 0 0]
[0 0 0 0]]

``````
``````

In [30]:

l = [[0, 8, 0, 3],
[100, 0, 2, 5],
[100, 100, 0, 6],
[100, 100, 100, 0]]

``````
``````

In [31]:

print(minimum_spanning_tree(l).toarray().astype(int))

``````
``````

[[0 0 0 3]
[0 0 2 5]
[0 0 0 0]
[0 0 0 0]]

``````
``````

In [32]:

l = [[0, 8, 0, 3],
[0, 0, 100, 5],
[0, 2, 0, 6],
[0, 0, 0, 0]]

``````
``````

In [33]:

print(minimum_spanning_tree(l).toarray().astype(int))

``````
``````

[[0 0 0 3]
[0 0 0 5]
[0 2 0 0]
[0 0 0 0]]

``````