-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyMatrix.java
More file actions
79 lines (66 loc) · 1.48 KB
/
AdjacencyMatrix.java
File metadata and controls
79 lines (66 loc) · 1.48 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
import java.util.List;
/**
* This class represents a directed graph with no parallel edges
*
* @author morin
*
*/
public class AdjacencyMatrix implements Graph {
protected int n;
protected boolean[][] a;
/**
* Create a new adjacency matrix with n vertices
* @param n
*/
public AdjacencyMatrix(int n0) {
n = n0;
a = new boolean[n][n];
}
public void addEdge(int i, int j) {
a[i][j] = true;
}
public void removeEdge(int i, int j) {
a[i][j] = false;
}
public boolean hasEdge(int i, int j) {
return a[i][j];
}
public List<Integer> outEdges(int i) {
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < n; j++)
if (a[i][j]) edges.add(j);
return edges;
}
public List<Integer> inEdges(int i) {
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < n; j++)
if (a[j][i]) edges.add(j);
return edges;
}
public int inDegree(int i) {
int deg = 0;
for (int j = 0; i < n; i++)
if (a[j][i]) deg++;
return deg;
}
public int outDegree(int i) {
int deg = 0;
for (int j = 0; i < n; i++)
if (a[i][j]) deg++;
return deg;
}
public int nVertices() {
return n;
}
public static void main(String[] args) {
for (int n = 10; n < 500; n *= 2) {
Graph am = new AdjacencyMatrix(n);
Graph al = new AdjacencyLists(n);
System.out.print("Running tests on graphs of size " + n + "...");
System.out.flush();
// Testum.graphTests(am, al);
System.out.println("done");
}
}
}