How to distinct cycles in path?

Hi everybody!

I'm trying to solve logistic task:
airports, named LAX, LOS and etc. - nodes
flights between aiports - relations with price

when I'm trying to find all paths between airports by query

MATCH path = (a:AIRPORT {IATA: 'LAX'})-[:FLIGHT*5]->(n:AIRPORT {IATA: 'LAS'}) 
RETURN path LIMIT 1 

as result I have

(:AIRPORT {IATA: "LAX"})-[:FLIGHT {price: 61.988831864862625}]->(:AIRPORT {IATA: "LAS"})-[:FLIGHT {price: 421.99489651923653}]->(:AIRPORT {IATA: "MIA"})-[:FLIGHT {price: 89.14919592013997}]->(:AIRPORT {IATA: "LAS"})-[:FLIGHT {price: 763.0517870927525}]->(:AIRPORT {IATA: "SEA"})-[:FLIGHT {price: 791.2622954267732}]->(:AIRPORT {IATA: "LAS"})

as you see on the way LAS-MIA-LAS - cycled way

how can I have path without cycles?

First, you need to allow for 1 hop path with [:FLIGHT*1..5] otherwise, the path made of just the direct flight will never be matched.

Avoiding cycle means avoiding any node to be several times in the path, so you can use apoc.coll.toSet that returns only unique elements of a list and filter path when the unique list of nodes in the path equals the list of nodes in the path

MATCH path = (a:AIRPORT {IATA: 'LAX'})-[:FLIGHT*1..5]->(n:AIRPORT {IATA: 'LAS'}) where apoc.coll.toSet(nodes(path))=nodes(path) RETURN [ x in nodes(path)|x.IATA ]

["LAX", "LAS"]

Alternatively, you can also sort paths by length and get the shortest
MATCH path = (a:AIRPORT {IATA: 'LAX'})-[:FLIGHT*1..5]->(n:AIRPORT {IATA: 'LAS'}) with path,length(path) as len order by len limit 1 RETURN [ x in nodes(path)|x.IATA ]
["LAX", "LAS"]