Is there a way to highlight the node in Memgraph - graph-databases

I have two questions. First, is there a way to highlight the node in Memgraph from the WHERE clause so I can find it in the graph?
Abd second, is there some documentation on how to (for example) highlight the shortest path between two subreddit nodes?
Thankful for any (beginner-friendly) learning resources.

You can find that part of the documentation here:
https://memgraph.com/docs/memgraph/next/reference-guide/graph-algorithms
To answer your questions:
-> Is there a way to highlight the node from the where clause so I can find it in the graph?
With match clause you can pass the value your node needs to have, without a need for WHERE clause, you can see more details here:
https://memgraph.com/docs/cypher-manual/clauses/match
Something like this (for movielens dataset, users are rating movies):
MATCH (u:User{name:"Brett"})-[rel *bfs]->(m:Movie)
UNWIND rel as r
RETURN u, r, m
-> There are sample codes in the documentation mentioned earlier, feel free to explore them for the usage of the shortest path.

Related

How do I find vertices without particular edges from a particular vertex in Gremlin?

I am using python to run my gremlin queries, for which I am still getting used to / learning. Let's say I have vertex labels A and B, and there can be an edge from A to B called abEdge. I can find B vertices that have such edges from a particular A vertex with:
g.V()\
.hasLabel("A")\
.has("uid", uid)\ # uid is from a variable
.outE("abEdge")\
.inV()\
.toList()
But how would I go about finding B vertices that have no such edges from a particular A vertex?
My instinct is to try the following:
# Find B edges without an incoming edge from a particular A vertex
gremlin.V()\
.hasLabel("B")\
.where(__.not_(inE("abEdge").from_(
V()\
.hasLabel("A")\
.has("uid", uid)
)))\
.next()
And that results in a bad query (Not sure where yet), I'm still reading up on Gremlin, but I am also under time contraints so here I am asking. If anyone can help but just using groovy syntax that is fine too as it's not too bad to convert between the two.
Any explanations would be helpful too as I am still learning this technology.
Update
I guess if this were a real-life question, and related to this website. We could ask (A=Person, B=Programming-Language, and abEdge=knows):
Which programming languages does particular person A not know?
It looks like you almost had the answer:
g.V().hasLabel("B"). \
filter(__.not_(__.in_('abEdge').has("A","uid",uid))). \
toList()
I prefer filter() when using just a single Traversal argument, but I think you could replace with where() if you liked.

Safe Datalog rules for the query

Consider the relations: edge(X,Y), Red(X,Y), Blue(X,Y). These relations are representing a graph whose edges can be colored red or blue(or no color).
provide safe datalog rules(with negation if necessary) for the following queries.
Q1. find the pairs of nodes X and Y where there is path (a sequence of linked edges) from X to Y ?
my attempt:- reachable(X,Y) :- edge(X,Y)
reachable(X,Y) :- edge(X,Z), reachable(Z,Y)
Q2. Find the pairs of nodes X and Y where there is path (a sequence of linked edges) of even length from X to Y with alternating red and blue colors?
my attempt:
I have made odd even datalog program and a red/blue program, but don't know how to combine the both to get even length alternate red/blue node
ODD(x,y):- edge(x,y)
ODD(x,y):- edge(x,z), EVEN(z,y)
EVEN(x,y):- edge(x,z), ODD(z,y).
path(X,Y) :- Red(X,Y)
path(X,Y) :- path(X,Z), Blue(Z,W), path(W,Y)
From your questions I get the impression that you're trying to answer these assignments without testing your potential solutions. That's a very difficult way to work. I would strongly suggest to make little examples to test whether your solution is correct and run it in a Datalog engine (easiest is something online like http://www.iris-reasoner.org/demo or https://developer.logicblox.com/playground/ ).
In this particular case, it is easy to see that if you have an edge("a", b"), edge("b", c") and edge("c", "d"), that your solution will not find a path from "a" to "d".
This query can only be solved with recursion. This path query is pretty basic recursion, search for ancestor or transitive closure.

Issue with OpenStreetMap administration area rendering

I imported latest OpenStreetMap data to local SQLServer db and generated "outer" shape for some administration area. Original area shape is available here:
http://www.openstreetmap.org/relation/2658573
What I received is below:
What I did is I took "outer" way definitions for this shape (mentioned above) in the order defined in the db and constructed POLYGON definition from points belonging to all the ways in the order defined in OpenStreetMap db.
To import data to SQLServer db I used OsmSharp.core nuget package.
The orientation of shape data is clockwise as per info on this page(see Naming and Orientation section)):
http://wiki.openstreetmap.org/wiki/Talk:Relation:multipolygon
My questions are:
Does anybody know why taking direct definitions for "ways"/lines describing some area shape doesn't work as it should?
Should I reject some points from line/way definitions?
Maybe I just missed something else...
Edit 1:
I did quick query to verify info from scai and it looks promising:
NodeNr column shows which point on the way/line definition I should take as the start/endpoint for polygon definition. WayNr shows which consecutive way shares point with which one.
Edit 2:
I tested data related to way definitions already and it looks the main issue is with order of nodes in the way definitions only. All the tested ways used all the nodes belonging to them (no any redundant points were there in case the way could cross some other way in the middle of that way).
Some sample data:
The definition of the way with nr 42 (WayNr = 42) shows that start point of this line (continuation of the way nr 41) is on position 44. It means point/node 44 in the definition of the way 42 has the same gps/coordinates as point on position 30 in the definition of way nr 41.
After taking such "reverted" lines in the correct order I received the shape shown below:
After adding an inner area (exclusion) it looks finally as the original one shape:
The other issue with OpenStreetMap data is it uses right-hand rule to define outer-ring of the shape (clockwise instead of counter-clockwise) so in case of SQLServer we have to use ReorientObject() method on Geography instance. To detect orientation of the shape you can use EnvelopeAngle() method on Geography object for instance.
Edit 3:
I received an answer on OSM forum the order of ways on relation members doesn't matter (we can't rely on it) so ways have to be reordered before any further processing yet...
There are already various similar questions here at stackoverflow and at gis.stackexchange.com.
Relations don't necessarily contain the ways in the correct order. Thus it will be necessary to order the ways yourself. Take a look at the ID of the last node of a way and search for the way who has the same ID for its start node. If you do this for all ways then the resulting geometry should be fine.

PROLOG / All paths in a directed graph with loops

I've got the following diagram given:
Diagram here
The first gateway/connector is an OR-gateway/connector (it has a circle in it). The gateway/connector with a 'x' in it is a XOR-gateway/connector.
An OR-gateway specifies that one or more of the available paths will be taken.
An XOR-gateway represents a decision to take exactly one path in the flow.
I need to transform this diagram to PROLOG in order to get all possible paths from node 1 to node 8 but I have problems to code the OR-gateway and to find all possible paths.
How can I transform this diagram easily to Prolog and how can i find all possible paths respecting the gateways between two nodes?
Thank you for answers in advance.
As you should know, a Prolog program is basically a set of rules. From your graph, each node could begin a rule where each directed edge gives an explicit rule. By encoding your graph as a set of rules, a query on what satisfies say, (1, X, 8), would give you every possible path, even infinitely.
Encoding the rules should be easy (basic Prolog). Maybe I'm not understanding the special functions behind the OR and XOR. Please explain more if this isn't as trivial as it seems.

How do spell checkers work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I need to implement a spell checker in C. Basically, I need all the standard operations... I need to be able to spell check a block of text, make word suggestions and dynamically add new words to the index.
I'd kind of like to write this myself, tho I really don't know where to begin.
Read up on Tree Traversal. The basic concept is as follows:
Read a dictionary file into memory (this file contains the entire list of correctly spelled words that are possible/common for a given language). You can download free dictionary files online, such as Oracle's example dictionary.
Parse this dictionary file into a search tree to make the actual text search as efficient as possible. I won't describe all of the dirty details of this type of tree structure, but the tree will be made up of nodes which have (up to) 26 links to child nodes (one for each letter), plus a flag to indicate wether or not the current node is the end of a valid word.
Loop through all of the words in your document, and check each one against the search tree. If you reach a node in the tree where the next letter in the word is not a valid child of the current node, the word is not in the dictionary. Also, if you reach the end of your word, and the "valid end of word" flag is not set on that node, the word is not in the dictionary.
If a word is not found in the dictionary, inform the user. At this stage, you can also suggest alternate spellings, but that gets a tad more complicated. You will have to loop through each character in the word, substituting alternate characters and test each of them against the search tree. There are probably more efficient algorithms for finding the recommended words, but I don't know what they are.
A really short example:
Dictionary:apex apple appoint appointed
Tree: (* indicates valid end of word)
update: Thank you to Curt Sampson for pointing out that this data structure is called a Patricia Tree
A -> P -> E -> X*
\\-> P -> L -> E*
\\-> O -> I -> N -> T* -> E -> D*
Document:apple appint ape
Results:
"apple" will be found in the tree, so it is considered correct.
"appint" will be flagged as incorrect. Traversing the tree, you will follow A -> P -> P, but the second P does not have an I child node, so the search fails.
"ape" will also fail, since the E node in A -> P -> E does not have the "valid end of word" flag set.
edit: For more details on spelling suggestions, look into Levenshtein Distance, which measures the smallest number of changes that must be made to convert one string into another. The best suggestions would be the dictionary words with the smallest Levenshtein Distance to the incorrectly spelled word.
Given you don't know where to begin, I'd suggest using an existing solution. See, for example, aspell
(GLPL licenced). If you really have to implement it yourself, please tell us why.
One should look at prefixes and suffixes.
suddenly = sudden + ly.
by removing ly's you can get away storing just the root word.
Likewise preallocate = pre + allocate.
And lovingly = love + ing + ly
gets a bit more complex, as the english rules for ing get invoked.
There is also the possibility of using some sort of hashing function to map a root word
into a specific bit is a large bit map, as a constant time method of determining if the root word is spelled correctly.
You can get even more complex by trying to provide an alternate list of possible correct spellings to a misspelled word. You might research the soundex algorithm to get some ideas.
I would advise prototyping with a small set of words. Do a lot of testing, then scale up.
It is a wonderful educational problem.
Splitting a word into root and suffix is knonw as the "Porter Stemming Algorithm" it's a good way of fitting an English ditionary into an amazingly small memory.
It's also useful for seach so "spell checker" will also find "spelling check" and "spell checking"
I've done this in class
You should consider python Natural Language Toolkit NLTK which is made specificaly to handle this.
It also allows to create text interpreters such as chatbots
The Open Office Spell checker Hunspell can be a good starting point. Here is the Homepage:
Hunspell at Sourceforge
E James gives a great answer for how to tell if a word is valid. It probably depends on the spell checker for how they determine likely misspellings.
One such method, and the one that I would use is the Levenshteinn String Similarity which looks at how many letters must be added, removed, or swaped in a word in order to make another word.
If you say spelled: Country as Contry. The levenshtein string similarity would be 1 since you have to only add 1 letter to transform contry into country.
You could then loop through all possible correct spellings of words (only 171,000 english words and 3000 of those account for 95% of text). Determine those with the lowest levenshtein string similarity value, and then return the top X words that are most similar to the misspelled word.
There's a great python package called Fuzzy Wuzzy which implements this efficiently and generates a % similarity between two words or sentences based on this formula.

Resources