-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjacencyMatrix.java
46 lines (37 loc) · 1.2 KB
/
AdjacencyMatrix.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
import java.util.ArrayList;
public class AdjacencyMatrix {
public static void printAdjacentMatrix(int[][] graph) {
int vertices = graph.length;
System.out.println("Adjacenct Matrix: ");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
public static void printAdjacentVertices(int[][] graph) {
int vertices = graph.length;
System.out.println("Adjacenct Vertices: ");
for (int i = 0; i < vertices; i++) {
System.out.print(i + " > ");
ArrayList<Integer> adjacentVertices = new ArrayList<>();
for (int j = 0; j < vertices; j++) {
if (graph[i][j] == 1) {
adjacentVertices.add(j);
}
}
System.out.println(adjacentVertices);
}
}
public static void main(String args[]) {
int[][] graph = {
{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 0, 0}
};
printAdjacentMatrix(graph);
printAdjacentVertices(graph);
}
}