-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7q4.java
More file actions
55 lines (43 loc) · 1.48 KB
/
Lab7q4.java
File metadata and controls
55 lines (43 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
import java.util.ArrayList;
public class Lab7q4 {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> list= new ArrayList<>();
int vertices=4;
for(int i=0;i<vertices;i++){
list.add(new ArrayList<>());
}
list.get(0).add(1);
list.get(0).add(2);
list.get(1).add(3);
list.get(2).add(1);
list.get(3).add(2);
System.out.println("Adjancecy list is:");
for(int j=0;j<list.size();j++){
System.out.print(j+ " : { ");
for(Integer element: list.get(j)){
System.out.print(element + " ");
}
System.out.println(" }");
}
int[][] matrix= new int[vertices][vertices];
matrix=convertListToMatrix(list);
System.out.println("Adjancecy matrix is:");
for(int i=0;i<list.size();i++){
for(int j=0;j<list.size();j++){
System.out.print(matrix[i][j]+ " ");
}
System.out.println("");
}
}
private static int[][] convertListToMatrix(ArrayList<ArrayList<Integer>> list){
int vertices= list.size();
int[][] matrix = new int[vertices][vertices];
for(int i=0;i<vertices;i++){
for(Integer element : list.get(i)){
matrix[i][element]=1;
//matrix[element][i]=1;
}
}
return matrix;
}
}