Graphs and Matrix Storage Structures EEE 574 Dr. Dan Tylavsky
Jan 15, 2016
Graphs and Matrix Storage Structures
EEE 574
Dr. Dan Tylavsky
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures In order to perform calculations on electric
power networks we’ll need to have a way of representing them.
The mathematical equivalent of a network is a graph.
Some definitions:– Graph - G(U,E) consists of a set, U, of vertices
(nodes) and a set, E, of edges (branches).– Edge (branch) - is uniquely determined by a pair
of distinct vertices, (u,v).
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
More definitions:– Digraph - a graph in which each edge,
characterized by an ordered pair of vertices, (u,v), has an orientation from u to v.
– Undirected graph - A graph in which edges have no orientation. (We’ll use this for our networks.)
– Adjacency - A vertex, u, is adjacent to a vertex, v, if an edge (u,v) exists.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures More definitions:
– Adjacency Data Structure of a Graph - For each graph vertex, the list of adjacent vertices is store in compact form. All compact forms are stored in one array.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
Position: 0 1 2 3 4 5 6 7 8 9 10Node(k): 2 4 1 3 4 2 4 1 2 3
ERP(k): 0 2 5 7 10
Node -List of Adjacent NodesERP - End of Row Pointer
Advantages1) Storage space is reasonable.2) Easy to retrieve data values(individual and strings)3)Coding is simple
Disadvantages1) Not easy to modify.2) Meaning of data not easily obtained.3) Edges stored twice.
Pointing to end of eachrow means our last pointerpoints to last element in Node(k).
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage StructuresPosition: 0 1 2 3 4 5 6 7 8 9 10Node(k): 2 4 1 3 4 2 4 1 2 3
ERP(k): 0 2 5 7 10
To find the number of connections to each node, say node j:# Conx Node j = ERP(j)-ERP(j-1)
For node 1:# Conx Node 1 = ERP(1)-ERP(0)(Explains why we must start our arrays at position 0.)
To list all nodes attached to a given node use the Fortran code:
Integer NODE(0:MXBR), ERP(0:MXBS) Nod=? Ibeg=ERP(Nod-1)+1 Iend=ERP(Nod) Write(6,10) (List(I), I=Ibeg,Iend)10 Format(….)
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures– How do we add a branch between 1 and 3 into this data structure?
Pos: 0 1 2 3 4 5 6 7 8 9 10Node(k): 2 4 1 3 4 2 4 1 2 3
ERP(k): 0 2 5 7 10
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Node(k): 2 4 1 3 4 2 4 1 2 3
ERP(k): 0 2 5 7 10
– Shift node values to make space to add node 3 to bus 1 list and node 1 to bus 3 list.
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Node(k): 2 3 4 1 3 4 2 1 4 1 2 3
ERP(k): 0 3 6 9 12
– Add node 3 to bus 1 list and node 1 to bus 3 list.
– Update End of Row Pointers.
– Because of difficulties with adding (or deleting) we use the adjacency structure for static data storage.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures– Let’s store the same
information using a linked list: (Note: Sets are not disjoint.)
Pos: 1 2 3 4 5 6 7 8 9 10 11 12Node(k): 2 4 1 3 4 2 4 2 1 3
NxNode(k): 2 -1 4 5 -3 7 -6 9 10 -8ILP(k): 1 3 6 8 11
– To add a branch between 1 and 3:
-13
1112
-61
1213
Pointer to available linked list storage.
– Linked list structure is much easier to modify but requires more memory.– We use it when data changes dynamically.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures– To eliminate a branch
between 2 and 4.
Pos: 1 2 3 4 5 6 7 8 9 10 11 12Node(k): 2 4 1 3 4 2 4 2 1 3 3 1
NxNode(k): 2 11 4 5 -3 7 12 9 10 -8 -1 -6ILP(k): 1 3 6 8 13
– Enter linked list for node 2, find node 4. Eliminate link.
Pointer to available linked list storage.
– Linked list structure is much easier to modify but requires more memory.
-39
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
Pos: 1 2 3 4 5 6 7 8 9 10 11 12 13 14Node(k): 2 4 1 3 4 2 4 2 1 3 3 1
NxNode(k): 2 11 4 -3 -3 7 12 9 10 -8 -1 -6ILP(k): 1 3 6 9 13
Teams: Modify the existing data structure to account for the removal of the branch (1,2) and addition of a node connected by a branch to node 3.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures There is a correspondence between graph storage and sparse
matrices. Recall the rules for constructing the nodal admittance matrix:
– Let yij be the branch admittance between nodes i and j.
– Let node 0 be the ground or reference node (not shown on our diagrams).
j
ij
ij
ij jiy
jiyY ,
,
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
j
ij
ij
ij jiy
jiyY ,
,
44434241
3433
242221
141211
00
0
0
YYYY
YY
YYY
YYY
15823
89
275
3510
– Note that the nodal admittance matrix is symmetric, that is yij=yji.
– Let’s look a means for storing symmetric matrices.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage StructuresSymmetric matrix storage using
an adjacency structure.
Pos: 0 1 2 3 4 5 6 7 8 9 10Node(k): 2 4 1 4 4 1 2 3
ERP(k): 0 2 4 5 8
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12CIndx(k): 1 2 4 2 1 4 3 4 4 1 2 3
ERP(k): 0 3 6 8 12
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -5.0 -2.0 9.0 -8.0 15.0 -3.0 -2.0 -8.0
CIndx(k): 1 2 4 2 1 4 3 4 4 1 2 3ERP(k): 0 3 6 8 12
– Then adjust the structure to accommodate the matrix.
– Then include an array corresponding to the values in the matrix.
15823
89
275
3510
Y
– Let’s first construct adjacency structure for storing the graph,
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures– This adjacency structure is known as: Row-Wise
Representation (Complete) Unordered (RR(C)U)Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -5.0 -2.0 9.0 -8.0 15.0 -3.0 -2.0 -8.0
CIndx(k): 1 2 4 2 1 4 3 4 4 1 2 3ERP(k): 0 3 6 8 12
– It is unordered because the node #’s with non-zero’s in each row are listed in neither ascending nor descending order.
– An RR(C)O (Ordered) form is:
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 -5.0 7.0 -2.0 9.0 -8.0 -3.0 -2.0 -8.0 15.0
CIndx(k): 1 2 4 1 2 4 3 4 1 2 3 4ERP(k): 0 3 6 8 12
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures Symmetric matrix storage using an adjacency structure (cont’d).
– CR(C)O Column-wise representation (complete) ordered. (Same as RR(C)O for symmetric matrices)
– CR(C)U (Unordered) - Same as RR(C)U for symmetric matrices.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures Symmetric matrix storage using an adjacency structure (cont’d).
– If the matrices are symmetric, we need only store the upper half of the matrix:
• RR(DU)O/U Row-wise rep. (diagonal & upper) ordered /unordered.
15
89
27
3510
UY
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -2.0 9.0 -8.0 15.0
CIndx(k): 1 2 4 2 4 3 4 4ERP(k): 0 3 5 7 8
• Same as CR(DL)O/U Column-wise rep. (diagonal & lower) ordered /unordered.
15823
9
75
10
LY
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -2.0 9.0 -8.0 15.0
RIndx(k): 1 2 4 2 4 3 4 4ECP(k): 0 3 5 7 8
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures Symmetric matrix storage using an adjacency
structure (cont’d).– A more minimal storage technique:
• RR(U)O/U Row-wise rep. (upper) ordered /unordered.
15
89
27
3510
UY
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Diag(k) 10.0 7.0 9.0 15.0
Y(k): -5.0 -3.0 -2.0 -8.0CIndx(k): 2 4 4 4
ERP(k): 0 2 3 4
– It is convenient (esp. in positive definite matrices) to store the diagonal separately.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage StructuresTeams: Compare RR(DU)X and RR(U)X storage schemes applied to the same matrix as shown below. Which requires less storage space? Where does the savings come from?
RR(DU)OPos: 0 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -2.0 9.0 -8.0 15.0
CIndx(k): 1 2 4 2 4 3 4 4ERP(k): 0 3 5 7 8
RR(U)OPos: 0 1 2 3 4 5 6 7 8 9 10 11 12
Diag(k) 10.0 7.0 9.0 15.0Y(k): -5.0 -3.0 -2.0 -8.0
CIndx(k): 2 4 4 4ERP(k): 0 2 3 4
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures Symmetric matrix storage using a linked list structure.
– Any of the forgoing storage techniques can be implemented with a linked list structure.
– Consider RR(DU)O using a linked list sturcture.
Pos: 1 2 3 4 5 6 7 8 9 10 11 12Y(k): 10.0 -5.0 -3.0 7.0 -2.0 9.0 -8.0 15.0
CIndx(k): 1 2 4 2 4 3 4 4NxColPt(k) 2 3 -1 5 -2 7 -3 -4
ILP(k): 1 4 3 8
• Here we have used as the last link of each row, the negative of the row index. This allows you to search Y then identify the row and column index of the element found.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures Incidence symmetric matrix storage using an adjacency structure (cont’d).
– Incidence symmetric matrix - A matrix whose sparsity pattern is symmetric, but whose numerical content is not symmetric.
– L-D-U RO/U - Lower-Diagonal-Upper Representation Ordered/Unordered. (This is an adaptation of the RR(U)x scheme for incidence symmetric matrices.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
157.612.3
89
276
4.3510
Y
– Apply L-D-U RU to the following matrix.
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12Diag(k) 10.0 7.0 9.0 15.0YU(k): -5.0 -3.4 -2.0 -8.0YL(k): -6 -3.2 -1 -6.7
Indx(k): 2 4 4 4ERP(k): 0 2 3 4
– YU is stored by rows.– YL is stored by columns.– Indx contains col. indices for YU, row indices for YL.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
.40.39
.38.37
.36.35
.34.33.32.31
.30.29
.28.27
.26.25
.24.23
.22.21.20.19
.18.17
.16.15
.14.13.12.11
.10.9
.8.7
.6.5
.4.3
.2.1
Y
Incidence symmetric matrix storage using an adjacency structure (cont’d).– When using a Newton-Raphson Method we get matrices that have a block-
incidence symmetric form.
– BRR(C)O/U Block-Row-Wise Representation (Complete) Ordered/Unordered.
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures
.40.39
.38.37
.36.35
.34.33.32.31
.30.29
.28.27
.26.25
.24.23
.22.21.20.19
.18.17
.16.15
.14.13.12.11
.10.9
.8.7
.6.5
.4.3
.2.1
A
– Apply BRR(C)O/U to the following matrix.
Pos: 0 1 2 3 4 5 6 7 8 9 10 11 12A11(k): 1.0 5.0 9.0 13.0 17.0 21.0 25.0 29.0 33.0 37.0A12(k): 2.0 6.0 10.0 14.0 18.0 22.0 26.0 30.0 34.0 38.0A21(k): 3.0 7.0 11.0 15.0 19.0 23.0 27.0 31.0 35.0 39.0A22(k): 4.0 8.0 12.0 16.0 20.0 24.0 28.0 32.0 36.0 40.0
BIndx(k): 1 3 4 2 3 1 2 3 1 4ERP(k): 0 3 5 8 10
1 2 3 4
1
2
3
4
The End
© Copyright 1999 Daniel Tylavsky
Graphs & Matrix Storage Structures– Let’s look at some other storage schemes:– Knuth’s Storage: