Neo4j finding shortest paths from all nodes, between all nodes - database

this is probably straightforward to most people here but I'm very new to neo4j. I am trying to do this problem where I need to match all genres in this movie database with all other genres and find the shortest path. I am given this skeleton of code:
match (g:Genre)
with g.genre as genre1
YOUR CODE GOES HERE
return distinct genre1, g2.genre as genre2, length(path)/2 as length
order by length desc, genre1, genre2;
It seems to imply all the code I need to write should go in that area, but I don't know what I need to put there? Do I need to put another match in there? All I really know is how do find the shortes path from one node to all the others:
match path = allshortestpaths(
(g1:GENRE{name:"Thriller"})-[Genre_in*]-(g2:GENRE)
)
but not from all nodes to all other nodes?

Well, that skeleton code is a bit weird anyhow. Why would you want to extract the
genre property when you need to find shortest paths between nodes.
There are a couple of approaches. I would use the following:
// Match all combination of genres
match (g1:Genre), (g2:Genre)
// remove duplicates
WHERE id(g1) < id(g2)
// find shortest path
MATCH path=((g1)-[*]-(g2))
// return results
RETURN g1.genre AS genre1, g2.genre AS genre2, length(path) AS length
ORDER BY length DESC, genre1, genre2
Why does the skeleton code divide the path by 2 I have no idea.

Related

How do I use the last value in an array as a path for .child() when retrieving a snapshot?

I'm new to Firebase and have a function that writes all of my event ID's to an array. I want to use the last value in that array (the last event ID) to lookup the children of that specific eventID.
I know how to get the last item in the array but how do I put that into my .child() path?
I tried the code below, but it doesn't seem to work. I'm guessing that because .child("(lastEvent)") isn't a valid path.
let lastEvent = eventIDArray.last
refHandle = ref.child("Bouts").child("\(lastEvent)")
How do I plug the lastEvent value in as my path? Or is that even possible? Again, total newbie- alternatives welcome.
Sorting and filtering data
you can use sorting and filtering function to get the item.
To get the last item you can write the query like this.**
let recentBoutsQuery = (ref?.child("Bouts").queryLimited(toLast: 1))!
This will return 1 entry from last of your database which the last entry.
You can learn more from the firebase documentation. Work with Lists of Data

How can I change an Element in an array, that contains a specific String?

As it is said in the title I could use some help regarding a problem I have with my App.
My App scans a Barcode and offers to safe this barcode in a table together with a second String that can be put in which describes the number of times you want to save this barcode.
The customInit of the cell displays this as the barcode on the left and a grey number on the right representing the count. All of this information is saved in an array of form list=["972537657, 12"; ...]
My last functionality I want to implement is to have a function check for a maybe already existing barcode in the table and if there is instead of inserting a new row with the same barcode and a different number I want my app to just add the number I put in , to the number of the already existing table element with this specific barcode.
My problem: The logic in this works fine ; if there is an already existing element execute this function if not just insert a new row with the input data.
But I Have no idea how to tell the app what to change and how I can access this element of the array.
Maybe someone has an idea (I can also add some of my code if someone would like to inspect my problem further)
First, find the index of the string your looking for, then remove it and insert it at the index.
let arr:Array = ["a","b","c"]
let indexOfA = arr.index(of: "a")
arr.remove(at: indexOfA)
arr.insert("YourString", at: indexOfA)
if you only care about the duplication, why don't use Set instead of Array.
var barcode:Set = ["972537657","972537651","972537653"]
let result = barcode.insert("972537651")
if result.inserted {
print("insert")
}else {
print("duplication")
}

VBA – Find object in array with specific property

Let's say I have an object called Card which has several properties like color, number, suit, etc. Then I store a bunch of Cards in an array called deck. Is there a way to search the deck for a Card with a specific property or set of properties?
Thanks!
Theoretical code for Card:
Public color As String
Public suit As String
Public number As Integer
Searching through data is a massive part of programming. There are countless ways to search depending on your needs. To do a basic linear search you could make a loop to the length of the array, and check each array element to see if it meets your criteria.
Since you haven't tried anything yet, or told us what you want to do with the data you find (or don't find) I'll give you some pseudo code to start you off:
for(counter integer that increments until it is equal to the length of the array)
{
if(array at position[counter] == the color/suit/number your looking for)
{
//do whatever
}
else
{
//do something else
}
}
You could make this into a function that takes input, and returns data, so you could call it with whatever data you want to find.
If this doesn't make sense to you, you will need to start by researching loops.

Solr find all ids that start with certain path

Have a number of id's that look like this:
/content/myProject/path1
/content/myProject/path1/page1
/content/myProject/path2
Now, I want to find all the children of path1, so I do /content/myProject/path1/*.
The problem is that I receive also /content/myProject/path2. How do I make a correct query ?
Thanks,
Peter
Sounds like you are using generic text tokenizer definition. You may want to look at the PathHierarchyTokinizer instead. It's designed to split at the path prefixes. And then, you will not need to do the * at the end.

Using A* search algorithm to solve 3x3 three-dimensional box puzzle?

I am working on a 3x3 three-dimensional box puzzle problem in my homework. I will code with C.
There are 26 boxes and at first, first place is empty. By sliding boxes I must arrange them in correct order. Red numbers shows correct order and 27th place must be empty at last. I do not want you to give me code; I searched in forums and it seems that I must use the A* search algorithm, but how?
Can you give me tips about how I can use the A* algorithm on this problem? What type of data structure should I use?
Define your problem as a states-graph:
G=(V,E) where V=S={(x_1,x_2,...,x_54) | all possible states the 3d board can be in} [each number is representing a single 'square' on the 3d board].
and define E={(v1,v2)| it is possible to move from state v1 to state v2 with a single step} an alternative definition [identical] for E is by using the function successors(v):
For each v in V: successors(v)={all possible boards you can get, with 1 step from v}
You will also need an admissible heuristic function, a pretty good one for this problem can be: h(state)=Sigma(manhattan_distance(x_i)) where i in range [1,54]) basically, it is the summation of manhattan distances for each number from its target.
Now, once we got this data, we can start running A* on the defined graph G, with the defined heuristic. And since our heuristic function is admissible [convince yourself why!], it is guaranteed that the solution A* finds will be optimal, due to admissibility and optimality of A*.
Finding the actual path: A* will end when you develop the target state. [x_i=i in the terms we used earlier]. You will find your path to it by stepping back from the target to the source, using the parent field in each node.
You know how graphs work and how A* finds shortest paths on them, right?
The basic idea is that each configuration of the puzzle can be considered a vertex in a graph and the edges represent the moves (by connecting the configurations before and after the move).
Finding a set of moves that leads from an original configuration to a desired one can be seen as a path finding problem.

Resources