Function iaadjmtxfromarb

Description

Converts an arborescence to an adjacency matrix. The arborescence stores only the parents' index for each node, and needs the original adjacency matrix to retrieve the costs of each edge.

Synopse

  • B = iaadjmtxfromarb(par, A)
    • par: The arborescence.
    • labels: The original adjacency matrix.
 1 def iaadjmtxfromarb(par, A):
 2     """
 3     Parameters:
 4         par: the parents of each node
 5         A: the adjacency matrix to get the costs from
 6 
 7     >>> from numpy import array
 8     >>> A = array([[10,  8,  9,  4,  6,  8],[ 8,  1,  6, 10,  4,  8],[ 9,  6, 10, 10,  3,  6],[ 4, 10, 10,  7,  4,  5],[ 6,  4,  3,  4,  2,  6],[ 8,  8,  6,  5,  6,  9]])
 9     >>> parents = [3, 4, 4, 3, 4, 3]
10     >>> print iaadjmtxfromarb(parents, A).astype('int')
11     [[0 0 0 4 0 0]
12      [0 0 0 0 4 0]
13      [0 0 0 0 3 0]
14      [4 0 0 0 0 5]
15      [0 4 3 0 0 0]
16      [0 0 0 5 0 0]]
17     """
18     from numpy import ones
19 
20     n = len(par)
21     B = ones((n,n))*float("inf")
22     for v in range(n):
23         p = par[v]
24         if p != v:
25             B[p,v] = A[p,v]
26 
27     return B

Examples

 1 from iaOPF import iaadjmtxfromarb, iaadjmxtcreate
 2 from numpy import array
 3 A = array([[10,  8,  9,  4,  6,  8],
 4            [ 8,  1,  6, 10,  4,  8],
 5            [ 9,  6, 10, 10,  3,  6],
 6            [ 4, 10, 10,  7,  4,  5],
 7            [ 6,  4,  3,  4,  2,  6],
 8            [ 8,  8,  6,  5,  6,  9]])
 9 parents =  [3, 4, 4, 3, 4, 3]
10 B = iaadjmtxfromarb(parents, A)
11 print B
12 mmgraphviz(iaadjmxtcreate(B,dist=True))
[[ Inf  Inf  Inf  Inf  Inf  Inf]
 [ Inf  Inf  Inf  Inf  Inf  Inf]
 [ Inf  Inf  Inf  Inf  Inf  Inf]
 [  4.  Inf  Inf  Inf  Inf   5.]
 [ Inf   4.   3.  Inf  Inf  Inf]
 [ Inf  Inf  Inf  Inf  Inf  Inf]]
/media/_xsb/iaOPF/iaadjmtxfromarb/GRVIZ74849_001.png