discontinuity  at  a  point  x  =  0,  the  specific  cost 
function c will be smooth, since its derivative has no 
discontinuity at a given point. 
Thus, to solve the trajectory optimization problem 
between two points A and B, it is necessary to find 
the  functions  x(t), y(t),  z(t)  satisfying  the following 
conditions: 
๐ถ๐๐ ๐ก(๐ฟ
๎ฎบ๎ฎป
)โ๐๐๐, 
๐ฅ(0) = ๐ฅ
๎ฎบ
,๐ฆ(0)=๐ฆ
๎ฎบ
,๐ง(0)=๐ง
๎ฎบ
, 
๐ฅ(1) = ๐ฅ
๎ฎป
,๐ฆ(1)=๐ฆ
๎ฎป
,๐ง(1)=๐ง
๎ฎป
. 
(7) 
In addition, the restrictions expressed in the codes 
of  practice  should  be  complied  with,  e.g.,  for  the 
railways  of  1520  mm  gauge  (PC  119.13330.2017), 
tunnels  (PC  122.13330.2012)  and  bridges  (PC 
46.13330.2012).  The  main  features  which  are  most 
affected by topography and which must be taken into 
account  when  tracing  a  railway  are  the  maximum 
track gradient,  the  minimum curvature radius  in  the 
plan and the minimum gradient in tunnels. 
Any discrete elevation matrix can be converted to 
analytical  form  by  interpolation,  e.g.,  by  Lagrange 
polynomials. At that, the functions with the number 
of parameters equal to the number of DEM elements 
will  be  analysed,  which  will  lead  to  high 
computational  complexity.  For  example,  the  DEM 
used below has the number of elements of the order 
of 65 million. It is worth noting that this method does 
not ensure that the railway requirements are met. If 
local  interpolation  is  used,  for  example  bicubic 
interpolation,  it  will  not  be  possible  to  apply 
variational calculus methods to the entire map set.  
Another  approach  is  to  represent  the  three-
dimensional space as a uniform graph with N levels 
of  discretization  along  each  of  the  axes.  Thus,  the 
number of vertices in this graph will be N
3
, and the 
number  of  edges  depends  on  the  connectivity.  For 
example,  a  Von  Neumann  neighbourhood  connects 
only cells that have a common side, while a Moore 
neighbourhood  also  connects  common  vertices. 
Applying graph theory and graph traversal methods, 
such as Dijkstra's algorithm, it is possible to obtain a 
path  of  minimum  length.  The  computational 
complexity of such an algorithm will be O(N
6
), which 
is  already  large  enough  for  a  number  of  sampling 
levels on the order of 1000, corresponding to a small 
DEM of 1MB size. The algorithm A* generally uses 
an exponential number of points relative to the path 
length, which is also not practical in this case. 
Nevertheless, it is not necessary to obtain a strictly 
optimal  solution  if  large  computational  power  is 
needed.  For  example,  if  a  trajectory  that  differs  by 
only  a  few  percent  from  the  optimal  cost  in  a 
sufficient amount of time is obtained, this would also 
be an acceptable result. 
One class of algorithms that have the property of 
quickly finding a solution, albeit not the most optimal 
one, but a good one, is heuristics. One of the heuristic 
methods  is  the  technique  of  gradual  optimization. 
Then a difficult optimization problem is solved first 
for a much-simplified problem, gradually increasing 
the complexity until the complexity equals the initial 
one. The intermediate results should be less and less 
different with  each  step:  when  this  change stops  or 
becomes  less  than  some  threshold,  the  heuristic 
algorithm stops. 
At step zero, the trajectory is an AB segment. To 
implement  the  iterative  steps  of  the  algorithm,  we 
should note the existence of invariants for passing any 
trajectory  from  A  to  B,  including  the  optimal 
trajectory. If we need to connect two points in three-
dimensional space with a curve, then it should in any 
case intersect some set of planes. One of such planes 
is the plane perpendicular to the given segment and 
passing through its middle. Assuming that points A 
and B are not above each other, which is adequate for 
the  problem,  then  such  a  plane  is  also  the  plane 
perpendicular  to  the  horizontal  plane  and  to  the 
segment AB in the plane plan. Correspondingly, we 
will select a new point exactly in this plane, using the 
parametric definition of the planes: 
๐ฅ
๎ฎผ
=
๐ฅ
๎ฎบ
+๐ฅ
๎ฎป
2
+
๐ฆ
๎ฎป
โ๐ฆ
๎ฎบ
2
โ
๐
๎ฌต
, 
๐ฆ
๎ฎผ
=
๐ฆ
๎ฎบ
+๐ฆ
๎ฎป
2
โ
๐ฅ
๎ฎป
โ๐ฅ
๎ฎบ
2
โ
๐
๎ฌต
, 
๐ง
๎ฎผ
=
๐ง
๎ฏ๎ฏ๎ฏ๎ฏ
+๐ง
๎ฏ๎ฏข๎ฏช
2
+
๐ง
๎ฏ๎ฏ๎ฏ๎ฏ
โ๐ง
๎ฏ๎ฏข๎ฏช
2
โ
๐
๎ฌถ
, 
(8) 
where  R
1
  and  R
2
  are  random  variables  having  a 
continuous  uniform  distribution  and  specifying  the 
variation,  z
high
 and z
low
  are  the  boundary  heights, 
which can be iteratively calculated as follows: 
๐ง
๎ฏ๎ฏ๎ฏ๎ฏ
=๐๐๐(๐ง
๎ฎบ
+๐
ั
โ
๐ด๐ถ,๐ง
๎ฎป
+๐
ั
โ
๐ต๐ถ,๐๐๐ฅ(๐ง
๎ฎบ
,๐ง
๎ฎป
)),
๐ง
๎ฏ๎ฏข๎ฏช
=๐๐๐ฅ(๐ง
๎ฎบ
โ๐
ั
โ
๐ด๐ถ,๐ง
๎ฎป
โ๐
ั
โ
๐ต๐ถ,๐๐๐(๐ง
๎ฎบ
,๐ง
๎ฎป
)), 
(9) 
where i
ั
 โ the value of the maximum gradient. First, 
the  heights  at  points  A  and  B  are  selected  as 
boundaries, then the boundary values are recalculated 
with the new value z
C
.  At  larger  distances,  the 
boundary  values  resulting  from  the  maximum 
gradient  are  chosen  more  often,  and  at  small 
distances,  the  elevation  values  at  points  A  and  B 
remain.  
To select the correct variation,  it is necessary to 
calculate the value  of  the  alignment  along the ACB 
polygon, which can be done in O(L
ACB
) operations. It