Search

๊ทธ๋ž˜ํ”„

๊ทธ๋ž˜ํ”„(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
๋ณต์‚ฌ