๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/์ž๋ฃŒ๊ตฌ์กฐ

[Java] Graph (Adjacency Matrix, Adjacency List)

by ๐Ÿณ Laboon 2024. 7. 9.
์ •์ (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

 (Directed & UnDirected)
Weight Graph

 

์ธ์ ‘ํ–‰๋ ฌ 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