Docsity

Notes on Graphs (INA240), Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
์ž๋ฃŒ๊ตฌ์กฐ ๋ฐ ์‹ค์Šต(INA240) NOTE 11
ยฉcopyright 2005
๊ทธ๋ž˜ํ”„
ํ•œ๊ตญ๊ธฐ์ˆ ๊ต์œก๋Œ€ํ•™๊ต
ํ•œ๊ตญ๊ธฐ์ˆ ๊ต์œก๋Œ€ํ•™๊ต ์ธํ„ฐ๋„ท๋ฏธ๋””์–ด๊ณตํ•™๋ถ€
์ธํ„ฐ๋„ท๋ฏธ๋””์–ด๊ณตํ•™๋ถ€ ๊น€์ƒ์ง„
๊น€์ƒ์ง„
2
2/39
/39
๊ต์œก๋ชฉํ‘œ
๊ฐœ์š”
๊ทธ๋ž˜ํ”„๋Š” ํŠธ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ณด๋‹ค ํฐ ๊ฐœ๋…์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ์„œ,
๋…ธ๋“œ์™€๋˜๋‹ค๋ฅธ๋…ธ๋“œ๊ฐ„์—์—ฌ๋Ÿฌ๊ฒฝ๋กœ๊ฐ€์กด์žฌํ• ์ˆ˜์žˆ๊ณ , ๊ฐ„์„ ์˜
๋ฐฉํ–ฅ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.
๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„
๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰: ๋„ˆ๋น„ ์šฐ์„ , ๊นŠ์ด ์šฐ์„ 
์‹ ์žฅ ํŠธ๋ฆฌ
๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„
์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

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

  • ๊ธธ์ด: 3

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