-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cpp
133 lines (90 loc) · 3 KB
/
Algorithms.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
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
#include "Algorithms.h"
Graph *Kruskal(Graph *g, int &totalWeight, string& line)
{
totalWeight = 0;
if (!g->isConnectedGraph())
{
line = "No MST, The graph is not connected \n";
return nullptr;
}
Graph *result = new Graph(g->get_Num_of_Vertices()); // of edges
if (!(g->get_edgesIsSorted())) {
g->QuickSort(0, g->get_edgesSize() - 1); // Sort Edges By weight
g->set_edgesIsSorted();
}
vector<weightedEdge> vecEdge = g->getWeightedEdgesVector(); // Items: 0 - n-1
Union Vertices(g->get_Num_of_Vertices());
int numOfEdges = 0;
// if result has n-1 edges, We can stop the loop (We have found the tree).
for (int i = 0; i < vecEdge.size() && numOfEdges < (g->get_Num_of_Vertices() - 1) ; i++) {
int vec1Rep = Vertices.Find(vecEdge[i].ver1);
int vec2Rep = Vertices.Find(vecEdge[i].ver2);
if (vec1Rep != vec2Rep) {
result->AddEdge(vecEdge[i].ver1 + 1, vecEdge[i].ver2 + 1, vecEdge[i].weight);
totalWeight += vecEdge[i].weight;
Vertices.UnionVertices(vec1Rep, vec2Rep);
numOfEdges++;
}
}
return result;
}
/*********************************************************************/
/*
* isInVec checks if the inverted edge of e {ver2, ver1, weight} already exist in the vector
*/
bool isInVec(vector<weightedEdge> &vecEdge, weightedEdge e)
{
bool check = false;
for (int i = 0; i < vecEdge.size() && !check; i++)
{
if (vecEdge[i].ver1 == e.ver2 && vecEdge[i].ver2 == e.ver1)
check = true;
}
return check;
}
/*********************************************************************/
Graph* Prim(Graph* g, int& totalWeight) {
vector<int> min; // in terms of 0 - n-1
vector<int> parents; // in terms of 0 - n-1
vector<bool> InTree; // in terms of 0 - n-1
totalWeight = 0;
Graph* res = new Graph(g->get_Num_of_Vertices()); // the found tree graph
min.reserve(g->get_Num_of_Vertices());
parents.reserve(g->get_Num_of_Vertices());
for (int i = 0; i < g->get_Num_of_Vertices(); i++) {
min.push_back(INFINITY);
parents.push_back(NULLPARENT);
InTree.push_back(false);
}
min[0] = 0;
InTree[0] = true;
MinHeap q(min);
// Main Loop:
while (!q.isEmpty()) {
heapNode u = q.deleteMin();
InTree[u.data] = true;
List* adjList = g->GetAdjList(u.data + 1); // need to get in terms of 1 - n
Node* curr = adjList->getHead();
while (curr != nullptr) {
int v = curr->getVertex(); // in terms of 0 - n-1
if (!InTree[v] && curr->getWeight() < min[v]) {
min[v] = curr->getWeight();
parents[v] = u.data; //update v's parent to be u
q.deceaseKey(v, min[v]);
}
curr = curr->getNext();
}
delete adjList;
}
buildGraphFromPMin(res, min, parents, totalWeight);
return res;
}
/*********************************************************************/
void buildGraphFromPMin(Graph* res, vector<int> min, vector<int> parents, int& totalWeight) {
for (int i = 1; i < parents.size(); i++)
{
res->AddEdge(parents[i] + 1, i + 1, min[i]);
totalWeight += min[i];
}
}
/*********************************************************************/