-
Notifications
You must be signed in to change notification settings - Fork 104
/
AdjMatrix.java
50 lines (39 loc) · 1.15 KB
/
AdjMatrix.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
47
48
49
50
import java.io.*;
import java.util.Scanner;
public class AdjMatrix {
private int V;
private int E;
private int[][] adj;
public AdjMatrix(String filename){
File file = new File(filename);
try(Scanner scanner = new Scanner(file)){
V = scanner.nextInt();
adj = new int[V][V];
E = scanner.nextInt();
for(int i = 0; i < E; i ++){
int a = scanner.nextInt();
int b = scanner.nextInt();
adj[a][b] = 1;
adj[b][a] = 1;
}
}
catch(IOException e){
e.printStackTrace();
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E));
for(int i = 0; i < V; i ++){
for(int j = 0; j < V; j ++)
sb.append(String.format("%d ", adj[i][j]));
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args){
AdjMatrix adjMatrix = new AdjMatrix("g.txt");
System.out.print(adjMatrix);
}
}