15
Sparse storage and solution methods MATH2071: Numerical Methods in Scientific Computing II http://people.sc.fsu.edu/jburkardt/classes/math2071 2020/sparse/sparse.pdf Let’s just concentrate on the seats that are occupied! Sparse Matrices Given a linear system in which the system matrix has very few nonzero elements, find efficient methods for storing the matrix, and for solving the linear system. 1 Where do sparse matrices come from? So far, the sparse matrices we have seen have had great regularity. Diagonal and tridiagonal matrices can be thought of as sparse, but their structure is so simple that there are many efficient ways to store the values and do linear algebra operations. Matrices associated with a 2D or 3D finite difference scheme over a rectangular region are sparse, and sparse storage can be useful for them; however, these examples are not typical, since almost every row of the matrix has the same number of nonzeros, showing up at predictable locations. A more challenging example of a sparse matrix occurs in finite element methods, where an irregular region is meshed with triangles, or other polygonal shapes. Just as in finite differences, we write an equation at each node of the mesh, and that equation will involve the approximate solution values at the node, and at all the nodes which are the immediate neighbors. Unlike the finite difference example, we generally can’t predict in advance the locations of these neighbors, or their number. For example, in a study of the ice sheets of Greenland, a mesh involving 900,000 nodes was created. These nodes were placed in such a way that the mesh was very fine near the coast, and near locations of rapid icesheet movement. It was then necessary to organize these nodes into triangles, using a technique known as Delaunay triangulation and then to build the finite element matrix, in a process similar to what we have done for finite element problems. 1

Sparse storage and solution methods

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sparse storage and solution methods

Sparse storage and solution methodsMATH2071: Numerical Methods in Scientific Computing II

http://people.sc.fsu.edu/∼jburkardt/classes/math2071 2020/sparse/sparse.pdf

Let’s just concentrate on the seats that are occupied!

Sparse Matrices

Given a linear system in which the system matrix has very few nonzero elements, find efficient methodsfor storing the matrix, and for solving the linear system.

1 Where do sparse matrices come from?

So far, the sparse matrices we have seen have had great regularity. Diagonal and tridiagonal matrices canbe thought of as sparse, but their structure is so simple that there are many efficient ways to store thevalues and do linear algebra operations. Matrices associated with a 2D or 3D finite difference scheme overa rectangular region are sparse, and sparse storage can be useful for them; however, these examples are nottypical, since almost every row of the matrix has the same number of nonzeros, showing up at predictablelocations.

A more challenging example of a sparse matrix occurs in finite element methods, where an irregular regionis meshed with triangles, or other polygonal shapes. Just as in finite differences, we write an equation ateach node of the mesh, and that equation will involve the approximate solution values at the node, and atall the nodes which are the immediate neighbors. Unlike the finite difference example, we generally can’tpredict in advance the locations of these neighbors, or their number.

For example, in a study of the ice sheets of Greenland, a mesh involving 900,000 nodes was created. Thesenodes were placed in such a way that the mesh was very fine near the coast, and near locations of rapidicesheet movement. It was then necessary to organize these nodes into triangles, using a technique knownas Delaunay triangulation and then to build the finite element matrix, in a process similar to what we havedone for finite element problems.

1

Page 2: Sparse storage and solution methods

Greenland, and a small portion of the mesh.

Consider the fact that, in the picture of the mesh, each node will generate an equation. Each neighbor node,that is joined by a connecting line, will be associated with a nonzero coefficient in that equation. Thus,each row of our matrix will have an unknown number of nonzeros, at unknown locations, which cannot bepredicted in advance, until we analyze the connections in the mesh.

2 Sparse tools in MATLAB

Given a matrix A stored in the usual m× n dense format, MATLAB provides us with some functions thatcan help us if we suspect that the matrix is sparse.

Whether a matrix is stored using full or sparse storage, we can count the number of nonzeros and report therelative sparsity by the following commands:

1 count = nnz ( A ) ;2 s p a r s i t y = count / m / n ;3 fpr intf ( 1 , ’ Matrix has %d nonzeros , with s p a r s i t y %g\n ’ , count , s p a r s i t y ) ;

To extract the values of the nonzeros in the matrix:

1 v = nonzeros ( A ) ;

To determine the locations and values of the nonzeros:

1 [ i , j , v ] = find ( A ) ;

To see a picture of the nonzero pattern of the matrix:

1 spy (A)

3 Exercise: Examine a matrix

Use the A=dif2 matrix(m,n) function from the web page to generate a dense second difference matrix ofdimension 20× 20. We can use the MATLAB sparse tools to analyze the matrix, extract the nonzeros, andsee a picture of the sparsity pattern.

Now let’s look at a random sparse matrix. Use MATLAB’s sprand(m,n,sparsity) function, which returnsa matrix in sparse format, with a sparsity percentage of the entries being nonzero. The location and valuesof these entries are chosen at random.

2

Page 3: Sparse storage and solution methods

1 m = 12 ;2 n = 9 ;3 s p a r s i t y = 0 . 1 5 ;4 A = sprand ( m, n , s p a r s i t y ) ;

Listing 1: How to define a random sparse matrix.

Determine the actual number of nonzeros, the actual sparsity, and extract the rows, columns, and values ofthe nonzero entries.

Use spy(A) to see a picture of this matrix.

Copy the file wathen sparse matrix.m, which, for a certain finite element problem, generates the systemmatrix in MATLAB sparse format. Create the matrix W = wathen sparse matrix(3, 3). Here the 3× 3 isnot the dimension of the grid, but a count of the number of elements used. This matrix will actually be ofdimensions 40 × 40. Determine the number of nonzeros, the sparsity, and extract the rows, columns, andvalues of the nonzero entries for this matrix.

Use spy(W) to see a picture of this matrix.

4 Creating a MATLAB sparse matrix

MATLAB can work with matrices in full (dense) or sparse format. If we have a matrix in full storage format,we can convert it to sparse format:

1 Asparse = sparse ( A ) ;

Similarly, if we have a matrix in sparse format, we can convert it to full format:

1 B fu l l = f u l l ( B ) ;

A matrix is a collection of values Ai,j . We could think of a matrix as a list of its nonzero entries, that is,a table of values i, j, v, where each v is a value Ai,j . This is how MATLAB thinks of a sparse matrix. Ifwe have three such vectors of rows, columns, and values, we can create the corresponding MATLAB sparsematrix:

1 A = sparse ( i , j , v )

Consider the following matrix:

A =

11 0 0 14 1521 22 0 0 00 0 33 0 0

41 0 0 44 45

We can set up this matrix sparsely as:

1 i = [ 1 , 1 , 1 , 2 , 2 , 3 , 4 , 4 , 4 ] ;2 j = [ 1 , 4 , 5 , 1 , 2 , 3 , 1 , 4 , 5 ] ;3 v = [11 , 14 , 1 5 , 21 , 22 , 3 3 , 41 , 44 , 45 ] ;4 A = sparse ( i , j , v ) ;

Listing 2: Converting i j v to a MATLAB sparse matrix.

3

Page 4: Sparse storage and solution methods

5 Exercise: Set up a MATLAB sparse -1,2,-1 matrix

Consider the following 4× 4 matrix C:

C =

2 −1 0 0−1 2 −1 0

0 −1 2 −10 0 −1 2

Use MATLAB commands to define the vectors i, j, v, and create a sparse matrix C.

1. Determine the number of rows and columns in the matrix;

2. Determine the number of nonzero elements in C?

3. What command will return the vectors i, j, v from C?

4. What is the inverse of C? Is it also stored as a sparse matrix? Is it sparse?

5. What happens if you use the [V,L]=eigs(C) command to get eigen information?

6 Reading a MATLAB sparse matrix from a file

Our example matrix could be described by the following table:

i j v

------

1 1 11

1 4 14

1 5 15

2 1 21

2 2 22

3 3 33

4 4 44

4 5 45

Suppose we had stored this information in a file: example sparse matrix.txt. We can read this data file intoMATLAB using the load() function, which results in an 8 × 3 array, whose columns are the row index,column index, and value of the nonzeros. MATLAB has an spconvert() command that will convert thisdata to a sparse matrix in one step:

1 data = load ( ’ example sparse matr ix . txt ’ ) ;2 A = spconvert ( data ) ;

Listing 3: Read sparse data from file to create sparse matrix.

7 The Sparse Triplet (ST) storage scheme

The sparse triplet storage scheme is extremely simple. We are given an m × n matrix A, which has nstnonzero elements. We can represent the matrix by storing, for each nonzero element Ai,j , the row index,column index, and value, in arrays ist(1:nst), jst(1:nst), vst(1:nst).

Of course, the ST scheme only makes sense for large, sparse matrices, for which 3 ∗ nst << m ∗ n. We mustalso assume that, once we have replaced the dense matrix by the ST version, we can still perform any linearalgebra tasks we need. We will see that matrix multiplication can still be done with this representation.However, computing the LU factorization would not be possible, because the LU factors of a sparse matrixare not necessarily sparse at all.

4

Page 5: Sparse storage and solution methods

As a reminder of how the ST scheme works, consider this 4× 5 matrix A:

A =

11 0 0 14 1521 22 0 0 00 0 33 0 0

41 0 0 44 45

for which the nst = 8 nonzero entries could be stored in ST form as:

ist jst vst1 1 112 1 214 1 412 2 223 3 331 4 144 4 444 5 45

Consider that a “small” version of our 2d Poisson problem, using a 100× 100 grid, involves 10,000 variables.The full matrix is therefore 100,000,000 entries. However, typically, there are at most 5 nonzero coefficientsin each equation, for a total of nst ≈ 50, 000 nonzero values. Therefore, we could store this matrix using50,000 real values (vst) and two integer arrays each of length 50,000. Our storage costs have gone down bya factor of roughly 700.

8 Matrix multiplication using ST

If we want to solve a big sparse linear system, we are likely to be using an iterative method. Most iterativemethods require the ability to compute matrix-vector products like y = A ∗ x or y = A′ ∗ x. If we are usingsome kind of sparse storage, then we need to know how to produce such results efficiently.

If our m × n matrix A is in ST storage format, how do we compute the product y = A ∗ x? Actually,this is quite simple. We initialize y to zero. Every nonzero matrix entry Ai,j must be used. We use it bymultiplying it times xj and adding this to yi. This can be done the following code:

1 function y = st mv ( m, n , nst , i s t , j s t , vst , x )2 y = zeros ( m, 1 ) ;3 for k = 1 : nst4 i = i s t ( k ) ;5 j = j s t ( k ) ;6 a i j = vst ( k ) ;7 y ( i ) = y ( i ) + a i j ∗ x ( j ) ;8 end9 return

10 end

Listing 4: st mv.m: Matrix-vector multiplication for an ST matrix.

9 Exercise: ST Matrix multiplication

If you copy the file dif2 st matrix.m, you can create an m×n version of the second difference matrix −1, 2,−1in Sparse Triplet form using the command:

1 [ nst , i s t , j s t , vs t ] = d i f 2 s t ma t r i x ( m, n )

5

Page 6: Sparse storage and solution methods

Create a 100× 100 second difference matrix this way. Then create a vector x

1 x = ( 1 : 100 ) ’ ; % Don’ t f o r g e t to t ranspose the vec to r !

Now create the function st mv(), and use it to compute y = A ∗ x. The result y should be entirely zero,except for a final entry of 101.

10 Residual calculation using ST

The residual r = b− A ∗ x is useful to measure our error when using Gauss elimination, but also can be animportant tool in Jacobi iteration. When the matrix is in ST format, we can write an appropriate function.

1 function r = s t r e s i d ( nst , i s t , j s t , vst , b , x )2 r = b ;3 for k = 1 : nst4 i = i s t ( k ) ;5 j = j s t ( k ) ;6 a i j = vst ( k ) ;7 r ( i ) = r ( i ) − a i j ∗ x ( j ) ;8 end9 return

10 end

Listing 5: st resid.m: Residual r

11 Jacobi iteration with a Sparse Triplet matrix

Now suppose we want to apply the Jacobi method to solve a linear system A ∗ x = b, where we have storedthe matrix in ST format. The Jacobi iteration boils down to repeatedly applying the Jacobi update step.When A is in full storage, or MATLAB sparse storage, this step looks like this:

1 x = x + ( b − A ∗ x ) . / diag ( A ) ;

We have just seen how to compute the term ( b - A * x ) for the ST case, but how do we efficientlyretrieve the diagonal elements? Although it is not part of the ST format requirements, we can make surethat we list the diagonal elements of the matrix first.

1 function x = s t j a c ob i upda t e ( n , nst , i s t , j s t , vst , b , x )2 r = b ;3 for k = 1 : nst4 i = i s t ( k ) ;5 j = j s t ( k ) ;6 a i j = vst ( k ) ;7 r ( i ) = r ( i ) − a i j ∗ x ( j ) ;8 end9

10 for i = 1 : n11 x ( i ) = x ( i ) + r ( i ) / vst ( i ) ; % vs t ( i ) = A( i , i )12 end1314 return15 end

Listing 6: st jacobi.m: One Jacobi update step in ST format.

6

Page 7: Sparse storage and solution methods

12 Exercise: Sparse triplet Jacobi iteration

Use dif2 st matrix.m, to create a 20× 20 version of the second difference matrix −1, 2,−1 in Sparse Tripletform.

1 n = 20 ;2 [ nst , i s t , j s t , vs t ] = d i f 2 s t ma t r i x ( n , n ) ;

Create a right hand side vector b:

1 b = zeros ( n , 1 ) ;2 b(n) = n + 1 ;

The exact solution of the linear system A ∗ x = b is xexact=(1:20)’. Let’s just try 100 Jacobi iterations(with no convergence checks):

1 xexact = ( 1 : n) ’ ;2 x = zeros ( n , 1 ) ;3 for i = 1 : 1004 x = s t j a c ob i upda t e ( n , nst , i s t , j s t , vst , b , x ) ;5 end

After 100 iterations, what is norm(x-xexact)?

13 The Compressed Sparse Row (CSR) storage scheme

The Compressed Sparse Row (CSR) storage scheme is most easily explained as a slight improvement onSparse Triplet (ST) storage.

Note that the ST storage scheme does not require that the values be listed in any particular order. Wehappened to have listed them column by column, but the values could have been scrambled. It makes nodifference. In order to use CSR, we need to be much more systematic in how we list the data.

We begin by rearranging the ST data vectors so that they are sorted in order of row and then column. Inother words, the nonzero entries in the matrix are recorded as though we started in the top left entry, (thatis, A1,1), read across the first row, and then proceeded left to right along each subsequent row. For ourexample matrix, this would give us an ordering like this:

# ist jst vst1 1 1 112 1 4 143 2 1 214 2 2 225 3 3 336 4 1 417 4 4 448 4 5 45

Now we list the data again, but we pay attention to the ist data, which lists the row index. This list willstart at 1, repeat 1’s a few times, then move up to 2, then 3 and so on. We want to note the index at whicheach row index appears for the first time, and we include one more entry that points just past the end ofthe array. We store these in an array called icrs.

7

Page 8: Sparse storage and solution methods

# ist row starts1 1 1: row 1 starts at entry 12 1 -3 2 3: row 2 starts at entry 34 2 -5 3 5: row 3 starts at entry 56 4 6: row 4 starts at entry 67 4 -8 4 8: row 5 starts at entry 89 * 9: row data ends before entry 9

The array icrs has length m + 1. In our example, icrs = [1,3,5,6,8,9]. You should realize that, fromthis array, we can reconstruct the ist array. Here’s how:

1 2 3 4 5 6 7 8 9

ist is equal to 1 from icrs(1) to icrs(2) - 1; 1 1

ist is equal to 2 from icrs(2) to icrs(3) - 1; 2 2

ist is equal to 3 from icrs(3) to icrs(4) - 1; 3

ist is equal to 4 from icrs(4) to icrs(5) - 1; 4 4

ist is equal to 5 from icrs(5) to icrs(6) - 1; 5 *

14 Exercise: Compressing the row index vector

Consider the following 5× 4 matrix:

A =

11 0 0 140 22 0 0

31 32 33 340 42 0 44

51 52 53 54

For this matrix, create the ST representation: ist, jst, vst:

# ist jst vst1 ..... ..... .....2 ..... ..... .....3 ..... ..... .....4 ..... ..... .....5 ..... ..... .....6 ..... ..... .....7 ..... ..... .....8 ..... ..... .....9 ..... ..... .....

10 ..... ..... .....11 ..... ..... .....12 ..... ..... .....13 ..... ..... .....

To determine the CRS representation icrs, jcrs, vcrs, we have jcrs=jst, vcrs=vst, but we must com-press the row vector ist to get icrs:

8

Page 9: Sparse storage and solution methods

# ist row starts1 ..... .....2 ..... .....3 ..... .....4 ..... .....5 ..... .....6 ..... .....7 ..... .....8 ..... .....9 ..... .....

10 ..... .....11 ..... .....12 ..... .....13 ..... .....14 * .....

so we have that icrs is:

# icrs1 .....2 .....3 .....4 .....5 .....6 .....

15 Matrix multiplication using CSR

The textbook gives a procedure for carrying out matrix multiplication y = A ∗ x in cases where the m × nmatrix A is stored in CRS format:

1 function y = crs mv ( m, n , i c r s , j c r s , vcrs , x )2 y = zeros ( m, 1 ) ;3 for i = 1 : m4 for k = i c r s ( i ) : i c r s ( i +1) − 15 j = j c r s ( k ) ;6 y ( i ) = y ( i ) + vcr s ( j ) ∗ x ( j ) ;7 end8 end9 return

10 end

Listing 7: crs mv.m: CRS matrix multiplication.

16 Exercise: A*x using CSR

Consider again the 5× 4 matrix:

A =

11 0 0 140 22 0 0

31 32 33 340 42 0 44

51 52 53 54

9

Page 10: Sparse storage and solution methods

Given the vector x = [1,2,3,4]’, we know that y = A*x = [67,44,330,260,530]’. Create a MATLABscript that uses the CRS representation of A, and then uses the crs mv() function to compute y = A ∗ x.Verify that you get the right result.

17 Exercise: A’*x using CSR

For the same 5×4 matrix A, given the vector x = [1,2,3,4,5]’, we know that y = A’*x = [359,568,364,562]’.Create a MATLAB script that uses the CRS representation of A. Starting from crs mv(), make a new func-tion crs mtv() to compute y = A′ ∗ x. Verify that you get the right result.

Hint: Only two lines of crs mv() need to be changed. The first line is changed to y=zeros(n,1).

18 Jacobi iteration using CSR

As you can imagine, you can also carry out Jacobi iteration using the CSR sparse matrix format. However,this is essentially the same as what we did for the ST format, with the additional step of compressing therow indices and making the corresponding change in the residual calculation. So we will not want to repeatthe discussion we have already had.

19 Graphs, adjacency, and transitions

We can think of a graph as dots with lines between some of them. Mathematicaly, a graph is a set of nodesN, and a set of edges E. Each edge is a pair of node indices. If nodes i and j are connected, then there isan edge e = (i, j). In an undirected graph, the edges come in pairs, (i, j) and (j, i). In a directed graph, theexistence of an edge (i, j), which is a path from node i to node j, does not imply the existence of an edge(j, i).

A graph can be represented by an adjacency matrix.

Ai,j =

{1, if (i, j) is an edge;

0, otherwise.

For an undirected graph, the adjacency matrix will be symmetric. For most graphs, the adjacency matrixwill also be sparse.

The adjacency matrix has further uses besides simply representing the structure of the graph. In particular,the k-th power of the adjacency matrix counts the number of paths of length k between nodes i and j:

Aki,j = number of paths from node i to node j.

The adjacency matrix can also be used to model the transition problem, that is, the study of movementsalong the edges of a graph.

20 Example: Number of paths from one node to another

Given the following small example of a graph, we can ask how many paths of length k there are from nodeE to any node.

10

Page 11: Sparse storage and solution methods

How can we reach other nodes from node E?

Let A be the adjacency matrix of the graph. You can access this matrix using the command A = graph adj(),available on the class web page.

A =

from/to A B C D E F GA 0 1 1 0 1 0 0B 1 0 1 1 0 0 1C 1 1 0 1 0 1 0D 0 1 1 0 0 1 1E 1 0 0 0 0 1 0F 0 0 1 1 1 0 0G 0 1 0 1 0 0 0

Obviously, A0 = I, and so row 5 of I tells us there is 1 path of length k = 0 from E to E, and no other paths.Similarly, A1 = A, tellings us that from E there is 1 path of length 1 from E to A, and from E to F.

Let’s compute A2:

A2 =

from/to A B C D E F GA 3 1 1 2 0 2 1B 1 4 2 2 1 2 1C 1 2 4 2 2 1 2D 2 2 2 4 1 1 1E 0 1 2 1 2 0 0F 2 2 1 1 0 3 1G 1 1 2 1 0 1 2

and if we concentrate on row E, there are no paths of length 2 from E to A, 1 from E to B, 2 from E to C

11

Page 12: Sparse storage and solution methods

and so on. Although we already found a path of length 1 from E to A, we still haven’t found any path to G.

A3 =

from/to A B C D E F GA 2 7 8 5 5 3 3B 7 6 9 9 3 5 6C 8 9 6 9 2 8 4D 5 9 9 6 3 7 6E 5 3 2 3 0 5 2E 3 5 8 7 5 2 3G 3 6 4 6 2 3 2

and we finally see, reading row E, that there are 2 different paths of length 3 by which we can travel to nodeG. Interestingly, we also see that there is no path from E back to itself that is exactly length 3.

21 Example: Modeling movement along a graph

Start with the adjacency matrix for a graph, and then scale each column of the matrix so that it sums to 1.This gives us what is called the transition matrix. To see what this would be for our example graph, you canaccess the file graph transit.m and type T=graph transit(). For our example graph, this matrix will be:

T =

A B C D E F GA 0 1

414 0 1

2 0 0B 1

3 0 14

14 0 0 1

2C 1

314 0 1

4 0 13 0

D 0 14

14 0 0 1

312

E 13 0 0 0 0 1

3 0F 0 0 1

414

12 0 0

G 0 14 0 1

4 0 0 0

Now let us suppose that we have a herd of 100 sheep, and that they start out at node E. We represent thisby the vector x = [0, 0, 0, 0, 100, 0, 0]′. Being sheep, they want to wander. Since there are two paths to goon, half go to A and half go to F. We can predict this by computing T ∗ x = [50, 0, 0, 0, 0, 50, 0]. It’s harderfor us to realize, but on step 2, the distribution of sheep is T 2 ∗ x = [0, 1/6, 1/3, 1/6, 1/3, 0, 0]′. And afterthat, things scramble for a while, but eventually we actually reach a steady solution:

A B C D E F G

0 0 0 0 0 100 0 0

1 50 0 0 0 0 50 0

2 0 17 33 17 33 0 0

3 29 13 8 13 0 29 8

4 5 19 26 19 19 5 6

5 21 16 13 16 3 21 1

10 12 18 19 18 10 12 9

20 14 18 18 18 9 14 9

30 14 18 18 18 9 14 9

In other words, the vector x = [14, 18, 18, 18, 9, 14, 9]′ (approximately) is an eigenvector of the transitionmatrix T , with eigenvalue 1, because T ∗ x = x.

Any undirected graph which is connected will have this property, that eventually the distribution of sheepwill become constant, even though the sheep will continue to move. In mathematical terms, the transition

12

Page 13: Sparse storage and solution methods

matrix will always have a single dominant eigenvalue 1, so that for any nonzero starting vector x0, thesequence x0, T ∗ x0, T

2 ∗ x0, ... will converge to the corresponding eigenvector.

This will suggest a way of ranking web pages on the internet. Start with any single page, and then moveyour attention to the pages it links to, then spread out from those pages, and so on. Keep going until yourattention is spread in such a way that it doesn’t change from step to step.

Of course, we said that the trick works when the matrix is connected, but of course, that’s not true for theinternet. We will look at a connected example for homework, but for the real internet, there are simpletechniques to take care of the fact that all the web pages on the internet do not form a connected graph.

22 A Version of HITS

Search algorithms such as Google must look at all the web pages in the world, and decide which pages arethe most important. This is called page ranking. When your search item is honey badger, Google looks atall the web pages associated with this topic, and shows them to you in order of importance, that is, basedon the rankings it has computed.

There are many ways to compute such rankings. One way is known as the HITS algorithm. To explain thealgorithm, we think of the Internet as a large graph with directed edges. The nodes are web pages. Thedirected edges are links, that is, a reference on web page i to web page j corresponds to an edge Ai,j in theadjacency graph.

Given a directed adjacency graph A, the HITS algorithm computes two vectors of node ratings:

• a, the authority index, the degree to which a node is pointed to by reference (hub) nodes;• h, the hub index, the degree to which a node points to important (authoritative) nodes;

If A is the adjacency matrix, the vectors satisfy:

a =A′ ∗ h||A′ ∗ h||

h =A ∗ a||A ∗ a||

You may notice that a is an eigenvector of A′A and h is an eigenvector of AA′.

Here is pseudocode algorithm to estimate the vectors a and h for a directed network of n nodes with adjacencymatrix A.

1 function [ a , h ] = h i t s ( A )23 n = s ize ( A, 1 ) ;45 a ( 1 : n ) = 1 % I n i t i a l i z e a and h vec t o r s to 1 , or randomly6 h ( 1 : n) = 178 do k t imes : % I t e r a t e k t imes9

10 for j = 1 to n11 a ( j ) = sum o f h( i ) ’ s for which node j i s po inted to by node i12 end for13 a = a / norm ( a )1415 for i = 1 to n16 h( i ) = sum o f a ( j ) ’ s for which node i po in t s to node j17 end for18 h = h / norm ( h )

13

Page 14: Sparse storage and solution methods

1920 end do2122 return23 end

Listing 8: Pseudocode HITS algorithm.

Both loops that set a(j) and h(i) can be replaced by a single MATLAB command. To see what thatcommand should be, think about how to use the nonzero matrix entries Ai,j (which will all be 1) to computethe result.

Write a computer program that implements the HITS algorithm for a given adjacency matrix A. Assumethat the matrix A is stored in sparse format in a file, so that the file must be read in, and then converted toa sparse matrix. After that, you can use standard MATLAB matrix multiplication notation A ∗ x or A′ ∗ x,because MATLAB automatically recognizes a sparse matrix and knows how to multiply by it.

You can test your program on the “tiny” dataset whose adjacency matrix is available on the class web pageas tiny sparse matrix.txt:

A =

0 0 0 0 01 0 1 0 00 0 0 1 01 1 1 0 00 0 0 1 0

for which the results should be approximately:

a =

0.65720.36900.65720.00040.0000

h =

0.00000.61540.00020.78820.0002

We can actually make a plot of this graph data using MATLAB:

1 data = load ( ’ t i ny spa r s e ma t r i x . txt ’ ) ;2 i = data ( : , 1 ) ;3 j = data ( : , 2 ) ;4 D = digraph ( i , j ) ;5 plot ( D ) ;

Listing 9: Plot tiny sparse matrix.txt.

The resulting plot is:

14

Page 15: Sparse storage and solution methods

The directed graph stored in tiny sparse matrix.txt

If you look at the graph, you can get some sense of why nodes 1 and 3 are the most “authoritative”: Theyhave multiple nodes pointing to them. Similarly, nodes 2 and 4 are strong ”hubs”, because they point tomany authoritative nodes.

Once your program is able to compute these results, it is time to work with the “large” dataset. This is adirected graph whose 1, 458 × 1, 458 sparse adjacency matrix A is stored on the class web page as the filelarge sparse matrix.txt. This file contains 3,545 lines, one for each nonzero entry of A, of the form:

i j Aij <-- means that A(i,j) = Aij

If you have copied the data file to your current directory, then a standard MATLAB sparse matrix A can becreated by the commands:

1 data = load ( ’ l a r g e s p a r s e ma t r i x . txt ’ ) ;2 A = spconvert ( data ) ;

Run your program on the “large” dataset, using k = 30 steps; when your iteration has completed, print thefirst 5 entries of the authority and hub vectors a and h.

Send your program hw7.m to me at [email protected]. I would like to see your work by Friday, February 21.

15