-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCMap.cpp
More file actions
230 lines (200 loc) · 4.3 KB
/
Copy pathCMap.cpp
File metadata and controls
230 lines (200 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include"CMap.h"
#include<iostream>
#include<vector>
using namespace std;
CMap::CMap(int capacity)
{
m_iCapacity = capacity;
m_iNodeCount = 0;
m_pNodeArray = new Node[m_iCapacity];
m_pMatrix = new int[m_iCapacity*m_iCapacity];
memset(m_pMatrix, 0, m_iCapacity*m_iCapacity * sizeof(int));
m_pEdge = new Edge[m_iCapacity - 1];
}
CMap::~CMap()
{
delete[] m_pNodeArray;
delete[] m_pMatrix;
}
bool
CMap::addNode(Node *pNode)
{
m_pNodeArray[m_iNodeCount].m_cData = pNode->m_cData;
m_iNodeCount++;
return true;
}
void
CMap::resetNode()
{
for (int i = 0; i < m_iNodeCount; i++)
{
m_pNodeArray[i].m_bIsVisited = false;
}
}
bool
CMap::setValueToMatrixForDirectedGraph(int row, int col, int val)
{
if (row >= 0 && row < m_iCapacity&&col >= 0 && col < m_iCapacity)
{
m_pMatrix[row * m_iCapacity + col] = val;
return true;
}
return false;
}
bool
CMap::setValueToMatrixForUndirectedGraph(int row, int col, int val)
{
if (row >= 0 && row < m_iCapacity&&col >= 0 && col < m_iCapacity)
{
m_pMatrix[row*m_iCapacity + col] = val;
m_pMatrix[col*m_iCapacity + row] = val;
return true;
}
return false;
}
void
CMap::printMatrix()
{
for (int i = 0; i < m_iCapacity; i++)
{
for (int k = 0; k < m_iCapacity; k++)
{
cout << m_pMatrix[i*m_iCapacity + k] << " ";
}
cout << endl;
}
}
bool
CMap::getValueFromMatrix(int row, int col, int &val)
{
if (row >= 0 && row < m_iCapacity&&col >= 0 && col < m_iCapacity)
{
val = m_pMatrix[row*m_iCapacity + col];
return true;
}
return false;
}
void
CMap::depthFirstTraverse(int nodeIndex)
{
int value = 0;
cout << m_pNodeArray[nodeIndex].m_cData << " ";
m_pNodeArray[nodeIndex].m_bIsVisited = true;
for (int i = 0; i < m_iCapacity; i++)
{
getValueFromMatrix(nodeIndex, i, value);
if (value == 1)
{
if (m_pNodeArray[i].m_bIsVisited)
continue;
else
{
depthFirstTraverse(i);
}
}
else
continue;
}
}
void
CMap::breadthFirstTraverse(int nodeIndex)
{
cout << m_pNodeArray[nodeIndex].m_cData << " ";
m_pNodeArray[nodeIndex].m_bIsVisited = true;
vector<int> curVec;
curVec.push_back(nodeIndex);
breadthFirstTraverImpl(curVec);
}
void
CMap::breadthFirstTraverImpl(vector<int>preVec)
{
int value = 0;
vector<int> curVec;
for (int i = 0; i < (int)preVec.size(); i++)
{
for (int k = 0; k < m_iCapacity; k++)
{
getValueFromMatrix(preVec[i], k, value);
if (value != 0)
{
if (m_pNodeArray[k].m_bIsVisited)
continue;
else
{
cout << m_pNodeArray[k].m_cData << " ";
m_pNodeArray[k].m_bIsVisited = true;
curVec.push_back(k);
}
}
}
}
if (curVec.size() == 0)
return;
else
breadthFirstTraverImpl(curVec);
}
void
CMap::primTree(int nodeIndex)
{
int value = 0;
int edgeCount = 0;
vector<int> nodeVec;
vector<Edge> edgeVec;
cout << m_pNodeArray[nodeIndex].m_cData << endl;
nodeVec.push_back(nodeIndex);
m_pNodeArray[nodeIndex].m_bIsVisited = true;
while (edgeCount < m_iCapacity - 1)
{
int temp = nodeVec.back();
for (int i = 0; i < m_iCapacity; i++)
{
getValueFromMatrix(temp, i, value);
if (value != 0)
{
if (m_pNodeArray[i].m_bIsVisited)
continue;
else
{
Edge edge(temp, i, value);
edgeVec.push_back(edge);
}
}
}
int edgeIndex = getMinEdge(edgeVec);
edgeVec[edgeIndex].m_bSelected = true;
cout << edgeVec[edgeIndex].m_iNodeIndexA << "----" << edgeVec[edgeIndex].m_iNodeIndexB;
cout << " " << edgeVec[edgeIndex].m_iWeightValue << endl;
m_pEdge[edgeCount] = edgeVec[edgeIndex];
edgeCount++;
int nextNodeIndex = edgeVec[edgeIndex].m_iNodeIndexB;
nodeVec.push_back(nextNodeIndex);
m_pNodeArray[nextNodeIndex].m_bIsVisited = true;
cout << m_pNodeArray[nextNodeIndex].m_cData << endl;
}
}
int
CMap::getMinEdge(vector<Edge> edgeVec)
{
int temp = 0;
int k = 0;
for (; k < edgeVec.size(); k++)
{
if (!edgeVec[k].m_bSelected)
break;
}
if (edgeVec[k].m_bSelected)
{
return -1;
}
temp = edgeVec[k].m_iWeightValue;
int i = k;
for(; i<edgeVec.size();i++)
{
if (temp > edgeVec[i].m_iWeightValue&&!edgeVec[i].m_bSelected)
{
temp = edgeVec[i].m_iWeightValue;
k = i;
}
}
return k;
}