I need help with this data structure question.
each tree has this properties:
right
left
color
key
here are the details:
picture of example tree
https://i.gyazo.com/85a59c69301c214ddc03f2d54324ca6f.png
A good path is a path where parent and child don't have the same colors (for example good path is red-white-red-white or white-red-white-red)
you need to find the longest good path inside a given tree and print its length. (in this example tree output would be 5)
for example in this tree the longest path is
17->13->32->18->22
rules:
you can have other functions to assist you.
you may use fixed number of variables like x,y,z.
you cannot use additional data structures.
not even sure if its recursion or not.
To get you moving, let's look at the basic properties that can affect a legal tree:
leaf node (dead end; return length of 1)
both children have same color as parent (dead end at parent node, return 0)
one child has color different from parent (follow the one different branch, return that depth + 1)
both children have color different from parent (follow both, return left + 1 + right)
That should get you to form detailed pseudo-code for a solution.
Related
Iam nooby in godot, I have to use A* to traslate the player to the goal position, but I do not know how to start, pls help!! basically I have just 2 tiles in the tilemap, 1 of them is allowed to pass over it, I have to extract I guess the allowed tile and calculate the distance between the position player with the position goal, getting the real distance and then check cell per cell which has the lowest cost, but I do not know how to do that :c
func get_player_init_pos():
var pos = map_to_world(Vector2(54,1))pos.y += half_cell_size.y
return pos
func is_tile_vacant(pos, direction):
var curr_tile = world_to_map(pos)
var next_tile = get_cellv(curr_tile + direction)
var next_tile_pos = Vector2()
if(next_tile == 0):
next_tile_pos = map_to_world(curr_tile + direction)
else:next_tile_pos = pos
return next_tile_pos
I have this, the first part of the code is to locate the player in the map and the second is for check the tile walls in the map
You could roll your own path finding algorithm. However, there is little point in doing so, since Godot has a AStar class you can use. However, you probably don't need to use that either, because Godot has a navigation system. And that is what I'm going to suggest you to use.
First of all, you can specify both navigation and collision polygons on your tiles. You need to have the navigation polygons. Go ahead and do them.
Second you want to have a Navigation2D in the scene tree and have your TileMap as a child.
And third, you can ask the Navigation2D for a path with get_simple_path, you pass the start and end positions as arguments and you get an array of points that make up the path.
Since you mention A*, I'll briefly explain using the AStar too anyway.
First, you need to add the cells with add_point. It requires ids. It is a good idea to be clever with the ids so you can compute the id for a given position. For example x * width + y if you know the size.
So you can iterate over the tiles in your TileMap and call add_point for each one (You don't need to add cell that are not passable).
Then you need to specify the connections with connect_points (it takes the ids of the points as parameters).
And finally you can call get_point_path passing the start and end ids. Again it gives you a array of points.
given the following graph modeled in neo4j
goal:
calculate the sum of all nodes multiplied by the edge percentage from the bottom up.
e.g.
(((30*.6)+(50*.1)+100)*.5)+10)=71,5
status:
I found the REDUCE function (http://neo4j.com/docs/stable/query-functions-collection.html#functions-reduce)
but in my opinion it sums from top to the bottom, instead of bootom up.
Is this a commen problem with a well known name, and I dont know it?
Is there any solution in neo4j or in another (graph)database/language?
This was a really interesting one :
I assumed 2 things, first all nodes have the :A label, second the property on nodes and relationship has the key p
Here is a working query :
MATCH p=(:A)-[r]->(pike)
WITH pike, collect(p) as paths
OPTIONAL MATCH (pike)-[r]->()
WITH
CASE r WHEN null THEN 1 ELSE r.p END as multiplier,
CASE r WHEN null THEN last(nodes(paths[0])).p
ELSE reduce(x=0, path in paths | x + (head(nodes(path)).p * head(rels(path)).p)) + last(nodes(paths[0])).p END as total
RETURN sum(total*multiplier) as total
The logic behind :
Find one depths paths, agreggate the children by the pike (first WITH)
In case the optional match doesn't pass, the multiplier will be 1 instead of a possible float value on the relationship property
The second case, do the math logic, if this is the top of the pikes (hence here node A) it will just add the value of the top node, otherwise it will take the value of the children
Then it sum the score + the multiplication
You can test it here : http://console.neo4j.org/r/ih8obf
A simple question as i am developing a java application based on dcm4che ...
I want to calculate/find the "position" of a dicom image into its sequence (series). By position i mean to find if this image is first, second etc. in its series. More specifically i would like to calculate/find:
Number of slices into a Sequence
Position of each slice (dicom image) into the Sequence
For the first question i know i can use tag 0020,1002 (however it is not always populated) ... For the second one?
If you are dealing with volumetric image series, best way to order your series is to use the Image Position (Patient) (0020, 0032). This is a required Type 1 tag (should always have value) and it is part of the image plane module. It will contain the X, Y and Z values coordinates representing the upper left corner of the image in mm. If the slices are parallel to each other, only one value should change between the slices.
Please note that the Slice Location (0020, 1041) is an optional (Type 3) element and it may not exist in the DICOM file.
We use the InstanceNumber tag (0x0020, 0x0013) as our first choice for the slice position. If there is no InstanceNumber, or if they are all the same, then we use the SliceLocation tag (0x0020, 0x1041). If neither tag is available, then we give up.
We check the InstanceNumber tag such that the Max(InstanceNumber) - Min(InstanceNumber) + 1 is equal to the number of slices we have in the sequence (just in case some manufacturers start counting at 0 or 1, or even some other number). We check the SliceLocation the same way.
This max - min + 1 is then the number of slices in the sequence (substitute for tag ImagesInAcquisition 0x0020, 0x1002).
Without the ImagesInAcquisition tag, we have no way of knowing in advance how many slices to expect...
I would argue that if the slice location is available, use that. It will be more consistent with the image acquisition. If it is not available, then you'll have to use or compute from the image position (patient) attribute. Part 3 section C.7.6.2.1 has details on these attributes.
The main issue comes when you have a series that is oblique. If you just use the z-value of the image position (patient), it may not change by the slice thickenss/spacing between slices attributes, while the slice location typically will. That can cause confusion to end users.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Tool for generating railroad diagram used on json.org
SQLite has some awesome graphs showing the grammer of the language on their website, does anyone know how these are made?
Is there a tool for generating graphs from grammas?
This example looks a lot like a finite automaton -- i.e. the graph equivalent of a regular expression. If you can repesent your grammar to a RE (naturally, not all grammars will be representable as REs!), you can use the Kleene's theorem to translate it into a FA graph.
Note that the alphabet in question for the REs is not single letters, but words and tokens. In the above example, the corresponding RE looks like:
DELETE FROM qualified-table-name
(WHERE expr|()) /* "WHERE expr" is optional; the alternative branch is the empty expression "()" */
(
(ORDER BY ordering-term (, ordering-term)*|()) /* ", ordering-term" may be repeated */
LIMIT expr ((OFFSET|,) expr|()) /* can use "OFFSET" or "," */
|()
)
This translates into a FA very similar to your diagram. GraphViz will do a passable job of drawing it legibly.
However that's not quite the same as the original, is it? Presenting it nicely is the next challenge. I'd suggest taking the nested RE expressions and rendering them recursively, starting at the leaves.
For example, to render (WHERE expr|()):
Two alternate paths. Render each separately:
Render WHERE expr:
Render WHERE as box.
Then an arrow.
Then render expr as a box.
Render () as a single arrow.
Find the longest (the first one) and stretch the others to match it.
Create start and end nodes at each end.
Draw connecting edges from the start node to each subpart, then from each subpart to the end node.
Doing this graphically means keeping track of box sizes and positions, including invisible boxes. There is an invisible box round each subpart. There are three things to note about the recursive structure:
The size of a part depends on the sizes of its children.
The location of a subpart depends on the location of its parent.
Locations of everything (may) depend on the sizes of everything else.
This implies that you should first calculate the sizes of each part, starting at the bottom. Then, once you know the size of the root, you can start positioning the parts, top-down.
I can't seem to find a definitive answer for this, I'm trying to do some elementary proofs on heaps but here's what's throwing me off a little bit:
Is an empty tree valid? If so, what is its height?
I would think this would be 0.
What is the height of a tree with a single node?
I would think this would be 1 but I have seen definitions where it is 0 (and if this is the case then I don't know how to account for an empty tree).
Height of a tree is the length of the path from root of that tree to its farthest node (i.e. leaf node farthest from the root).
A tree with only root node has height 0 and a tree with zero nodes would be considered as empty. An empty tree has height of -1. Please check this.
I hope this helps.
I think you should take a look at the Dictionary of Algorithms and Data Structures at the NIST website. There definition for height says a single node is height 0.
The definition of a valid tree does include an empty structure. The site doesn't mention the height of such a tree, but based on the definition of the height, it should also be 0.
I have seen it used in both ways (counting a single node as 0 or 1), but the majority of sources would define a root-only tree as a tree of height 0, and would not consider a 0-node tree valid.
If your tree is a recursively defined data structure which may be either empty or a node with a left and right subtree (for example search trees, or your heap), then the natural definition is to assign 0 to the empty tree and 1 + the height of the highest subtree to a nonempty tree.
If your tree is a graph then the natural definition is the longest path from the root to a leaf, so a root-only tree has depth 0. You normally wouldn't even consider empty trees in this case.
The height of a tree is the length of the longest path to a terminal node in either of its children.
Wikipedia says the height of an empty tree is -1. I disagree. An empty tree is literally just a tree containing one terminal node (a null or special value which represents an empty tree). Since the node has no children, the length of its longest path must be the empty sum = 0, not -1.
Likewise, a non-empty tree has two children, so by definition there is at least a path >= 1 to a terminal node.
We might define our tree as follows:
type 'a tree =
| Node of 'a tree * 'a * 'a tree
| Nil
let rec height = function
| Node(left, x, right) -> 1 + max (height left) (height right)
| Nil -> 0
According to Wikipedia, the height of a (sub-)tree with one single node is 0. The height of a tree with no nodes would be -1. But I think it's up to you, how you define the height and your proofs should work with either definition.
The definition of the height of a rooted tree is the length of the longest path from the root to a leaf, expressed in the number of edges. This definition does not cover the case of an empty tree, as there is no path at all.
However, for practical reasons, it is convenient to define the height of an empty tree as −1. Here are some of those reasons:
For non-empty trees we have this rule: the height of the tree is equal to the number of levels in that tree, minus 1. If we extrapolate this rule to an empty tree, then we have 0 levels, and thus a height of −1.
A tree with height ℎ has at least ℎ+1 nodes. If the tree is binary, then it has at most 2ℎ+1−1 nodes. If we substitute −1 for ℎ we get 0 for both expressions, and indeed an empty tree has zero nodes.
The height of a tree is one more than the maximum among the heights of the root's subtrees. If the root happens to have no children, we could say it only has "empty" subtrees. And if we consider the height of those empty subtrees to be −1, then we come to the (correct) conclusion that this tree's height is 0.
It would be impractical to define the height of an empty tree as 0, as you would need to define exceptions to the points raised above.
actully a perfect defn for height of tree is d level of leaf of d longest path from root plus 1..accordin 2 this defn f a tree s empty,it wont b havin any level n v cant consider it had zero,coz level of a root s zero ..so empty tree level is -1,than accordin 2 defn its -1+1=0..so ZERO s d height of empty tree...bt n many book they hav given -1 bt no explanation s given