Skip to content

Commit

Permalink
[Graph] Fix Adj-Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 30, 2020
1 parent 4b2f42d commit e005ad0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
11 changes: 6 additions & 5 deletions Graph/AdjacencyMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace Deun {
delete[] matrix;
}

inline char AdjacencyMatrix::getRaw(int from, int to) {
return matrix[from * vSize + to];
inline bool AdjacencyMatrix::getRaw(int from, int to) {
return (bool)matrix[from * vSize + to];
}

inline void AdjacencyMatrix::setRaw(int from, int to, char value) {
inline void AdjacencyMatrix::setRaw(int from, int to, bool value) {
matrix[from * vSize + to] = value;
}

Expand All @@ -46,9 +46,9 @@ namespace Deun {
return false;
}

setRaw(from, to, 1);
setRaw(from, to, true);
if (undirected) {
setRaw(to, from, 1);
setRaw(to, from, true);
}
return true;
}
Expand All @@ -68,6 +68,7 @@ namespace Deun {
}

void AdjacencyMatrix::clear() {
vCount = 0;
for (unsigned int i = 0; i < (unsigned int)vSize * (unsigned int)vSize; i++) {
matrix[i] = 0;
}
Expand Down
11 changes: 7 additions & 4 deletions Graph/AdjacencyMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ namespace Deun {
};

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

inline char getRaw(int from, int to);
inline void setRaw(int from, int to, char value);
inline bool getRaw(int from, int to);
inline void setRaw(int from, int to, bool value);

public:
/**
Expand All @@ -31,6 +31,9 @@ namespace Deun {
*/
AdjacencyMatrix(int vSize = 1000);

/**
* 인접 행렬 소멸자
*/
~AdjacencyMatrix();

/**
Expand All @@ -42,7 +45,7 @@ namespace Deun {
int insertVertex();

/**
* 간선을 삽입합니다.
* 간선을 삽입하고 성공 여부를 반환합니다.
* 간선은 from과 to를 연결하며 방향성이 있습니다.
* undirected가 true인 경우에는 to와 from을 잇는 간선도 삽입합니다.
*
Expand Down

0 comments on commit e005ad0

Please sign in to comment.