This repository was archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
48 lines (42 loc) · 1.41 KB
/
Matrix.java
File metadata and controls
48 lines (42 loc) · 1.41 KB
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
/* This class represents an n*n Matrix.
* Each node of the matrix has a Vector containing all the information of that node in relation to other nodes.
* This method is quite memory-heavy since for 10 nodes, no matter how many edges, it will consist of 10 Vectors of length 10.
* Author: Seppe Lampe
*/
public class Matrix
{
private Vector data;
private int nrNodes;
public Matrix(int nrNodes)
{
this.nrNodes = nrNodes;
data = new Vector(nrNodes*nrNodes); // allocate an N-by-N matrix where N = nrNodes
for(int i=0; i<nrNodes; i++) { //O(n)
for(int x=0; x<nrNodes; x++) {
data.set(i*nrNodes+x, 0); //Set every element to 0 instead of null
}
}
}
public void set(int row, int col, Comparable weight) //O(1)
{
data.set(row*nrNodes + col, weight); // store the weight at the given row and column.
}
public Comparable get(int row, int col) //O(1)
{
return data.get(row*nrNodes + col); // return the weight at the given row and column.
}
// Return a String notation of the matrix
public String toString() { //O(n)
String result = new String();
for(int i=0; i<nrNodes; i++) {
for(int x=0; x<nrNodes; x++) {
result += data.get(i*nrNodes+x).toString() + " ";
}
result += " ";
}
return result;
}
public void print() { //O(n)
System.out.println(this.toString());
}
}