-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmgraphpath.cpp
More file actions
219 lines (164 loc) · 4.84 KB
/
frmgraphpath.cpp
File metadata and controls
219 lines (164 loc) · 4.84 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
#include <iostream>
#include <sstream>
#include <unordered_map>
#include "frmgraphpath.h"
#include "ui_frmgraphpath.h"
#include "graphs.h"
#include "graphsalgorithms.h"
using namespace std;
using namespace algorithms;
using namespace graphs;
using namespace graphs::ui;
frmGraphPath::frmGraphPath(bool directed, QWidget *parent) :
QWidget(parent),
ui(new Ui::frmGraphPath),
_bDirected(directed)
{
ui->setupUi(this);
widget = new graphs::ui::GraphWidget(directed);
widget->showArrowLabel();
ui->lytHor->addWidget(widget);
ui->optDjskt->setChecked(true);
connect(widget, SIGNAL(errorRaised(int)), this, SLOT(errorRaised(int)));
connect(widget, SIGNAL(nodeCreated(GNode*)), this, SLOT(nodeCreated(GNode*)));
}
frmGraphPath::~frmGraphPath()
{
delete ui;
}
void frmGraphPath::writeLog(string log)
{
QString data;
data = ui->txtLogs->toPlainText();
data += log.c_str();
ui->txtLogs->setText(data);
}
/*********************
* SLOTS *
*********************/
void frmGraphPath::errorRaised(int code)
{
std::cout <<"frmGraphPath::errorRaised: " << code << endl;
}
void frmGraphPath::nodeCreated(GNode* node)
{
stringstream data;
stringstream dataSize;
stringstream out;
//G.addNode(node->getId());
data << node->getId();
dataSize << node->getId() +1;
ui->cmbFrom->addItem(data.str().c_str());
ui->cmbTo->addItem(data.str().c_str());
ui->cmbSize->addItem(dataSize.str().c_str());
cout << "Node [" << node->getId() << "] was added.";
writeLog(out.str());
}
/*********************
* FORM *
*********************/
void frmGraphPath::on_cmdFind_clicked()
{
long from = ui->cmbFrom->currentIndex();
if(ui->optDjskt->isChecked())
{
long to = ui->cmbTo->currentIndex();
procDijkstra(from,to);
}
else if(ui->optPrim->isChecked())
procPRIM(from);
else if(ui->optKruskal->isChecked())
procKruskatCluster();
else if(ui->optCluster->isChecked())
{
long size = ui->cmbSize->currentIndex() +1;
procKruskatCluster(size, true);
}
}
void frmGraphPath::selectOption()
{
ui->cmbFrom->setEnabled( ui->optKruskal->isChecked() ? false : true);
ui->cmbTo->setEnabled( ui->optDjskt->isChecked() ? true : false );
ui->cmbSize->setEnabled(ui->optCluster->isChecked());
}
void frmGraphPath::on_cmdClear_clicked()
{
ui->txtLogs->clear();
}
void frmGraphPath::on_optDjskt_clicked()
{
selectOption();
}
void frmGraphPath::on_optPrim_clicked()
{
selectOption();
}
void frmGraphPath::on_optKruskal_clicked()
{
selectOption();
}
void frmGraphPath::on_optCluster_clicked()
{
selectOption();
}
/*********************
* PROC RESULTS *
*********************/
void frmGraphPath::procDijkstra(long from, long to)
{
stringstream steps;
if(widget->Graph().empty()) return;
Graph G{widget->Graph()}; //Clone the original Graph
stack<tuple<long,long,long >> path = shortpath::dijkstra(from,to,G,steps);
widget->resetGraphAppearance(false);
steps << "::Shorter Path: ";
bool started = true;
while(!path.empty())
{
tuple<long,long,long > pthStep = path.top();
path.pop();
long idNode = std::get<0>(pthStep);
long idEdge = std::get<1>(pthStep);
long cost = std::get<2>(pthStep);
if(started) started = false;
else steps << " : ";
steps << "N[" << idNode << "]-E[" << idEdge << "]-C[" << cost << "]";
widget->setItemOpacity(idNode, GITEM_NODE, true);
if(idEdge > -1)
widget->setItemOpacity(idEdge, GITEM_EDGE,true);
}
steps << " (END) " << endl;
writeLog(steps.str());
}
void frmGraphPath::procPRIM(long from)
{
stringstream steps;
if(widget->Graph().empty()) return;
Graph G{widget->Graph()}; //Clone the original Graph
minimun_spanning::prim(from,G,steps);
widget->resetGraphAppearance(false);
//redraw the Tree, the new graph.
for(int n=0; n < G.countNodes(); n++)
widget->setItemOpacity(n, GITEM_NODE, G[n].isEnabled());
for(int e=0; e < G.countEdges(); e++)
widget->setItemOpacity(e, GITEM_EDGE, G(e).isEnabled());
steps << " (END) " << endl;
writeLog(steps.str());
}
void frmGraphPath::procKruskatCluster(long size, bool isCluster)
{
stringstream steps;
if(widget->Graph().empty()) return;
Graph G{widget->Graph()}; //Clone the original Graph
if(!isCluster)
minimun_spanning::kruskal(G,steps);
else
minimun_spanning::findClusters(size,G,steps);
widget->resetGraphAppearance(false);
//redraw the Tree, the new graph.
for(int n=0; n < G.countNodes(); n++)
widget->setItemOpacity(n, GITEM_NODE, G[n].isEnabled());
for(int e=0; e < G.countEdges(); e++)
widget->setItemOpacity(e, GITEM_EDGE, G(e).isEnabled());
writeLog(steps.str());
}