











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
These are algorithm courses materials.
Typology: Lecture notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!
Path(M,A): M, B, L, A
(complete directed graph)
(complete undirected graph)
<
MatrixGraph ListGraph
WeightedMatrixGraph UnweightedMatrixGraph WeightedListGraph UnweightedListGraph
DirectedWeightedMatrixGraph
UndirectedUnweightedMatrixGraph
UndirectedUnweightedListGraph
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 { … } }
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 정점들은 배열을 이용한 비정렬 방식으로 유지
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
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
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
무방향 그래프이므로 양쪽 인접리스트에 모두 제거해야 함.
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); }
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); }
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 { … } }
public void insertVertex(String label) throws GraphOverflowException{ if(label==null) throw new NullPointerException(“…”); if(isFull()) throw new GraphOverflowException(“…”); graph[size] = label; size++; } // MatrixGraph: insertVertex
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))=