-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
95 lines (76 loc) · 1.69 KB
/
Copy pathprim.cpp
File metadata and controls
95 lines (76 loc) · 1.69 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
#include "prim.h"
#include "binomial.h"
#include "fibonacci.h"
#include <limits.h>
struct tVertex
{
bool operator<(const tVertex& v) const
{
return Weight < v.Weight;
}
bool operator>(const tVertex& v) const
{
return Weight > v.Weight;
}
bool operator<=(const tVertex& v) const
{
return Weight <= v.Weight;
}
bool operator>=(const tVertex& v) const
{
return Weight >= v.Weight;
}
bool InQ;
int Vertex;
int Weight;
int Parent;
tStub* HeapStub;
};
tGraph*
PrimSpanningTree(const tGraph* g, int r, tHeap<tVertex>* heap)
{
int i;
tVertex* v = new tVertex[g->N];
for (i = 0; i < g->N; ++i) {
v[i].Vertex = i;
v[i].Weight = INT_MAX;
v[i].InQ = true;
v[i].Parent = -1;
if (i == r)
v[i].Weight = 0;
v[i].HeapStub = heap->Insert(&(v[i]));
}
while (heap->Minimum()) {
tVertex* u = heap->ExtractMin();
tAdjacency* a = g->Adjacency[u->Vertex];
while (a) {
if (v[a->Vertex].InQ && a->Weight < v[a->Vertex].Weight) {
v[a->Vertex].Weight = a->Weight;
v[a->Vertex].Parent = u->Vertex;
heap->DecreaseKey(v[a->Vertex].HeapStub);
}
a = a->Next;
}
u->InQ = false;
}
// constroi a arvore geradora minima a partir dos 'Parent's dos
// vertices
tGraph* tree = new tGraph(g->N);
for (i = 0; i < g->N; ++i) {
if (v[i].Parent >= 0)
tree->CreateEdge(i, v[i].Parent, v[i].Weight);
}
return tree;
}
tGraph*
BinomialPrimSpanningTree(const tGraph* g, int r)
{
tBinomialHeap<tVertex> heap;
return PrimSpanningTree(g, r, &heap);
}
tGraph*
FibonacciPrimSpanningTree(const tGraph* g, int r)
{
tFibonacciHeap<tVertex> heap;
return PrimSpanningTree(g, r, &heap);
}