# From graph to time series

Let's consider the time series:

seria={38, 30, 23, 23, 14, 27, 21, 37, 25, 10, 24, 3, 5, 2, 5, 4, 8, 6, 13, 14, 6, 11, 3, 3, 15, 8, 17, 31, 56, 28, 9, 9, 52, 43, 11, 4, 15, 10,7, 10, 7, 7, 10, 12, 35, 40, 13, 13, 5, 10}

fig1=ListPlot[seria -> Range[Length[seria]], Filling -> Axis]


We are looking for the visibility of each peak:


AbsoluteTiming[
lseria = Length[seria];
pary = {};
Do[
kmax = n;
q2 = seria[[kmax]];

If[IntegerQ[n/500] == True, Print[n]];

Do[
kmin = p;
q1 = seria[[kmin]];
q3 = Take[seria, {kmin + 1, kmax - 1}];
If[
kmin + 1 == kmax,
AppendTo[pary, {kmin, kmax}],
If[
Min[{q1, q2}] >= Max[q3],
AppendTo[pary, {kmin, kmax}],
If[
Max[{q1, q2}] >= Max[q3],
If[q1 < q2,
temp = {};

Table[
q5 = seria[[kmin + k]];
If[
q5 > q1,
AppendTo[temp, (q5 - q1)/k]], {k, 1, kmax - kmin - 1}
];

If[
Max[temp] < (q2 - q1)/(kmax - kmin),
AppendTo[pary, {kmin, kmax}]],
temp = {};

Clear[q5];
Table[
q5 = seria[[kmax - k]];
If[
q5 > q2,
AppendTo[temp, (q5 - q2)/k]], {k, 1, kmax - kmin - 1}
];

If[
Max[temp] < (q1 - q2)/(kmax - kmin),
AppendTo[pary, {kmin, kmax}]
]

]]]],
{p, 1, n - 1}], {n, 2, lseria}
(*Label["end1"]*)
];
]



Nest,

k = Sort[Join[pary, Map[({#[[2]], #[[1]]}) &, pary]]]


The set 'k' contains all the visibility of each peak e.g. peak 'no.2' see the {1, 3, 4, 6, 8} peaks - see fig.1.

On the basis of 'k' we can create a graph:

k1 = Map[(#[[1]] \[UndirectedEdge] #[[2]]) &, k];
graph = Graph[k1]


The question is: how to recreate the 'series' set on the basis of this graph (set 'k')? Of course, identical values will not be kept :)

What does 29 see? In the set 'k' there are values for 29: {29, 1}, {29, 8}, {29, 9}, {29, 11}, {29, 12}, {29, 13}, {29, 14}, {29, 15}, {29, 17}, {29, 19}, {29, 20}, {29, 21}, {29, 22}, {29, 23}, {29, 25} . {29, 26}, {29, 27}, {29, 28}, {29, 30}, {29, 31}, {29, 32}, {29, 33}, {29, 46}.

• I do not understand what exactly you are doing. However, k contains 50 different values and your original series only 30. Therefore you must give some criterion how to eliminate superfluous values. Oct 7, 2020 at 13:36
• The code looks hard to follow. What do you mean by visibility? Do you mean like a landscape / mountains? Why isn't 1 visible from 29? Oct 7, 2020 at 13:36
• ^ if so your code could be highly simplified to create the graph: lerp[list_] := Subdivide[First[list], Last[list], Length[list] - 1]; visible[heights_] := VectorLessEqual[{heights, lerp[heights]}]; graph = Graph[ If[visible[seria[[Span @@ #]]], UndirectedEdge @@ #, Nothing] & /@ Subsets[Range[Length[seria]], {2}]] In general it is not possible to recreate the original landscape based on the visibility graph. It is only possible to create a new landscape that shares the same graph. A trivial example shows that {1, 1000, 1} and {1,2,1} have the same graph. Oct 7, 2020 at 14:41
• Of course, I am aware that there are many scenarios. But what, for example, can arise? Oct 7, 2020 at 14:53
• Yes there is - it's the first element 1<->2. You don't need the edge 2<->1 because obviously visibility is undirected. In the graph I showed in my comment, if you add VertexLabels->Automatic, the edge 2<->1 is clearly there. Also what are you asking when you say But what, for example, can arise? ? I was saying you cannot reconstruct the original landscape from the graph. You need more information embedded in the graph, possibly using edge weights to encode relative distances. Oct 7, 2020 at 15:16