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
์ฃผ๊ธฐ์ฃผ๊ธฐ(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))=