Could you help me understand red black trees insertion algorithm? - data-structures

I'm diving deeper into data structures and trying to understand in all details Red-Black trees. It doesn't seem to way hard, I checked the Wikipedia page. The problem that I can't understand the sentence:
G is known to be black, since its former child P could not have been red without violating property 4.
from the insertion procedure Case 4, step 2. It's clear G is black but it's unclear why P can't be red while it's red. It was red without violating property 4. And I don't see a connection between G and P in this sentence.
There are can be two cases - either I don't understand something or there is an error on Wikipedia and it should be fixed.
Could you help me clarify this moment? Thank you in advance!

Related

thinning/skeletonization algorithm with 4 known neighbors

I am searching for a thinning/skeletonization algorithm which works if I only know 4 neighbors not 8.
From all algorithms I could find I assume that I have knowledge about the diagonal neighbors.
So does anybody know about a thinning algorithm which also works if I only know the top, right, bottom, left neighbor?
The outcome should be like this:
http://www.cs.ru.nl/~ths/rt2/col/h9/thinning.GIF
These are not what I am looking for:
http://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Skel.png/220px-Skel.png
The shape should be maintained as in the first example
I'd suggest using one of the 8-neighbour algorithms, but feeding it dummy information for the diagonal cells or otherwise modifying the part of the algorithm that considers neighbours.
Since you're not too specific about the kinds of things you're looking at it's hard to offer concrete suggestions. Most algorithms will contain a part that looks like this:
for n in neighbours:
do stuff
in which case you need to edit neighbours.
Others will apply some kind of mask or kernel function. Edit that kernel.

Level solving and pathfinding

I have played a little flash game recently called Just A Trim Please and really liked the whole concept.
The basic objective of the game is to mow the whole lawn by going over each square once. Your lawn mower starts on a tile and from there you can move in all directions (except where there are walls blocking you). If you run on the grass tiles more than once it will deteriorate and you will lose the level. You can only move left, right, up, or down.
However, as you finish the game, more tiles get added:
A tile you can only mow once (grass).
A tile you can run over twice before deteriorating it (taller grass).
A tiie you can go over as much as you want (concrete).
A tile you can't go over (a wall).
If you don't get what I mean, go play the game and you'll understand.
I managed to code a brute-force algorithm that can solve puzzles with only the first kind of tile (which is basically a variation of the Knight's Tour problem). However, this is not optimal and only works for puzzles with tiles that can only be ran on once. I'm completely lost as to how I'd deal with the extra tiles.
Given a starting point and a tile map, is there a way or an algorithm to find the path that will solve the level (if it can be solved)? I don't care about efficiency, this is just a question I had in mind. I'm curious as to how you'd have to go to solve it.
I'm not looking for code, just guidelines or if possible a plain text explanation of the procedure. If you do have pseudocode however, then please do share! :)
(Also, I'm not entirely sure if this has to do with path-finding, but that's my best guess at it.)
The problem is finite so, sure, there is an algorithm.
A non-deterministic algorithm can solve the problem easily by guessing the correct moves and then verifying that they work. This algorithm can be determinized by using for example backtracking search.
If you want to reduce the extra tiles (taller grass and concrete) to standard grass, you can go about it like this:
Every continuous block of concrete is first reduced into a single graph vertex, and then the vertex is removed, because the concrete block areas are actually just a way to move around to reach other tiles.
Every taller grass tile is replaced with two vertices that are connected to the original neighbors, but not to each other.
Example: G = grass, T = tall grass, C = concrete
G G T
G C T
C C G
Consider it as a graph:
Now transform the concrete blocks away. First shrink them to one (as they're all connected):
Then remove the vertex, connecting "through" it:
Then expand the tall grass tiles, connecting the copies to the same nodes as the originals.
Then replace T, T' with G. You have now a graph that is no longer rectangular grid but it only contains grass nodes.
The transformed problem can be solved if and only if the original one can be solved, and you can transform a solution of the transformed problem into a solution of the original one.
There is a DP approach for the travelling salesman.
Perhaps you could modify it (recalculating as more pieces are added).
For a long piece of grass, you could perhaps split it into two nodes since you must visit it twice. Then reconnect the two nodes to the nodes around it.

Fast, stackless kd-tree traversal in raytracing, clarification needed

I am trying to implement a real time ray tracer, and I was reading this interesting paper on a fast, stackless kd-tree traversal method, but it is unclear regarding certain concepts. At page 4, where it presents the rope construction algorithm, it doesn't explain what the 'split-plane' and 'split-axis' are exactly, and how the 'split-axis' could be parallel to the left side but not the right side.
Would anyone with more experience in writing ray tracers and/or who understood the two concepts found in the paper please explain them to me?
Thanks in advance.
Paper [PDF]: http://www.johannes-guenther.net/StacklessGPURT/StacklessGPURT.pdf
I don't think the authors meant to imply that the split-axis is parallel to only the right or left side. They are optimizing the ropes for each side. In order to do this, they need to know if the split plane is parallel to that side. So they test split-axis(R) || S. Here, R is the ropes of the current node. R_L and R_R are the ropes for the left and right sub-trees of that node. The test is trying to see if the current node is splitting on the side for which the ropes are being optimized.
For example: if we're trying to optimize the left and right ropes, we first check whether the current node has a split plane that splits the world into a left and right side (in other words, the split plane is parallel to the YZ plane). If it isn't, then we give up on optimizing the left and right ropes.

Find start and end position of "cover-all" path then connect them

I have the shape in my 2dArray like this (for example):
It is known that the points A and B (I do not know where) and a path that covers the entire shape (must walk through each cell) must exist. Can you give me some help on how to determine points A and B and then the "cover-all" path? Maybe there are some known algorithms for such case. Or some help with a pseudo-code algorithm. Thanks in advance.
Check nhahdth's link to see that your problem in general is np-hard. this mathoverflow article cites a paper establishing the result for graphs on grids with holes - you won't fare significantly better than using brute force unless you can come up with more constraints.
You may be lucky in identifying at least one of your start and end nodes by searching for vertices of degree 1 in the underlying grid cell graph.

How hard is this graph problem?

I have a problem to solve for a social-networks application, and it sounds hard: I'm not sure if its NP-complete or not. It smells like it might be NP-complete, but I don't have a good sense for these things. In any case, an algorithm would be much better news for me.
Anyhow, the input is some graph, and what I want to do is partition the nodes into two sets so that neither set contains a triangle. If it helps, I know this particular graph is 3-colorable, though I don't actually know a coloring.
Heuristically, a "greedy" algorithm seems to converge quickly: I just look for triangles in either side of the partition, and break them when I find them.
The decision version of problem is NP-Complete for general graphs: http://users.soe.ucsc.edu/~optas/papers/G-free-complex.pdf and is applicable not just for triangles.
Of course, that still does not help resolve the question for the search version of 3-colourable graphs and triangle freeness (the decision version is trivially in P).
Here's an idea that might work. I doubt this is the ideal algorithm, so other people, please build off of this.
Go through your graph and find all the triangles first. I know it seems ridiculous, but it wouldn't be too bad complexity-class wise, I think. You can find any triangles a given node is part of just by following all its edges three hops and seeing if you get to where you started. (I suspect there's a way to get all of the triangles in a graph that's faster than just finding the triangles for each node.)
Once you have the triangles, you should be able to split them any way you please. By definition, once you split them up, there are no more triangles left, so I don't think you have to worry about connections between the triangles or adjacent triangles or anything horrible like that.
This is not possible for any set with 5 tightly interconnected nodes, and I can prove it with a simple thought experiment. 5 tightly interconnected nodes is very common in social networks; a cursory glance at my facebook profile found with among my family members and one among a group of coworkers.
By 'tightly interconnected graph', I mean a set of nodes where the nodes have a connection to every other node. 5 nodes like this would look like a star within a pentagon.
Lets start with a set of 5 cousins named Anthony, Beatrice, Christopher, Daniel, and Elisabeth. As cousins, they are all connected to each other.
1) Lets put Anthony in Collection #1.
2) Lets put Beatrice in Collection #1.
3) Along comes Christopher through our algorithm... we can't put him in collection #1, since that would form a triangle. We put him in Collection #2.
4) Along comes Daniel. We can't put him in collection #1, because that would form a triangle, so we put him in Collection #2.
5) Along comes Elisabeth. We can't put her in Collection #1, because that would form a triangle with Anthony and Beatrice. We can't put her in Collection #2, because that would for a triangle with Christopher and Daniel.
Even if we varied the algorithm to put Beatruce in Collection #2, the thought experiment concludes with a similar problem. Reordering the people causes the same problem. No matter how you pace them, the 5th person cannot go anywhere - this is a variation of the 'pidgenhole principle'.
Even if you loosened the requirement to ask "what is the smallest number of graphs I can partition a graph into so that there are no triangles, I think this would turn into a variation of the Travelling Salesman problem, with no definitive solution.
MY ANSWER IS WRONG
I'll keep it up for discussion. Please don't down vote, the idea might still be helpful.
I'm going to argue that it's NP-hard based on the claim that coloring a 3-colorable graph with 4 colors is NP-hard (On the hardness of 4-coloring a 3-collorable graph).
We give a new proof showing that it is NP-hard to color a 3-colorable graph using just four colors. This result is already known, but our proof is novel as [...]
Suppose we can partition a 3-colorable graph into 2 sets A, B, such that neither has a triangle, in polynomial time. Then we can solve the 4-coloring as follows:
color set A with C1,C2 and set B with C3,C4.
each set is 2-colorable since it has no triangle <- THIS IS WHERE I GOT IT WRONG
2-coloring a 2-colorable graph is polynomial
we have then 4-colored a 3-colorable graph in polynomial time
Through this reduction, I claim that what you are doing must be NP-hard.
This problem has an O(n^5) algorithm I think, where n is the number of vertices.

Resources