Cody

Problem 45467. Find the fastest reaction chain to reach a target compound

Solution 2768953

Submitted on 2 Aug 2020 by Are Mjaavatten
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass

filetext = 'function y = reaction_chain(R,S,T) [~,y] = shortestpath(digraph(R(:,1),R(:,2),R(:,3)),S,T); y = round(y*100)/100; end %This code written by profile_id 3934907 '

2   Pass
R = [1 2 1.5; 1 3 2.5; 2 3 0.6; 3 4 4.1; 4 2 3.2]; assert(isequal(reaction_chain(R,1,4),6.20))

3   Pass
R = [3 4 9.6489;1 4 9.5717;2 4 1.4189;2 4 7.9221;4 3 0.3571; 4 3 3.9223]; assert(isequal(reaction_chain(R,1,3),9.93))

4   Pass
R = [2 3 3.1864;3 2 4.9359;1 2 7.7339;5 1 5.2448;2 5 1.3431; ... 4 1 4.8876;4 1 9.5712;4 1 2.6840;4 3 0.0273;5 4 1.7028; ... 5 4 5.2548;3 2 4.2046;3 4 8.6170;3 4 9.1101;2 1 3.3861]; assert(isequal(reaction_chain(R,3,4),7.25))

5   Pass
R = [1 4 8.1730;4 1 3.9978;2 4 4.3141;4 1 2.6380;4 3 3.5095; ... 3 2 0.7597;1 2 0.4965;2 1 7.8025;2 1 4.0391;4 3 0.5978; ... 1 2 8.2119;2 3 2.9632;3 1 6.8678;1 2 6.2562;4 1 9.2939; ... 4 2 4.3586;3 4 7.9483;3 2 8.1158;3 2 9.3900;4 3 6.2248]; assert(isequal(reaction_chain(R,3,1),4.80))

6   Pass
R = [6 1 4.8990;2 6 7.1269;4 3 0.5962;5 1 0.7145;4 1 8.1815]; assert(isequal(reaction_chain(R,5,5),0.00)) assert(isequal(reaction_chain(R,2,1),12.03)) assert(isequal(reaction_chain(R,2,3),Inf)) assert(isequal(reaction_chain(R,3,4),Inf))

7   Pass
R = [1 3 1.3056;1 3 6.0879;6 7 9.7350;6 5 0.4248;5 7 3.1795; ... 10 11 8.0540;11 9 9.7753;11 9 5.1325;14 15 7.6027;15 14 8.0163; ... 14 15 5.9045;17 1 1.4939;1 17 2.5989;1 17 6.2406;4 5 1.9871; ... 3 4 0.6724;3 5 8.6804;7 9 7.2118;8 7 8.1865;9 7 2.9695; ... 12 11 5.1967;11 12 4.1216;11 12 3.9005;16 17 9.2406;15 16 6.7641; ... 17 16 0.6583;3 2 5.0605;2 3 4.9252;1 3 6.1090;5 7 4.1419; ... 6 7 4.7752;7 5 7.2523;9 10 2.9741;11 9 0.1670;11 9 8.7837; ... 14 13 1.5026;13 15 3.3175;15 14 6.1016;18 1 6.7336;1 17 2.5181; ... 17 1 9.1524;4 3 6.0197;3 5 6.5784;4 3 3.0603;]; assert(isequal(reaction_chain(R,18,3),8.04)) assert(isequal(reaction_chain(R,13,12),50.1)) assert(isequal(reaction_chain(R,14,12),51.6))

8   Pass
R = [9 13 1.5437;8 4 7.5811;18 8 6.8554;6 11 8.3242;12 7 2.9923; ... 10 9 3.5961;12 15 4.2433;9 3 0.2443;6 7 6.5369;20 19 4.5789; ... 5 16 7.5933;14 10 2.1216;2 17 1.7501;4 14 8.9439;11 15 1.5359; ... 20 11 6.7973;1 17 7.4862;3 11 3.2583;11 8 4.1509;4 6 0.2054; ... 19 14 9.3261;4 19 7.9466;12 9 2.5761;16 5 0.6419;16 14 7.1521; ... 13 9 3.9076;17 7 8.1454;16 18 5.0564;13 20 4.4396;2 18 6.3119]; assert(isequal(reaction_chain(R,8,20),28.23)) assert(isequal(reaction_chain(R,2,13),36.95))

9   Pass
R = [9 20 9.8797;18 8 4.5474;5 16 8.8284;19 12 5.9887;3 18 4.5039; ... 5 18 7.6259;18 6 6.7323;14 3 4.0732;6 15 2.8338;18 17 3.9003; ... 10 14 8.3437;13 12 3.2604;10 15 8.8441;15 1 6.7478;17 7 2.4623; ... 7 8 5.4655;12 8 3.9813;11 14 9.5092;15 9 8.3187;3 2 0.8425; ... 4 7 3.0173;1 11 0.9537;3 13 8.5932]; assert(isequal(reaction_chain(R,20,12),Inf)) assert(isequal(reaction_chain(R,15,8),30.34))

10   Pass
R = [11 12 2.1328;12 3 0.5222;14 13 2.1966;9 13 5.5531;3 4 0.0100; ... 9 10 1.5987;14 1 1.1968;10 18 2.4288;1 8 9.0441;14 8 6.3195; ... 5 12 9.8173;17 6 6.8246;8 20 0.8399;6 17 0.8442;11 17 7.3882; ... 3 9 3.5038;10 12 1.4581;19 13 1.6294;12 19 7.8310;14 10 2.6032; ... 12 5 3.1930;19 18 7.9459;19 4 5.1754;13 19 6.6397;8 15 8.1763; ... 13 2 9.2236;2 11 1.1885;8 17 2.4410;18 15 3.7815;5 6 7.6724; ... 1 14 6.2028;15 20 3.8391;6 18 8.0610;10 2 5.6427;4 11 3.5503; ... 7 15 5.1577;16 5 6.7811;2 17 6.7857;19 2 9.0844;11 13 3.1607; ... 2 18 1.4453;8 13 9.9755]; assert(isequal(reaction_chain(R,11,20),17.81))

11   Pass
R = [3 1 7.1176;14 2 0.3902;9 10 5.1643;1 11 9.4602;14 2 3.5457; ... 4 9 6.1273;5 12 7.9564;12 6 0.5430;11 15 1.6248;2 14 4.8166; ... 4 1 6.0896;12 8 0.2775;15 8 3.3200;3 10 5.7513;12 3 3.5679; ... 3 13 3.3787;5 1 8.0191;8 9 8.7091;9 15 5.9602;13 15 8.8592; ... 4 1 4.5112;1 8 9.5120;4 6 4.3143;6 14 7.4084;12 15 5.1010; ... 12 7 8.4920;6 12 9.7644;8 7 2.0716;5 2 3.7521;5 6 8.1712; ... 2 10 3.4665;6 9 8.6394;3 11 9.0183;3 5 4.9652;14 8 2.7700; ... 1 11 5.0675;6 1 3.5858;6 1 3.7505;12 3 9.1222;12 2 9.5003; ... 3 5 6.8713;3 8 7.2133;14 11 7.4985;7 4 5.2085;4 13 6.6293]; assert(isequal(reaction_chain(R,13,12),33.54))

12   Pass
R = [7 13 9.2048;12 5 7.9682;3 8 6.1069;11 6 7.2868;14 1 1.3822; ... 13 7 4.1131;15 12 9.8100;4 2 3.8458;8 9 9.7663;8 7 9.9499; ... 4 10 9.6426;11 5 5.3113;1 14 4.0438;5 15 4.6065;5 2 5.8218; ... 3 2 5.8056;5 6 7.2482;13 6 9.6175;15 4 7.6824;10 14 6.0254; ... 11 12 3.8510;4 1 4.7212;10 5 5.1786;4 5 6.5047;14 13 2.0992; ... 6 14 2.5653;15 10 1.6535;13 10 5.4645;4 1 2.3338;6 10 9.8610; ... 4 12 8.8633;8 3 8.1092;8 2 8.7572;10 2 9.0844;11 6 5.0432; ... 12 1 7.2593;11 7 5.8229;6 3 3.9919;14 4 3.6101;5 2 5.1267; ... 13 14 7.2360;6 5 6.9171;8 2 2.5291;14 3 1.2135;9 5 3.8204; ... 12 13 6.8024;7 10 2.1408;10 11 6.0102;12 8 3.5462;12 4 8.4483]; assert(isequal(reaction_chain(R,1,12),16.52)) assert(isequal(reaction_chain(R,15,9),23.12)) assert(isequal(reaction_chain(R,9,15),8.43))

13   Pass
R = [14 10 9.0000;14 10 10.0000;4 13 10.0000;5 2 8.0000;2 11 5.0000; ... 10 3 6.0000;9 1 5.0000;13 2 5.0000;10 5 10.0000;10 3 6.0000; ... 13 8 8.0000;13 12 2.0000;1 9 7.0000;13 11 4.0000;7 4 8.0000; ... 2 11 9.0000;11 5 6.0000;7 2 9.0000;10 4 10.0000;3 5 5.0000; ... 4 3 8.0000;3 8 3.0000;7 13 3.0000;1 13 7.0000;14 6 7.0000; ... 6 1 6.0000;8 4 1.0000;12 1 4.0000;11 14 6.0000;10 14 6.0000; ... 6 10 5.0000;2 7 6.0000;8 7 1.0000;4 7 7.0000;10 14 10.0000; ... 2 14 2.0000;14 9 3.0000;1 5 9.0000;2 5 2.0000;3 1 8.0000]; assert(isequal(reaction_chain(R,8,9),14))

14   Pass
R = [1 2 5;1 2 9]; assert(isequal(reaction_chain(R,2,1),Inf))

15   Pass
R = [4 16 0.4237;2 9 0.0306;8 11 0.6388;1 13 0.1693;2 16 0.3843; ... 8 6 0.5554;6 7 0.3490;17 7 0.1930;17 6 0.5509;17 2 0.2577; ... 8 11 0.8995;4 18 0.4340;15 10 0.3313;8 13 0.9162;17 3 0.1199; ... 17 12 0.0403;10 17 0.3857;6 5 0.2009;7 11 0.2684;12 15 0.1040; ... 14 12 0.4747;17 2 0.5991;5 1 0.5799;16 11 0.8399;4 12 0.1740; ... 6 1 0.7015;18 14 0.7567;10 6 0.2449;6 18 0.2307;10 4 0.4340; ... 3 7 0.7936;15 17 0.5404;15 13 0.0432;3 5 0.2467;4 5 0.2755; ... 18 7 0.2973;8 6 0.7573;7 3 0.6172;16 9 0.0776;17 6 0.6139; ... 12 4 0.9600;12 10 0.8690;11 1 0.4827;15 14 0.5723;1 13 0.4494; ... 12 14 0.8047;1 15 0.5674;2 5 0.1335;11 10 0.0689;18 5 0.3155; ... 6 1 0.5279;5 8 0.9475;17 3 0.5919]; assert(isequal(reaction_chain(R,7,13),0.91)) assert(isequal(reaction_chain(R,1,18),1.37)) assert(isequal(reaction_chain(R,14,2),1.38))

16   Pass
R = [3 2 8.2070;2 1 1.0576;5 6 4.3201;5 6 1.1111;6 7 5.3338; ... 11 10 9.7877;10 11 5.9987;9 10 4.3743;14 13 2.7591;13 15 8.6333; ... 14 13 5.6640;17 18 1.6193;17 1 2.8767;17 18 6.9178;4 3 5.6304; ... 4 3 4.3412;5 4 0.5619;9 8 9.5380;9 7 0.0224;9 8 7.1412; ... 11 13 2.7473;11 12 0.6646;12 11 1.2049;17 15 8.9250;16 15 3.4592; ... 17 15 0.4951;1 2 4.0666;1 2 0.5611;2 3 6.7063;6 5 2.7088; ... 5 7 7.1288;5 6 0.6856;11 9 4.1500;11 10 5.9775;9 10 8.3965; ... 15 14 7.5966;14 15 4.1755;14 13 8.3002;1 18 5.0146;1 17 9.7139; ... 17 18 0.2792;3 5 5.4500;5 3 2.3200;5 3 8.7088;7 8 4.0699; ... 8 7 1.8611;9 8 7.8793;13 11 7.7952;11 13 5.4524;13 12 1.3119]; assert(isequal(reaction_chain(R,3,9),36.50))

17   Pass
R = [2 1 7.2341;3 1 1.9214;6 7 7.0439;6 7 4.1053;5 6 1.2955; ... 11 10 8.3926;10 11 9.0473;10 11 7.1775;14 15 7.2521;15 14 4.9151; ... 14 15 9.6543;17 19 2.7357;17 18 2.7711;17 18 8.5647;3 2 4.8920; ... 4 2 7.0194;2 3 8.9539;8 6 1.9255;6 8 1.1520;8 6 1.3625; ... 12 10 3.7379;11 12 4.7925;12 10 7.2198;16 14 6.2466;16 15 7.1463; ... 16 15 3.7445;1 18 6.6056;19 1 9.1260;19 1 3.0015;3 5 2.1327; ... 4 3 3.6967;3 5 0.7346;8 9 9.3318;8 7 4.9644;8 9 3.0559; ... 11 13 7.6445;11 12 1.6309;11 13 2.0184;17 15 7.9096;17 15 3.8180; ... 16 15 4.1780;2 19 1.3889;19 2 2.6529;1 19 9.4927;5 4 6.9571; ... 5 6 3.8858;4 5 8.8546]; assert(isequal(reaction_chain(R,4,14),Inf)) assert(isequal(reaction_chain(R,9,12),Inf))

18   Pass
R = [2 1 1.5592;1 2 2.4465;7 6 5.9819;7 6 1.9563;5 7 4.9169; ... 9 10 2.6206;9 10 3.6554;10 9 6.9576;2 1 1.5000;2 13 9.0677; ... 13 2 7.3477;4 5 8.9883;5 4 7.8082;6 5 0.7312;10 8 2.5875; ... 8 9 4.9516;10 9 3.9300;12 1 0.0830;1 12 9.7302;13 12 6.0841; ... 3 5 0.0807;3 5 5.3663;4 5 2.4256;7 9 0.1973;7 9 8.2272; ... 8 9 1.6038;11 12 8.2550;13 11 1.9458;13 11 1.3879;2 4 9.8468; ... 3 2 4.8237;2 3 5.5103;7 6 9.9712;7 8 5.0623;7 6 8.8766; ... 11 12 4.6054;10 12 1.0703;10 12 5.3461;3 2 5.4698;1 2 5.6282; ... 2 1 2.9354;7 6 1.5597;6 5 8.5908;5 7 7.9862;9 11 5.0525; ... 9 11 3.1038;11 10 2.6583]; assert(isequal(reaction_chain(R,10,5),9.19))

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!