Download Notes on Graphs (INA240) and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!
ยฉcopyright 2005 ์๋ฃ๊ตฌ์กฐ ๋ฐ ์ค์ต( INA240) NOTE 11
๊ทธ๋ํ
๊ทธ๋ํ๊ทธ๋ํ (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 , โฆ, v n , b ๊ฐ ์กด์ฌํ๊ธฐ ์ํด์๋
๊ฐ์ ( a , v 1 ), ( v 1 , v 2 ), โฆ, ( v n -1 , v n ), ( v n , b ) ๊ฐ ์กด์ฌํด์ผ ํ๋ค.
๊ทธ๋ํ โ ๊ณ์
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)๋ก ๋๋์ด ๊ณ ๋ ค๋๋ค.
๋น๊ฐ์ค์น ๊ทธ๋ํ์ ํํ
๋ฐฉ๋ฒ๋ฐฉ๋ฒ^ 1.1.^ ์ธ์ ํ๋ ฌ
A
B D
C
D 1 0 1 0
C 0 1 0 1
B 1 0 1 0
A 0 1 0 1
A B C D
M
E
L
B
A 1
0
0
0
0
B
B 0 0 1 0
M 0 1 0 0
L 1 0 1 0
E 1 0 0 0
A 0 1 0 0
A E L M
๋จ์ ๋จ์ .. n^2 ๊ณต๊ฐ์ด ํ์
- ๋ฌด๋ฐฉํฅ ํ๋ ฌ์ ์ด๊ฒ์ ๋ฐ๋ง์ผ๋ก ํํ๊ฐ๋ฅ
๋น๊ฐ์ค์น ๊ทธ๋ํ์ ํํ โ ๊ณ์
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 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);
UndirectedUnweightedListGraph
public abstract UndirectedUnweightedListGraph implements UnweightedGraph{
public UndirectedUnweightedListGraph(){
super(DEF_CAPACITY);
public UndirectedUnweightedListGraph(int capacity){
super(DEF_CAPACITY);
public void removeVertex(String label)
throws GraphUnderflowException { โฆ }
public void removeEdge(String from, String to)
throws GraphUnderflowException { โฆ }
public void insertEdge(String from, String to)
throws GraphUnderflowException { โฆ }
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 ์ ์ ๋ค์ ๋ฐฐ์ด์ ์ด์ฉํ ๋น์ ๋ ฌ ๋ฐฉ์์ผ๋ก ์ ์ง
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
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
Traverse
๋๋น์ฐ์ ํ์๋๋น์ฐ์ ํ์(BFS, Breadth First Search)
๋จ๊ณ ๋จ๊ณ 1.1. ์ ์ i ๋ฅผ ๋ฐฉ๋ฌธํ๋ค.
๋จ๊ณ ๋จ๊ณ 2.2. ์ ์ i ์ ์ธ์ ํ ์ ์ ์ค์ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ด
๋จ๊ณ ๋จ๊ณ 3.3. ํ์์ ์ ์ ์ ์ ๊ฑฐํ๊ณ , ์ด ์ ์ ์ i ๋ก ํ์ฌ ๋จ๊ณ 1 ๋ถํฐ
ํ๋ค. A
C
E
B
D
์ํ ์์: A,B,C,E,D
A
C
F
B
D
E
G
์ํ ์์: A,B,C,D,F,E,G
ListGraph์ ๋ฐ๋ณต์ ํด๋์ค
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); }
์ฐ๊ฒฐ ์ฌ๋ถ
์ฐ๊ฒฐ์ฐ๊ฒฐ ๊ทธ๋ํ๊ทธ๋ํ(connected graph):^ ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์์ ์๋ก ๋ค๋ฅธ ๋ชจ๋
DFS, BFS๋ฅผ ์ด์ฉํ์ฌ ์ฐ๊ฒฐ ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์๋ค.
DFS( G , i ): i ๋
ธ๋๋ก๋ถํฐ ์์ํ์ฌ ๋ฐฉ๋ฌธํ ๋ชจ๋ ์ ์
์ด ๋ G = DFS( G , i )์ด๋ฉด ์ฐ๊ฒฐ ๊ทธ๋ํ
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
๊ฐ์ค์น ๊ทธ๋ํ์ ํํ โ ๊ณ์
๋ฐฉ๋ฒ๋ฐฉ๋ฒ^ 2.2.^ ์ธ์ ํ๋ ฌ
public abstract MatrixGraph implements Graph{ public static final int NULLEDGE = -1; protected class GraphIterator implements Iterator{ โฆ } protected String[] graph; protected int[][] adjMatrix; 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; }
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 { โฆ }
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(โโฆโ);
size--;
graph[v] = graph[size];
for(int i=0;i<size; i++){
adjMatrix[v][i] = adjMatrix[size][i];
adjMatrix[size][i] = NULLEDGE;
adjMatrix[i][v] = adjMatrix[i][size];
adjMatrix[i][size] = NULLEDGE;
adjMatrix[v][v] = NULLEDGE;
}// DirectedWeightedMatrixGraph: insertEdge
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ 1.
๋ชจ๋ ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ๊ฐ์ ์ ํ ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์์ ์ ์ 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 ๋ถํฐ ๋ฐ๋ณต
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ 1. โ ๊ณ์
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
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ 3.
์ ํ ์ ๋ฐฉํฅ ๊ทธ๋ํ์์ ์ ์ s ์์
์ ์ t ๊น์ง ๊ธธ์ด๊ฐ ๊ฐ์ฅ ์งง์ ๊ฒฝ๋ก๋ฅผ
n ์ด ์ ์ ์ ์์ผ ๋, ์์ ์ฃผ๊ธฐ๊ฐ
๊ธธ์ด๋ ์ต๋ n -1 ์ด๋ค.
๊ธธ์ด๊ฐ 1 ์ธ ๊ฒ๋ถํฐ n -1 ์ธ ๊ฒ๊น์ง
A
C
B
A
C
B
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ 3. โ ๊ณ์
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
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ 3. โ ๊ณ์
BellmanBellman๊ณผ๊ณผ^ FordFord^ ์๊ณ ๋ฆฌ์ฆ์๊ณ ๋ฆฌ์ฆ
Dist k [ u ] : ์์ ์ ์ v ์์ u ๊น์ง ์ต๋ k ๊ฐ์ ์ํฌ๋ฅผ ํฌํจํ ์ ์๋
1 1
Dist [ ] min(Dist [ ],min(Dist [ ] weight[ , ]))
k k k
u u i 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))=