-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpanningTreePrim.cpp
76 lines (63 loc) · 1.86 KB
/
SpanningTreePrim.cpp
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
#include "pch.h"
#include "Graph.h"
#include <vector>
#include <queue>
using namespace std;
// =============================================================================
// SpanningTreePrim ============================================================
// =============================================================================
CSpanningTree SpanningTreePrim(CGraph & graph)
{
// Check if the graph is empty
CSpanningTree tree(&graph);
if (graph.m_Edges.empty() || graph.m_Vertices.empty()) {
return tree;
}
struct comparator {
bool operator()(const CEdge* pE1, const CEdge* pE2) const
{
return pE1->m_Length > pE2->m_Length;
}
};
priority_queue<CEdge*, std::vector<CEdge*>, comparator> queue;
// Add initial candidates to the queue
CVertex& first_vertex = graph.m_Vertices.front();
for(CEdge* edge : first_vertex.m_Edges) {
queue.push(edge);
}
// Setting all nodes as outside the tree
for (CVertex& vertex : graph.m_Vertices) {
vertex.m_PrimInTree = false;
}
first_vertex.m_PrimInTree = true;
// Loop till the tree is created or all nodes are visited
while (!queue.empty() && tree.m_Edges.size() != graph.m_Vertices.size() - 1) {
// Add best candidate
auto minimum_cost_edge = queue.top();
minimum_cost_edge->m_pDestination->m_PrimInTree = true;
tree.m_Edges.push_back(minimum_cost_edge);
// Remove used candidate
queue.pop();
// Add new candidates
for (const auto edge : minimum_cost_edge->m_pDestination->m_Edges) {
// Check if the candidate is valid
if (edge->m_pDestination->m_PrimInTree == true)
{
continue;
}
queue.push(edge);
}
// Clean the queue
for (int i = 0; i < queue.size(); i++)
{
if (queue.top()->m_pDestination->m_PrimInTree == true) {
queue.pop();
}
else
{
break;
}
}
}
return tree;
}