Skip to content

Commit 0685507

Browse files
committed
Add adjacency matrix activity
1 parent af383d7 commit 0685507

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.weightedundirected;
2+
3+
public class AdjacencyMatrixWeightedUndirected {
4+
int[][] adj;
5+
6+
public AdjacencyMatrixWeightedUndirected(int nodes) {
7+
this.adj = new int[nodes][nodes];
8+
}
9+
10+
public void addEdge(int u, int v, int weight) { }
11+
12+
public int edgeWeight(int u, int v) { return 0; }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.weightedundirected.solution;
2+
3+
public class AdjacencyMatrixWeightedUndirected {
4+
int[][] adj;
5+
6+
public AdjacencyMatrixWeightedUndirected(int nodes) {
7+
this.adj = new int[nodes][nodes];
8+
}
9+
10+
public void addEdge(int u, int v, int weight) {
11+
this.adj[u][v] = weight;
12+
this.adj[v][u] = weight;
13+
}
14+
15+
public int edgeWeight(int u, int v) {
16+
return this.adj[u][v];
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.packt.datastructuresandalg.lesson6.activity.weightedundirected;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
8+
public class AdjacencyMatrixWeightedUndirectedTest extends TestCase {
9+
public void test1() {
10+
AdjacencyMatrixWeightedUndirected g = new AdjacencyMatrixWeightedUndirected(5);
11+
g.addEdge(0, 1, 10);
12+
g.addEdge(0, 4, 5);
13+
g.addEdge(1, 2, 3);
14+
g.addEdge(2, 3, 1);
15+
g.addEdge(4, 3, 2);
16+
17+
assertTrue(g.edgeWeight(4, 0) == 5);
18+
assertTrue(g.edgeWeight(0, 4) == 5);
19+
assertTrue(g.edgeWeight(0, 1) == 10);
20+
assertTrue(g.edgeWeight(1, 0) == 10);
21+
assertTrue(g.edgeWeight(3, 4) == 2);
22+
assertTrue(g.edgeWeight(1, 3) == 0);
23+
}
24+
}

0 commit comments

Comments
 (0)