전처리문 :
[code cpp:nocontrols]
#include <list>
using namespace std;
[/code]
#include <list>
using namespace std;
[/code]
자료구조 :
[code cpp:nocontrols]
typedef struct tagEDGE{
int iNodeA;
int iNodeB;
int iWeight;
public:
bool operator < (const struct tagEDGE &vector) {
if (iWeight < vector.iWeight) return 1;
else return 0;
}
void OutputScreen() {
printf("( %d, %d : %d )\n", iNodeA, iNodeB, iWeight );
}
}EDGE;
[/code]
typedef struct tagEDGE{
int iNodeA;
int iNodeB;
int iWeight;
public:
bool operator < (const struct tagEDGE &vector) {
if (iWeight < vector.iWeight) return 1;
else return 0;
}
void OutputScreen() {
printf("( %d, %d : %d )\n", iNodeA, iNodeB, iWeight );
}
}EDGE;
[/code]
자료 변환 함수 : ConvertData(2차원 배열그래프, STL 리스트)
[code cpp:nocontrols]
void ConvertData(const int iTree[SIZE][SIZE], std::list<EDGE> &listData)
{
EDGE edge;
for (int i=1; i<SIZE; i++) {
for (int j=0; j<i; j++) {
if (iTree[i][j]!=INFINITE) {
edge.iNodeA = i;
edge.iNodeB = j;
edge.iWeight = iTree[i][j];
listData.push_back(edge);
}
}
}
listData.sort();
}
[/code]
void ConvertData(const int iTree[SIZE][SIZE], std::list<EDGE> &listData)
{
EDGE edge;
for (int i=1; i<SIZE; i++) {
for (int j=0; j<i; j++) {
if (iTree[i][j]!=INFINITE) {
edge.iNodeA = i;
edge.iNodeB = j;
edge.iWeight = iTree[i][j];
listData.push_back(edge);
}
}
}
listData.sort();
}
[/code]
사용법 : Prim(변환된 그래프 리스트, 반환할 MST 리스트)
[code cpp:nocontrols]
void Prim(std::list<EDGE> &listData, std::list<EDGE> &listOutput)
{
std::list<EDGE>::iterator it;
EDGE edgeMin;
int iMinWeight;
bool *pBoundNode = new bool[SIZE];
memset(pBoundNode, 0, sizeof(bool)*SIZE);
it = listData.begin();
listOutput.push_back(*it);
pBoundNode[it->iNodeA] = TRUE;
pBoundNode[it->iNodeB] = TRUE;
while(listOutput.size() < SIZE-1) {
iMinWeight = INFINITE;
for (int i=0; i<SIZE; i++) {
if (pBoundNode[i] == TRUE) {
it = listData.begin();
while(it != listData.end()) {
if (it->iNodeA == i && pBoundNode[it->iNodeB] == FALSE ||
it->iNodeB == i && pBoundNode[it->iNodeA] == FALSE ) {
if (iMinWeight > it->iWeight) {
iMinWeight = it->iWeight;
edgeMin = *it;
}
}
++it;
}
}
}
if (iMinWeight != INFINITE) {
pBoundNode[edgeMin.iNodeA] = TRUE;
pBoundNode[edgeMin.iNodeB] = TRUE;
listOutput.push_back(edgeMin);
}
}
delete[] pBoundNode;
}
[/code]
void Prim(std::list<EDGE> &listData, std::list<EDGE> &listOutput)
{
std::list<EDGE>::iterator it;
EDGE edgeMin;
int iMinWeight;
bool *pBoundNode = new bool[SIZE];
memset(pBoundNode, 0, sizeof(bool)*SIZE);
it = listData.begin();
listOutput.push_back(*it);
pBoundNode[it->iNodeA] = TRUE;
pBoundNode[it->iNodeB] = TRUE;
while(listOutput.size() < SIZE-1) {
iMinWeight = INFINITE;
for (int i=0; i<SIZE; i++) {
if (pBoundNode[i] == TRUE) {
it = listData.begin();
while(it != listData.end()) {
if (it->iNodeA == i && pBoundNode[it->iNodeB] == FALSE ||
it->iNodeB == i && pBoundNode[it->iNodeA] == FALSE ) {
if (iMinWeight > it->iWeight) {
iMinWeight = it->iWeight;
edgeMin = *it;
}
}
++it;
}
}
}
if (iMinWeight != INFINITE) {
pBoundNode[edgeMin.iNodeA] = TRUE;
pBoundNode[edgeMin.iNodeB] = TRUE;
listOutput.push_back(edgeMin);
}
}
delete[] pBoundNode;
}
[/code]
'Development & Tips > Algorithms' 카테고리의 다른 글
| 3-Standard Quick Sort (0) | 2007/05/22 |
|---|---|
| Prim Algorithm (0) | 2007/05/18 |
| Kruscal Algorithm (0) | 2007/05/18 |
| Quick Sort (0) | 2007/05/18 |
| Merge Sort (0) | 2007/05/18 |