์ ์ (Vertex)๊ณผ ๊ฐ์ (Edge)๋ก ๊ตฌ์ฑ๋ ๊ฒ, Graph(G) = (Vertex(V), Edge(E))
Graph ์ฉ์ด
์ ์ (Vertex) - ๋ ธ๋, ํน์ ํ ์ ์๋ ์ค์ฒด
๊ฐ์ (Edge) - ์ ์ ๊ฐ ์ด์ด์ ธ ์๋ ์
๊ฐ์ค์น(Weight) - ์ ์ ๊ฐ์ ๊ฑฐ๋ฆฌ, ๋น์ฉ (๊ฐ์ ์ ๊ธธ์ด๋ผ๊ณ ์๊ฐํด๋ ์ข์)
๊ฒฝ๋ก(Path) - ์ถ๋ฐ ์ ์ ์์ ๋์ฐฉ ์ ์ ๊น์ง์ ๊ฑฐ์ณ ๊ฐ ์์
์ํ(Cycle) - ๊ฒฝ๋ก์ ์ถ๋ฐ ์ ์ ๊ณผ ๋์ฐฉ ์ ์ ์ด ๊ฐ์ ๊ฒฝ์ฐ
์ฐจ์(Degree) - ์ ์ ์ ๋ถ์ ๊ฐ์ ์
์ง์ ์ฐจ์(In-Degree) - ์ ์ ์ผ๋ก ๋ค์ด์ค๋ ๊ฐ์ ์
์ง์ถ ์ฐจ์(Out-Degree) - ์ ์ ์์ ๋๊ฐ๋ ๊ฐ์ ์
Graph ์ข ๋ฅ
์ ํฅ ๊ทธ๋ํ(Directed Graph) - ํน์ ์ ์ ์์ ํน์ ์ ์ ์ผ๋ก ์ด๋ํ๋ ๊ทธ๋ํ
๋ฌดํฅ ๊ทธ๋ํ(UnDirected Graph) - ํน์ ์ ์ ๊ณผ ํน์ ์ ์ ์ด ์ฐ๊ฒฐ๋ ๊ทธ๋ํ (์๋ฐฉํฅ ์ด๋ ๊ฐ๋ฅ)
๊ฐ์ค ๊ทธ๋ํ(Weighted Graph) - ๊ฐ์ ์ ๊ฐ์ค์น(Weight)๊ฐ ๋ถ์ (์ ํฅ, ๋ฌดํฅ์ผ๋ก ํํ๊ฐ๋ฅ)
๋น์ํ ๊ทธ๋ํ(Acyclic Graph) - ์ํ์ด ์๋ ๊ทธ๋ํ
๋ถ๋ถ ๊ทธ๋ํ(Sub Graph) - ํน์ ๊ทธ๋ํ ๋ด์ ์กด์ฌํ๋ ๊ทธ๋ํ
๋จ์ ๊ทธ๋ํ(Disconnection Graph) - ๋ชจ๋ ์ ์ ์ด ์ฐ๊ฒฐ๋์ด ์์ง ์์ ๊ทธ๋ํ (์ฌ๋ฌ ๊ทธ๋ํ๊ฐ ์กด์ฌ ๊ฐ๋ฅ)
Tip
1. ๋ณดํต ์ ํฅ ๊ทธ๋ํ ๋๋ ๋ฌดํฅ ๊ทธ๋ํ์ด๋ค.
2. ๋ฌดํฅ ๊ทธ๋ํ์ ๊ฒฝ์ฐ A - B ๋ ์ ์ ๊ฐ์ ์ฌ์ดํด๋ก ์ธ์ํ ์ ์์ผ๋ฏ๋ก ๋ฐฉ๋ฌธ์ฒ๋ฆฌ ํ์
3. ๊ฐ ๊ทธ๋ํ๋ฅผ ์กฐํฉํด์ ํน์ ์ฑ์ง์ ๋ ๋ ๊ทธ๋ํ ์์ฑ๊ฐ๋ฅ ex) ์ ํฅ ๋น์ํ ๊ทธ๋ํ (DAG)
4. ๋จ์ ๊ทธ๋ํ๋ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ์์ ๊น๋จน๊ธฐ ์ฌ์ด ์ฑ์ง์ด๋ค. (๋ณดํต Connection Graph๋ก ์๊ฐํจ)
๊ตฌํ (์ธ์ ํ๋ ฌ ๋ฐฉ์๊ณผ ์ธ์ ๋ฆฌ์คํธ ๋ฐฉ์)
์ธ์ ํ๋ ฌ(Adjacency Matrix)
package com.study.datastructrue.graph;
public class AdjacencyMatrix {
private int[][] directedGraph;
private int[][] unDirectedGraph;
public AdjacencyMatrix(int size) {
this.directedGraph = new int[size][size];
this.unDirectedGraph = new int[size][size];
}
public void dirAdd(int u, int v) {
directedGraph[u][v] = 1;
}
public void dirAdd(int u, int v, int w) {
directedGraph[u][v] = w;
}
public void dirPrint() {
for (int i = 0; i < directedGraph.length; i++) {
System.out.print((char)(i + 'A'));
for (int j = 0; j < directedGraph[0].length; j++) {
System.out.printf("%2d", directedGraph[i][j]);
}
System.out.println();
}
}
public void unDirAdd(int u, int v) {
unDirectedGraph[u][v] = unDirectedGraph[v][u] = 1;
}
public void unDirAdd(int u, int v, int w) {
unDirectedGraph[u][v] = unDirectedGraph[v][u] = w;
}
public void unDirPrint() {
for (int i = 0; i < unDirectedGraph.length; i++) {
System.out.print((char)(i + 'A'));
for (int j = 0; j < unDirectedGraph[0].length; j++) {
System.out.printf("%2d", unDirectedGraph[i][j]);
}
System.out.println();
}
}
}
์ธ์ ๋ฆฌ์คํธ(Adjacency List)
package com.study.datastructrue.graph;
import java.util.ArrayList;
import java.util.Objects;
public class AdjacnecyList {
private ArrayList<ArrayList<Object>> directedGraph = new ArrayList<>();
private ArrayList<ArrayList<Object>> unDirectedGraph = new ArrayList<>();
public AdjacnecyList(int size) {
for (int i = 0; i <= size; i++) {
directedGraph.add(new ArrayList<>());
unDirectedGraph.add(new ArrayList<>());
}
}
public void dirAdd(int u, int v) {
directedGraph.get(u).add(v);
}
public void dirAdd(int u, int v, int w) {
directedGraph.get(u).add(new Edge(v, w));
}
public void dirPrint() {
for (int i = 0; i < directedGraph.size(); i++) {
System.out.print((char)(i + 'A') + ": ");
for (Object obj : directedGraph.get(i)) {
if (obj instanceof Integer) {
System.out.print((char)((int)obj + 'A'));
} else if (obj instanceof Edge) {
Edge edge = (Edge) obj;
System.out.print("{" + (char)(edge.vertex + 'A') + ", " + edge.weight + "}");
}
System.out.print(" -> ");
}
System.out.println();
}
}
public void unDirAdd(int u, int v) {
unDirectedGraph.get(u).add(v);
unDirectedGraph.get(v).add(u);
}
public void unDirAdd(int u, int v, int w) {
directedGraph.get(u).add(new Edge(v, w));
directedGraph.get(v).add(new Edge(u, w));
}
public void unDirPrint() {
for (int i = 0; i < unDirectedGraph.size(); i++) {
System.out.print((char)(i + 'A') + ": ");
for (Object obj : unDirectedGraph.get(i)) {
if (obj instanceof Integer) {
System.out.print((char)((int)obj + 'A'));
} else if (obj instanceof Edge) {
Edge edge = (Edge) obj;
System.out.print("{" + (char)(edge.vertex + 'A') + ", " + edge.weight + "}");
}
System.out.print(" -> ");
}
System.out.println();
}
}
private class Edge {
int vertex;
int weight;
Edge(int vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
}
}
Main
package com.study.datastructrue.graph;
public class Main {
public static void main(String[] args) {
AdjacencyMatrix matGraph = new AdjacencyMatrix(5);
AdjacnecyList listGraph = new AdjacnecyList(5);
// a -> b, a -> c, a -> d
// b -> d, d -> e, e -> b
System.out.println("Directed Adj Matrix Graph");
System.out.println(" A B C D E");
matGraph.dirAdd(0, 1); matGraph.dirAdd(0, 2);
matGraph.dirAdd(0, 3); matGraph.dirAdd(1, 3);
matGraph.dirAdd(3, 4); matGraph.dirAdd(4, 1);
matGraph.dirPrint();
System.out.println();
System.out.println("Directed Adj List Graph");
listGraph.dirAdd(0, 1); listGraph.dirAdd(0, 2);
listGraph.dirAdd(0, 3); listGraph.dirAdd(1, 3);
listGraph.dirAdd(3, 4); listGraph.dirAdd(4, 1);
listGraph.dirPrint();
System.out.println();
// a - b, a - c, a - d
// c - d, d - e
System.out.println("UnDirected Adj Matrix Graph");
System.out.println(" A B C D E");
matGraph.unDirAdd(0, 1); matGraph.unDirAdd(0, 2);
matGraph.unDirAdd(0, 3); matGraph.unDirAdd(2, 3);
matGraph.unDirAdd(3, 4);
matGraph.unDirPrint();
System.out.println();
System.out.println("UnDirected Adj List Graph");
listGraph.unDirAdd(0, 1); listGraph.unDirAdd(0, 2);
listGraph.unDirAdd(0, 3); listGraph.unDirAdd(2, 3);
listGraph.unDirAdd(3, 4);
listGraph.unDirPrint();
}
}
โป Overloading ์ผ๋ก ์ธํด dirAdd ๋๋ unDirAdd ๋ฉ์๋์ Argument๋ฅผ ํ๋ ๋ ์ถ๊ฐํ๋ฉด ๊ฐ์ค์น๊ฐ ํ ๋น๋๋ค.
Result
์ธ์ ํ๋ ฌ vs ์ธ์ ๋ฆฌ์คํธ
์ธ์ ํ๋ ฌ์ ๋ ์ ์ ๊ฐ์ 1๋ฒ๋ง์ ์ ๊ทผํ ์ ์๋ค.
์ธ์ ๋ฆฌ์คํธ๋ ๋ ์ ์ ๊ฐ์ ์ถ๋ฐ ์ ์ ์ ๊ฐ์ ๊ฐ์๋ง์ ์ ๊ทผํ ์๋ ์๋ค.
์ธ์ ํ๋ ฌ์ ๊ณต๊ฐ์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
์ธ์ ๋ฆฌ์คํธ๋ ๊ณต๊ฐ์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๋ค.
์ ๋ฆฌ : ์ธ์ ํ๋ ฌ, ์ธ์ ๋ฆฌ์คํธ ์ค ๊ตฌํํ๊ธฐ ๋ ์ฌ์ด๊ฑธ ์ฐ์. ๋ค๋ง N > 10,000 ์ธ ๊ฒฝ์ฐ๋ ๋ฌด์กฐ๊ฑด ์ธ์ ๋ฆฌ์คํธ!
'์๊ณ ๋ฆฌ์ฆ > ์๋ฃ๊ตฌ์กฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Binary Tree (0) | 2024.07.08 |
---|---|
[Java] Binary Search Tree (0) | 2024.07.08 |
[Java] Tree (0) | 2024.07.07 |
6. STL ์ฌ์ฉํ๊ธฐ - std::list (0) | 2024.02.11 |
5. ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Circular Linked List) (1) | 2024.02.10 |