Matrix and Graph
Matrix and Graph
Matrix and Graph
• Matrix
• Binary Matrix
• Sparse Matrix
• Operations for Vectors/Matrices
• Graph and Adjacent Matrix
• Adjacent List
Matrix and Graph
Simple structure
01010
10001
11110
11000
Representation by Bits
• A row composed of 01 values can be considered as a big integer
• Prepare an array
BIT_MASK[]= {1,2,4,8,16,…}
BIT_MASK_[]= {0xfffffffe, 0xfffffffd, 0xfffffffb, …}
+ Such data needs algorithms designed so that the dense part will
not affect badly; will be the bottle neck of the computation
Non-binary Sparse Matrix
0,0,1,4,0
0,1,0,0,5
2,0,0,0,0
1,2,5,0,2
0,0,0,0,0
Column: Memory Saving for Matrix
1 5 5 1 7 3
1 1 3 3 5 4
A Code for Sparse Inner Product
int SVECTOR_innerpro (int *va, int ta, int *vb, int tb){
int ia=0, ib=0, c=0;
while ( ia<ta && ib<tb){
if (va[ia*2] < vb[ib*2] ) ia++;
else if (va[ia*2] > vb[ib*2] ) ib++;
else {
c = c + va[ia*2+1]*vb[ib*2+1];
ia++; ib++;
}
}
return ( c );
}
1 5 5 1 7 3
2 1 3 3 5 4
Addition of Two Vectors
• The addition can be done in a similar way
1 5 5 1 7 3
2 1 3 3 5 4
A Code for Addition
int SVECTOR_add (int *vc, int *va, int ta, int *vb, int tb){
int ia=0, ib=0, ic=0, c, cc;
while ( ia<ta || ib<tb){
if (ia == ta ){ c = vb[ib*2+1]; cc = vb[ib*2]; ib++; }
else if ( ib == tb ){c = va[ia*2+1]; cc = va[ia*2]; ia++; }
else if (va[ia*2] > vb[ib*2] ) { c = vb[ib*2+1]; cc = vb[ib*2]; ib++; }
else if (va[ia*2] < vb[ib*2] ) { c = va[ia*2+1]; cc = va[ia*2]; ia++; }
else {
c = va[ia*2+1] + vb[ib*2+1]; cc = vb[ib*2];
ia++; ib++;
}
vc[ic*2] = cc; vc[ic*2+1] = c; ic++;
}
return ( ic );
}
1 5 5 1 7 3
2 1 3 3 5 4
Column: Endmarks do a Good Job!
• Each cell has four arms, that point the neighboring cells in
directions of (←, →, ↑, ↓)
2
7
Pointing the Neighbors
• Links to four directions seems to form a mesh network, but not
• …since, the links can cross
4 4
4 7 2
4
Having Lists of 2-Directions
• If we have lists of row vectors and column vectors both, we can
have the same accessibility, but insertions/deletions are not same
4 7
Graph Structure
• When there are two or more edges connecting two vertices, the
edges are called multiple edges
0: 0,1
• Multiple edges are also easy to handle
1: 1,5
2: 0,3
0 0
0: 1,3 0: 0,2 3: 1,4
1 1: 0,2,4,5 1: 0,1,3,4 4: 1,2
5 2
1 2: 1,3,4,5 2: 4,6,7,8 5: 4,5
5
6 3 4 +
3: 0,2 3: 2,7 6: 2,5
4 8 2 4: 1,2,5 4: 3,5,8 7: 2,3
7 5: 1,2,4 5: 1,5,6
3 8: 2,4
Allocate Memory for Cells
• Incidence matrix can be realized by cells of lists having four links
like sparse matrix
(two for vertices of the edges, and two for the edges in the vertex)
disadvantages of arrays are eliminated
• Also can be of two array lists
• or, prepare an array and edge i corresponds to
0: 0,1
cells 2i and 2i+1, to represent four links
1: 1,5
2: 0,3
0 0 0: 1,3 0: 0,2 3: 1,4
1 1: 0,2,4,5 1: 0,1,3,4 4: 1,2
5 2
1 2: 1,3,4,5 2: 4,6,7,8 5: 4,5
5
6 3 4 +
3: 0,2 3: 2,7 6: 2,5
4 8 2 4: 1,2,5 4: 3,5,8 7: 2,3
3 7 5: 1,2,4 5: 1,5,6 8: 2,4
Exercise
• Make an adjacency matrix of the following graph, and that in
A sparse incidence matrix
6 0
5 1
4 2
3
Bipartite Graph
• A bipartite graph is often seen as a representation of a (binary)
(sparse) matrix
3
Column: Store Huge Graph
• A graph needs two pointer (or integer) per edge
weight, and etc. need more
Ex.)
• If the bit sequence representing a number begins with “0”, the
following 7 bits represent [0-127]
• If “10”, the following 14 bits represent 128+[0-16383]
• If “11”, the following 30 bits represent 16384+128,…
Column: Store Huge Graph (3)
② Sort the sites in dictionary order of their URLs
links are usually to near, thus difference of ID’s becomes small