๊ทธ๋ํ(Graph)
์ ์: ์ ์ (Vertex)๊ณผ ๊ฐ์ (Edge)๋ค์ ์ ํ ์งํฉ์ผ๋ก, ์ ์ ๋ค ์ฌ์ด์ ์ฐ๊ฒฐ ๊ด๊ณ๋ฅผ ํํํ๋ ์๋ฃ๊ตฌ์กฐ. N:N ๊ด๊ณ๋ฅผ ํํํ๋๋ฐ ์ฉ์ดํ๋ฉฐ 100์ ์ง๋ฆฌ ์๋ฃ๊ตฌ์กฐ์.
ํต์ฌ ์์
๊ตฌ์ฑ ์์ | ์ค๋ช
|
์ ์ (Vertex/Node) | ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ ์์น |
๊ฐ์ (Edge) | ์ ์ ๊ฐ์ ์ฐ๊ฒฐ์ ๋ํ๋ด๋ ์ |
์ธ์ (Adjacent) | ๋ ์ ์ ์ด ๊ฐ์ ์ผ๋ก ์ง์ ์ฐ๊ฒฐ๋ ์ํ |
์ฐจ์ (Degree) | ํ๋์ ์ ์ ์ ์ฐ๊ฒฐ๋ ๊ฐ์ ์ ์ |
๊ทธ๋ํ ํ์
โข
๋๋น ์ฐ์ ํ์ - BFS
โข
๊น์ด ์ฐ์ ํ์ - DFS
๋๋น ์ฐ์ ํ์ - BFS
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/*
(์
๋ ฅ์์)
1
7 6
1 7
1 2
2 4
2 5
1 3
3 6
6 7
(๊ทธ๋ํ ์์)
1
/ \
2 3
/ \ \
4 5 6
|
7
*/
public class bfs_Samplecode {
static int T, N, M, A, B;
// ์ ์ ์ ๋ด์ ํ (row, col, cost)
static Queue<Integer> que = new LinkedList<Integer>();
// ์ถ๋ฐ์ง S, ๋ชฉ์ ์ง E
static int S, E;
// ์ธ์ ํ๋ ฌ
static int MAT[][] = new int[101][101];
static int visited[] = new int[101];
// ๋ฐฉ๋ฌธ ํ์
static int cnt = 0;
public static void bfs() {
// 1. ์์์ ์ ํ์ ๋ฃ๋๋ค.
que.add(S);
visited[S] = 1;
cnt++;
// 5. ํ๊ฐ ๋น์ด์๋์ง ์๋ค๋ฉด ๋ฐ๋ณต
while(!que.isEmpty()) {
// 2. ํ์์ ํ ์ ์ ๊บผ๋ด์ ๊ธฐ์ค์ ์ผ๋ก ์ผ๋๋ค.
System.out.println("now : " + que.peek());
int now = que.poll();
// 3. ๊ธฐ์ค์ ์ด ๋ชฉ์ ์ง์ด๋ฉด ํ์์ ์ข
๋ฃํ๋ค.
if( now == E )
break;
// 4. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ๊ธฐ์ค์ ์์ ๊ฐ ์ ์๋ ๋ค๋ฅธ ์ ์ ๋ค์ ํ์ ๋ฃ๋๋ค.
else {
for (int i = 1; i <= N; i++) {
// ๊ธฐ์กด์ ๋ฐฉ๋ฌธํ์ง ์๊ณ
// ํ์ฌ ์ ์ ๊ณผ ์ฐ๊ฒฐ๋ ์ ์ ํ์
if( visited[i] == 0 && MAT[now][i] == 1 ) {
visited[i] = 1;
cnt++;
// ํด๋น ์ง์ ์ ํ์ ๋ฃ๋๋ค.
que.add(i);
System.out.println(i + "๋ฒ ์ ์ ๋ฐฉ๋ฌธ");
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
// N : ์ ์ ์ ์
// M : ๊ฐ์ ์ ์
N = sc.nextInt();
M = sc.nextInt();
// S : ์ถ๋ฐ์ง
// E : ๋ชฉ์ ์ง
S = sc.nextInt();
E = sc.nextInt();
// visited๋ฐฐ์ด ์ด๊ธฐํ
for (int i = 1; i <= N; i++) {
visited[i] = 0;
}
// ์ธ์ ๋ฐฐ์ด ์ด๊ธฐํ
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
MAT[i][j] = 0;
}
}
// ์ธ์ ๋ฐฐ์ด ์ ์ ๊ฐ์ ๊ด๊ณ ์ง์
for (int i = 1; i <= M; i++) {
A = sc.nextInt();
B = sc.nextInt();
MAT[A][B] = 1;
}
// ์ธ์ ๋ฐฐ์ด ์ถ๋ ฅ
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
System.out.print(MAT[i][j] + " ");
}
System.out.println();
}
// ๊ทธ๋ํ ํ์ - BFS(Breadth First Search)
bfs();
System.out.println(cnt + "๊ฐ์ ์ ์ ๋ฐฉ๋ฌธ");
}
}
}
Java
๋ณต์ฌ
๊น์ด ์ฐ์ ํ์ - DFS
import java.util.Scanner;
/*
๊น์ด ์ฐ์ ํ์(Depth First Search, DFS)
- ๊น์ด ์ฐ์ ํ์์ ํธ๋ฆฌ๋ ๊ทธ๋ํ๋ฅผ ํ์ํ๋ ์๊ณ ๋ฆฌ์ฆ ์ค ํ๋๋ก,
ํ ์ ์ ์์ ์ถ๋ฐํ์ฌ ๊ฐ๋ฅํ ๋ฉ๋ฆฌ๊น์ง ํ์ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
- dfs๋ ์ง๋์จ ๊ฒฝ๋ก๋ฅผ ์ฝ๊ฒ ํ์
ํ ์ ์๋ ์ฅ์ ์ด ์์ผ๋ฉฐ,
์คํ(Stakc) ์ด๋ ์ฌ๊ทํจ์(Recursion Func)๋ก ๊ตฌํํ ์ ์๋ค.
(์
๋ ฅ์์)
1
6 5
1 2
2 3
2 4
1 5
5 6
(๊ทธ๋ํ ์์)
1
/ \
2 5
/ \ \
3 4 6
*/
public class dfs_Samplecode {
static int T, N, M, A, B;
// i๋ฒ์งธ ์ ์ ์ ๋ฐฉ๋ฌธํ๋์ง ์ฌ๋ถ๋ฅผ ์ฒดํฌํ ๋ฐฐ์ด
// i๋ฒ์งธ ์ ์ ์ ๋ฐฉ๋ฌธ(O) visited[i] = 1
// i๋ฒ์งธ ์ ์ ์ ๋ฐฉ๋ฌธ(x) visited[i] = 0
static int visited[] = new int[101];
// ์ธ์ ๋ฐฐ์ด
static int MAT[][] = new int[101][101];
// ๋ฐฉ๋ฌธ ํ์
static int cnt = 0;
// void dfs(int idx, int depth)
// - idx : ํ์ฌ์์น, depth : ๊น์ด 2๊ฐ์ง๋ฅผ ๊ธฐ๋ณธ์ ์ธ ์ธ์๋ก ๊ฐ์ง๋ค.
public static void dfs(int idx, int depth) {
System.out.println("depth: " + depth);
// ์ข
๋ฃ์กฐ๊ฑด
if( idx == N ) {
}
// ํ์์กฐ๊ฑด
else {
for (int i = 1; i <= N; i++) {
// ๊ธฐ์กด์ ๋ฐฉ๋ฌธํ ์ ์ ์ด ์๋๊ณ
// ํ์ฌ ์ ์ ๊ณผ ์ฐ๊ฒฐ๋ ์ ์ ์ธ ๊ฒฝ์ฐ์๋ง ํ์
if( visited[i] == 0 && MAT[idx][i] == 1 ) {
// i๋ฒ์งธ ์ ์ ์ ๋ฐฉ๋ฌธ
System.out.println(i + "๋ฒ ์ ์ ๋ฐฉ๋ฌธ " );
cnt++;
visited[i] = 1;
dfs(i, depth+1);
// ํ์์ด ๋๋๋ฉด ํด์
System.out.println(i + "๋ฒ์ผ๋ก ๋์์ด");
visited[i] = 0;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
// N : ์ ์ ์ ์
// M : ๊ฐ์ ์ ์
N = sc.nextInt();
M = sc.nextInt();
// visisted๋ฐฐ์ด ์ด๊ธฐํ
for (int i = 0; i < N; i++) {
visited[i] = 0;
}
// ์ธ์ ๋ฐฐ์ด ์ด๊ธฐํ
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
MAT[i][j] = 0;
}
}
// ์ธ์ ๋ฐฐ์ด ์ ์ ๊ฐ์ ๊ด๊ณ ์ง์
for (int i = 1; i <= M; i++) {
A = sc.nextInt();
B = sc.nextInt();
MAT[A][B] = 1;
}
// ์ธ์ ๋ฐฐ์ด ์ถ๋ ฅ
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
System.out.print(MAT[i][j] + " ");
}
System.out.println();
}
// ๊ทธ๋ํ ํ์ - DFS(Depth First Search)
dfs(1, 1);
// root(+1) ํฌํจ ์ ์ ๋ฐฉ๋ฌธ ์
System.out.println(cnt+1 + "๊ฐ์ ์ ์ ๋ฐฉ๋ฌธ");
}
}
}
Java
๋ณต์ฌ
Java๋ก ๊ตฌํํ ๊ธฐ๋ณธ ๊ทธ๋ํ ์์
public class Graph {
private int V; // ์ ์ ์ ๊ฐ์
private LinkedList<Integer>[] adj; // ์ธ์ ๋ฆฌ์คํธ
// ๊ทธ๋ํ ์์ฑ์
public Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
// ๊ฐ์ ์ถ๊ฐ
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v); // ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์ ๊ฒฝ์ฐ
}
}
Java
๋ณต์ฌ
Java Collections๋ฅผ ํ์ฉํ ๊ทธ๋ํ ๊ตฌํ
import java.util.*;
public class GraphWithCollections {
private Map<Integer, List<Integer>> adjacencyList;
public GraphWithCollections() {
adjacencyList = new HashMap<>();
}
// ์ ์ ์ถ๊ฐ
public void addVertex(int vertex) {
adjacencyList.putIfAbsent(vertex, new ArrayList<>());
}
// ๊ฐ์ ์ถ๊ฐ
public void addEdge(int source, int destination) {
// ์ ์ ์ด ์๋ค๋ฉด ์ถ๊ฐ
adjacencyList.putIfAbsent(source, new ArrayList<>());
adjacencyList.putIfAbsent(destination, new ArrayList<>());
// ์๋ฐฉํฅ ๊ฐ์ ์ถ๊ฐ
adjacencyList.get(source).add(destination);
adjacencyList.get(destination).add(source);
}
// ์ธ์ ์ ์ ๊ฐ์ ธ์ค๊ธฐ
public List<Integer> getAdjVertices(int vertex) {
return adjacencyList.get(vertex);
}
}
Java
๋ณต์ฌ