(Fix) Added Homework

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

PROJECT REPORT MATRIX COMPUTATION I

[Added Homework]

Lecturer:
1. Prof. Edmond Chow
2. Prof. Tsung-Min Hwang
3. Prof. Matthew M. Lin
4. Prof. Weichung Wang

By:
1. Irfan Nurhidayat [60640035S]
2. Lee Chen Han [60640007S]
3. Shih Hsiang Ren [60640006S]

NATIONAL TAIWAN NORMAL UNIVERSITY


TAIPEI CITY
2017
PROJECT REPORT MATRIX COMPUTATION I

By

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren

Tuesday, October 10, 2017


Department of Mathematics, National Taiwan Normal University

Contents:
Outline ……………………………… (2)
1. Dealing sparse matrix with MATLAB ……………………………… (3)
2. Inverse power method ……………………………… (15)
3. Solve linear system with LU and Cholesky ……………………………… (23)
References ……………………………… (40)

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 1
Outline

We downloaded data size 1000 – 10.000 from [1], then we choose Facebook (NIPS), US power
grid, Euroroad, Human Protein (Vidal), Protein, and U. Rovira i Virgili. In this report, we will
explain six difference data as (1) Dealing sparse matrix with MATLAB, (2) Inverse power
method, and (3) Solve linear system with LU and Cholesky.

In section (1), we only display to network information and then using MATLAB to plot a graph.
In section (2), we answered subsection (a) use all of the material from other references
concerning Inverse power method or Inverse iteration, for subsection (b), we added codes from
putty approaching copy and paste, we only change char flag = ‘D’; becoming char flag =
‘H’; later for subsection (c) there exists two problems, the first numerical results, we get the
results from putty, and the second for observations, we use SPSS14 to check normality data
using one sample Kolmogorov-Smirnov test and then seeing correlations between variable used,
we using Pearson test on a test of significance two-tailed. In section (3), we answered subsection
(a) use all of the material from other references concerning LU method and Cholesky method, for
subsection (b), we added codes from putty approaching copy and paste, and finally (c) we get
numerical results from putty, and also used SPSS14 to check normality data using one sample
Kolmogorov-Smirnov test and then seeing correlations between variable used, we using Pearson
test on a test of significance two-tailed.

We use a laptop ASUS with specification: Windows 7, Processor: Intel (R) Celeron (R) CPU
1007U@1.59GHz 1.50GHz, system type 32-bit Operating system, SPSS14, and also MATLAB
R2015a.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 2
1. Dealing sparse matrix with MATLAB

In this section, we explain some remarks for Facebook (NIPS), US power grid, Euroroad,
Human Protein (Vidal), Protein, and U. Rovira i Virgili.

(a) Facebook (NIPS) (Graph of Laplacian)


This directed network contains Facebook user–user friendships. A node represents a user.
An edge indicates that the user represented by the left node is a friend of the user
represented by the right node [2].

Network info:

Name Facebook (NIPS)


Data size 2,888 vertices (users)
Format Undirected Unweighted
Volume 2,981 edges (friendships)
Nonzeros 8,850
Code and Elapsed time clear;
clc;
E = load('out.ego-facebook');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['ego-facebook spy(L)']);

Elapsed time is 0.279204 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 3
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 4
(b) US power grid (Graph of Laplacian)
This undirected network contains information about the power grid of the Western States
of the United States of America. An edge represents a power supply line. A node is either
a generator, a transformation or a substation [3].

Network info:

Name US power grid


Data size 4,941 vertices (nodes)
Format Undirected Unweighted
Volume 6,594 edges (supplies)
Nonzeros 18,129
Code and Elapsed time clear;
clc;
E = load('out.opsahl-powergrid');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['opsahl-powergrid
spy(L)']);

Elapsed time is 0.146274 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 5
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 6
(c) Euroroad (Graph of Laplacian)
This is the international E-road network, a road network located mostly in Europe. The
network is undirected; nodes represent cities and an edge between two nodes denotes that
they are connected by an E-road [4].

Network info:

Name Euroroad
Data size 1,174 vertices (cities)
Format Undirected Unweighted
Volume 1,417 edges (roads)
Nonzeros 4,008
Code and Elapsed time clear;
clc;
E =
load('out.subelj_euroroad_euroroad');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['subelj_euroroad_euroroad
spy(L)']);

Elapsed time is 0.112663 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 7
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 8
(d) Human Protein (Vidal) (Graph of Laplacian)
This network represents an initial version of a proteome-scale map of Human binary
protein-protein interactions [5].

Network info:

Name Human Protein (Vidal)


Data size 3,133 vertices (proteins)
Format Undirected Unweighted
Volume 6,726 edges (interactions)
Nonzeros 15,321
Code and Elapsed time clear;
clc;
E = load('out.maayan-vidal');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['maayan-vidal spy(L)']);

Elapsed time is 3.119617 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 9
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 10
(e) Protein (Graph of Laplacian)
This undirected network contains protein interactions contained in yeast. Research
showed that proteins with a high degree were more important for the survival of the yeast
than others. A node represents a protein and an edge represents a metabolic interaction
between two proteins. The network contains loops [6].

Network info:

Name Protein
Data size 1,870 vertices (proteins)
Format Undirected Unweighted
Volume 2,277 edges/vertex
Nonzeros 6,252
Code and Elapsed time clear;
clc;
E =
load('out.moreno_propro_propro');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['moreno_propro_propro
spy(L)']);

Elapsed time is 0.126463 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 11
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 12
(f) U. Rovira i Virgili (Graph of Laplacian)
This is the email communication network at the University Rovira i Virgili in Tarragona
in the south of Catalonia in Spain. Nodes are users and each edge represents that at least
one email was sent. The direction of emails or the number of emails is not stored [7].

Network info:

Name U. Rovira i Virgili


Data size 1,133 vertices (users)
Format Undirected Unweighted
Volume 5,451 edges (communications)
Nonzeros 12,035
Code and Elapsed time clear;
clc;
E = load('out.arenas-email');
A = GraphAdjacency(E);
L = GraphLaplacian(A);
tic;
spy(L);
toc
title(['arenas-email spy(L)']);

Elapsed time is 0.134533 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 13
Plot:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 14
2. Inverse Power Method

(a) Give an introduction about your problem. Which method will be used to solve this
problem, and describe the parameter setting if it has.
Answer:
 The Inverse Power Method or Inverse Iteration is a modification of the Power method
that gives faster convergence. It is used to determine the eigenvalue of A that is closest
to a specified number q. Suppose the matrix A has eigenvalues λ1 , … , λn with linearly
independent eigenvectors 𝐯 (1) , 𝐯 (2) , … , 𝐯 (n) . The eigenvalues of (A − qI)−1 where
q ≠ λi for i = 1,2, … , n, are
1 1 1
, ,…,
λ1 − q λ2 − q λn − q

with these same eigenvectors 𝐯 (1) , 𝐯 (2) , … , 𝐯 (n) . [8, p.583].


We will use inverse power method to find an approximation for the smallest
eigenvalue of A. Inverse power method can be also effectively employed to compute
the eigenvector associated with a given (approximate) eigenvalue. Inverse power
method is thus ideally suited for refining an initial estimate 𝜆 of an eigenvalue of A,
which can be obtained, for instance, by applying the localization techniques. The
inverse power method has the advantage that it can coverage to any desired eigenvalue
of A.
 The inverse power method consists of inverse power method with a fixed shift, inverse
power method with variant shifts, inverse power method with a relation with Newton
method, and inverse power method with Rayleigh quotient. In this section, we use the
inverse power method with a fixed shift.
 Compute 𝑎𝑖+1 = (A − qI)−1 𝑏𝑖 and 𝑐𝑖+1 = 𝑑(𝑎𝑖+1 ). Set 𝑏𝑖+1 = 𝑎𝑖+1 ⁄𝑐𝑖+1.

(b) Add your codes


Answer:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file sgp_main.cpp
/// @brief The main function.
///
/// @author William Liao
///

#include <cstdio>

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 15
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <timer.hpp>
#include "sgp.hpp"

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Main function for spectral graph partitioning.
///
int main( int argc, char** argv ){
int err_test;

// need at least 2 argument!


assert( argc >= 2 );

// check 2nd argument


assert( argv[1] != NULL );

// read file
int E_size_r, E_size_c, *E;
cout << "Read the graph data from file..............." << flush;
err_test = readGraph(argv[1], &E, &E_size_r, &E_size_c);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "Size of data is " << E_size_r << "x" << E_size_c << endl;

// set graph type


int type;
char flag1;
if ( argc == 2 )
{
type = setgraphtype(E_size_c);
}else if( argc == 3 ){
type = setgraphtype(argv[2], E_size_c);
}

if ( type == 0 )
{

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 16
flag1 = 'S';
cout << "type of graph: simple graph" << endl;
}else if( type == 1 ){
flag1 = 'D';
cout << "type of graph: directed (multi) graph" << endl;
}else if ( type == 2 ){
flag1 = 'W';
cout << "type of graph: directed weighted graph" << endl;
}else if ( type == 3 )
{
flag1 = 'U';
cout << "type of graph: undirected weighted graph" << endl;
}

// Construct adjacency matrix of graph


int nnz, *cooRowIndA, *cooColIndA, n;
double *cooValA;
cout << "Construct adjacency matrix of graph........." << flush;
err_test = GraphAdjacency(E, E_size_r, &nnz, &cooRowIndA, &cooColIndA,
&cooValA, &n, flag1);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "size of matrix = " << n << endl;
cout << "nnz of A = " << nnz << endl;

// Construct Laplacian
int *csrRowIndA, *csrColIndA;
double *csrValA;
double shift_sigma = 0; // Modify shift_sigma to set the shift
cout << "Construct Laplacian matrix of graph........." << flush;
GraphLaplacian(&nnz, cooRowIndA, cooColIndA, cooValA, n, &csrRowIndA,
&csrColIndA, &csrValA, shift_sigma);
cout << " Done. " << endl;
cout << "nnz of L = " << nnz << endl;

// Shift to zero-based indexing


int tmp;
for (int i = 0; i < nnz; i++)
{
tmp = csrColIndA[i]-1;
csrColIndA[i] = tmp;

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 17
}
for (int i = 0; i < n+1; i++)
{
tmp = csrRowIndA[i]-1;
csrRowIndA[i] = tmp;
}

// Solve EVP
double mu0 = 0.6, mu; // Modify mu0 to change the initial
// guess of eigenvalue
double *x, timer;
x = new double[n];
char flag = 'H'; // Modify flag to choose solver on GPU
// or CPU. Possible options are
// 'H': solver on host (CPU)
// 'D': solver on device (GPU)

cout << "Solving Eigenvalue Problem.................." << flush;

switch (flag){
case 'H':
tic(&timer);
solveShiftEVPHost(n, nnz, csrValA, csrRowIndA, csrColIndA, mu0,
&mu, x);
cout << " Done. ";
toc(&timer);
break;
case 'D':
tic(&timer);
solveShiftEVP(n, nnz, csrValA, csrRowIndA, csrColIndA, mu0, &mu, x);
cout << " Done. ";
toc(&timer);
break;
}

cout << "The estimated eigenvalue near " << mu0 << " = ";
cout << fixed << setprecision(13) << mu << endl;

return 0;
}

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 18
(c) Numerical results and observations
Answer:

 Numerical results:

Facebook (NIPS)
tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out ego-facebook
Read the graph data from file............... Done.
Size of data is 2981x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 2888
nnz of A = 5962
Construct Laplacian matrix of graph......... Done.
nnz of L = 8850
Solving Eigenvalue Problem.................. Done. Elapsed time is 4.92705 seconds.
The estimated eigenvalue near 0.6 = 0.3059906953263

US power grid
tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out opsahl-powergrid
Read the graph data from file............... Done.
Size of data is 6594x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 4941
nnz of A = 13188
Construct Laplacian matrix of graph......... Done.
nnz of L = 18129
Solving Eigenvalue Problem.................. Done. Elapsed time is 1.19833 seconds.
The estimated eigenvalue near 0.6 = 0.5995172358402

Euroroad
tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out subelj_euroroad_euroroad
Read the graph data from file............... Done.
Size of data is 1417x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1174
nnz of A = 2834
Construct Laplacian matrix of graph......... Done.
nnz of L = 4008
Solving Eigenvalue Problem.................. Done. Elapsed time is 0.342537 seconds.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 19
The estimated eigenvalue near 0.6 = 0.5559011188133

Human Protein (Vidal)


tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out maayan-vidal
Read the graph data from file............... Done.
Size of data is 6726x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 3133
nnz of A = 13452
Construct Laplacian matrix of graph......... Done.
nnz of L = 16008
Solving Eigenvalue Problem.................. Done. Elapsed time is 17.528 seconds.
The estimated eigenvalue near 0.6 = 0.5766127932350

Protein
tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out moreno_propro_propro
Read the graph data from file............... Done.
Size of data is 2277x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1870
nnz of A = 4554
Construct Laplacian matrix of graph......... Done.
nnz of L = 6350
Solving Eigenvalue Problem.................. Done. Elapsed time is 3.11122 seconds.
The estimated eigenvalue near 0.6 = 0.5942596841620

U. Rovira i Virgili
tms17nla71@z-gotham:~/NLA17/hands_on$ ./sgp_main.out arenas-email
Read the graph data from file............... Done.
Size of data is 5451x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1133
nnz of A = 10902
Construct Laplacian matrix of graph......... Done.
nnz of L = 12035
Solving Eigenvalue Problem.................. Done. Elapsed time is 0.587308 seconds.
The estimated eigenvalue near 0.6 = 0.6011546382331

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 20
The above result can be represented properly via the following table:
Num. Data name Matrix size Estimated Eigenvalue Elapsed time (s)
1 Facebook (NIPS) 2888 0.6 = 0.3059906953263 4.92705
2 US power grid 4941 0.6 = 0.5995172358402 1.19833
3 Euroroad 1174 0.6 = 0.5559011188133 0.342537
4 Human Protein (Vidal) 3133 0.6 = 0.5766127932350 17.528
5 Protein 1870 0.6 = 0.5942596841620 3.11122
6 U. Rovira i Virgili 1133 0.6 = 0.6011546382331 0.587308

 Observations:

We use SPSS14 on a test of normality data for Estimated Eigenvalue and Elapsed time
then be used one sample Kolmogorov-Smirnov test. The following below the results:

NPar Tests
One-Sample Kolmogorov-Smirnov Test

Estimated_Eig
envalue
N 6
Mean .5389
Normal Parameters(a,b)
Std. Deviation .11539
Most Extreme Absolute .392
Differences Positive .295
Negative -.392
Kolmogorov-Smirnov Z .960
Asymp. Sig. (2-tailed) .315
a Test distribution is Normal.
b Calculated from data.

One-Sample Kolmogorov-Smirnov Test

Elapsed_time
N 6
Mean 4.6157
Normal Parameters(a,b)
Std. Deviation 6.56131
Most Extreme Absolute .314
Differences Positive .314
Negative -.257
Kolmogorov-Smirnov Z .770
Asymp. Sig. (2-tailed) .593
a Test distribution is Normal.
b Calculated from data.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 21
Afterwards, we use SPSS14 on a test of correlations for Estimated Eigenvalue and Elapsed
time then be used correlation coefficients, Pearson, with a test of significance two-tailed,
and also we displayed descriptive statistics like mean and standard deviations. The
following below the results:

Correlations
Descriptive Statistics

Mean Std. Deviation N


Estimated_Eigenvalue .5389 .11539 6
Elapsed_time 4.6157 6.56131 6

Correlations

Estimated_Eig
envalue Elapsed_time
Estimated_Eigenvalue Pearson Correlation 1 -.053
Sig. (2-tailed) .921
N 6 6
Elapsed_time Pearson Correlation -.053 1
Sig. (2-tailed) .921
N 6 6

We have a conclusion that Estimated Eigenvalue and Elapsed time there exists a
correlation.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 22
3. Solve linear system with LU and Cholesky

In this section, we will have explaining 2 kinds method that is LU Method and Cholesky
Method like below:

LU Method

(a) Give an introduction about your problem. Which method will be used to solve this
problem, and describe the parameter setting if it has.
Answer: See [8].
 If Gaussian elimination can be performed on the linear system 𝐴𝑥 = 𝑏 (Suppose size
of 𝐴 is n×n) without row interchanges, then the matrix 𝐴 can be factored into the
product of a lower-triangular matrix 𝐿 and an upper-triangular matrix 𝑈 , that is,
𝐴 = 𝐿𝑈. To solve 𝐴𝑥 = 𝑏, it is equivalent to solve 𝐿𝑈𝑥 = 𝑏. Then first, we let 𝑈𝑥 = 𝑦
and solving 𝐿𝑦 = 𝑏, we can solve this easily because 𝐿 is a lower-triangular matrix.
After we get y, second we have to solve 𝑈𝑥 = 𝑦 with this is also easy to solve because
𝑈 is an upper-triangular matrix.
 To turn 𝐴 into 𝐿𝑈, we consider the method that we used to change 𝐴 into row echelon
form, that is use row operation:
(𝐸𝑗 − 𝑚𝑗,𝑘 𝐸𝑘 ) → (𝐸𝑗 ), for 𝑗 = 𝑘 + 1, … , 𝑛.
and to use this operation on 𝐴, it is equivalent to multiplied a matrix 𝐿(𝑘) by the left
side of matrix 𝐴, which 𝐿(𝑘) is define by:

Then 𝐴 = 𝐿(1) 𝐿(2) … 𝐿(𝑛−3) 𝐿(𝑛−2) 𝐿(𝑛−1) 𝑈 = 𝐿𝑈.


 Set 𝐴 ∈ 𝑀𝑛×𝑛 and b ∈ ℝ𝑛 . Compute 𝐴 = 𝐿𝑈, and solve 𝐿𝑦 = 𝑏, 𝑈𝑥 = 𝑦.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 23
(b) Add your codes
Answer:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file sgp_main.cpp
/// @brief The main function.
///
/// @author William Liao
///

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <timer.hpp>
#include "sgp.hpp"

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Main function for spectral graph partitioning.
///
int main( int argc, char** argv ){
int err_test;

// need at least 2 argument!


assert( argc >= 2 );

// check 2nd argument


assert( argv[1] != NULL );

// read file
int E_size_r, E_size_c, *E;
cout << "Read the graph data from file..............." << flush;
err_test = readGraph(argv[1], &E, &E_size_r, &E_size_c);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "Size of data is " << E_size_r << "x" << E_size_c << endl;

// set graph type

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 24
int type;
char flag1;
if ( argc == 2 )
{
type = setgraphtype(E_size_c);
}else if( argc == 3 ){
type = setgraphtype(argv[2], E_size_c);
}

if ( type == 0 )
{
flag1 = 'S';
cout << "type of graph: simple graph" << endl;
}else if( type == 1 ){
flag1 = 'D';
cout << "type of graph: directed (multi) graph" << endl;
}else if ( type == 2 ){
flag1 = 'W';
cout << "type of graph: directed weighted graph" << endl;
}else if ( type == 3 )
{
flag1 = 'U';
cout << "type of graph: undirected weighted graph" << endl;
}

// Construct adjacency matrix of graph


int nnz, *cooRowIndA, *cooColIndA, n;
double *cooValA;
cout << "Construct adjacency matrix of graph........." << flush;
err_test = GraphAdjacency(E, E_size_r, &nnz, &cooRowIndA, &cooColIndA,
&cooValA, &n, flag1);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "size of matrix = " << n << endl;
cout << "nnz of A = " << nnz << endl;

// Construct Laplacian
int *csrRowIndA, *csrColIndA;
double *csrValA;
double shift_sigma = 1e-5; // Modify shift_sigma to set the
// shift

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 25
cout << "Construct Laplacian matrix of graph........." << flush;
GraphLaplacian(&nnz, cooRowIndA, cooColIndA, cooValA, n, &csrRowIndA,
&csrColIndA, &csrValA, shift_sigma);
cout << " Done. " << endl;
cout << "nnz of L = " << nnz << endl;

// Shift to zero-based indexing


int tmp;
for (int i = 0; i < nnz; i++)
{
tmp = csrColIndA[i]-1;
csrColIndA[i] = tmp;
}
for (int i = 0; i < n+1; i++)
{
tmp = csrRowIndA[i]-1;
csrRowIndA[i] = tmp;
}

// Generate RHS
double *b;
b = new double[n];
genRHS(b, n, nnz, csrValA, csrRowIndA, csrColIndA);

// Solve LS
double *x, timer;
x = new double[n];
char flag = 'H'; // Modify flag to choose solver on GPU
// or CPU. Possible options are
// 'H': solver on host (CPU)
// 'D': solver on device (GPU)

int solver = 0; // Modify solver to switch between


// different linear solvers. Possible
// options are
// 0: LU
// 1: Cholesky
// 2: QR

cout << "Solving Linear System......................." << flush;

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 26
switch (flag){
case 'H':
tic(&timer);
solvelsHost(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x, solver);
cout << " Done. ";
toc(&timer);
break;
case 'D':
tic(&timer);
solvels(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x, solver); cout << "
Done. ";
toc(&timer);
break;
}

// Compute redsidual
double res;
res = residual(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x);

cout << "||Ax - b|| = " << res << endl;

return 0;
}

(c) Numerical results and observations


Answer:

 Numerical results:

Facebook (NIPS)
tms17nla70@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out ego-facebook
Read the graph data from file............... Done.
Size of data is 2981x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 2888
nnz of A = 5962
Construct Laplacian matrix of graph......... Done.
nnz of L = 8850

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 27
Solving Linear System....................... Done. Elapsed time is 0.141535 seconds.
||Ax - b|| = 8.22781e-12

US power grid
tms17nla70@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out opsahl-powergrid
Read the graph data from file............... Done.
Size of data is 6594x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 4941
nnz of A = 13188
Construct Laplacian matrix of graph......... Done.
nnz of L = 18129
Solving Linear System....................... Done. Elapsed time is 0.239474 seconds.
||Ax - b|| = 4.06615e-14

Euroroad
tms17nla71@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out subelj_euroroad_euroroad
Read the graph data from file............... Done.
Size of data is 1417x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1174
nnz of A = 2834
Construct Laplacian matrix of graph......... Done.
nnz of L = 4008
Solving Linear System....................... Done. Elapsed time is 0.154559 seconds.
||Ax - b|| = 1.63342e-14

Human Protein (Vidal)


tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out maayan-vidal
Read the graph data from file............... Done.
Size of data is 6726x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 3133
nnz of A = 13452
Construct Laplacian matrix of graph......... Done.
nnz of L = 16008

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 28
Solving Linear System....................... Done. Elapsed time is 0.694007 seconds.
||Ax - b|| = 0.00138355

Protein
tms17nla70@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out moreno_propro_propro
Read the graph data from file............... Done.
Size of data is 2277x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1870
nnz of A = 4554
Construct Laplacian matrix of graph......... Done.
nnz of L = 6350
Solving Linear System....................... Done. Elapsed time is 0.148069 seconds.
||Ax - b|| = 0.00496796

U. Rovira i Virgili
tms17nla70@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out arenas-email
Read the graph data from file............... Done.
Size of data is 5451x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1133
nnz of A = 10902
Construct Laplacian matrix of graph......... Done.
nnz of L = 12035
Solving Linear System....................... Done. Elapsed time is 0.242995 seconds.
||Ax - b|| = 4.02995e-13

The above result can be represented properly via the following table:
Num. Data name Matrix size ‖Ax − b‖ Elapsed time (s)
1 Facebook (NIPS) 2888 8.22781e-12 0.141535
2 US power grid 4941 4.06615e-14 0.239474
3 Euroroad 1174 1.63342e-14 0.154559
4 Human Protein (Vidal) 3133 0.00138355 0.694007
5 Protein 1870 0.00496796 0.148069
6 U. Rovira i Virgili 1133 4.02995e-13 0.242995

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 29
 Observations:

Let ‖Ax − b‖ is called Norm, we use SPSS14 on a test of normality data for Norm and
Elapsed time then be used one sample Kolmogorov-Smirnov test. The following below
the results:

NPar Tests
One-Sample Kolmogorov-Smirnov Test

Norm
N 6
Mean .0011
Normal Parameters(a,b)
Std. Deviation .00199
Most Extreme Absolute .369
Differences Positive .369
Negative -.298
Kolmogorov-Smirnov Z .904
Asymp. Sig. (2-tailed) .388
a Test distribution is Normal.
b Calculated from data.

One-Sample Kolmogorov-Smirnov Test

Elapsed_time
N 6
Mean .2701
Normal Parameters(a,b)
Std. Deviation .21267
Most Extreme Absolute .384
Differences Positive .384
Negative -.273
Kolmogorov-Smirnov Z .941
Asymp. Sig. (2-tailed) .339
a Test distribution is Normal.
b Calculated from data.

Afterwards, we use SPSS14 on a test of correlations for Norm and Elapsed time then be
used correlation coefficients, Pearson, with a test of significance two-tailed, and also we
displayed descriptive statistics like mean and standard deviations. The following below
the results:

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 30
Correlations
Descriptive Statistics

Mean Std. Deviation N


Elapsed_time .2701 .21267 6
Norm .0011 .00199 6

Correlations

Elapsed_time Norm
Elapsed_time Pearson Correlation 1 -.009
Sig. (2-tailed) .986
N 6 6
Norm Pearson Correlation -.009 1
Sig. (2-tailed) .986
N 6 6

We have a conclusion that Norm and Elapsed time there exists a correlation.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 31
Cholesky Method

(a) Give an introduction about your problem. Which method will be used to solve this
problem, and describe the parameter setting if it has.
Answer: See [8].
 If matrix 𝐴 is positive definite, then the matrix 𝐴 can be factored into the product of a
lower-triangular matrix 𝐿 and the transpose matrix of matrix 𝐿, denoted 𝐿𝑡 that is,
𝐴 = 𝐿𝐿𝑡 .
To solve 𝐴𝑥 = 𝑏, it is equivalent to solve 𝐿𝐿𝑡 𝑥 = 𝑏. Then first, we let 𝐿𝑡 𝑥 = 𝑦 and
solveing 𝐿𝑦 = 𝑏, we can solve this easily because 𝐿 is a lower-triangular matrix. After
we get y, second we have to solve 𝐿𝑡 𝑥 = 𝑦 with this is also easy to solve because 𝐿𝑡 is
an upper-triangular matrix.
 We found the lower-triangular matrix 𝐿 by following algorithm:
Suppose 𝐿 = (𝑙𝑖𝑗 )𝑛×𝑛 .
(1) Set 𝑙11 = √𝑎11
(2) For 𝑗 = 2,… , n, set 𝑙𝑗𝑖 = 𝑎𝑗1 /𝑙11
(3) For 𝑖 = 2, … , n-1 do (4) and (5)
2 1/2
(4) Set 𝑙𝑖𝑖 = (𝑎𝑖𝑖 − ∑𝑖−1
𝑘=1 𝑙𝑖𝑘 )
(5) For 𝑗 = 𝑖 + 1, … , 𝑛. Set 𝑙𝑗𝑖 = (𝑎𝑗𝑖 − ∑𝑖−1
𝑘=1 𝑙𝑗𝑘 𝑙𝑖𝑘 )/𝑙𝑖𝑖
2 1/2
(6) Set 𝑙𝑛𝑛 = (𝑎𝑛𝑛 − ∑𝑛−1
𝑘=1 𝑙𝑛𝑘 )
 Set 𝐴 ∈ 𝑀𝑛×𝑛 and b ∈ ℝ𝑛 . Compute 𝐴 = 𝐿𝐿𝑡 , and solve 𝐿𝑦 = 𝑏, 𝐿𝑡 𝑥 = 𝑦.

(b) Add your codes


Answer:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file sgp_main.cpp
/// @brief The main function.
///
/// @author William Liao
///

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <timer.hpp>
#include "sgp.hpp"

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 32
using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Main function for spectral graph partitioning.
///
int main( int argc, char** argv ){
int err_test;

// need at least 2 argument!


assert( argc >= 2 );

// check 2nd argument


assert( argv[1] != NULL );

// read file
int E_size_r, E_size_c, *E;
cout << "Read the graph data from file..............." << flush;
err_test = readGraph(argv[1], &E, &E_size_r, &E_size_c);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "Size of data is " << E_size_r << "x" << E_size_c << endl;

// set graph type


int type;
char flag1;
if ( argc == 2 )
{
type = setgraphtype(E_size_c);
}else if( argc == 3 ){
type = setgraphtype(argv[2], E_size_c);
}

if ( type == 0 )
{
flag1 = 'S';
cout << "type of graph: simple graph" << endl;
}else if( type == 1 ){
flag1 = 'D';
cout << "type of graph: directed (multi) graph" << endl;
}else if ( type == 2 ){

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 33
flag1 = 'W';
cout << "type of graph: directed weighted graph" << endl;
}else if ( type == 3 )
{
flag1 = 'U';
cout << "type of graph: undirected weighted graph" << endl;
}

// Construct adjacency matrix of graph


int nnz, *cooRowIndA, *cooColIndA, n;
double *cooValA;
cout << "Construct adjacency matrix of graph........." << flush;
err_test = GraphAdjacency(E, E_size_r, &nnz, &cooRowIndA, &cooColIndA,
&cooValA, &n, flag1);
assert( err_test == 0 ); cout << " Done. " << endl;
cout << "size of matrix = " << n << endl;
cout << "nnz of A = " << nnz << endl;

// Construct Laplacian
int *csrRowIndA, *csrColIndA;
double *csrValA;
double shift_sigma = 1e-5; // Modify shift_sigma to set the
// shift
cout << "Construct Laplacian matrix of graph........." << flush;
GraphLaplacian(&nnz, cooRowIndA, cooColIndA, cooValA, n, &csrRowIndA,
&csrColIndA, &csrValA, shift_sigma);
cout << " Done. " << endl;
cout << "nnz of L = " << nnz << endl;

// Shift to zero-based indexing


int tmp;
for (int i = 0; i < nnz; i++)
{
tmp = csrColIndA[i]-1;
csrColIndA[i] = tmp;
}
for (int i = 0; i < n+1; i++)
{
tmp = csrRowIndA[i]-1;
csrRowIndA[i] = tmp;

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 34
}

// Generate RHS
double *b;
b = new double[n];
genRHS(b, n, nnz, csrValA, csrRowIndA, csrColIndA);

// Solve LS
double *x, timer;
x = new double[n];
char flag = 'H'; // Modify flag to choose solver on GPU
// or CPU. Possible options are
// 'H': solver on host (CPU)
// 'D': solver on device (GPU)

int solver = 1; // Modify solver to switch between


// different linear solvers. Possible
// options are
// 0: LU
// 1: Cholesky
// 2: QR

cout << "Solving Linear System......................." << flush;

switch (flag){
case 'H':
tic(&timer);
solvelsHost(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x, solver);
cout << " Done. ";
toc(&timer);
break;
case 'D':
tic(&timer);
solvels(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x, solver); cout << "
Done. ";
toc(&timer);
break;
}

// Compute redsidual

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 35
double res;
res = residual(n, nnz, csrValA, csrRowIndA, csrColIndA, b, x);

cout << "||Ax - b|| = " << res << endl;

return 0;
}

(c) Numerical results and observations


Answer:

 Numerical results:

Facebook (NIPS)
tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out ego-facebook
Read the graph data from file............... Done.
Size of data is 2981x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 2888
nnz of A = 5962
Construct Laplacian matrix of graph......... Done.
nnz of L = 8850
Solving Linear System....................... Done. Elapsed time is 0.282097 seconds.
||Ax - b|| = 1.05751e-11

US power grid
tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out opsahl-powergrid
Read the graph data from file............... Done.
Size of data is 6594x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 4941
nnz of A = 13188
Construct Laplacian matrix of graph......... Done.
nnz of L = 18129
Solving Linear System....................... Done. Elapsed time is 0.340004 seconds.
||Ax - b|| = 7.89707e-14

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 36
Euroroad
tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out subelj_euroroad_euroroad
Read the graph data from file............... Done.
Size of data is 1417x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1174
nnz of A = 2834
Construct Laplacian matrix of graph......... Done.
nnz of L = 4008
Solving Linear System....................... Done. Elapsed time is 0.321828 seconds.
||Ax - b|| = 2.85548e-14

Human Protein (Vidal)


tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out maayan-vidal
Read the graph data from file............... Done.
Size of data is 6726x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 3133
nnz of A = 13452
Construct Laplacian matrix of graph......... Done.
nnz of L = 16008
Solving Linear System....................... Done. Elapsed time is 0.801672 seconds.
||Ax - b|| = 0.00138355

Protein
tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out moreno_propro_propro
Read the graph data from file............... Done.
Size of data is 2277x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1870
nnz of A = 4554
Construct Laplacian matrix of graph......... Done.
nnz of L = 6350
Solving Linear System....................... Done. Elapsed time is 0.325734 seconds.
||Ax - b|| = 0.00230557

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 37
U. Rovira i Virgili
tms17nla69@z-gotham:~/NLA17/H03$ ./sgp_main_ls.out URVirgili
Read the graph data from file............... Done.
Size of data is 5451x2
type of graph: simple graph
Construct adjacency matrix of graph......... Done.
size of matrix = 1133
nnz of A = 10902
Construct Laplacian matrix of graph......... Done.
nnz of L = 12035
Solving Linear System....................... Done. Elapsed time is 0.41268 seconds.
||Ax - b|| = 5.4033e-13

The above result can be represented properly via the following table:
Num. Data name Matrix size ‖Ax − b‖ Elapsed time (s)
1 Facebook (NIPS) 2888 1.05751e-11 0.282097
2 US power grid 4941 7.89707e-14 0.340004
3 Euroroad 1174 2.85548e-14 0.321828
4 Human Protein (Vidal) 3133 0.00138355 0.801672
5 Protein 1870 0.00230557 0.325734
6 U. Rovira i Virgili 1133 5.4033e-13 0.41268

 Observations:

Let ‖Ax − b‖ is called Norm, we use SPSS14 on a test of normality data for Norm and
Elapsed time then be used one sample Kolmogorov-Smirnov test. The following below
the results:

NPar Tests
One-Sample Kolmogorov-Smirnov Test

Norm
N 6
Mean .0006
Normal Parameters(a,b)
Std. Deviation .00100
Most Extreme Absolute .398
Differences Positive .398
Negative -.269
Kolmogorov-Smirnov Z .975
Asymp. Sig. (2-tailed) .298
a Test distribution is Normal.
b Calculated from data.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 38
One-Sample Kolmogorov-Smirnov Test

Elapsed_time
N 6
Mean .4140
Normal Parameters(a,b)
Std. Deviation .19465
Most Extreme Absolute .336
Differences Positive .336
Negative -.249
Kolmogorov-Smirnov Z .823
Asymp. Sig. (2-tailed) .507
a Test distribution is Normal.
b Calculated from data.

Afterwards, we use SPSS14 on a test of correlations for Norm and Elapsed time then be
used correlation coefficients, Pearson, with a test of significance two-tailed, and also we
displayed descriptive statistics like mean and standard deviations. The following below
the results:

Correlations
Descriptive Statistics

Mean Std. Deviation N


Elapsed_time .4140 .19465 6
Norm .0006 .00100 6

Correlations

Elapsed_time Norm
Elapsed_time Pearson Correlation 1 .343
Sig. (2-tailed) .505
N 6 6
Norm Pearson Correlation .343 1
Sig. (2-tailed) .505
N 6 6

We have a conclusion that Norm and Elapsed time there exists a correlation.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 39
REFERENCES

[1] http://konect.uni-koblenz.de/networks/
[2] http://konect.uni-koblenz.de/networks/ego-facebook
[3] http://konect.uni-koblenz.de/networks/opsahl-powergrid
[4] http://konect.uni-koblenz.de/networks/subelj_euroroad
[5] http://konect.uni-koblenz.de/networks/maayan-vidal
[6] http://konect.uni-koblenz.de/networks/moreno_propro
[7] http://konect.uni-koblenz.de/networks/arenas-email
[8] Burden, R.L., and Faires, J.D., (2011). Numerical Analysis. 9ed. Cengage Learning.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 40

You might also like