forked from ehfmakan123/java_algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_Test.java
57 lines (52 loc) · 1.63 KB
/
Graph_Test.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
51
52
53
54
55
56
57
package Test;
import java.io.*;
import java.util.*;
public class GraphTest {
static int N;
static boolean[][] adjMatrix;
public static void main(String[] args) throws NumberFormatException, IOException {
System.setIn(new FileInputStream("res/Graph_input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
adjMatrix = new boolean[N][N];
int C = Integer.parseInt(br.readLine());
for(int i = 0 ; i < C ; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
adjMatrix[to][from] = adjMatrix[from][to] = true;
}
System.out.println("====================bfs======================");
bfs();
System.out.println("====================dfs======================");
boolean[] visited = new boolean[N];
dfs(0,visited);
br.close();
}
private static void bfs() {
Queue<Integer> queue = new LinkedList<Integer>();
boolean[] visited = new boolean[N];
queue.offer(0);
visited[0] = true;
while(!queue.isEmpty()) {
int current = queue.poll();
System.out.println((char)(current+65));
for(int i = 0 ; i < N ; i++) {
if(!visited[i] // 방문한지 안한지 확인
&& adjMatrix[current][i]) { // 인접한건지 확인(연결 되었는가)
queue.offer(i);
visited[i] = true;
}
}
}
}
private static void dfs(int current , boolean[] visited) {
visited[current] = true;
System.out.println((char)(current+65));
for(int i = 0 ; i < N ; i++) {
if(!visited[i] && adjMatrix[current][i]) {
dfs(i,visited);
}
}
}
}