I am new to Cypher, I would like to know whether the following case is possible in Neo4j Cypher:
When I want to query what the buses should be taken from Station 1 to Station 4, the output should be (which consists of least number of interchange):
Station 1 -> route 1 -> Station 3 -> route 3 -> station 4
Station 1 -> route 2 -> Station 3 -> route 3 -> station 4
But not every possible combinations:
Station 1 -> route 1 -> Station 2 -> route 1 -> Station 3 -> route 3 -> station 4
Station 1 -> route 1 -> Station 2 -> route 2 -> Station 3 -> route 3 -> station 4
Station 1 -> route 2 -> Station 2 -> route 1 -> Station 3 -> route 3 -> station 4
Station 1 -> route 2 -> Station 2 -> route 2 -> Station 3 -> route 3 -> station 4
Thanks!
This is difficult without conditional expressions (CASE/WHEN is now in 2.0). This is as close as I got in my few minutes of trying. You'd have to pull out the start nodes from the resulting relationship collection.
start st1=node:node_auto_index(name="station1"), st4=node:node_auto_index(name="station4")
match p=st1-[r*]->st4
with reduce(acc=[], route in rels(p):
case
when length(acc) > 0 and last(extract(a in acc: a.name)) = route.name then acc
else acc + route
end) as reducedRoutes
return reducedRoutes, length(reducedRoutes) as len
order by len;
http://console.neo4j.org/r/koe6fo
Related
I try to connect a few ESP8266 (STA-Mode) to one ESP8266 (AP Mode)
All of the ESP Stations connect well, but one will not connect.
The one can connect to other networks.
Debug Serial off well connecting ESPs:
15:03:31.355 -> fpm close 3
15:03:31.355 -> mode : sta(c8:c9:a3:57:c7:2e)
15:03:31.402 -> add if0
15:03:31.402 -> ..scandone
15:03:33.605 -> scandone
15:03:34.492 -> state: 0 -> 2 (b0)
15:03:34.492 -> state: 2 -> 3 (0)
15:03:34.492 -> state: 3 -> 5 (10)
15:03:34.492 -> add 0
15:03:34.492 -> aid 1
15:03:34.539 -> cnt
15:03:34.539 ->
15:03:34.539 -> connected with cardReaderConfig, channel 1
15:03:34.539 -> dhcp client start...
15:03:34.727 -> ip:10.10.10.101,mask:255.255.255.0,gw:10.10.10.1
15:03:34.727 -> WL-CONNECTED[HTTP] begin...
Debug Serial when I try to connect the one, which is failing:
15:28:39.458 -> fpm close 3
15:28:39.458 -> mode : sta(bc:ff:4d:f9:91:12)
15:28:39.458 -> add if0
15:28:39.458 -> ..scandone
15:28:43.785 -> scandone
15:28:43.785 -> no cardReaderConfig found, reconnect after 1s
15:28:43.926 -> reconnect
15:28:46.063 -> scandone
15:28:46.954 -> state: 0 -> 2 (b0)
15:28:46.954 -> state: 2 -> 3 (0)
15:28:46.954 -> state: 3 -> 5 (10)
15:28:46.954 -> add 0
15:28:46.954 -> aid 1
15:28:46.954 -> cnt
15:28:47.970 -> ..state: 5 -> 0 (0)
15:28:47.970 -> rm 0
15:28:50.176 -> scandone
15:28:52.353 -> scandone
15:28:52.353 -> no cardReaderConfig found, reconnect after 1s
15:28:52.446 -> reconnect
15:28:54.639 -> scandone
15:28:54.639 -> no cardReaderConfig found, reconnect after 1s
15:28:54.733 -> reconnect
15:28:56.136 -> ..scandone
15:28:58.347 -> scandone
15:29:00.531 -> scandone
15:29:00.531 -> no cardReaderConfig found, reconnect after 1s
15:29:00.612 -> reconnect
15:29:02.802 -> scandone
15:29:03.688 -> state: 0 -> 2 (b0)
15:29:03.688 -> state: 2 -> 3 (0)
15:29:03.688 -> state: 3 -> 5 (10)
15:29:03.688 -> add 0
15:29:03.688 -> aid 1
15:29:03.688 -> cnt
15:29:04.670 -> ..state: 5 -> 0 (0)
15:29:04.670 -> rm 0
When I try to connect to other networks, it works:
15:09:50.008 -> fpm close 3
15:09:50.008 -> mode : sta(bc:ff:4d:f9:91:12)
15:09:50.008 -> add if0
15:09:50.008 -> ..scandone
15:09:52.252 -> scandone
15:09:53.139 -> state: 0 -> 2 (b0)
15:09:53.139 -> state: 2 -> 3 (0)
15:09:53.139 -> state: 3 -> 5 (10)
15:09:53.139 -> add 0
15:09:53.139 -> aid 5
15:09:53.139 -> cnt
15:09:53.186 ->
15:09:53.186 -> connected with WLAN-AF, channel 6
15:09:53.186 -> dhcp client start...
15:09:53.232 -> ip:192.168.178.94,mask:255.255.255.0,gw:192.168.178.1
15:09:53.232 -> WL-CONNECTED[HTTP] begin...
Hope anyone can help, but assuming this is a hardware problem...
EDIT: I can connect via normal ESP8266Wifi (Wifi Client example script)
But get the error when trying via WifiMulti or Enterprise (BasicHttpClient example script).
The question states that we are given an empty list and 10^5 queries.For each query, we have two integers a and b.a can only be 1, 2 or 3.
if a=1, then append element b in the list.
if a=2, then add value b to every element of the list.
if a=3, then we have to print the bth smallest element in the list.
e.g. query = 5, list = []
q1 -> 1 2 -> list = [2]
q2 -> 1 4 -> list = [2,4]
q3 -> 2 5 -> list = [7,9]
q4 -> 3 1 -> print(7)
I have tried a brute force approach, which takes O(N logN) for each query. How can I improve this for large queries?
My brute force link
elif a==2:
for j in range(len(l)):
l[j]+=b
This step can be avoided and by doing the following
if a == 2
sum += b #value ToBe Added to every element at the end of all queries at once Last
l=[]
query =10**5
sum = 0
for _ in range(query):
if a==1:
l.append(b)
elif a==2:
sum += b
else:
l.sort()
print(l[b-1] + **sum**)
This can be further improved as below
l=[]
query =10**5
sum = 0
for _ in range(query):
if a==1:
// Place the element b in proper place O(n) operation increasing order
elif a==2:
sum += b
else:
print(l[b-1] + **sum**)
i want to return paths of size 0..4 starting from node a. but i want to return only the longest paths (skip subpaths). For graph:
a -> b - > c
|
| -> d
i'd like to return only a -> b -> c and a -> d but not a nor a -> b
edit
it means that if there is a path longer than 4, i still need the longest paths of size 0..4. so for:
a -> b -> c -> d -> e -> f
i'd like to get a -> b -> c -> d -> e
To get all paths up to length 4 that start at a root node and either end at a leaf node or have a length of 4:
MATCH path = (a)-[*0..4]->(b)
WHERE NOT ()-->(a) AND (LENGTH(path) = 4 OR NOT (b)-->())
RETURN path;
Reproducing with your sample data set:
CREATE (a:Node {name:"A"})-[:RELATION]->(b:Node {name:"B"})-[:RELATION]->(c:Node {name:"C"}),
(a)-[:RELATION]->(d:Node {name:"D"})
You can restrict the returned paths doing a WHERE this way:
match path = (a)-[*0..4]->(b)
where not ()-->(a) and not (b)-->()
return path
The output will be:
╒═══════════════════════════════════════════════════════════╕
│"path" │
╞═══════════════════════════════════════════════════════════╡
│[{"name":"A"},{},{"name":"B"},{"name":"B"},{},{"name":"C"}]│
├───────────────────────────────────────────────────────────┤
│[{"name":"A"},{},{"name":"D"}] │
└───────────────────────────────────────────────────────────┘
say I have a Frame, which looks like below,
" Name ID Amount
0 -> Joe 51 50
1 -> Tomas 52 100
2 -> Eve 65 20
3 -> Suzanne 67 10
4 -> Suassss 69 10
5 -> Suzanne 70 10
6 -> Suzanne 78 1
7 -> Suzanne 79 10
8 -> Suzanne 80 12
9 -> Suzanne 85 10
10 -> Suzanne 87 10
...
What I would like to achieve is to group or aggregate base on the ID column such that if a sequence of running number is encountered, those rows should be grouped together, otherwise, the row itself is a group.
I belive a recursive function is your friend here.
Feed a list of tuples
let data = [(Joe, 51, 50);
(Tomas, 52, 100);
(Eve, 65, 20);
(Suzanne, 67, 10)]
to a function
let groupBySequencialId list =
let rec group result acc data lastId =
match data with
| [] -> acc :: result
| (name, id, amount) :: tail ->
if lastId + 1 = id then
group result ((name, id, amount) :: acc) tail id
else
group (acc :: result) ([(name, id, amount)]) tail id
group [] [] data 0
and you'll get the result you are looking for.
This should get the job done save three caveats.
You need to parse your string into the tuples required
There's an empty list in the result set because the first recursion wont match and appends the empty accumulator to the result set
The list will come out be reversed
Also note that this is a highly specialized function.
If I was you, I'd try to make this more general, if you ever plan on reusing it.
Have fun.
Is there a way to do a match within a match in F#? I've noticed that you can do one tailed on another like so...
match L.Head with
| null -> []
| _ -> match N with
| 1 -> [L.Head]
| _ -> []
But is there a way to do it so that a match, ending with a _ can be placed in the MIDDLE of another match? This seems to give a bug... is there a better way to do this, should you need it for your logic? EX:
match A with
| 0 -> match B with
| 1 -> 1
| _ -> 0
| _ -> 2
Why not use match on a tuple -
match (A,B) with
|0,1 -> 1
|0,_ -> 0
|_, -> 2
Not sure whether it was possible 7 years ago, but now you can use struct to reduce allocations:
match struct (A, B) with
| 0, 1 -> 1
| 0, _ -> 0
| _ -> 2
Old answer creates instance of System.Tuple`2, while this creates instance of System.ValueTuple`2
Decompilation