-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.cpp
1353 lines (1134 loc) · 35 KB
/
path.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BetaCome: betacome of future network routing
*
* Code to accompany the competition:
* huawei software challenge : future network routing,
* Yuanyuan Qin, Song Tang,2016
*
* Copyright (C) 2016 Yuanyuan Qin, Peking University, Shenzhen, China
*
* This file is part of BetaCome.
*
* BetaCome is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BetaCome is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BetaCome. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "path.h"
#include "assist.h"
#include "lib_record.h"
#include "pathEdge.h"
#include <iostream>
#include <sstream>
#include <queue>
#include <unordered_set>
#include <cassert>
#include <memory.h>
using namespace std;
Path::Path(GraphVector &graph, char *demand, PATH_ID pathIdIn)
{
pathId = pathIdIn;
list<PathPiece>::iterator xx;
pathPieceOne.resize(MAXNODEID + 1 , xx);//resize
pathPieceTwo.resize(MAXNODEID + 1, xx);
memset(pi, 0, MAXPOINTNUM*sizeof(int));
memset(distance, 0, MAXPOINTNUM*sizeof(int));
crackTimes = new int*[MAXPOINTNUM];
for (int i = 0; i < MAXPOINTNUM; i++)
{
crackTimes[i] = new int[MAXPOINTNUM];
memset(crackTimes[i], 0, MAXPOINTNUM*sizeof(int));
}
memset(propertyArray, 0xff, sizeof(int)*MAXPOINTNUM);//initial to be invalid
memset(validityArray, 0xff, sizeof(int)*MAXPOINTNUM);
memset(usedArray, 0, sizeof(int)*MAXPOINTNUM);
//get information from graph, set array
auto iterGraphNode = graph.getGraphNode().begin();
auto iterGraphNodeEnd = graph.getGraphNode().end();
for (; iterGraphNode != iterGraphNodeEnd; ++iterGraphNode)
{
int pointId = (*iterGraphNode).getPointId();
if (pointId != POINT_INVALID)
{
propertyArray[pointId] = POINT_V;
validityArray[pointId] = POINT_VALID;
}
}
//set demand:start end vPrimeIndex vPrimeSize
string strLine(demand);
string::size_type j = 0;
for (; j != strLine.size(); ++j)//change character to space
{
if (isdigit(strLine[j])) continue;//if is digit continue
else strLine[j] = ' ';
}
istringstream istr(strLine);
istr >> vStart;//first num is pathid
istr >> vStart >> vEnd;
vPrimeSize = 0;
while (istr >> vPrimeIndex[vPrimeSize])//input vPrime point
{
vPrimeSize++;
}
//according to demand,set property of vprime, start and end point
propertyArray[vStart] = POINT_START;
propertyArray[vEnd] = POINT_END;
for (int i = 0; i < vPrimeSize; i++)
{
propertyArray[vPrimeIndex[i]] = POINT_VPRIME;
}
}
Path::~Path()
{
for (int i = 0; i < MAXPOINTNUM; i++)
{
delete[] crackTimes[i];
}
delete[] crackTimes;
}
Path::Path()
{
crackTimes = new int*[MAXPOINTNUM];
for (int i = 0; i < MAXPOINTNUM; i++)
{
crackTimes[i] = new int[MAXPOINTNUM];
}
}
Path& Path::operator=(const Path &otherPath)
{
pathList = otherPath.pathList;
pathPieceOne = otherPath.pathPieceOne;//路径片段1
pathPieceTwo = otherPath.pathPieceTwo;//路径片段2,只有V’点才有片段2
pathId = otherPath.pathId;
memcpy(pi, otherPath.pi, MAXPOINTNUM*sizeof(int));
memcpy(distance, otherPath.distance, MAXPOINTNUM*sizeof(int));
//attention, do not copy this: int **crackTimes;
//for path
vStart = otherPath.vStart;
vEnd = otherPath.vEnd;
memcpy(vPrimeIndex, otherPath.vPrimeIndex, MAXVPRIME*sizeof(int));
vPrimeSize = otherPath.vPrimeSize;//if we have 3 v' points, this should be 3
//point's property of path
memcpy(propertyArray, otherPath.propertyArray, MAXPOINTNUM*sizeof(int));
memcpy(validityArray, otherPath.validityArray, MAXPOINTNUM*sizeof(int));
memcpy(usedArray, otherPath.usedArray, MAXPOINTNUM*sizeof(int));
return *this;
}
//delete one piece, we set points unused, then delete this piece,such as:m->a->b->n,we delete a->b
//attention: after we use this func, we must add a piece of a~b, or the path will get wrong
//return iter of piece that after which you deleted,support for continuous deletion
//attention: this may delete VStart and VEnd, so be carefully
list<PathPiece>::iterator Path::deleteOnePiece(GraphVector &graph, list<PathPiece>::iterator iterPiece, int enableSetFalse)
{
//set points unused,just set ordinary, do not set endpoint ie vprime
if (enableSetFalse) setPieceNodeFalse(graph, iterPiece);
//check vprime before and after the deleted piece,if it is not continuous,set vprime unused
list<PathPiece>::iterator iterTemp;
iterTemp = iterPiece;
if (iterPiece == pathList.begin())
{
//iterTemp is the first piece
setUsed((*iterPiece).getFirstNode(), false);
}
else
{
--iterTemp;
if ((*iterTemp).getLastNode() != (*iterPiece).getFirstNode()) setUsed((*iterPiece).getFirstNode(), false);
}
iterTemp = iterPiece;
if (iterPiece == (--(pathList.end())))
{
//iterTemp is the last piece
setUsed((*iterPiece).getLastNode(), false);
}
else
{
++iterTemp;
if ((*iterTemp).getFirstNode() != (*iterPiece).getLastNode()) setUsed((*iterPiece).getLastNode(), false);
}
//delete piece
return pathList.erase(iterPiece);
}
//this func use edge to compute weight
//pair<int, int> Path::getPathWeight(GraphVector &graph)
//{
// int nodeLeft = 0;
// int nodeRight = 0;
// int weight = 0;
// auto iterPiece = pathList.begin();
//
// for (iterPiece = pathList.begin(); iterPiece != pathList.end(); ++iterPiece)
// {
// nodeLeft = (*iterPiece).getFirstNode();
// vector<int> &linkNodesVector = (*iterPiece).getLinkNodesVector();
//
// for (auto iterLinkNodes = linkNodesVector.rbegin(); iterLinkNodes != linkNodesVector.rend(); ++iterLinkNodes)
// {
// nodeRight = (*iterLinkNodes);
// weight += graph.getEdge(nodeLeft, nodeRight).getEdgeWeight();
//
// nodeLeft = nodeRight;
// }
//
// nodeRight = (*iterPiece).getLastNode();
// weight += graph.getEdge(nodeLeft, nodeRight).getEdgeWeight();
// }
// return weight;
//}
//return a pair<int,int>, pair.first=weight pair.second=pieceNum
pair<int, int> Path::getPathWeight(GraphVector &graph)
{
int weight = 0;
int pieceNum = 0;
for (auto iterPiece = pathList.begin(); iterPiece != pathList.end(); ++iterPiece)
{
weight += (*iterPiece).getWeight();
pieceNum++;
}
return pair<int, int>(weight, pieceNum);
}
//insert crack piece for two vprime points before iterIn,return iter of the inserted piece
list<PathPiece>::iterator Path::insertCrackPiece(GraphVector &graph, int firstNode, int secondNode,
list<PathPiece>::iterator iterIn)
{
int curFirstNode = secondNode;
int curLastNode = secondNode;
PathPiece pieceTemp;
list<PathPiece>::iterator iter;
iter = pathList.insert(iterIn, pieceTemp);
//set points properties
setUsed(firstNode, true);
setPathPieceTwo(firstNode, iter);
setUsed(secondNode, true);
setPathPieceOne(secondNode, iter);
//set piece properties
(*iter).setFirstNode(firstNode);
(*iter).setLastNode(secondNode);
(*iter).setLinkProperty(CRACKED);
(*iter).setWeight(INFINITEWEIGHT);
return iter;
}
//check all pieces in path, call func insertVPrimeToPiece
//insert one vPrime to one piece
int Path::insertVPrimeToPath(GraphVector &graph, int vPrimeId, int enableUsed)
{
int firstNode = 0;
int lastNode = 0;
int firstNodeSccNum = 0;
int lastNodeSccNum = 0;
int curVPrimeSccNum = graph.getNode(vPrimeId).getSccNum();
int ansInsertVPrimeToPiece = 0;
//for loop,check pieces satisfied scc needs
for (auto iterTemp = pathList.begin(); iterTemp != pathList.end(); ++iterTemp)
{
firstNode = (*iterTemp).getFirstNode();
lastNode = (*iterTemp).getLastNode();
firstNodeSccNum = graph.getNode(firstNode).getSccNum();
lastNodeSccNum = graph.getNode(lastNode).getSccNum();
//if sccnum do not satisfied needs
if (lastNodeSccNum > curVPrimeSccNum) continue;
if (firstNodeSccNum < curVPrimeSccNum) break;//have passed the range of curVPrimeSccNum
//the rest are ok
ansInsertVPrimeToPiece = insertVPrimeToPiece(graph, iterTemp, vPrimeId, enableUsed);
//check result
if (ansInsertVPrimeToPiece == ALGORITHMFAILURE) continue;
else return ALGORITHMSUCCESS;
}
//if can not insert to all pieces,return failure
return ALGORITHMFAILURE;
}
//insert a vprime point to a piece
//such as we have a piece a->b, now we want a->c->b
int Path::insertVPrimeToPiece(GraphVector &graph, list<PathPiece>::iterator iterPiece, int vPrimeId, int enableUsed)
{
int piAC[MAXPOINTNUM];
int piCB[MAXPOINTNUM];
int commonColor[MAXPOINTNUM];
memset(piAC, 0, sizeof(int)*MAXPOINTNUM);
memset(piCB, 0, sizeof(int)*MAXPOINTNUM);
memset(commonColor, 0, sizeof(int)*MAXPOINTNUM);
int nodeA = (*iterPiece).getFirstNode();
int nodeB = (*iterPiece).getLastNode();
int nodeC = vPrimeId;
int resultMultiBfs = 0;
list<PathPiece>::iterator iterTemp;
if (enableUsed == false)
{
//set points in a->b unused
setPieceNodeFalse(graph, iterPiece);
//two bfs,multi-bfs,need common color array
resultMultiBfs = multiBfsPathPiece(graph, nodeA, nodeC, commonColor, piAC, false);
if (resultMultiBfs == FOUND)
{
setCommonColor(nodeA, nodeC, commonColor, piAC);
resultMultiBfs = multiBfsPathPiece(graph, nodeC, nodeB, commonColor, piCB, false);
if (resultMultiBfs == FOUND)//if can find, delete piece a-b,add a->c->b
{
//delete old piece
iterTemp = deleteOnePiece(graph, iterPiece, true);
//insert 2 new pieces,insert c->b first, then a->c
iterTemp = createPieceList(graph, nodeC, nodeB, iterTemp, piCB);
iterTemp = createPieceList(graph, nodeA, nodeC, iterTemp, piAC);
return ALGORITHMSUCCESS;
}
else//if can not, return ALGORITHMFAILURE
{
//set points in a->b used
setPieceNodeTrue(graph, iterPiece);
return ALGORITHMFAILURE;
}
}
else
{
//set points in a->b used
setPieceNodeTrue(graph, iterPiece);
return ALGORITHMFAILURE;
}
}
else//can use used points
{
//set points in a->b unused
setPieceNodeFalse(graph, iterPiece);
//two bfs,multi-bfs,need common color array
resultMultiBfs = multiBfsPathPiece(graph, nodeA, nodeC, commonColor, piAC, true);
if (resultMultiBfs == FOUND)
{
setCommonColor(nodeA, nodeC, commonColor, piAC);
resultMultiBfs = multiBfsPathPiece(graph, nodeC, nodeB, commonColor, piCB, true);
if (resultMultiBfs == FOUND)//if can find, deal with used points, delete piece a-b,add a->c->b
{
//deal with used piece
dealUsedNodes(graph, nodeC, nodeB, piCB);
dealUsedNodes(graph, nodeA, nodeC, piAC);
//delete old piece
iterTemp = deleteOnePiece(graph, iterPiece, true);
//insert 2 new pieces,insert c->b first, then a->c
iterTemp = createPieceList(graph, nodeC, nodeB, iterTemp, piCB);
iterTemp = createPieceList(graph, nodeA, nodeC, iterTemp, piAC);
return ALGORITHMSUCCESS;
}
else
{
//set points in a->b used
setPieceNodeTrue(graph, iterPiece);
return ALGORITHMFAILURE;
}
}
else
{
//set points in a->b used
setPieceNodeTrue(graph, iterPiece);
return ALGORITHMFAILURE;
}
}
return ALGORITHMFAILURE;
}
//connect two nodes
//when enableUsed == false,we only use unused points to find a path
//when enableUsed == true, we ensure to find a path
int Path::connectTwoNodes(GraphVector &graph, int firstNode, int secondNode, int enableUsed)
{
int searchResult = 0;
if (enableUsed == false)
{
//search without used point, see wether we can get a pathpiece
searchResult = bfsPathPiece(graph, firstNode, secondNode, false);
if (searchResult == FOUND)
{
addPathPiece(graph, firstNode, secondNode);
}
return searchResult;
}
else
{
//search without used point fisrt,see wether we can get a pathpiece
searchResult = bfsPathPiece(graph, firstNode, secondNode, false);
if (searchResult == FOUND)
{
addPathPiece(graph, firstNode, secondNode);
}
else
{
//use used points to search pathpiece
if (bfsPathPiece(graph, firstNode, secondNode, true) == NOTFOUND)
{
return NOSOLUTION;
}
//deal with used points and pathpiece associated with
//restore:used/pathpieceone/pathpiecetwo
dealUsedNodes(graph, firstNode, secondNode);
addPathPiece(graph, firstNode, secondNode);
}
return searchResult;
}
}
//bfs graph to find a path for two point
//bfs graph to find a path for two point
//bfs的算法可以参考 算法导论 上的伪代码,大师确实写得很好
int Path::bfsPathPiece(GraphVector &graph, int firstNode, int secondNode, bool enableUsed)
{
queue<int> pointQueue;
int flag = 0;
int color[MAXPOINTNUM];
//initial,需要的话,这种初始化操作可以优化掉
for (int i = 0; i <= MAXNODEID; ++i)
{
color[i] = WHITE;
pi[i] = INVALID;
distance[i] = INVALID;
}
//initial start point
color[firstNode] = GRAY;
distance[firstNode] = 0;
pi[firstNode] = INVALID;
pointQueue.push(firstNode);
while (!pointQueue.empty() && !flag)//time complexity:O(V+E), actually less than this
{
int uu = pointQueue.front();
pointQueue.pop();
vector<Edge> &edgeVector = graph.getNode(uu).getEdgeVector();
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)//traverse every edge of point uu
{
int vv = (*iter).getNodeTwo();
Point &pointTemp = graph.getNode(vv);
if (vv == secondNode)//meet point we need
{
color[vv] = GRAY;
distance[vv] = distance[uu] + 1;
pi[vv] = uu;
pointQueue.push(vv);
flag = 1;
break;//jump out of loop
}
if ((color[vv] != WHITE) || (getValidity(vv) != POINT_VALID) || ((getUsed(vv) != false) && !enableUsed)
|| (getProperty(vv) == POINT_START) || (getProperty(vv) == POINT_END)) continue;
//the rest can satisfy needs
color[vv] = GRAY;
distance[vv] = distance[uu] + 1;
pi[vv] = uu;
pointQueue.push(vv);
}
color[uu] = BLACK;
}
if (flag) return FOUND;
else return NOTFOUND;
}
//this bfs is used for finding more than one path, so we need commoncolor and unique pi
int Path::multiBfsPathPiece(GraphVector &graph, int firstNode, int secondNode, int *commonColor, int *pi, bool enableUsed)
{
queue<int> pointQueue;
int flag = 0;
int color[MAXPOINTNUM];
memset(color, 0, sizeof(int)*MAXPOINTNUM);
//initial start point
color[firstNode] = GRAY;
pi[firstNode] = INVALID;
pointQueue.push(firstNode);
while (!pointQueue.empty() && !flag)//time complexity:O(V+E), actually less than this
{
int uu = pointQueue.front();
pointQueue.pop();
vector<Edge> &edgeVector = graph.getNode(uu).getEdgeVector();
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)//traverse every edge of point uu
{
int vv = (*iter).getNodeTwo();
Point &pointTemp = graph.getNode(vv);
if (vv == secondNode)//meet point we need
{
color[vv] = GRAY;
pi[vv] = uu;
pointQueue.push(vv);
flag = 1;
break;//jump out of loop
}
// (used) || (the point has been deleted by me) || (has visited) || (used by other bfs)
if ((!enableUsed && (getUsed(vv) != false)) || (getValidity(vv) != POINT_VALID)
|| (color[vv] != WHITE) || (commonColor[vv] != WHITE)) continue;
//the rest can satisfy needs
color[vv] = GRAY;
pi[vv] = uu;
pointQueue.push(vv);
}
color[uu] = BLACK;
}
if (flag) return FOUND;
else return NOTFOUND;
}
int Path::dealUsedNodes(GraphVector &graph, int firstNode, int secondNode)
{
int nodeId = pi[secondNode];//从secondNode的父节点开始
for (; nodeId != firstNode; nodeId = pi[nodeId])
{
Point &pointTemp = graph.getNode(nodeId);
if (getUsed(nodeId) == false) continue;
else
{
int xx = getProperty(nodeId);
if (xx == POINT_V)//ordinary point
{
crackOnePiece(graph, nodeId);
}
else if (xx == POINT_VPRIME)//vprime point
{
crackTwoPiece(graph, nodeId);
}
}
}
if (nodeId == firstNode) return EXIT_SUCCESS;
else
{
#ifdef DEBUG
assert(0);//ie,wrong
#endif
return EXIT_FAILURE;
}
}
int Path::dealUsedNodes(GraphVector &graph, int firstNode, int secondNode, int *piLocal)
{
int nodeId = piLocal[secondNode];//从secondNode的父节点开始
for (; nodeId != firstNode; nodeId = piLocal[nodeId])
{
Point &pointTemp = graph.getNode(nodeId);
if (getUsed(nodeId) == false) continue;
else
{
int xx = getProperty(nodeId);
if (xx == POINT_V)//ordinary point
{
crackOnePiece(graph, nodeId);
}
else if (xx == POINT_VPRIME)//vprime point
{
crackTwoPiece(graph, nodeId);
}
}
}
if (nodeId == firstNode) return EXIT_SUCCESS;
else
{
#ifdef DEBUG
assert(0);//ie,wrong
#endif
return EXIT_FAILURE;
}
}
int Path::crackOnePiece(GraphVector &graph, int nodeId)
{
#ifdef DEBUG
assert(getProperty(nodeId) == POINT_V);
#endif
list<PathPiece>::iterator iterPieceTemp = getPathPieceOne(nodeId);
//restore properties of points
list<PathPiece>::iterator pathPieceNull;
auto &linkNodesVector = (*iterPieceTemp).getLinkNodesVector();
auto iterLinkNodesVectorBegin = linkNodesVector.begin();
auto iterLinkNodesVectorEnd = linkNodesVector.end();
for (auto iter = iterLinkNodesVectorBegin; iter != iterLinkNodesVectorEnd; ++iter)
{
Point &xx = graph.getNode(*iter);
setUsed((*iter), false);
setPathPieceOne((*iter), pathPieceNull);
}
//set properties of path piece
(*iterPieceTemp).getLinkNodesVector().clear();
(*iterPieceTemp).setLinkProperty(CRACKED);
(*iterPieceTemp).setWeight(INFINITEWEIGHT);
//cracktimes ++
crackTimes[(*iterPieceTemp).getFirstNode()][(*iterPieceTemp).getLastNode()]++;
return EXIT_SUCCESS;
}
int Path::crackTwoPiece(GraphVector &graph, int nodeId)
{
#ifdef DEBUG
assert(getProperty(nodeId) == POINT_VPRIME);
#endif
Point &node = graph.getNode(nodeId);
list<PathPiece>::iterator iterPieceOne = getPathPieceOne(nodeId);
list<PathPiece>::iterator iterPieceTwo = getPathPieceTwo(nodeId);
int firstNode = (*iterPieceOne).getFirstNode();
int lastNode = (*iterPieceTwo).getLastNode();
//restore properties of points
list<PathPiece>::iterator pathPieceNull;
for (auto iter = (*iterPieceOne).getLinkNodesVector().begin(); iter != (*iterPieceOne).getLinkNodesVector().end(); ++iter)
{
Point &xx = graph.getNode(*iter);
setUsed((*iter), false);
setPathPieceOne((*iter), pathPieceNull);
}
for (auto iter = (*iterPieceTwo).getLinkNodesVector().begin(); iter != (*iterPieceTwo).getLinkNodesVector().end(); ++iter)
{
Point &xx = graph.getNode(*iter);
setUsed((*iter), false);
setPathPieceOne((*iter), pathPieceNull);
}
setUsed(nodeId, false);
setPathPieceOne(nodeId, pathPieceNull);
setPathPieceTwo(nodeId, pathPieceNull);
//crack times ++
crackTimes[firstNode][nodeId]++;//不知道这个crack增加合不合适
crackTimes[nodeId][lastNode]++;//不知道这个crack增加合不合适
crackTimes[firstNode][lastNode]++;
//create cracked pathpiece,set new piece,insert new piece
PathPiece pieceNew;
pieceNew.setFirstNode(firstNode);
pieceNew.setLastNode(lastNode);
pieceNew.setLinkProperty(CRACKED);
auto iter = pathList.insert(iterPieceOne, pieceNew);
//modify point's PathPiece,and delete unused pathpiece
setPathPieceTwo(firstNode, iter);
setPathPieceOne(lastNode, iter);
iter = pathList.erase(++iter);//erase old pieceOne
pathList.erase(iter);//erase old pieceTwo
return EXIT_SUCCESS;
}
//add created pathpiece list to path
//this func can add piece to empty path, to the tail of path
//or can repair cracked piece
int Path::addPathPiece(GraphVector &graph, int firstNode, int secondNode)
{
//if path is empty,create a pathpiece(or pathpiece list),and add to path
if (pathList.empty())
{
//here,pathList.begin()==pathList.end()
createPieceList(graph, firstNode, secondNode, pathList.begin());
return EXIT_SUCCESS;
}
list<PathPiece>::iterator iter = pathList.end();
if (firstNode == (*(--iter)).getLastNode())//firstnode is lastnode of path
{
createPieceList(graph, firstNode, secondNode, pathList.end());//add to tail of path
}
else
{
//locate to piece,for path a->b x c,we locate to a->b
//for path a->b c->d,we locate to a->b
for (iter = pathList.begin(); ((iter != pathList.end()) && ((*iter).getFirstNode() != firstNode)); ++iter);
//delete cracked piece b x c,return iter of c->d
iter = pathList.erase(iter);
//create path list for b x c
createPieceList(graph, firstNode, secondNode, iter);
}
return EXIT_SUCCESS;
}
//set points used/pathpieceone/pathpiecetwo,create pathpiece
//firstNode and secondNode are endpoints of our piece,we insert piece before iterIn
list<PathPiece>::iterator Path::createPieceList(GraphVector &graph, int firstNode, int secondNode,
list<PathPiece>::iterator iterIn)
{
#ifdef DEBUG
assert((getProperty(firstNode) != POINT_V)
&& (getProperty(secondNode) != POINT_V));
#endif
int uu = pi[secondNode];
int vv = secondNode;
int weight = 0;
int curFirstNode = secondNode;
int curLastNode = secondNode;
PathPiece pieceTemp;
list<PathPiece>::iterator iter;
auto iterEnd = pathList.end();
if (pathList.empty())//1:pathlist is empty
{
iter = pathList.insert(iterEnd, pieceTemp);
}
else if (iterIn == iterEnd)//2:add pathpiece to the tail of pathlist
{
iter = pathList.insert(iterEnd, pieceTemp);
}
else//3:repair cracked piece, should arase old piece
{
iter = pathList.insert(iterIn, pieceTemp);
//!!!!!remember delete old piece!!!
}
//set point vv property
setUsed(vv, true);
setPathPieceOne(vv, iter);
while (uu != firstNode)//deal with all points in searched path except firstnode
{
if (getProperty(uu) != POINT_VPRIME)//uu is ordinary v point
{
//set piece
(*iter).getLinkNodesVector().push_back(uu);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
//set node of graph
setUsed(uu, true);
setPathPieceOne(uu, iter);//for v point,only have piece one
}
else//uu is vprime point
{
#ifdef DEBUG
assert(getProperty(uu) == POINT_VPRIME);
#endif
//get current piece's first and second node
curFirstNode = uu;
//set piece
(*iter).setFirstNode(curFirstNode);
(*iter).setLastNode(curLastNode);
(*iter).setLinkProperty(LINKED);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
(*iter).setWeight(weight);
//set point
setUsed(uu, true);
setPathPieceTwo(uu, iter);//for vprime point,set piece two
setPathPieceOne(curLastNode, iter);//for last vprime point,set piece one
//add new path piece,last piece setted over
weight = 0;
iter = pathList.insert(iter, pieceTemp);
//get current piece's first and second node
curLastNode = curFirstNode;
}
//refresh uu and vv
vv = uu;
uu = pi[uu];
}
if (uu == firstNode)
{
//get current piece's first and second node
curFirstNode = uu;
//set piece
(*iter).setFirstNode(curFirstNode);
(*iter).setLastNode(curLastNode);
(*iter).setLinkProperty(LINKED);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
(*iter).setWeight(weight);
//set point
setUsed(uu, true);
setPathPieceTwo(uu, iter);//for vprime point,set piece two
setPathPieceOne(curLastNode, iter);
}
return iter;
}
//set points used/pathpieceone/pathpiecetwo,create pathpiece
//firstNode and secondNode are endpoints of our piece,we insert piece before iterIn
list<PathPiece>::iterator Path::createPieceList(GraphVector &graph, int firstNode, int secondNode,
list<PathPiece>::iterator iterIn, int *piLocal)
{
#ifdef DEBUG
assert((getProperty(firstNode) != POINT_V)
&& (getProperty(secondNode) != POINT_V));
#endif
int uu = piLocal[secondNode];
int vv = secondNode;
int weight = 0;
int curFirstNode = secondNode;
int curLastNode = secondNode;
PathPiece pieceTemp;
list<PathPiece>::iterator iter;
auto iterEnd = pathList.end();
if (pathList.empty())//1:pathlist is empty
{
iter = pathList.insert(iterEnd, pieceTemp);
}
else if (iterIn == iterEnd)//2:add pathpiece to the tail of pathlist
{
iter = pathList.insert(iterEnd, pieceTemp);
}
else//3:repair cracked piece, should arase old piece
{
iter = pathList.insert(iterIn, pieceTemp);
//!!!!!remember delete old piece!!!
}
//set point vv property
setUsed(vv, true);
setPathPieceOne(vv, iter);
while (uu != firstNode)//deal with all points in searched path except firstnode
{
if (getProperty(uu) != POINT_VPRIME)//uu is ordinary v point
{
//set piece
(*iter).getLinkNodesVector().push_back(uu);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
//set node of graph
setUsed(uu, true);
setPathPieceOne(uu, iter);//for v point,only have piece one
}
else//uu is vprime point
{
#ifdef DEBUG
assert(getProperty(uu) == POINT_VPRIME);
#endif
//get current piece's first and second node
curFirstNode = uu;
//set piece
(*iter).setFirstNode(curFirstNode);
(*iter).setLastNode(curLastNode);
(*iter).setLinkProperty(LINKED);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
(*iter).setWeight(weight);
//set point
setUsed(uu, true);
setPathPieceTwo(uu, iter);//for vprime point,set piece two
setPathPieceOne(curLastNode, iter);//for last vprime point,set piece one
//add new path piece,last piece setted over
weight = 0;
iter = pathList.insert(iter, pieceTemp);
//get current piece's first and second node
curLastNode = curFirstNode;
}
//refresh uu and vv
vv = uu;
uu = piLocal[uu];
}
if (uu == firstNode)
{
//get current piece's first and second node
curFirstNode = uu;
//set piece
(*iter).setFirstNode(curFirstNode);
(*iter).setLastNode(curLastNode);
(*iter).setLinkProperty(LINKED);
//weight += graph.getEdge(uu, vv).getEdgeWeight();
weight = INVALID;
(*iter).setWeight(weight);
//set point
setUsed(uu, true);
setPathPieceTwo(uu, iter);//for vprime point,set piece two
setPathPieceOne(curLastNode, iter);
}
return iter;
}
//such as: a->b->c,a->b x c,a x b->c
//we should set b false,set points in pieces a->b,b->c false
//delete pieces a->b,b->c
list<PathPiece>::iterator Path::deleteOneVPrime(GraphVector &graph, int vPrimeId)
{
list<PathPiece>::iterator iter;
for (auto iterTemp = pathList.begin(); iterTemp != pathList.end(); ++iterTemp)
{
if ((*iterTemp).getLastNode() == vPrimeId)
{
//set v point property used false
setPieceNodeFalse(graph, iterTemp);
iter = iterTemp; ++iter;
setPieceNodeFalse(graph, iter);
//set vprime property used false
setUsed(vPrimeId, false);
//delete piece
iter = pathList.erase(iterTemp);
iter = pathList.erase(iter);
return iter;
}
}
#ifdef DEBUG
assert(0);//shouldn't happen
#endif
return iter;
}
int Path::printPath()
{
int weight = 0;
list<PathPiece>::iterator iter;
cout << endl;
cout << "the path is: ";