-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
81 lines (72 loc) · 2.13 KB
/
graph.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
//
// Created by Emil Akopyan on 08.10.2020.
//
#include "graph.h"
#include <queue>
#include <functional>
#include <iostream>
#include <iomanip>
void Graph::addNewEdge(int u, double weight, int v)
{
_adjlist[u].push_back(edge(weight,v));
}
Graph::vertex Graph::getVertex(int n)
{
return _vertices[n];
}
double Graph::calculateDistance(vertex u, vertex v)
{
double x1 = u.first;
double y1 = u.second;
double x2 = v.first;
double y2 = v.second;
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
void Graph::addNewVert(int i, double x, double y)
{
_vertices[i] = {x,y};
}
void Graph::primsAlgorithm()
{
std::vector<bool> visits = std::vector<bool> (_vertices.size(), 0);
std::priority_queue<edge, std::vector<edge>, std::greater<>> PQ;
edge current;
double minimalCost = 0;
int source = 0;
PQ.push({0, source});
std::vector<std::pair<vertex,vertex>> MST;
std::vector<std::pair<int,double>> parents;
for (int i = 0; i < _vertices.size(); i++)
{
parents.push_back({2147483647, 2147483647});
}
while (MST.size() != _vertices.size() - 1)
{
current = PQ.top();
PQ.pop();
source = current.second;
if (visits[source] == 1)
continue;
visits[source] = true;
if (current.second != 0)
MST.push_back({_vertices[parents[current.second].first],_vertices[current.second]});
minimalCost += current.first;
for (auto it = _adjlist[source].begin(); it != _adjlist[source].end(); it++) {
if (!visits[it->second])
{
if(parents[it->second].second > (*it).first)
{
parents[it->second].first = source;
parents[it->second].second = (*it).first;
}
PQ.push(*it);
}
}
}
std::cout << std::setprecision(9) <<minimalCost << std::endl;
for (size_t i = 0; i < MST.size(); ++i)
{
std::cout << MST[i].first.first << ' ' << MST[i].first.second << ' ' <<
MST[i].second.first << ' ' << MST[i].second.second << std::endl;
}
}