Skip to content

Commit

Permalink
[Graph] Add Adj-Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 29, 2020
1 parent 2918c95 commit 914e955
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Graph/AdjacencyMatrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef __DEUN_GRAPH_ADJ_MATRIX_H__
#define __DEUN_GRAPH_ADJ_MATRIX_H__

#include <iostream>
#include <new>

namespace Deun {
enum class AdjacencyMatrixError {
MEMORY_ALLOCATION_FAILED = 1000,
TOO_MANY_VERTICES,
};

// 배열 기반 인접 행렬 그래프
class AdjacencyMatrix {
private:
int vSize; // 정점의 최대 개수 (메모리 할당량)
int vCount; // 정점의 개수
char* matrix; // 인접 행렬

public:
/**
* 인접 행렬 생성자
*
* @param {int} vSize: 정점의 최대 개수
* @throw {AdjacencyMatrixError} 메모리 할당 오류
*/
AdjacencyMatrix(int vSize = 1000);

~AdjacencyMatrix();

/**
* 정점을 삽입하고 삽입된 정점의 인덱스를 반환합니다.
*
* @return {int} 삽입된 정점의 인덱스(0-based)
* @throw {AdjacencyMatrixError} 정점 개수 초과 오류
*/
int insertVertex();

/**
* 간선을 삽입합니다.
* 간선은 from과 to를 연결하며 방향성이 있습니다.
* undirected가 true인 경우에는 to와 from을 잇는 간선도 삽입합니다.
*
* @param {int} from: 시작 정점
* @param {int} to: 끝 정점
* @param {bool} undirected: 무방향 그래프 여부 (default: false)
* @return {bool} 성공 여부
*/
bool insertEdge(int from, int to, bool undirected = false);

/**
* 정점의 존재 여부를 반환합니다.
*
* @param {int} v: 정점 번호
* @return {bool} 존재 여부
*/
inline bool hasVertex(int v);

/**
* 간선의 존재 여부를 반환합니다.
*
* @param {int} from: 시작 정점
* @param {int} to: 끝 정점
* @return {bool} 존재 여부
*/
inline bool hasEdge(int from, int to);

/**
* 인접 행렬을 초기화합니다.
*/
void clear();

/**
* 인접 행렬을 출력합니다.
*/
void print();
};
}

#endif

0 comments on commit 914e955

Please sign in to comment.