Prim's algorithm not working on a certain implementation - c++
I have two similar C++ implementations of Prim's algorithm for a minimum spanning tree, but one of them works in every situation, while the other fails in some. The difference is the method of comparison used within the priority queue.
This is the one that fails in a few situations (it uses a struct function in order to compare the cost of given arches):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>``
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue<int, vector <int>, comp> pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push(1);
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top();
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] >= c)
{
C[v] = c;
pq.push(v);
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
And this is the one that succeeds every time (it uses the greater within the priority queue and stores both the cost associated with a node and the node itself within the queue):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
typedef pair<int, int> iPair;
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] < C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue< pair<int, int>, vector <pair<int, int> > , greater<pair<int, int> > > pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push((make_pair(0, 1)));
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top().second;
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] > c)
{
C[v] = c;
pq.push(make_pair(C[v], v));
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
Why doesn't the first one work as intended every time and how would I go about changing it while still using the struct function and the code basis I already have? I tried a similar implementation to the one I use for Dijkstra's algorithm.
The program reads the number of nodes and arches and all the given arches from a file and it's supposed to show the minimum cost of the spanning tree, the number of arches and the arches in any order.
The examples I have that fail for the 1st implementation are of pretty abnormal sizes, I don't know if they will be of any help:
apm.in:
164 531
155 74 113
73 15 817
38 87 -153
92 100 699
125 114 -210
5 50 -29
1 4 335
11 39 846
112 59 -745
157 86 -146
92 47 397
146 48 -614
81 123 539
8 36 -799
39 17 175
55 36 -133
14 129 809
134 107 948
153 97 -428
144 73 -975
31 41 293
76 95 -550
111 96 -906
132 54 35
19 36 -627
142 31 -949
127 52 -363
44 17 -913
141 69 36
163 2 -610
17 12 -187
65 41 941
160 39 931
69 127 966
98 128 528
144 145 354
49 86 -201
7 102 569
113 31 -151
155 139 752
13 94 -659
140 69 672
32 8 -945
72 19 -592
104 68 -631
5 87 -760
37 58 -211
32 74 134
88 60 622
128 77 -480
4 20 151
38 66 823
133 41 886
128 58 -522
115 68 403
62 128 379
24 156 3
152 22 -630
126 160 322
61 16 852
57 69 -650
126 99 371
87 54 -659
101 93 -44
4 22 899
128 16 222
96 132 -523
67 74 993
143 110 -346
128 9 -892
146 86 -773
14 8 306
114 156 116
91 77 -581
44 95 978
89 38 609
147 139 -486
67 34 -952
119 36 829
44 90 653
1 164 -197
77 157 -273
7 130 606
3 143 701
37 91 363
44 18 -814
53 34 -170
124 39 -384
38 80 -51
72 4 30
133 59 -53
121 10 676
20 142 967
159 84 -456
118 110 75
68 75 828
154 91 -83
94 128 -740
22 72 269
1 81 -371
58 154 98
73 146 -240
11 6 -254
80 62 -70
72 156 611
119 118 990
109 23 -67
103 97 -281
6 135 388
116 159 -948
1 50 -551
31 60 -577
52 47 -514
164 91 562
113 112 178
45 106 -667
160 22 952
17 156 748
125 34 36
94 93 440
139 117 890
43 144 -179
91 60 346
85 96 853
118 156 -991
67 107 718
60 89 649
17 28 -105
64 47 666
26 136 -174
31 147 835
79 115 -235
106 66 693
8 74 107
162 136 712
83 116 506
82 139 489
110 114 -299
96 69 -94
19 129 -361
30 129 612
122 91 -571
62 2 -318
38 143 662
117 142 -34
91 124 385
112 50 -460
14 81 405
129 145 227
71 106 -774
27 15 -339
157 85 251
146 84 726
127 46 -711
95 134 175
134 51 103
31 154 661
95 150 56
164 27 -845
31 127 -452
46 29 -264
74 102 -477
72 39 320
89 23 -811
12 83 -672
68 136 194
82 90 -326
52 81 819
109 47 -204
148 24 870
146 141 148
83 158 -198
77 9 -351
64 25 575
114 28 -992
29 139 630
21 130 -979
153 104 611
80 162 217
155 74 768
2 51 -643
75 97 -706
124 62 -69
36 135 -181
100 43 -163
146 129 54
126 106 799
15 155 907
125 153 658
85 51 702
112 95 80
149 84 -782
46 105 859
69 73 280
98 150 11
60 93 -997
82 134 -786
20 40 -166
134 90 -810
93 76 893
100 51 -161
43 92 314
162 28 525
121 141 -953
30 58 991
97 100 -890
54 23 314
63 92 -480
97 155 -736
29 136 -817
64 86 -424
86 53 613
96 38 -4
57 65 -789
8 6 -586
18 145 -959
136 19 -497
74 86 -895
7 155 -281
43 6 -814
43 69 361
134 141 257
67 46 -326
111 73 -249
11 13 -581
98 143 -909
133 78 -551
83 25 325
93 162 -503
138 31 -386
161 92 135
84 79 -80
12 108 41
86 158 109
71 3 334
27 62 -517
59 15 613
46 63 -316
74 39 996
37 11 -438
38 102 369
15 129 313
131 6 -841
152 109 -529
124 53 878
35 78 -994
116 117 -312
47 109 -12
47 36 534
84 63 -626
152 142 667
87 79 -201
52 22 -518
99 103 -434
139 53 262
162 1 -524
120 69 836
131 93 368
155 135 5
133 117 -818
59 15 854
37 139 931
111 8 -15
92 58 411
126 19 789
111 12 763
66 147 605
20 142 -446
156 163 -566
137 36 -350
127 156 -307
49 22 376
124 24 -863
36 161 175
63 27 -687
5 29 -375
27 69 -260
24 149 447
141 117 32
58 64 239
87 149 -919
65 101 -807
127 117 237
145 44 -731
32 74 535
142 13 -869
49 5 315
152 105 -747
10 133 -232
69 98 -253
134 86 458
129 48 -322
121 132 46
64 15 -216
103 38 -530
63 17 -871
69 38 -577
55 106 141
111 90 583
138 100 -478
67 43 -608
141 88 921
45 55 596
155 91 338
128 3 532
28 124 -839
134 101 240
36 41 -888
3 156 -139
79 151 446
41 150 978
160 3 70
5 45 -597
89 103 -849
123 56 -820
16 123 779
114 89 -534
160 147 -359
52 140 239
142 80 66
124 66 -18
89 23 945
162 108 -353
151 143 -912
68 121 656
23 122 730
17 8 -81
87 130 44
116 158 -234
16 123 430
66 130 -398
29 65 -844
105 43 -854
84 99 -730
107 162 729
128 153 175
146 13 538
135 43 -753
93 123 996
23 3 -436
45 133 -203
151 132 931
49 159 -358
147 120 -274
132 131 -192
94 141 -758
19 153 946
155 33 284
85 18 646
69 148 -720
142 125 965
80 63 -96
29 140 -129
116 50 -111
38 124 -750
156 102 -674
39 67 -459
50 150 -261
110 29 904
11 83 -520
58 65 449
34 144 -362
103 76 567
97 85 322
151 76 118
61 58 -636
7 143 535
61 26 40
10 57 -155
120 33 -871
28 53 -176
57 86 -602
161 92 -96
69 151 555
49 17 876
43 71 -91
47 118 -191
70 49 576
102 139 -920
60 153 873
80 124 222
30 20 -147
37 158 587
65 9 314
46 69 957
117 16 -831
74 106 -756
95 92 -222
52 147 310
2 61 -427
138 21 -256
113 94 273
162 105 -53
40 80 572
21 143 386
154 115 229
97 126 362
106 40 -164
60 100 -405
29 109 -506
133 35 -867
114 70 -169
132 46 -145
161 133 434
94 47 -939
86 91 543
110 155 -277
20 162 -683
32 145 304
135 124 -23
5 123 283
11 65 -858
31 128 223
54 97 585
8 93 688
91 58 -974
123 97 931
140 61 822
19 13 69
161 162 976
126 115 887
47 84 -502
12 157 -824
135 98 420
114 6 -605
66 138 -354
70 138 -596
56 87 145
37 159 -207
78 136 -573
49 57 271
154 14 859
103 139 -848
32 105 -927
163 126 -935
49 19 262
14 108 408
164 145 -612
37 135 -454
68 20 892
78 164 470
24 109 -598
70 53 -403
71 148 -983
92 46 851
26 34 -602
88 47 -667
66 59 750
142 99 319
43 57 967
109 17 654
85 108 -947
74 33 694
150 83 -845
129 7 -37
65 70 -668
4 102 771
158 69 -615
85 148 225
12 55 172
7 41 -83
123 103 937
110 137 -36
42 131 -392
153 18 -844
86 94 -404
77 44 -307
18 114 -372
75 115 99
53 4 -472
140 159 -844
46 67 -991
84 162 599
91 106 406
149 108 -811
142 22 -653
136 44 496
131 43 -2
122 37 375
103 109 -692
26 58 -822
84 128 -967
71 53 -162
74 18 -123
145 103 722
65 99 -961
56 15 -908
44 147 176
41 133 -145
96 63 915
35 20 -106
122 73 -845
11 150 917
5 105 -361
119 44 -261
27 76 215
129 11 -102
157 140 338
160 120 958
28 131 -194
36 106 -468
130 52 679
16 76 -514
151 156 236
2 137 -335
112 80 724
35 78 -535
44 121 119
62 59 -379
110 141 780
52 37 342
29 33 -49
89 7 828
87 131 171
44 130 419
86 45 438
103 128 558
24 129 -565
86 99 -758
51 110 -571
117 94 476
61 78 -875
127 101 -359
146 40 -871
87 48 -600
97 149 -165
3 58 507
88 37 -764
51 43 918
41 76 10
13 88 -835
apm.ok:
-105854
163
60 93
35 78
114 28
46 67
118 156
71 148
21 130
144 73
91 58
84 128
65 99
18 145
121 141
67 34
142 31
116 159
85 108
32 8
94 47
163 126
32 105
102 139
87 149
44 17
151 143
98 143
56 15
111 96
74 86
128 9
97 100
36 41
61 78
120 33
63 17
146 40
142 13
133 35
124 24
11 65
105 43
89 103
103 139
150 83
164 27
122 73
140 159
153 18
29 65
131 6
28 124
13 88
117 16
12 157
26 58
123 56
133 117
29 136
43 6
44 18
89 23
149 108
134 90
65 101
8 36
57 65
82 134
149 84
71 106
146 86
88 37
5 87
94 141
86 99
74 106
135 43
38 124
152 105
112 59
94 128
97 155
84 99
69 148
127 46
75 97
103 109
63 27
20 162
156 102
12 83
65 70
45 106
88 47
87 54
142 22
2 51
61 58
104 68
152 22
19 36
84 63
158 69
146 48
163 2
67 43
114 6
26 34
24 109
70 138
72 19
91 77
31 60
122 91
51 110
156 163
24 129
1 50
76 95
162 1
96 132
11 83
52 22
27 62
16 76
93 162
147 139
63 92
138 100
53 4
112 50
39 67
159 84
23 3
64 86
70 53
66 130
42 131
1 81
160 147
49 159
66 138
137 36
143 110
27 15
7 155
147 120
119 44
111 73
79 115
10 133
125 114
87 79
113 31
30 20
55 36
80 63
161 92
154 91
134 51
68 136
14 8
83 25
67 107
apm.out:
-105439
163
2 163
3 23
4 53
5 87
6 43
7 155
8 32
9 128
10 133
11 13
12 83
13 142
14 8
15 27
16 117
17 63
18 44
19 36
20 162
21 130
22 142
23 89
24 124
25 83
26 58
27 63
28 114
29 65
30 20
31 60
32 105
33 120
34 67
35 78
36 8
37 88
38 69
39 67
40 146
41 36
42 131
43 105
44 17
45 106
46 67
47 88
48 146
49 159
50 1
51 2
52 22
53 70
54 87
55 36
56 15
57 65
58 61
59 112
60 93
61 78
62 27
63 84
64 86
65 11
66 138
67 43
68 136
69 148
70 65
71 106
72 19
73 122
74 86
75 97
76 16
77 91
78 136
79 87
80 63
81 1
82 134
83 11
84 128
85 108
86 99
87 149
88 13
89 114
90 134
91 58
92 63
93 162
94 47
95 76
96 111
97 100
98 143
99 65
100 138
101 65
102 139
103 89
104 68
105 152
106 74
107 67
108 149
109 103
110 51
111 73
112 50
113 31
114 6
115 79
116 159
117 133
118 156
119 44
120 147
121 141
122 91
123 56
124 28
125 114
126 163
127 46
128 94
129 24
130 66
131 6
132 96
133 35
134 51
135 43
136 29
137 36
138 70
139 103
140 159
141 94
142 31
143 110
144 73
145 18
146 86
147 139
148 71
149 84
150 83
151 143
152 22
153 18
154 91
155 97
156 102
157 12
158 69
159 84
160 147
161 92
162 1
163 156
164 27
Every times you pop a node u from priority_queue, you need to update C[v] with v is adjacent node with u. These v may be already in the queue, so technically, you indirectly update the value of some elements in priority_queue (since value of an element is expressed via C[]). It will break the structure of the queue and lead to incorrect answer.
In the correct version, you push 2 int to queue, and these values never change. Therefore you do not get the above problem.
Looking through the exposed sample codes, I got a suspicion about an essential difference of comparison behaviors. To prove that suspicion, I put both versions of the sample codes into WinMerge and compared them. They look really similar. So, I'm quite sure I'm on the right track:
The first version uses this for comparison:
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
The second version uses this instead:
std::greater<pair<int, int> >
The big difference: std::greater considers both members of std::pair in comparison but the comparison function in first sample only C[x] and C[y] (but not x and y).
According to cppreference.com
template< class T1, class T2 >
constexpr bool operator>(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
is defined as rhs < lhs and
template< class T1, class T2 >
constexpr bool operator<(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
does
If lhs.first<rhs.first, returns true. Otherwise, if rhs.first<lhs.first, returns false. Otherwise, if lhs.second<rhs.second, returns true. Otherwise, returns false.
So, if the first comparison shall resemble the second, the comparison functor of first sample has to be changed to e.g.:
struct comp{
bool operator()(int x, int y)
{
return x == y ? C[x] > C[y] : x > y;
}
};
That looks a bit stupid as x == y surely results in C[x] == C[y]. However, it should return the exact same results as the std::greater<std::pair<int, int> > for the second sample code.
Concerning the original problem of the OP, the first version may actually be the correct one (and there is yet another mistake or a wrong expectation about output). In this case, the comparison of second sample had to be adjusted, e.g. introducing a resp. functor for std::pair:
struct comp{
bool operator()(
const std::pair<int, int> &xCx, const std::pair<int, int> &yCy) const
{
return xCx.second > yCy.second;
}
};
As I do not know Prim's algorithm, I cannot decide which one is the actually correct solution. The OP might leave a note regarding this (and I'm would be glad to edit this in).
Related
Why CGAL isotropic_remeshing generates self-intersections?
It's my first time using CGAL and I'm trying to use CGAL isotropic_remeshing following CGAL guide and examples. typedef CGAL::Simple_cartesian<double> geometric_kernel; typedef CGAL::Surface_mesh<geometric_kernel::Point_3> triangle_mesh; typedef boost::graph_traits<triangle_mesh>::edge_descriptor edge_descriptor; typedef boost::graph_traits<triangle_mesh>::halfedge_descriptor halfedge_descriptor; struct halfedge2edge { halfedge2edge(const triangle_mesh& m, std::vector<edge_descriptor>& edges) : m_mesh(m), m_edges(edges) {} void operator()(const halfedge_descriptor& h) const { m_edges.push_back(edge(h, m_mesh)); } const triangle_mesh& m_mesh; std::vector<edge_descriptor>& m_edges; }; void remesh(std::string fname) { fmt::print("reading input file {}\n", fname); std::ifstream input(fname); bool b = CGAL::IO::read_PLY(input, _mesh); input.close(); if (!b) throw std::runtime_error("cannot read input file"); // this returns false (initial mesh is ok) fmt::print("Self-intersaction: {}", CGAL::Polygon_mesh_processing::does_self_intersect(faces(_mesh), _mesh) ? "YES\n" : "NO\n"); std::vector<edge_descriptor> border; PMP::border_halfedges(faces(_mesh), _mesh, boost::make_function_output_iterator(halfedge2edge(_mesh, border))); split_long_edges(border, 0.05, _mesh); CGAL::Polygon_mesh_processing::isotropic_remeshing(faces(_mesh), 0.05, _mesh, CGAL::Polygon_mesh_processing::parameters::number_of_iterations(3).protect_constraints(true)); // this returns true ... fmt::print("Self-intersaction: {}", CGAL::Polygon_mesh_processing::does_self_intersect(faces(_mesh), _mesh) ? "YES\n" : "NO\n"); } and this is the PLY file: ply format ascii 1.0 element vertex 240 property double x property double y property double z element face 416 property list uchar int vertex_indices end_header -0.0677552 1.75428e-11 0.359551 0.559233 -5.07748e-11 -0.00371597 -0.422648 -0.0809302 0.0767055 -0.422648 0.0809302 0.0767055 0.353976 -0.31933 -0.0764848 0.353976 0.31933 -0.0764848 0.18628 -0.105913 0.136968 0.18628 0.105913 0.136968 0.0734964 -0.042885 0.171005 0.0734964 0.042885 0.171005 0.000894007 -0.101015 0.219853 0.000894007 0.101015 0.219853 -0.317115 -0.490302 -0.14088 -0.317115 0.490302 -0.14088 0.0363557 -0.18484 0.0595056 0.0363557 0.18484 0.0595056 0.679924 -0.284836 -0.185795 0.679924 0.284836 -0.185795 -0.902296 1.60765e-11 -0.0147902 -0.334173 -0.308304 -0.0317029 -0.226906 -0.388018 -0.0664415 0.238526 -0.495303 -0.175104 0.682336 -0.249117 -0.140847 -0.527435 -0.227159 0.105247 -0.915072 -0.285449 -0.0849493 -0.588108 -0.384526 0.0274976 -0.414261 -0.558083 -0.173693 -0.709628 -0.546197 -0.209069 -0.279147 -0.551078 -0.191796 -0.441742 -0.0761666 0.0919278 0.0750773 -0.548552 -0.169267 0.151201 -0.0861054 0.142655 -0.328035 -0.0785272 0.130721 -0.188634 -0.160355 0.0781907 -0.319349 -0.157333 -0.00956133 -0.695544 -0.0781028 0.0827349 -0.207819 -0.26498 -0.0189174 -0.186662 -0.497279 -0.102856 -0.0315025 -0.481533 -0.0632797 -0.101123 -0.565625 -0.138967 -0.954181 -0.0987909 -0.0469328 -0.448314 -0.217296 0.00954906 -0.855668 -0.454036 -0.168054 -0.685034 -0.225247 0.0590258 -0.19149 -0.085893 0.23609 -0.346289 -0.446491 -0.0910963 -0.0677861 -0.240685 0.03329 -0.570304 -0.563494 -0.181845 -0.0991308 -0.373187 -0.0199879 0.0256409 -0.0662504 0.220869 0.12104 -0.443556 -0.0725518 -0.559714 -0.0601683 0.126809 0.7294 -0.0778938 -0.0619168 -0.485049 -0.500588 -0.0492544 0.0544319 -0.163186 0.072704 -0.659303 -0.466641 -0.0854264 -0.769765 -0.338041 -0.0415794 -0.0697669 -0.153351 0.179993 0.0177175 -0.341852 0.0051975 0.142156 -0.249748 0.0257109 0.395681 -0.491973 -0.246075 0.585311 -0.0507501 -0.00507005 0.579522 -0.360325 -0.214198 0.375654 -0.398838 -0.122424 -0.0771929 -0.0805348 0.321587 -0.83454 -0.127215 0.00225332 -0.445651 -0.371942 0.010079 0.584187 -0.166126 -0.0560823 0.491718 -0.270257 -0.0989297 -0.334173 0.308304 -0.0317029 -0.226906 0.388018 -0.0664415 0.238526 0.495303 -0.175104 0.682336 0.249117 -0.140847 -0.527435 0.227159 0.105247 -0.915072 0.285449 -0.0849493 -0.588108 0.384526 0.0274976 -0.414261 0.558083 -0.173693 -0.709628 0.546197 -0.209069 -0.279147 0.551078 -0.191796 -0.441742 0.0761666 0.0919278 0.0750773 0.548552 -0.169267 0.151201 0.0861054 0.142655 -0.328035 0.0785272 0.130721 -0.188634 0.160355 0.0781907 -0.319349 0.157333 -0.00956133 -0.695544 0.0781028 0.0827349 -0.207819 0.26498 -0.0189174 -0.186662 0.497279 -0.102856 -0.0315025 0.481533 -0.0632797 -0.101123 0.565625 -0.138967 -0.954181 0.0987909 -0.0469328 -0.448314 0.217296 0.00954906 -0.855668 0.454036 -0.168054 -0.685034 0.225247 0.0590258 -0.19149 0.085893 0.23609 -0.346289 0.446491 -0.0910963 -0.0677861 0.240685 0.03329 -0.570304 0.563494 -0.181845 -0.0991308 0.373187 -0.0199879 0.0256409 0.0662504 0.220869 0.12104 0.443556 -0.0725518 -0.559714 0.0601683 0.126809 0.7294 0.0778938 -0.0619168 -0.485049 0.500588 -0.0492544 0.0544319 0.163186 0.072704 -0.659303 0.466641 -0.0854264 -0.769765 0.338041 -0.0415794 -0.0697669 0.153351 0.179993 0.0177175 0.341852 0.0051975 0.142156 0.249748 0.0257109 0.395681 0.491973 -0.246075 0.585311 0.0507501 -0.00507006 0.579522 0.360325 -0.214198 0.375654 0.398838 -0.122424 -0.0771929 0.0805348 0.321587 -0.83454 0.127215 0.00225332 -0.445651 0.371942 0.010079 0.584187 0.166126 -0.0560823 0.491718 0.270257 -0.0989297 0.244787 0.44166 -0.124611 0.224863 0.422926 -0.103295 0.203555 0.403002 -0.0851674 0.181818 0.382468 -0.0666096 0.160161 0.361466 -0.0494549 0.138703 0.340243 -0.0343855 0.116587 0.315975 -0.0211481 0.0930881 0.292328 -0.010644 0.0683716 0.270062 -0.00112955 0.0437934 0.247639 0.00896137 0.0147427 0.229286 0.0184032 0.244787 -0.44166 -0.124611 0.224863 -0.422926 -0.103295 0.203555 -0.403002 -0.0851674 0.181818 -0.382468 -0.0666096 0.160161 -0.361466 -0.0494549 0.138703 -0.340243 -0.0343855 0.116587 -0.315975 -0.0211481 0.0930881 -0.292328 -0.010644 0.0683716 -0.270062 -0.00112955 0.0437934 -0.247639 0.00896137 0.0147427 -0.229286 0.0184032 0.353943 0.318951 -0.0768402 0.318176 0.310251 -0.0529674 0.28752 0.294134 -0.0220852 0.263653 0.270201 0.00932505 0.248486 0.239857 0.0406095 0.237599 0.20638 0.0706802 0.228386 0.170062 0.097771 0.220138 0.131333 0.121548 0.221814 0.0891843 0.140093 0.237727 0.0468576 0.144191 0.240837 -4.4941e-11 0.144112 0.353943 -0.318951 -0.0768402 0.318176 -0.310251 -0.0529674 0.28752 -0.294134 -0.0220852 0.263653 -0.270201 0.00932505 0.248486 -0.239857 0.0406095 0.237599 -0.20638 0.0706802 0.228386 -0.170062 0.097771 0.220138 -0.131333 0.121548 0.221814 -0.0891843 0.140093 0.237727 -0.0468576 0.144191 0.334994 0.290335 -0.0602502 0.324017 0.260279 -0.0388522 0.31833 0.233087 -0.011531 0.312891 0.206844 0.0165917 0.306376 0.17827 0.0421742 0.301065 0.146996 0.0646615 0.297445 0.113434 0.0839683 0.295181 0.07813 0.10025 0.293328 0.0406645 0.110471 0.290482 2.27582e-11 0.115974 0.334994 -0.290335 -0.0602502 0.324017 -0.260279 -0.0388522 0.31833 -0.233087 -0.011531 0.312891 -0.206844 0.0165917 0.306376 -0.17827 0.0421742 0.301065 -0.146996 0.0646615 0.297445 -0.113434 0.0839683 0.295181 -0.07813 0.10025 0.293328 -0.0406645 0.110471 0.347914 0.280814 -0.0613852 0.33858 0.251578 -0.0407904 0.333526 0.224505 -0.0155032 0.330611 0.199101 0.010933 0.326488 0.172047 0.0361431 0.321941 0.142269 0.0582371 0.318458 0.109138 0.0750352 0.316637 0.0738964 0.0871513 0.31877 0.0372044 0.0929536 0.318747 -5.36519e-12 0.0939851 0.347914 -0.280814 -0.0613852 0.33858 -0.251578 -0.0407904 0.333526 -0.224505 -0.0155032 0.330611 -0.199101 0.010933 0.326488 -0.172047 0.0361431 0.321941 -0.142269 0.0582371 0.318458 -0.109138 0.0750352 0.316637 -0.0738964 0.0871513 0.31877 -0.0372044 0.0929536 0.383912 0.288495 -0.0737723 0.401167 0.260743 -0.0567028 0.416481 0.23323 -0.0356612 0.427658 0.205483 -0.0128446 0.433461 0.176582 0.0104455 0.435967 0.146037 0.0318464 0.436745 0.113498 0.0486202 0.434922 0.0783314 0.0602004 0.432704 0.0414137 0.0672302 0.439201 4.6519e-12 0.0584096 0.383912 -0.288495 -0.0737723 0.401167 -0.260743 -0.0567028 0.416481 -0.23323 -0.0356612 0.427658 -0.205483 -0.0128446 0.433461 -0.176582 0.0104455 0.435967 -0.146037 0.0318464 0.436745 -0.113498 0.0486202 0.434922 -0.0783314 0.0602004 0.432704 -0.0414137 0.0672302 -0.399426 0.601789 -0.253601 -0.434361 0.571336 -0.18351 -0.469967 0.544585 -0.115531 -0.50467 0.50818 -0.052334 -0.524224 0.45265 0.00308497 -0.529609 0.384371 0.046803 -0.53244 0.309845 0.0804658 -0.54185 0.233414 0.106686 -0.558825 0.155064 0.120583 -0.554692 0.074207 0.125803 -0.526166 3.22982e-11 0.133583 -0.399426 -0.601789 -0.253601 -0.434361 -0.571336 -0.18351 -0.469967 -0.544585 -0.115531 -0.50467 -0.50818 -0.052334 -0.524224 -0.45265 0.00308497 -0.529609 -0.384371 0.046803 -0.53244 -0.309845 0.0804658 -0.54185 -0.233414 0.106686 -0.558825 -0.155064 0.120583 -0.554692 -0.074207 0.125803 3 221 76 220 3 131 21 130 3 33 46 57 3 2 32 82 3 82 94 83 3 37 20 45 3 235 234 66 3 2 3 79 3 82 32 94 3 39 30 38 3 12 37 45 3 50 21 131 3 189 190 208 3 2 41 34 3 13 76 103 3 77 97 219 3 94 0 114 3 124 109 143 3 86 84 83 3 47 27 230 3 234 53 45 3 107 94 114 3 26 12 53 3 18 40 65 3 27 47 55 3 37 39 38 3 25 55 234 3 20 19 45 3 237 23 238 3 160 6 159 3 36 20 48 3 40 18 90 3 74 115 106 3 35 18 65 3 56 25 43 3 227 3 91 3 18 35 85 3 19 20 36 3 32 2 34 3 229 29 79 3 3 82 84 3 3 2 82 3 13 95 87 3 46 36 48 3 21 30 60 3 36 46 33 3 44 57 64 3 30 21 50 3 140 14 57 3 210 62 68 3 132 131 63 3 59 14 139 3 54 59 158 3 57 14 54 3 3 84 91 3 223 75 224 3 149 81 150 3 64 54 10 3 215 214 67 3 104 81 147 3 158 6 31 3 18 85 115 3 85 93 115 3 75 225 224 3 115 93 106 3 106 75 105 3 84 69 91 3 80 88 100 3 69 70 95 3 87 70 98 3 69 86 70 3 15 109 128 3 95 70 87 3 96 107 129 3 100 124 123 3 13 87 78 3 80 89 88 3 71 80 100 3 80 71 110 3 206 207 111 3 61 52 111 3 141 5 142 3 118 17 112 3 24 42 56 3 55 25 56 3 232 26 53 3 42 27 56 3 27 55 56 3 40 24 65 3 43 35 65 3 37 12 28 3 230 26 231 3 47 230 231 3 231 26 232 3 47 231 232 3 55 47 233 3 47 232 233 3 12 26 28 3 26 230 28 3 233 232 53 3 53 12 45 3 55 233 234 3 233 53 234 3 25 235 236 3 45 19 66 3 28 230 39 3 37 28 39 3 25 234 235 3 234 45 66 3 235 66 236 3 43 25 236 3 85 35 51 3 23 41 238 3 66 19 236 3 236 19 41 3 236 23 237 3 43 236 237 3 23 236 41 3 239 238 29 3 32 33 44 3 38 50 58 3 24 56 65 3 56 43 65 3 90 18 115 3 74 90 115 3 35 43 238 3 43 237 238 3 238 41 29 3 35 238 239 3 51 35 239 3 41 2 29 3 51 239 29 3 29 2 79 3 41 19 34 3 19 36 34 3 34 36 33 3 32 34 33 3 51 29 229 3 85 51 101 3 101 51 229 3 101 229 79 3 93 85 227 3 85 101 228 3 227 85 228 3 228 101 79 3 3 227 79 3 227 228 79 3 86 69 84 3 84 82 83 3 107 114 11 3 83 94 107 3 38 30 50 3 130 60 63 3 50 131 132 3 154 133 153 3 133 132 153 3 59 134 154 3 63 60 62 3 21 60 130 3 20 37 48 3 37 38 48 3 48 38 58 3 46 48 58 3 46 140 57 3 138 59 139 3 139 14 140 3 58 138 139 3 46 58 140 3 58 139 140 3 134 133 154 3 59 138 137 3 134 59 135 3 50 132 133 3 135 59 136 3 50 133 134 3 58 50 135 3 50 134 135 3 136 59 137 3 58 135 136 3 138 58 137 3 58 136 137 3 195 194 213 3 152 4 210 3 4 62 210 3 59 154 155 3 4 152 153 3 59 155 156 3 155 154 172 3 131 130 63 3 211 210 68 3 4 63 62 3 63 4 153 3 132 63 153 3 153 152 172 3 154 153 172 3 156 155 173 3 155 172 173 3 156 173 174 3 192 191 210 3 191 152 210 3 157 174 175 3 156 174 157 3 62 16 68 3 22 52 67 3 193 192 211 3 192 210 211 3 193 211 212 3 194 212 213 3 211 68 212 3 68 16 67 3 0 94 44 3 94 32 44 3 0 44 64 3 33 57 44 3 0 64 49 3 14 59 54 3 64 57 54 3 6 160 31 3 158 157 175 3 59 156 157 3 10 54 31 3 59 157 158 3 6 158 159 3 161 180 151 3 31 161 151 3 54 158 31 3 49 10 31 3 8 0 49 3 64 10 49 3 0 8 9 3 8 49 31 3 108 88 98 3 86 83 96 3 99 9 81 3 31 160 161 3 109 15 104 3 114 0 99 3 0 9 99 3 11 99 81 3 114 99 11 3 15 107 104 3 107 11 104 3 104 11 81 3 81 7 147 3 8 31 151 3 149 7 81 3 7 149 148 3 146 109 147 3 160 159 178 3 193 212 194 3 158 175 176 3 159 158 177 3 158 176 177 3 16 22 67 3 213 68 214 3 159 177 178 3 212 68 213 3 214 68 67 3 196 195 214 3 195 213 214 3 196 214 215 3 150 81 151 3 198 197 216 3 196 215 197 3 161 160 179 3 160 178 179 3 9 8 151 3 161 179 180 3 197 215 216 3 216 215 67 3 81 9 151 3 1 61 111 3 150 151 170 3 199 198 217 3 198 216 217 3 218 217 61 3 199 217 218 3 217 216 67 3 67 52 61 3 1 218 61 3 217 67 61 3 218 1 209 3 170 151 171 3 151 180 171 3 150 170 169 3 148 149 168 3 149 150 169 3 199 218 190 3 190 218 209 3 208 1 111 3 208 190 209 3 1 208 209 3 167 148 168 3 168 149 169 3 186 187 205 3 147 167 166 3 189 208 207 3 188 189 207 3 187 188 206 3 204 117 118 3 165 147 166 3 205 187 206 3 205 117 204 3 206 188 207 3 207 208 111 3 111 52 102 3 205 206 117 3 206 111 117 3 117 111 102 3 97 221 220 3 93 75 106 3 106 77 92 3 74 106 92 3 97 77 105 3 77 106 105 3 226 73 225 3 91 69 225 3 93 227 226 3 227 73 226 3 73 227 91 3 73 91 225 3 75 93 225 3 93 226 225 3 105 75 223 3 105 223 222 3 224 225 116 3 225 69 116 3 95 13 103 3 223 224 116 3 95 223 116 3 69 95 116 3 223 95 103 3 97 105 222 3 103 76 221 3 221 97 222 3 103 221 222 3 223 103 222 3 88 89 87 3 88 87 98 3 76 13 78 3 87 89 78 3 219 97 220 3 76 219 220 3 219 76 78 3 89 219 78 3 128 96 129 3 83 107 96 3 109 145 144 3 128 109 127 3 107 15 129 3 15 128 129 3 70 86 98 3 96 128 108 3 108 128 127 3 143 109 144 3 145 109 146 3 109 104 147 3 7 148 147 3 145 146 164 3 148 167 147 3 146 147 165 3 127 109 126 3 108 127 126 3 124 108 125 3 109 124 125 3 125 108 126 3 109 125 126 3 119 120 113 3 110 71 119 3 163 145 164 3 144 145 163 3 86 96 98 3 96 108 98 3 108 124 100 3 88 108 100 3 122 143 142 3 124 143 123 3 121 100 122 3 121 122 142 3 122 100 123 3 143 122 123 3 71 100 119 3 100 121 120 3 119 100 120 3 110 119 113 3 182 201 200 3 143 144 163 3 182 183 201 3 164 146 165 3 186 205 204 3 183 184 202 3 185 186 204 3 184 185 203 3 5 141 200 3 143 163 162 3 201 183 202 3 200 201 118 3 202 184 203 3 117 17 118 3 203 185 204 3 203 204 118 3 142 5 113 3 120 121 113 3 121 142 113 3 142 143 162 3 141 142 162 3 113 5 112 3 181 182 200 3 141 181 200 3 202 203 118 3 201 202 118 3 17 117 72 3 117 102 72 3 5 200 112 3 200 118 112 3 110 113 112 As my program reads the PLY file containing the mesh (which is stored in _mesh and does not self-intersect), isotropic remeshing is done but, after that, CGAL does_self_intersect returns true. Is it normal? If not, What did I do wrong?
I noticed that I was using Simple_cartesian kernel, so I changed it with Exact_predicates_inexact_constructions_kernel. Now, with those same parameters (target_edge_length = 0.05, nb_iter = 3) CGAL::Polygon_mesh_processing::does_self_intersect returns False
Question about tabling and declaring functions in main
the problem is in the int main() #include <iostream> #include <iomanip> #include <fstream> #include <cstring> using namespace std; const int R = 15; const int C = 20; void clearArray(int arr[R][C]) { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { arr[i][j] = 0; } } } void makeSpiral(int arr[R][C],int r , int c) { int i, a = 0, b = 0; int value = 1; while (a < r && b < c) { for (i = a; i < r; ++i) { arr[i][b] = value++; } b++; for (i = b; i < c; ++i) { arr[r - 1][i] = value++; } r--; if (a < r) { for (i = r - 1; i >= a; --i) { arr[i][c-1] = value++; } c--; } if (b < c) { for (i = c - 1; i >= b; --i) { arr[a][i] = value++; } a++; } } } void printSpiral(int arr[R][C],int r , int c, char str[]) { str[30]; ofstream fout; fout.open(str, ios::out | ios::app); if (!fout) { cout << "Error: file could not be opened" << endl; exit(1); } fout << endl; fout << "---------------------------------------------"; for (int i = 0; i < r; i++) { fout << endl; for (int j = 0; j < c; j++) { fout <<setw(5)<< arr[i][j] << " "; } } fout.close(); } int main(int argc, char *argv[]) { char str[30]; ofstream fout; int r,c; int i,j,t; int arr[R][C]; if (argc < 2) { cout << "Error missing file name!" << endl; return -1; } strcpy(str, argv[1]); int matrix [R][C] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}}; for (i = 0 ; i < 8 ; i++) { clearArray(matrix); makeSpiral(matrix,R,C); printSpiral (matrix,R,C,str); } return 0; } I'm a new C++ programmer please go easy on me. So i have this piece of code, the professor requires me to store corner sizes of a spiral matrix into a table, then use a loop to call out my functions so i don't have to call my functions repeatedly like this: clearArray(matrix); makeSpiral(matrix,1,1); printSpiral (matrix,1,1,str); clearArray(matrix); makeSpiral(matrix,2,2); printSpiral (matrix,2,2,str); clearArray(matrix); makeSpiral(matrix,3,3); printSpiral (matrix,3,3,str); clearArray(matrix); makeSpiral(matrix,4,4); printSpiral (matrix,4,4,str); I want to print a counter-clockwise matrix 8 times which with corner 1 by 1 ; 2 by 2; 3 by 3; 4 by 4; 5 by 5; 4 by 7; 7 by 4; and 15 by 20. My code doesn't give me the correct output, can you tell me where I did wrong, thank you! The output that i supposed to have --------------------------------- 1 --------------------------------- 1 3 --------------------------------- 3 2 --------------------------------- 1 4 2 3 --------------------------------- 1 8 7 2 9 6 3 4 5 --------------------------------- 1 12 11 10 2 13 16 9 3 14 15 8 4 5 6 7 --------------------------------- 1 16 15 14 13 2 17 24 23 12 3 18 25 22 11 4 19 20 21 10 5 6 7 8 9 --------------------------------- 1 18 17 16 15 14 13 2 19 28 27 26 25 12 3 20 21 22 23 24 11 4 5 6 7 8 9 10 --------------------------------- 1 18 17 16 2 19 28 15 3 20 27 14 4 21 26 13 5 22 25 12 6 23 24 11 7 8 9 10 --------------------------------- 1 20 19 18 17 16 15 14 2 21 32 31 30 29 28 13 3 22 23 24 25 26 27 12 4 5 6 7 8 9 10 11 --------------------------------- 1 16 15 14 2 17 24 13 3 18 23 12 4 19 22 11 5 20 21 10 6 7 8 9 What my code output looks like: --------------------------------------------------------- 1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47 3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46 4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45 5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44 6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43 7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42 8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41 9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40 10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39 11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38 12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37 13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36 14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ------------------------------------------------------------ 1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47 3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46 4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45 5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44 6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43 7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42 8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41 9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40 10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39 11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38 12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37 13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36 14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 --------------------------------------------------------- 1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47 3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46 4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45 5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44 6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43 7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42 8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41 9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40 10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39 11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38 12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37 13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36 14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 -------------------------------------------------------- ...... It prints the 15x20 corner size 8 times.
In your main function, arguments to makeSpiral are fixed R and C. This means makeSpiral always tries to fill a 15 rows, 20 columns spiral matrix to your matrix: makeSpiral(matrix,R,C); printSpiral(matrix,R,C,str); You may want this instead: int matrix[R][C]; // no need of initialization int matrixSize[8][2] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}}; for (i = 0 ; i < 8 ; i++) { clearArray(matrix, matrixSize[i][0], matrixSize[i][1]); makeSpiral(matrix, matrixSize[i][0], matrixSize[i][1]); printSpiral (matrix, matrixSize[i][0], matrixSize[i][1], str); } Here you use an assist matrixSize to store expected matrix sizes. Within the for loop makeSpiral will fill matrixSize[i][0] rows, matrixSize[i][1] columns only, so does printSpiral.
How to scan input from user if graph is given in form of adjancency list?
I have an input in format as shown in below Image.I am using the vector graph[200] to read input. Let's suppose program read first number of first row and column (as shown in Image) . Now I want to add all vertices which is connected to first node like graph[1].push_back(next integer in same row) and so on . But when should I stop reading input for particular Node(graph[i]). Because after reading first row I need to add vertices into another graph[i+1] to node's corresponding list . If you don't understand my questions plz have a look to my code. #include <bits/stdc++.h> using namespace std; vector<int> graph[201]; int main() { int n=2,i,node,temp; for(i=1;i<=n;i++) { cin>>node; while(scanf("%d",&temp)!=EOF/*What is the correct conditon to stop loop*/) { graph[node].push_back(temp); } } return 0; } Input format : 1 37 79 164 155 32 87 39 113 15 18 78 175 140 200 4 160 97 191 100 91 20 69 198 196 20 123 134 10 141 13 12 43 47 3 177 101 179 77 182 117 116 36 103 51 154 162 128 30 3 48 123 134 109 41 17 159 49 136 16 130 141 29 176 2 190 66 153 157 70 114 65 173 104 194 54 14 91 171 118 125 158 76 107 18 73 140 42 193 127 100 84 121 60 81 99 80 150 55 1 35 23 93 5 193 156 102 118 175 39 124 119 19 99 160 75 20 112 37 23 145 135 146 73 35 60 155 56 52 120 131 160 124 119 14 196 144 25 75 76 166 35 87 26 20 7 156 185 178 79 27 52 144 107 78 22 71 26 31 15 56 76 112 39 8 113 93 8 185 155 171 178 108 64 164 53 140 25 100 133 9 52 191 46 20 150 144 39 62 131 42 119 127 31 7 9 91 155 8 160 107 132 195 26 20 133 39 76 100 78 122 127 38 156 191 196 10 190 184 154 49 2 182 173 170 161 47 189 101 153 50 30 109 177 148 179 16 163 116 13 90 185 111 123 134 163 41 12 28 130 13 101 83 77 109 114 21 82 88 74 24 94 48 33 12 161 109 169 21 24 36 65 50 2 101 159 148 54 192 88 47 11 142 43 70 182 177 179 189 194 33 13 161 141 157 44 83 90 181 41 2 176 10 29 116 134 182 170 165 173 190 159 47 82 111 142 72 154 110 21 103 130 11 33 138 152 and so on... Here is the screenshot of my input format
Assuming you want to read line-by-line basis, you could do following: #include <bits/stdc++.h> using namespace std; vector<int> graph[201]; int main(){ string line; int n=2,i,node,temp; for(i=1;i<=n;i++){ getline(cin, line); istringstream in( line ); in>>node; while(in>>temp){ graph[node].push_back(temp); } } return 0; }
Pandas DataFrame: How to get a min value in a vectorized way?
I have a pandas dataframe: import numpy import pandas df1 = abs((pandas.DataFrame(numpy.random.randn(20, 8))*100).astype(int)) df1.columns = list('abcdefgh') df1.index = pandas.date_range('1/1/2014', periods=20) How would I create a new column that will give me the minimum value of the first half of the current row and the last 3 values in the previous row? For example, the first five rows in the created column would be: Nan 12 4 14 21
Here is one way to do it. Basically, you need to first shift last three columns and then combine with the first 4 columns, and finally calculate the min. import numpy import pandas # your data # =================================== numpy.random.seed(0) df1 = abs((pandas.DataFrame(numpy.random.randn(20, 8))*100).astype(int)) df1.columns = list('abcdefgh') df1.index = pandas.date_range('1/1/2014', periods=20) # processing # =================================== df1['custom_min'] = pandas.concat([df1[df1.columns[:5]], df1[df1.columns[-3:]].shift(1)], axis=1).min(axis=1) print(df1) a b c d e f g h custom_min 2014-01-01 176 40 97 224 186 97 95 15 40 2014-01-02 10 41 14 145 76 12 44 33 10 2014-01-03 149 20 31 85 255 65 86 74 12 2014-01-04 226 145 4 18 153 146 15 37 4 2014-01-05 88 198 34 15 123 120 38 30 15 2014-01-06 104 142 170 195 50 43 125 77 30 2014-01-07 161 21 89 38 51 118 2 42 21 2014-01-08 6 30 63 36 67 35 81 172 2 2014-01-09 17 40 163 46 90 5 72 12 17 2014-01-10 113 123 40 68 87 57 31 5 5 2014-01-11 116 90 46 153 148 189 117 17 5 2014-01-12 107 105 40 122 20 97 35 70 17 2014-01-13 1 178 12 40 188 134 127 96 1 2014-01-14 117 194 41 74 192 148 186 90 41 2014-01-15 86 191 26 80 94 15 61 92 26 2014-01-16 37 109 29 132 69 14 43 184 15 2014-01-17 67 40 76 53 67 3 63 67 14 2014-01-18 57 20 39 109 149 43 16 63 3 2014-01-19 238 94 91 111 131 46 6 171 16 2014-01-20 74 82 9 66 112 107 114 43 6
how to print a really big datastructure in clojure?
When I try to print a really long array, it gets cut off at a certain length [-1 -40 -1 -32 0 16 74 70 73 70 0 1 1 0 0 1 0 1 0 0 -1 -37 0 67 0 8 6 6 7 6 5 8 7 7 7 9 9 8 10 12 20 13 12 11 11 12 25 18 19 15 20 29 26 31 30 29 26 28 28 32 36 46 39 32 34 44 35 28 28 40 55 41 44 48 49 52 52 52 31 39 57 61 56 50 60 46 51 52 50 -1 -37 0 67 1 9 9 9 12 11 12 ...] I would like it not to do that if I'm persisting a data structure to file. How can this be done?
The special variable *print-length* determines how much of a given structure is printed. Like any other dynamic var, you can use binding to set its value in a block. user> (binding [*print-length* 2] (prn (range 200))) (0 1 ...) nil user> (binding [*print-length* nil] (prn (range 200))) (0 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) nil