Docsity

These are algorithm courses materials., Lecture notes of Algorithms and Programming

Toggle sidebar
bg1
- 159 -
์ œ12์žฅ ๊ทธ๋ž˜ํ”„
์ด ์žฅ์—์„œ๋Š” ๊ทธ๋ž˜ํ”„๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค.
12.1. ๊ต์œก๋ชฉํ‘œ
์ด ์žฅ์˜ ๊ต์œก๋ชฉํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
๊ต์œก๋ชฉํ‘œ
๊ฐœ์š”
๊ทธ๋ž˜ํ”„๋Š” ํŠธ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ณด๋‹ค ํฐ ๊ฐœ๋…์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ์„œ,
๋…ธ๋“œ์™€๋˜๋‹ค๋ฅธ๋…ธ๋“œ๊ฐ„์—์—ฌ๋Ÿฌ๊ฒฝ๋กœ๊ฐ€์กด์žฌํ• ์ˆ˜์žˆ๊ณ , ๊ฐ„์„ ์˜
๋ฐฉํ–ฅ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.
๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰: ๋„ˆ๋น„ ์šฐ์„ , ๊นŠ์ด ์šฐ์„ 
์‹ ์žฅ ํŠธ๋ฆฌ
๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„
์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜
12.2. ๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„(graph): ๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ 
์ •์ (vertex)๋“ค์˜ ์œ ํ•œ ์ง‘ํ•ฉ๊ณผ
์ด ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ 
๊ฐ„์„ (edge)์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
์ˆ˜ํ•™์  ์ •์˜: ๊ทธ๋ž˜ํ”„ G๋Š”G = (V, E)๋กœ ์ •์˜๋˜๋ฉฐ, ์—ฌ๊ธฐ์„œ
V(G): ๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ ๋“ค์˜ ์œ ํ•œ์ง‘ํ•ฉ
E(G): ๊ฐ„์„ ๋“ค์˜ ์ง‘ํ•ฉ
๋ฌด๋ฐฉํ–ฅ
๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„(undirected graph): ๊ฐ„์„ ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„
์œ ๋ฐฉํ–ฅ
์œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„(directed graph, digraph): ๊ฐ„์„ ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š”
๊ทธ๋ž˜ํ”„
๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ๊ฐ„์„ ์„ ์•„ํฌ
์•„ํฌ(arc)๋ผ๊ณ ๋„ ํ•œ๋‹ค.
์ธ์ ‘
์ธ์ ‘ ์ •์ 
์ •์ (adjacent vertex): ๊ฐ„์„ ์— ์˜ํ•ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์ 
๊ฒฝ๋กœ
๊ฒฝ๋กœ(path): ๋‘ ๊ฐœ์˜ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ผ๋ จ์˜ ์ •์ ๋“ค
์ •์  a์—์„œ b๊นŒ์ง€์˜ ๊ฒฝ๋กœ a, v1, v2, โ€ฆ, vn, b๊ฐ€ ์กด์žฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
๊ฐ„์„  (a, v1), (v1, v2), โ€ฆ, (vn-1, vn), (vn, b)๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ (vertex)๋“ค์˜ ์œ ํ•œ์ง‘ํ•œ๊ณผ ์ด ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ (edge)์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š”
์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ทธ๋ž˜ํ”„(graph)๋ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 10์žฅ, 11์žฅ์—์„œ ์‚ดํŽด๋ณธ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋Š” ๊ทธ๋ž˜ํ”„์— ์†
ํ•œ๋‹ค. ์ฆ‰, ํŠธ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„์˜ ํŠน์ˆ˜ํ•œ ํ˜•ํƒœ์ด๋‹ค. ๊ทธ๋ž˜ํ”„๋Š” ๊ฐ„์„ ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋ฌด๋ฐฉ
ํ–ฅ(undirected) ๋˜๋Š” ์œ ๋ฐฉํ–ฅ(directed) ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜๋‰˜์–ด ์ง„๋‹ค. ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š” ๊ฐ„์„ ์„ ๋‹ค๋ฅธ ๋ง
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download These are algorithm courses materials. and more Lecture notes Algorithms and Programming in PDF only on Docsity!

๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„ (graph): ๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ ์ •์  (vertex) ๋“ค์˜ ์œ ํ•œ ์ง‘ํ•ฉ๊ณผ

์ด ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ๊ฐ„์„  (edge) ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

์ˆ˜ํ•™์  ์ •์˜ : ๊ทธ๋ž˜ํ”„ G ๋Š” G = ( V , E ) ๋กœ ์ •์˜๋˜๋ฉฐ , ์—ฌ๊ธฐ์„œ

V ( G ): ๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ ๋“ค์˜ ์œ ํ•œ์ง‘ํ•ฉ

E ( G ): ๊ฐ„์„ ๋“ค์˜ ์ง‘ํ•ฉ

๋ฌด๋ฐฉํ–ฅ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„ (undirected graph):^ ๊ฐ„์„ ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„

์œ ๋ฐฉํ–ฅ์œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„ (directed graph, digraph): ๊ฐ„์„ ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š”

๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ๊ฐ„์„ ์„ ์•„ํฌ์•„ํฌ (arc) ๋ผ๊ณ ๋„ ํ•œ๋‹ค.

์ธ์ ‘์ธ์ ‘ ์ •์ ์ •์  (adjacent vertex):^ ๊ฐ„์„ ์— ์˜ํ•ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์ 

๊ฒฝ๋กœ๊ฒฝ๋กœ (path): ๋‘ ๊ฐœ์˜ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ผ๋ จ์˜ ์ •์ ๋“ค

์ •์  a ์—์„œ b ๊นŒ์ง€์˜ ๊ฒฝ๋กœ a , v 1 , v 2 , โ€ฆ, vn , b ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

๊ฐ„์„  ( a , v 1 ), ( v 1 , v 2 ), โ€ฆ, ( v n -1 , vn ), ( vn , b ) ๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.

๊ณต์ง‘ํ•ฉ์ด ์•„๋‹Œ ์ •์ (vertex)๋“ค์˜ ์œ ํ•œ์ง‘ํ•œ๊ณผ ์ด ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ (edge)์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š”

์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ทธ๋ž˜ํ”„(graph)๋ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 10 ์žฅ, 11์žฅ์—์„œ ์‚ดํŽด๋ณธ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋Š” ๊ทธ๋ž˜ํ”„์— ์†

ํ–ฅ(undirected) ๋˜๋Š” ์œ ๋ฐฉํ–ฅ(directed) ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜๋‰˜์–ด ์ง„๋‹ค. ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š” ๊ฐ„์„ ์„ ๋‹ค๋ฅธ ๋ง

๋กœ ์•„ํฌ(arc)๋ผ ํ•œ๋‹ค. ์ธ์ ‘์ •์ (adjacent vertex)์ด๋ž€ ๊ฐ„์„ ์— ์˜ํ•ด ์—ฐ๊ฒฐ๋˜๋Š” ์ •์ ์„ ๋งํ•˜๋ฉฐ,

๊ฒฝ๋กœ(path)๋ž€ ๋‘ ๊ฐœ์˜ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ผ๋ จ์˜ ์ •์ ๋“ค์„ ๋งํ•œ๋‹ค.

M

E

L

B

A

A

B D

C

V(G1)={A,B,C,D}

E(G1)={(A,B),(A,D),(B,C),(C,D)}

V(G1)= {A,B,E,L,M}

E(G1)= {(A,E),(B,L),(E,A)

(L,L),(L,A),(M,B),(M,E)}

Path(M,A): M, B, L, A

  • ๊ธธ์ด: 3

์ฃผ๊ธฐ์ฃผ๊ธฐ(cycle):^ ์ฒซ ๋ฒˆ์งธ ์ •์ ๊ณผ ๋งˆ์ง€๋ง‰ ์ •์ ์ด ๊ฐ™์€ ๊ฒฝ๋กœ

์ง„์ž…์ง„์ž… ์ฐจ์ˆ˜์ฐจ์ˆ˜(indegree)์™€ ์ง„์ถœ์ง„์ถœ ์ฐจ์ˆ˜์ฐจ์ˆ˜(outdegree)๋กœ ๋‚˜๋ˆ„์–ด ๊ณ ๋ ค๋œ๋‹ค.

์ด ์Šฌ๋ผ์ด๋“œ์— ์žˆ๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ A์˜ ์ธ์ ‘์ •์ ์€ B์™€ D์ด๋‹ค. ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ž€ ๊ฒฝ๋กœ๋ฅผ

๊ตฌ์„ฑํ•˜๋Š” ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. ๊ฒฝ๋กœ ์ค‘ ์ฒซ ๋ฒˆ์งธ ์ •์ ๊ณผ ๋ ์ •์ ์ด ๊ฐ™์œผ๋ฉด ์ฃผ๊ธฐ(cycle)๋ผ ํ•œ

๋‹ค. ์œ„ ์Šฌ๋ผ์ด๋“œ์— ์žˆ๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ (A, B), (B, C), (C, D), (D, A)๋Š” ์ฃผ๊ธฐ์ด๋ฉฐ, ์ด

๊ฒฝ๋กœ์˜ ๊ธธ์ด๋Š” 4 ์ด๋‹ค. ๊ฒฝ๋กœ์˜ ์ฐจ์ˆ˜(degree)๋ž€ ์ •์ ์— ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ๋งํ•˜๋ฉฐ, ์œ ๋ฐฉํ–ฅ ๊ทธ

์„œ A์˜ ์ฐจ์ˆ˜๋Š” 2 ์ด๊ณ , ์œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ A์˜ ์ง„์ถœ์ฐจ์ˆ˜๋Š” 1 ์ด๊ณ , ์ง„์ž…์ฐจ์ˆ˜๋Š” 2 ์ด๋‹ค.

(complete directed graph)

์™„์ „์™„์ „ ๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„(complete graph): ์ตœ๋Œ€ ์ˆ˜์˜ ๊ฐ„์„ ์„ ๊ฐ€์ง„ ๊ทธ๋ž˜ํ”„

N ๊ฐœ์˜ ์ •์ ์ด ์žˆ์œผ๋ฉด ์œ ๋ฐฉํ–ฅ์€ N ( N -1) ๊ฐœ , ๋ฌด๋ฐฉํ–ฅ์€ N ( N -1)/2 ๊ฐœ์˜

(complete undirected graph)

์™„์ „ ๊ทธ๋ž˜ํ”„(complete graph)๋ž€ ์ตœ๋Œ€ ์ˆ˜์˜ ๊ฐ„์„ ์„ ๊ฐ€์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งํ•œ๋‹ค. ์ฆ‰, ๋ชจ๋“  ์ •์ ์€

์—์„œ๋Š” ์ด^ ๎€ ๎„๎€ ๎† ๎€ด ๎…๎”๎€ต๊ฐœ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค.

A

B D

C

M

E

L

B A A B C D

B D

A C

B D

A C

A

B

E

L

E

L

A

A L

M B E

  • ์—ญ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์œ ์ง€

Graph ADT

<> Graph

MatrixGraph ListGraph

WeightedMatrixGraph UnweightedMatrixGraph WeightedListGraph UnweightedListGraph

DirectedWeightedMatrixGraph

UndirectedUnweightedMatrixGraph

UndirectedUnweightedListGraph

๊ทธ๋ž˜ํ”„๋Š” ํฌ๊ฒŒ ์ธ์ ‘ ํ–‰๋ ฌ ๋˜๋Š” ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ Graph๋ผ๋Š”

MatrixGraph์™€ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ListGraph๋ฅผ ์ •์˜

ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์— ๊ทธ๋ž˜ํ”„๊ฐ€ ๊ฐ€์ค‘์น˜, ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์ธ์ง€์— ๋”ฐ๋ผ ListGraph๋Š” ๋‹ค์‹œ ๊ทธ๊ฒƒ์˜ ์ž

์‹ ์ถ”์ƒํด๋ž˜์Šค์ธ WeightedListGraph, UnweightedListGraph๋ฅผ ์ •์˜ํ•˜๊ณ , MatrixGraph๋Š”

WeightedMatrixGraph, UnweightedMatrixGraph๋ฅผ ์ •์˜ํ•œ๋‹ค.

Graph ADT

import java.util.Iterator;

interface Graph{ int DEF_CAPACITY = 10; int DFS = 1; // ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ ์ƒ์ˆ˜ int BFS = 2; // ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ ์ƒ์ˆ˜ boolean isEmpty(); boolean isFull(); void clear(); void insertVertex(String label) throws GraphOverflowException; void removeVertex(String label) throws GraphUnderflowException; void removeEdge(String from, String to) throws GraphUnderflowException; // ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ // void insertEdge(String from, String to); // ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ // void insertEdge(String from, String to, int weight); Iterator iterator(int type, String start); }

public abstract ListGraph implements Graph{ protected class Vertex{ public String label; public SortedLinkedList edges; } protected class GraphIterator implements Iterator{ โ€ฆ } protected Vertex[] graph; protected int size = 0; // ์ •์ ์˜ ๊ฐœ์ˆ˜ public ListGraph(){ setup(DEF_CAPACITY); } public ListGraph(int capacity){ if(capcity>0) setup(capacity); else setup(DEF_CAPACITY); } private void setup(int capacity){ โ€ฆ } public boolean isEmpty(){ return (size == 0); } public boolean isFull(){ return (size == graph.length); } public void clear() { โ€ฆ } protected int index(String label){ โ€ฆ } // ์ •์ ์˜ ์ƒ‰์ธ ์ฐพ๊ธฐ public void insertVertex(String label) throws GraphOverflowException { โ€ฆ } public abstract void removeVertex(String label) throws GraphUnderflowException; public abstract void removeEdge(String from, String to) throws GraphUnderflowException; public boolean search(int type, String from, String to) throws GraphUnderflowException { โ€ฆ } public Iterator iterator(int type, String start) throws GraphUnderflowException { โ€ฆ } }

๋Š” ๊ฐ€์„ ์˜ ๊ฐ€์ค‘์น˜, ๋ฐฉํ–ฅ์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด ์Šฌ๋ผ์ด๋“œ์— ์ •์˜๋œ ListGraph ํด๋ž˜์Šค์—

Insert Vertex

public void insertVertex(String label) throws GraphOverflowException{ if(label==null) throw new NullPointerException(โ€œโ€ฆโ€); if(isFull()) throw new GraphOverflowException(โ€œโ€ฆ"); Vertex v = new Vertex(); v.label = label; v.edges = new SortedLinkedList(); graph[size] = v; size++; } // ListGraph: insertVertex

protected int index(String label){ for(int i=0; i<size; i++){ if(label.equals(graph[i].label)) return i; } return -1; } // ListGraph: index ์ •์ ๋“ค์€ ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ๋น„์ •๋ ฌ ๋ฐฉ์‹์œผ๋กœ ์œ ์ง€

์—๋Š” ๊ทธ ์ •์ ์— ๊ด€ํ•œ ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์˜ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด index๋ผ๋Š” ๋‚ด๋ถ€

Insert Edge

public void insertEdge(String from, String to) throws GraphUnderflowException{ if(from==null||to==null) throw new NullPointerException(โ€œโ€ฆโ€); if(isEmpty()) throw new GraphUnderflowException(โ€œโ€ฆโ€); int v1 = index(from); int v2 = index(to); // v1 ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(v1==-1) throw new GraphUnderflowException(โ€œโ€ฆโ€); // v2 ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(v2==-1) throw new GraphUnderflowException(โ€œโ€ฆโ€); graph[v1].edges.insert(graph[v2].label); graph[v2].edges.insert(graph[v1].label); } // UndirectedUnweightedListGraph: insertEdge

๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ A์—์„œ B๋ฅผ ์ž‡๋Š” ๊ฐ„์„ ์„ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ์—๋Š” B์—์„œ A๋ฅผ ์ž‡๋Š” ๊ฐ„์„ ๋„ ํ•จ

Remove Vertex

public void removeVertex(String label) throws GraphUnderflowException{ if(label==null) throw new NullPointerException(โ€œโ€ฆโ€); if(isEmpty()) throw new GraphUnderflowException(โ€œโ€ฆโ€); int v = index(label); // ์ œ๊ฑฐํ•˜๊ณ ์ž ํ•˜๋Š” ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(v == -1) throw new GraphUnderflowException(โ€œโ€ฆโ€); graph[v] = graph[size-1]; size--; for(int i=0;i<size;i++){ if(!graph[i].edges.isEmpty()) graph[i].edges.delete(label); } } // UndirectedUnweightedListGraph: RemoveVertex

Remove Edge

public void removeEdge(String from, String to) throws GraphUnderflowException{ if(isEmpty()) throw new GraphUnderflowException(โ€œโ€ฆโ€); if(from==null||to==null) throw new NullPointerException(โ€œโ€ฆโ€); int v1 = index(from); int v2 = index(to); // v1 ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(v1==-1) throw new GraphUnderflowException(โ€œโ€ฆโ€); // v2 ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(v2==-1) throw new GraphUnderflowException(โ€œโ€ฆโ€); // v1์—์„œ v2๋ฅผ ์ž‡๋Š” ๊ฐ„์„ ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ if(!graph[v1].edges.delete(graph[v2].label)) throw new GraphUnderflowException(โ€œโ€ฆโ€); graph[v2].edges.delete(graph[v1].label); } // UndirectedUnweightedListGraph: RemoveEdge

๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ ์–‘์ชฝ ์ธ์ ‘๋ฆฌ์ŠคํŠธ์— ๋ชจ๋‘ ์ œ๊ฑฐํ•ด์•ผ ํ•จ.

A์—์„œ B๋ฅผ ์ž‡๋Š” ๊ฐ„์„ ์„ ์ œ๊ฑฐํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ์—๋Š” A ์ •์ ๊ณผ B ์ •์ ์ด ์กด์žฌํ•ด์•ผ ํ•  ๋ฟ๋งŒ ์•„

๋‹ˆ๋ผ A์—์„œ B๋ฅผ ์ž‡๋Š” ๊ฐ„์„ ์ด ์‹ค์ œ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ A์—์„œ B๋ฅผ ์ž‡

๋Š” ๊ฐ„์„ ์„ ์ œ๊ฑฐํ•  ๋•Œ B์—์„œ A๋ฅผ ์ž‡๋Š” ๊ฐ„์„  ์ •๋ณด ์—ญ์‹œ ํ•จ๊ป˜ ์ œ๊ฑฐํ•ด์•ผ ํ•œ๋‹ค.

ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ์— B์™€ C๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ enqueue๋œ๋‹ค. ํ•œ ์ •์ ์˜ ์ธ์ ‘ ์ •์ ๋“ค์„ ํ์—

enqueue๋ฅผ ํ•œ ๋‹ค์Œ์—๋Š” ๋ฐ”๋กœ ์ •์ ์„ ํ•˜๋‚˜ dequeueํ•œ๋‹ค. B๊ฐ€ ํ front์— ์žˆ์œผ๋ฏ€๋กœ ์ด ์ •์ 

์˜ ์ธ์ ‘ ์ •์ ์„ ํ์— enqueueํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ์— E๊ฐ€ enqueue๋œ๋‹ค. ํ˜„์žฌ ํ์— ์žˆ๋Š” ์ •์ ์€

ํ front๋ถ€ํ„ฐ ๋‚˜์—ดํ•˜๋ฉด C, E์ด๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” A, B, C, E, D ์ˆœ์œผ๋กœ

UnweightedListGraph์˜ ๋ฐ˜๋ณต์ž ํด๋ž˜์Šค

protected class GraphIterator implements Iterator{ LinkedQueue traverseQueue; public GraphIterator(int type, int start){ โ€ฆ } public boolean hasNext() { โ€ฆ } public Object next() { โ€ฆ } public void remove() { โ€ฆ } private void BreadthFirstSearch(int start); private void DepthFirstSearch(int start); }

public Iterator iterator(int type, String start) throws GraphUnderflowException { if(start==null) throw new NullPointerException(โ€œโ€ฆโ€); int v = index(start); if(v==-1) throw new GraphUnderflowException(โ€œโ€ฆโ€); if(type==Graph.BFS||type==Graph.DFS) return new GraphIterator(type, v); else throw new GraphUnderflowException(โ€œโ€ฆโ€); }

public GraphIterator(int type, int start){ traverseQueue = new LinkedQueue(); if(type==Graph.BFS) BreadthFirstSearch(start); else DepthFirstSearch(start); }

๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ traverseQueue๋Š” ๊ฐ ์ˆœํšŒ ๋ฐฉ๋ฒ•์„

๋”ฐ๋ผ์„œ BreadthFirstSearch์™€ DepthFirstSearch ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ๋Š” traverseQueue ์™ธ์— ๊ฐ๊ฐ

์—ฐ๊ฒฐ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„(connected graph): ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชจ๋“ 

DFS, BFS๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

DFS( G , i ): i ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋ฐฉ๋ฌธํ•œ ๋ชจ๋“  ์ •์ 

์ด ๋•Œ G = DFS( G , i )์ด๋ฉด ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„

์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„(connected graph)๋ž€ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชจ๋“  ์Œ์˜ ์ •์ ๋“ค ์‚ฌ์ด์— ๊ฒฝ

Spanning Tree

๋ถ€๋ถ„๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„๊ทธ๋ž˜ํ”„(subgraph): ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•˜๋ฉด G' ( V' , E' ) ๋Š” G ( V , E ) ์˜

V' โІ V , E' โІ E

์‹ ์žฅ์‹ ์žฅ ํŠธ๋ฆฌํŠธ๋ฆฌ(spanning tree):^ G ์˜ ๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„ ์ค‘^ G ์˜ ๋ชจ๋“  ์ •์ ๋“ค์„

A

C

E

B

D

A

C

E

B

D

A

C

E

B

D

A

C B

D

๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„(subgraph)๋ž€ ์› ๊ทธ๋ž˜ํ”„์˜ ์ •์  ์ง‘ํ•ฉ ์ค‘์— ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์ทจํ•˜๊ณ , ์› ๊ทธ๋ž˜ํ”„์—์„œ

๋ถ„ ๊ทธ๋ž˜ํ”„๋ผ ํ•œ๋‹ค. ์‹ ์žฅ ํŠธ๋ฆฌ(spanning tree)๋ž€ ์› ๊ทธ๋ž˜ํ”„์˜ ๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„ ์ค‘ ์› ๊ทธ๋ž˜ํ”„์˜ ๋ชจ

์ตœ์†Œ์ตœ์†Œ ์‹ ์žฅ์‹ ์žฅ ํŠธ๋ฆฌํŠธ๋ฆฌ(minimum spanning tree): ์ตœ์†Œ์˜ ๊ฐ„์„  ์ˆ˜๋กœ

๊ทธ๋ž˜ํ”„์˜ ์ •์  ์ˆ˜๊ฐ€ N ์ด๋ฉด ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์˜ ๊ฐ„์„  ์ˆ˜๋Š” N -1 ์ด๋‹ค.

DFS, BFS๋กœ ๋งŒ๋“ค์–ด์ง„ ์‹ ์žฅ ํŠธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

๊นŠ์ด์šฐ์„ ๊นŠ์ด์šฐ์„  ์‹ ์žฅํŠธ๋ฆฌ์‹ ์žฅํŠธ๋ฆฌ(depth first spanning tree)

๋„ˆ๋น„์šฐ์„ ๋„ˆ๋น„์šฐ์„  ์‹ ์žฅํŠธ๋ฆฌ์‹ ์žฅํŠธ๋ฆฌ(breadth first spanning tree)

A

C

E

B

D

A

C

E

B

D

A

C

E

B

D

DFS(A)์˜ ์‹ ์žฅ ํŠธ๋ฆฌ BFS(A)์˜ ์‹ ์žฅ ํŠธ๋ฆฌ

์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(minimum spanning tree)๋ž€ ์ตœ์†Œ์˜ ๊ฐ„์„  ์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค.

public abstract MatrixGraph implements Graph{ public static final int NULLEDGE = -1; protected class GraphIterator implements Iterator{ โ€ฆ } protected String[] graph; protected int size = 0; // ์ •์ ์˜ ๊ฐœ์ˆ˜ public MatrixGraph(){ setup(DEF_CAPACITY); } public MatrixGraph(int capacity){ if(capcity>0) setup(capacity); else setup(DEF_CAPACITY); } private void setup(int capacity){ โ€ฆ } public boolean isEmpty(){ return (size == 0); } public boolean isFull(){ return (size == graph.length); } public void clear() { โ€ฆ } protected int index(String label){ โ€ฆ } // ์ •์ ์˜ ์ƒ‰์ธ ์ฐพ๊ธฐ public void insertVertex(String label) throws GraphOverflowException { โ€ฆ } public abstract void removeVertex(String label) throws GraphUnderflowException; public abstract void removeEdge(String from, String to) throws GraphUnderflowException; public boolean search(int type, String from, String to) throws GraphUnderflowException { โ€ฆ } public Iterator iterator(int type, String start) throws GraphUnderflowException { โ€ฆ } }

private void setup(int capacity){ graph = new String[capacity]; adjMatrix = new int[capacity][capacity]; for(int i=0; i<capacity; i++) for(int j=0; j<capacity; j++) if(i!=j) adjMatrix[i][j] = NULLEDGE; else adjMatrix[i][j] = 0; }

์ด ์ ˆ์—์„œ๋Š” ์ธ์ ‘ ํ–‰๋ ฌ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ์œ ๋ฐฉํ–ฅ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค. ๋ฌด๋ฐฉํ–ฅ ๋น„๊ฐ€

์ค‘์น˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •์ ๋“ค์„ ์ •๋ ฌํ•˜์—ฌ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ

์„  Graph ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค MatrixGraph๋ฅผ ์ •์˜ํ•œ๋‹ค. MatrixGraph๋„ ์•ž์„œ

์‚ดํŽด๋ณธ ListGraph์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •์ ์˜ ์ถ”๊ฐ€, ๊ฒ€์ƒ‰, ์ˆœํšŒ๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜ ์—ฌ๋ถ€, ๋ฐฉํ–ฅ์„ฑ ์—ฌ

๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

WeightedMatrixGraph, UnweightedMatrixGraph

public abstract class WeightedMatrixGraph extends MatrixGraph{ public WeightedMatrixGraph(){ super(); } public WeightedMatrixGraph(int capacity){ super(capacity); } public abstract void insertEdge(String from, String to, int weight); }

public abstract class UnweightedMatrixGraph extends MatrixGraph{ public WeightedMatrixGraph(){ super(); } public WeightedMatrixGraph(int capacity){ super(capacity); } public abstract void insertEdge(String from, String to); }

DirectedWeightedMatrixGraph

public class DirectedWeightedMatrixGraph implements WeightedListGraph{ public DirectedWeightedMatrixGraph(){ super(); } public DirectedWeightedMatrixGraph(int capacity){ super(capacity); } public void removeVertex(String label) throws GraphUnderflowException { โ€ฆ } public void removeEdge(String from, String to) throws GraphUnderflowException { โ€ฆ } public void insertEdge(String from, String to, int weight) throws GraphUnderflowException { โ€ฆ } }

Insert Vertex

public void insertVertex(String label) throws GraphOverflowException{ if(label==null) throw new NullPointerException(โ€œโ€ฆโ€); if(isFull()) throw new GraphOverflowException(โ€œโ€ฆโ€); graph[size] = label; size++; } // MatrixGraph: insertVertex

๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ™์€ ์œ ํ•œ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ •์  s ์—์„œ

์ •์  t ๊นŒ์ง€ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋ผ.

BFS ๊ฒ€์ƒ‰์„ ์ด์šฉํ•˜๋ฉด ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

MooreMoore^ ์•Œ๊ณ ๋ฆฌ์ฆ˜์•Œ๊ณ ๋ฆฌ์ฆ˜:^ ์ •์ ์˜ ์ˆ˜๋Š”^ n ์ด๋ผ ํ•˜์ž.

๋‹จ๊ณ„๋‹จ๊ณ„ 1.1. โˆ€ฮป [ v ] = -

ฮป [ s ] = 0;

๋‹จ๊ณ„๋‹จ๊ณ„ 2.2. l = 0;

๋‹จ๊ณ„๋‹จ๊ณ„ 3.3. ฮป [ v ] = l ์ธ ์ •์  v ์™€ ์ธ์ ‘ํ•œ ๋ชจ๋“  ์ •์  u ์ค‘ ฮป [ u ] ๊ฐ€ -1 ์ด๋ฉด

ฮป [ u ] = l +1, ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.

๋‹จ๊ณ„๋‹จ๊ณ„ 4.4. ฮป [ t ] != -1 ์ด๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.

๋‹จ๊ณ„๋‹จ๊ณ„ 5.5. l = l +1; ๋‹จ๊ณ„ 3 ๋ถ€ํ„ฐ ๋ฐ˜๋ณต

ํŽด๋ณธ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Moore๊ฐ€ ์ œ์•ˆํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ์„œ ๋„ˆ๋น„์šฐ์„  ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค.

A

B

D

C

E

A์—์„œ D๊นŒ์ง€

A B C D E

A B C D E

A B C D E

A B C D E

C์—์„œ A๊นŒ์ง€

A B C D E

A B C D E

A B C D E

A B C D E

Moore์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋จผ์ € ์ •์  ๊ฐœ์ˆ˜๋งŒํผ์˜ ์ •์ˆ˜ ๋ฐฐ์—ด

์ด A์ด๋ฏ€๋กœ A์˜ ํ•ญ ๊ฐ’์€ 0 ์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์—ˆ๊ณ , A์˜ ์ธ์ ‘ ์ •์ ์ธ B์™€ E๋Š” 1 ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋œ

์—ด ๊ฐ’์„ 2 ๋กœ ๋ฐ”๊พผ๋‹ค. ๋”ฐ๋ผ์„œ B์˜ ์ธ์ ‘ ์ •์ ์ธ C์™€ D๋Š” 2 ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜์—ฌ

๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์–‘์ˆ˜์ธ ์œ ํ•œ ์œ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ •์  s ์—์„œ

์ •์  t ๊นŒ์ง€ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋ผ.

DijkstraDijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜์•Œ๊ณ ๋ฆฌ์ฆ˜

์ •์ ์˜ ์ˆ˜๋Š” n ์ด๋ผ ํ•˜๊ณ  , ์ •์ ๋“ค์˜ ์ง‘ํ•ฉ์„ V ๋ผ ํ•˜์ž.

๋‹จ๊ณ„๋‹จ๊ณ„ 1.1. โˆ€ฮป [ v ] = โˆž ;

ฮป [s] = 0;

๋‹จ๊ณ„๋‹จ๊ณ„ 2.2. T ร… V

๋‹จ๊ณ„๋‹จ๊ณ„ 3.3. T ์ค‘์— ฮป [ u ] ๊ฐ€ ์ตœ์†Œ์ธ ์ •์ ์„ u ๋ผ ํ•˜์ž.

๋‹จ๊ณ„๋‹จ๊ณ„ 4.4. u ๊ฐ€ t ์ด๋ฉด ์ข…๋ฃŒ

๋‹จ๊ณ„๋‹จ๊ณ„ 5.5. ์ •์  u ์—์„œ ์ง„์ถœํ•˜๋Š” ๋ชจ๋“  ๊ฐ„์„  ์— ๋Œ€ํ•ด v โˆˆ T ์ด๊ณ 

ฮป [ v ] > ฮป [ u ]+ l ( e ) ์ด๋ฉด ฮป [ v ] = ฮป [ u ]+ l ( e )

๋‹จ๊ณ„๋‹จ๊ณ„ 6.6. T ร… T - { u }, ๋‹จ๊ณ„ 3 ๋ถ€ํ„ฐ ๋ฐ˜๋ณต

e

u โŽฏโŽฏโ†’ v

๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ์„œ Dijkstra๊ฐ€ ์ œ์•ˆํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

A

C

3 B

D

E

A B C D E

A B C D E

A B C D E

A B C D E

A B C D E

A B C D E

A B C D E

A B C D E

Bellman๊ณผ Ford๊ฐ€ ์ œ์•ˆํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ž˜ํ”„์— ๎ƒฒ๊ฐœ์˜ ์ •์ ์ด ์žˆ์„ ๋•Œ ์ถœ๋ฐœ ์ •์ ๋ถ€ํ„ฐ ๊ฒฝ๋กœ

์„œ ์šฐ์„  A๋ฅผ ์ถœ๋ฐœ ์ •์ ์œผ๋กœ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๊ฐ€ 1 ์ธ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์— ๊ฒฝ๋กœ์˜ ๊ธธ์ด

BellmanBellman๊ณผ๊ณผ^ FordFord^ ์•Œ๊ณ ๋ฆฌ์ฆ˜์•Œ๊ณ ๋ฆฌ์ฆ˜

Dist k [ u ] : ์‹œ์ž‘ ์ •์  v ์—์„œ u ๊นŒ์ง€ ์ตœ๋Œ€ k ๊ฐœ์˜ ์•„ํฌ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š”

Dist [ ] k^ u โ† min(Dist k^ โˆ’^1^^ [ u ],min(Dist k โˆ’^1 [ ] i + weight[ , i u ]))

A

C

3 B

D

E

E

D

C

B

A 0 5 3 -2 99

A B C D E

A B C D E

Dist^1

Dist^2 ๊ฐ ์ •์ ์˜ ์ง„์ž…์ฐจ์ˆ˜ ๊ณ ๋ ค(์—ด)

Dist^2 [B] = min(5, (3+(-2))= Dist^2 [D] = min(-2, (5+(-1))=- Dist^2 [E] = min(99, min(3+(-1), -2+3))=