I have a data frame with over 4000 pts. Each point has 2 ID columns that identify a hierarchical grouping system let's call them l_1 and l_2. l_1 indicates points that are grouped together. I want to make a convex hull for each of these groups and then measure the area for each convex hull polygon.
Then, I want to estimate the average convex hull area based on the second grouping ID named l_2. Ideally the outcome would be a data frame with a row for the average convex polygon area for each unique l_2 identifier.
So far I am trying to create a list of data frames based on the l_1 column. Something like:
areas <- lapply(df$l_1, function(x){
sfObs<-df %>% filter(l_1 == x) %>%
st_as_sf(., coords = c('x', 'y'), crs = 4326)
areas<-st_convex_hull(st_union(chk)) %>% st_area()
return(areas)
})
But I only get empty polygons so far. It works with a single value for x but when I run it as a list of values it spits out empty polygons.
After that, I would average convex polygon area by using group_by and summarise(mean()) as follows, employing the l_2 ID column.
df.areas<-do.call(areas, 'rbind') %>% cbind(unique(df$l_1), .)%>%
left_join(., df, by=l_1) %>%
group_by(l_2)%>%
summarise(aveArea=mean(area))
But as I can't get past the first bit so I am stuck. I would be grateful for any ideas on how to achieve the end goal, but please only using sf functions.
A subset of the data:
l_2 l_1 x y
1 17 149 151.8930 -23.42907
2 17 149 151.8815 -23.41670
3 17 149 151.8805 -23.42031
4 17 149 151.8532 -23.41637
5 17 149 151.8284 -23.41455
6 17 149 151.8212 -23.40360
7 17 149 151.8057 -23.39490
8 17 149 151.7897 -23.39090
9 17 149 151.8055 -23.40893
10 17 149 151.8041 -23.40735
11 17 149 151.7980 -23.41180
12 17 149 151.7958 -23.41051
13 17 149 151.8015 -23.40578
14 17 149 151.8023 -23.40141
15 17 149 151.7873 -23.39065
16 17 149 151.7690 -23.39123
17 17 149 151.7663 -23.38577
18 17 149 151.7654 -23.39139
19 17 151 151.8086 -23.44059
20 17 151 151.7972 -23.43462
21 17 151 151.8080 -23.43974
22 17 153 151.7794 -23.36882
23 17 153 151.7792 -23.34290
24 17 153 151.7802 -23.34012
25 17 157 151.7664 -23.37117
26 17 157 151.7783 -23.37342
27 17 157 151.7962 -23.36544
28 17 157 151.8079 -23.35681
29 17 157 151.8006 -23.35412
30 17 157 151.8030 -23.35334
31 17 157 151.8030 -23.36052
32 17 157 151.8075 -23.36844
33 17 157 151.8057 -23.37128
34 17 157 151.7990 -23.37499
35 17 157 151.7937 -23.37959
36 17 159 151.8643 -23.42937
37 17 159 151.8726 -23.41774
38 17 159 151.8905 -23.42103
39 17 159 151.9041 -23.43649
40 17 161 151.8440 -23.38699
41 17 161 151.8498 -23.37978
42 17 161 151.8499 -23.36631
43 17 161 151.8344 -23.33939
44 17 161 151.8332 -23.33175
45 17 161 151.8370 -23.33839
46 17 161 151.8384 -23.33640
47 17 161 151.8440 -23.33435
48 17 161 151.8317 -23.34718
49 17 161 151.8279 -23.34407
50 17 161 151.8310 -23.34102
51 17 161 151.8337 -23.34140
52 17 163 151.8272 -23.36147
53 17 163 151.8161 -23.35445
54 17 163 151.8159 -23.34914
55 17 163 151.8134 -23.33415
56 6 649 151.9532 -23.42466
57 6 649 151.9680 -23.42602
58 6 649 151.9744 -23.42791
59 6 649 151.9925 -23.42612
60 6 649 152.0139 -23.42027
61 6 649 152.0235 -23.41462
62 6 649 152.0243 -23.41289
63 6 649 152.0236 -23.40959
64 6 649 152.0268 -23.40911
65 6 649 152.0276 -23.40897
66 6 649 152.0259 -23.40767
67 6 651 151.8505 -23.44435
68 6 651 151.8516 -23.44453
69 6 651 151.8400 -23.44005
70 6 651 151.8260 -23.44468
71 6 651 151.8196 -23.44625
72 6 651 151.8213 -23.44360
73 6 651 151.8111 -23.42271
74 6 651 151.8220 -23.40930
75 6 651 151.8160 -23.42438
76 6 651 151.8115 -23.43400
77 6 651 151.8269 -23.44965
78 6 651 151.8485 -23.45157
79 6 651 151.8471 -23.45342
80 6 651 151.8506 -23.45705
81 6 651 151.8489 -23.45228
82 6 651 151.8562 -23.45304
83 6 651 151.8552 -23.45212
84 6 651 151.8579 -23.44707
85 6 651 151.8644 -23.44840
86 6 651 151.8667 -23.44603
87 6 651 151.8775 -23.44708
88 6 653 151.9705 -23.42842
89 6 653 151.9733 -23.42767
90 6 655 151.9024 -23.41702
91 6 655 151.9138 -23.40610
92 6 655 151.9095 -23.40876
93 6 655 151.9015 -23.39602
94 6 655 151.9252 -23.37706
95 6 655 151.9308 -23.37199
96 6 655 151.9307 -23.36946
97 6 655 151.9805 -23.39567
98 6 655 152.0065 -23.41577
99 6 655 152.0196 -23.41305
100 6 655 152.0211 -23.41244
101 6 655 152.0113 -23.41101
102 6 655 152.0142 -23.40985
103 6 655 152.0150 -23.40754
104 6 655 152.0041 -23.40394
105 8 669 151.8945 -23.64410
106 8 669 151.8890 -23.66261
107 8 669 151.9000 -23.66387
108 8 669 151.9067 -23.66830
109 8 669 151.9094 -23.68123
110 8 669 151.8967 -23.69244
111 8 669 151.9107 -23.69545
112 8 669 151.9192 -23.69091
113 8 669 151.9273 -23.68480
114 8 669 151.9409 -23.66136
115 8 669 151.9361 -23.66283
116 8 669 151.9396 -23.66090
117 8 669 151.9432 -23.65804
118 8 669 151.9488 -23.65748
119 8 669 151.9521 -23.65517
120 8 669 151.9595 -23.65920
121 8 669 151.9666 -23.66185
122 8 669 151.9724 -23.65896
123 8 669 151.9802 -23.65798
124 8 669 151.9735 -23.63510
125 8 669 151.9558 -23.61360
126 8 669 151.9589 -23.61100
127 8 669 151.9623 -23.60884
128 8 669 151.9645 -23.61030
129 8 669 151.9685 -23.61122
130 8 669 151.9681 -23.60686
131 8 669 151.9612 -23.60467
132 8 671 151.9500 -23.47789
133 8 671 151.9495 -23.47786
134 8 671 151.9456 -23.47541
135 8 671 151.9448 -23.47416
136 8 671 151.9606 -23.48151
137 8 671 151.9637 -23.47959
138 8 671 151.9766 -23.47657
139 8 673 151.9711 -23.53105
140 8 673 151.9903 -23.51980
141 8 673 152.0149 -23.52661
142 8 673 152.0172 -23.52828
143 8 673 152.0168 -23.53076
144 8 673 152.0146 -23.53149
145 8 673 152.0108 -23.53228
146 8 673 152.0114 -23.53236
147 8 673 152.0145 -23.53364
148 8 675 152.0148 -23.47530
149 8 675 152.0200 -23.46649
150 8 675 152.0185 -23.46562
151 8 675 152.0181 -23.44782
152 8 675 152.0190 -23.43633
153 8 675 152.0049 -23.41639
154 8 675 152.0067 -23.40699
155 8 675 152.0127 -23.41182
156 8 675 152.0138 -23.41197
157 8 675 152.0136 -23.40980
158 8 675 152.0183 -23.40843
159 8 675 152.0190 -23.40862
160 8 677 151.8494 -23.55435
161 8 677 151.8476 -23.54912
162 8 679 151.8122 -23.62238
163 8 679 151.8100 -23.61953
164 8 679 151.8074 -23.61739
165 8 679 151.8040 -23.61299
166 8 679 151.8101 -23.61499
167 8 679 151.8097 -23.61255
168 8 679 151.8049 -23.61203
169 8 679 151.8048 -23.60668
170 8 679 151.8048 -23.60774
171 8 679 151.8209 -23.61589
172 8 679 151.8223 -23.60883
173 8 679 151.8217 -23.61741
174 8 679 151.8229 -23.61998
175 8 679 151.8241 -23.62179
176 8 679 151.8394 -23.62616
177 8 679 151.8384 -23.62278
178 8 681 151.8474 -23.62581
179 8 681 151.8470 -23.62196
180 8 681 151.8505 -23.62026
181 8 681 151.8511 -23.61996
182 8 681 151.8506 -23.62811
183 8 681 151.8394 -23.65246
184 8 681 151.8179 -23.65648
185 8 681 151.8081 -23.65494
186 8 681 151.8008 -23.65538
187 8 681 151.8032 -23.64207
188 8 681 151.8129 -23.64435
189 8 681 151.8141 -23.64182
190 8 681 151.8167 -23.63823
I am working on a program where I need to read ints from a file into an array and use the array to do work. not super complicated and shouldtm take next to no time at all.
i am still a student learning c++ and i've exhausted all my options trying to get this to function as I understand it.
The text file has 200 lines each line contains a int followed by a '\n' newline character.
When I read the file in the main function into an int it works as expected. when I pass the ifstream by reference into the function that does the exact same for loop it breaks giving me an uninitialized in as the only value.
#include<iostream> // required
#include<fstream>
#include<random>
#include<string>
using namespace std; // using standard namespace as for this entire program
void readNumbers(ifstream&, int[], int); // passing a input filestream by reference, c style array, and an integer for the array length
int totalInts(ifstream&); // this will read how many lines the file has
int main()
{
ifstream is("numbers.txt"); // this is the text file that we are reading from
int ar[200]; // the array of length 200 that everything will be read into
int temp; // temporary holding for our ints
if (!is) // error checking
cout << "somethings <expletive deleted>";
else {
for (int i = 0; i < 200; i++) // iterating through the file 200 times and inputting the int into the temp holding, this is being used to test if this method works as expected.
{
is >> temp;
cout << i << ' ' << temp << '\n'; // prints it out for troubleshooting
}
// cout << totalInts(is);
readNumbers(is, ar, 200);
}
return 0;
}
int totalInts(ifstream& file)
{
string i;
int intNum;
intNum = 0;
while (getline(file, i))
{
intNum++;
}
return intNum;
}
void readNumbers(ifstream &reading, int papi[], int rayLength) // where we really want to read the file
{
int temp; // for temp holding while we troubleshoot
if (!reading)
cout << "somethings fucked";
else
{
for (int i = 0; i < rayLength;i++) // reads the file for as many time as we have space in the array.
{
reading >> temp; // reads into temp
cout << temp; // outputs for troubleshooting
}
}
}
the program outputs below
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 12832) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
as you can see the output for the first for loop is exactly as expected, however the function which has all the same functionality is not working and is showing an uninitialized int for temp.
this means that it is not reading from the ifstream which I passed to the function. so wrote another function that reads the file using a newly created ifstream as well as the passed ifstream and prints what is read from both streams.
totalInts(ifstream &file)
{
ifstream tester("numbers.txt");
string l,i;
int intNum;
intNum = 0;
for (int j = 0; j < 200; j++)
{
file >> l;
tester >> i;
intNum++;
cout << intNum << ' ' << i << ' ' << l << '\n';
}
file.close();
return intNum;
}
when this is used the output is as expected, so passing the ifstream as a param using reference is the issue, as when I make a new ifstream the program runs fine using that ifstream and reading the file as expected where as the passed ifstream shows no values read.
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
1 41
2 485
3 340
4 526
5 188
6 739
7 489
8 387
9 988
10 488
11 710
12 173
13 304
14 843
15 970
16 491
17 997
18 953
19 831
20 441
21 423
22 618
23 905
24 153
25 292
26 394
27 438
28 734
29 737
30 914
31 452
32 747
33 785
34 549
35 870
36 931
37 692
38 325
39 52
40 903
41 731
42 834
43 353
44 363
45 690
46 668
47 156
48 718
49 281
50 874
51 572
52 671
53 694
54 789
55 57
56 871
57 731
58 750
59 556
60 778
61 328
62 38
63 212
64 843
65 288
66 136
67 49
68 950
69 283
70 670
71 473
72 828
73 905
74 735
75 394
76 365
77 21
78 132
79 417
80 551
81 648
82 635
83 108
84 973
85 774
86 851
87 970
88 383
89 944
90 334
91 960
92 471
93 650
94 334
95 542
96 559
97 134
98 84
99 951
100 557
101 837
102 146
103 643
104 687
105 726
106 939
107 990
108 308
109 704
110 408
111 26
112 773
113 950
114 91
115 276
116 834
117 803
118 588
119 102
120 528
121 10
122 303
123 170
124 654
125 377
126 791
127 678
128 589
129 35
130 64
131 377
132 151
133 957
134 745
135 979
136 433
137 138
138 221
139 25
140 348
141 472
142 299
143 780
144 393
145 959
146 917
147 241
148 767
149 245
150 606
151 428
152 970
153 533
154 43
155 429
156 197
157 900
158 623
159 780
160 656
161 427
162 365
163 651
164 557
165 569
166 489
167 622
168 45
169 605
170 374
171 301
172 866
173 383
174 31
175 600
176 45
177 375
178 222
179 687
180 508
181 289
182 738
183 53
184 1
185 444
186 965
187 906
188 791
189 145
190 467
191 731
192 907
193 672
194 505
195 824
196 423
197 324
198 623
199 835
200 523
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 24600) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
If this was up to me I would just be passing the filename through to the functions as a string param and making new ifstreams in these functions to get the thing to work, however the requirement for this is to have ad least one function that passes an ifstream through reference and utilize it. however after lots of troubleshooting and reading stackexange posts on the topic I cannot get it to function as expected.
what am I doing wrong?
any help would be appreciated.
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).
I have file that looks like this
gene_id_100100 sp|Q53IZ1|ASDP_PSESP 35.81 148 90 2 13 158 6 150 6e-27 109 158 531
gene_id_100600 sp|Q49W80|Y1834_STAS1 31.31 99 63 2 1 95 279 376 7e-07 50.1 113 402
gene_id_100 sp|A7TSV7|PAN1_VANPO 36.36 44 24 1 41 80 879 922 1.9 32.3 154 1492
gene_id_10100 sp|P37348|YECE_ECOLI 32.77 177 104 6 3 172 2 170 2e-13 71.2 248 272
gene_id_101100 sp|B0U4U5|SURE_XYLFM 29.11 79 41 3 70 148 143 206 0.14 35.8 175 262
gene_id_101600 sp|Q5AWD4|BGLM_EMENI 35.90 39 25 0 21 59 506 544 4.9 30.4 129 772
gene_id_102100 sp|P20374|COX1_APILI 38.89 36 22 0 3 38 353 388 0.54 32.0 92 521
gene_id_102600 sp|Q46127|SYW_CLOLO 79.12 91 19 0 1 91 1 91 5e-44 150 92 341
gene_id_103100 sp|Q9UJX6|ANC2_HUMAN 53.57 28 13 0 11 38 608 635 2.1 28.9 42 822
gene_id_103600 sp|C1DA02|SYL_LARHH 35.59 59 30 2 88 138 382 440 4.6 30.8 140 866
gene_id_104100 sp|B8DHP2|PROB_LISMH 25.88 85 50 2 37 110 27 109 0.81 32.3 127 276
gene_id_105100 sp|A1ALU1|RL3_PELPD 31.88 69 42 2 14 77 42 110 2.2 31.6 166 209
gene_id_105600 sp|P59696|T200_SALTY 64.00 125 45 0 5 129 3 127 9e-58 182 129 152
gene_id_10600 sp|G3XDA3|CTPH_PSEAE 28.38 74 48 1 4 77 364 432 0.56 31.6 81 568
gene_id_106100 sp|P94369|YXLA_BACSU 35.00 100 56 3 25 120 270 364 4e-08 53.9 120 457
gene_id_106600 sp|P34706|SDC3_CAEEL 60.00 20 8 0 18 37 1027 1046 2.3 32.7 191 2150
Now, I need to extract the gene ID, which is the one between || in the second column. In other words, I need an output that looks like this:
Q53IZ1
Q49W80
A7TSV7
P37348
B0U4U5
Q5AWD4
P20374
Q46127
Q9UJX6
C1DA02
B8DHP2
A1ALU1
P59696
G3XDA3
P94369
P34706
I have been trying to do it using the following command:
awk '{for(i=1;i<=NF;++i){ if($i==/[A-Z][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]/){print $i} } }'
but it doesn't seem to work.
Pattern matching is not really necessary. I'd suggest
awk -F\| '{print $2}' filename
This splits the line into |-delimited fields and prints the second of them.
Alternatively,
cut -d\| -f 2 filename
achieves the same.
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