matrix/scc.py

This example looks at scc

View output (PDF) | source (python)

See also
casadi::diagcat();
casadi_int scc(std::vector< casadi_int > &index, std::vector< casadi_int > &offset) const
Find the strongly connected components of the bigraph defined by the sparsity pattern.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 from casadi import *
25 import numpy
26 
27 
28 
29 A = diagcat(1,DM([[2,3],[3,4]]),DM([[5,6,7],[6,8,9],[7,9,10]]),11)
30 print(A)
31 A.sparsity().spy()
32 
33 numpy.random.seed(2)
34 
35 
36 
37 perm = list(numpy.random.permutation(list(range(A.size1()))))
38 AP = A[perm,perm]
39 
40 print(AP)
41 AP.sparsity().spy()
42 
43 
44 
45 n,p,r = AP.sparsity().scc()
46 
47 APrestored = AP[p,p]
48 
49 print(APrestored)
50 APrestored.sparsity().spy()
51 print("# blocks: ", n)
52 print("block boundaries: ", r[:n])