-
Notifications
You must be signed in to change notification settings - Fork 1
/
SparseMatrix.java
57 lines (42 loc) · 1.11 KB
/
SparseMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package Tarjan;
import java.util.Iterator;
import edu.princeton.cs.algs4.Bag;
import edu.princeton.cs.algs4.Digraph;
import edu.princeton.cs.algs4.In;
public class SparseMatrix {
public int[] col_ind;
public int[] row_ptr;
public SparseMatrix(Digraph graph) {
col_ind = new int[graph.E()];
row_ptr = new int[graph.V() + 1];
int iter = 0, v;
Iterable<Integer> adj;
for (v = 0; v < graph.V(); v++) {
row_ptr[v] = iter;
// if (v != 0 && row_ptr[v] == row_ptr[v-1]) {
// row_ptr[v-1] = 0; //???
// }
adj = graph.adj(v);
for (int w : adj) {
col_ind[iter] = w;
iter++;
}
}
row_ptr[v] = graph.E(); // just a convention
}
public void print(int[] vec) {
for (int i = 0; i < vec.length; i++) {
System.out.print(vec[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
String graphFile = "/Users/shuangwei/Documents/Java/data/tinyDG.txt";
// In in = new In(args[0]);
In in = new In(graphFile);
Digraph G = new Digraph(in);
SparseMatrix matrix = new SparseMatrix(G);
matrix.print(matrix.col_ind);
matrix.print(matrix.row_ptr);
}
}