red-black tree - construction - creation

Recently, I have been going through search trees and I encountered red-black trees, the point confusing me is, In r-b tree, the root node should be black thats fine, now how will I decide whether the incoming node assumes red or black color.
I have gone through the wiki article but have not found a solution for this. I might be wrong, but I would be happy if someone can guide me through the exact material.
[Edit]
That is for example, if my keys are {7, 2, 4, 1, 9, 10, 8}
Here 7 is root and it assumes black color, but what color does 2 assume? How do we decide that? And how do we decide what color the other nodes assume?
7 - (Black)
2 9
1 4 8 10
NIL NIL NIL NIL NIL NIL NIL NIL
Do we have a random toss that decides the color of the node to be red or black. Or is it some other process.
Thank you.

Look at the lecture about red-black trees on MIT open courseware.
http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/VideoLectures/
I found them to be very helpful.
Now if I remember correctly, you always insert new node as black node and then proceed to the necessary corrections (repainting and/or rotations)

The incoming node must be colored RED because if you color an incoming node to be black than height of all leaf to root path for newly inserted node is going to increase by one which is going to violate RB tree property that every root to leaf path must contain equal number of black nodes.Refer this if you want more insight in insertion of RB tree http://www.youtube.com/watch?v=6QOKk_pcv3U

Related

Find a path from vertex s to vertex t with minimal number of color alternates

Let be a directed graph, and let be an edge-coloring in red and blue. Let s,t be vertices in G. Find a path from s to t (if exists) so that the number of color changes along this path is minimal.
I have tried to do as follows:
Let be the graph obtained by removing all the
blue-colored edges of G. Let be the graph obtained
by removing all the red-colored of G.
Let
be the strongly connected graph of , computed
using this algorithm.
Let be the
strongly connected graph of , computed using
this algorithm.
Color the vertices of
in red, and color the vertices of
in blue.
Let be
the graph obtained by merging with
.
Define the weight of each (existing)
edge in G' as 0.
For each such that u
belongs to the strongly connected component and v
belongs to the strongly connected component do as
follows:
if and add edge to G' and
define its weight as 1.
Use Dijkstra algorithm to find a shortest
path from the blue strongly connected component of s, to both the
blue and red strongly connected components of t.
Use Dijkstra
algorithm to find a shortest path from the red strongly connected
component of s, to both the blue and red strongly connected
components of t.
Let p denote the shortest path among the four we
have just found. (namely, p has minimal number of color alternates). p is a series of strongly connected components.
Expand each of them using DFS, to find a corresponding path in G.
This algorithm can run in O(E+V*log(v)). Can it be improved or simplified?
I don't fully understand your algorithm, specifically in stage 4 you will color every vertex with two different colored edges in two colors - blue and red...
Therefor I will not try and improve your algorithm but will present one of my own - a variant of BFS with time of O(E + V).
The idea: Iterate over the edges of the graph and measure depth as the number of times you switched colors.
Note: We will run the algorithm twice, first assume that the first edge of the path is red, second assume that the first edge of the path is blue, than take the minimum.
Run BFS only on red edges starting from s (which is the first element in the BFS queue), if you viewed a vertex on a blue edge keep it in a different queue.
Mark all of the nodes you saw with the number i (at the beginning i=0).
Take the queue for the blue edges and make it your primary queue.
Run stages 1 to 3 but switch the colors and add 1 to i.
At the end the number in t is the minimal number of swaps performed to reach t.

How is this Huffman Table created?

I have a table that shows the probability of an event happening.
I'm fine with part 1, but part 2 is not clicking with me. I'm trying to get my head around how
the binary numbers are derived in part 2?
I understand 0 is assigned to the largest probability and we work back from there, but how do we work out what the next set of binary numbers is? And what do the circles around the numbers mean/2 shades of grey differentiate?
It's just not clicking. Maybe someone can explain it in a way that will make me understand?
To build huffman codes, one approach is to build a binary tree, using a priority queue, in which the data to be assigned codes are inserted, sorted by frequency.
To start with, you have a queue with only leaf nodes, representing each of your data.
At each step you take the two lowest priority nodes from the queue, make a new node with a frequency equal to the sum of the two removed nodes, and then attach those two nodes as the left and right children. This new node is reinserted into the queue, according to it's frequency.
You repeat this until you only have one node in the queue, which will be the root.
Now you can traverse the tree from the root to any leaf node, and the path you take (whether you go left or right) at each level gives you either a 0 or a 1, and the length of the path (how far down the tree the node is) gives you the length of the code.
In practice you can just build this code as you build the tree, but appending 0 or 1 to the code at each node, according to whether the sub-tree it is part of is being added to the left or the right of some new parent.
In your diagram, the numbers in the circles are indicating the sum of the frequency of the two nodes which have been combined at each stage of building the tree.
You should also see that the two being combined have been assigned different bits (one a 0, the other a 1).
A diagram may help. Apologies for my hand-writing:

A* path finding: backed into a corner, now what?

I'm writing a tower defense game, and I'm implementing the A* path finding algorithm from tutorials and what not, but I've encountered a situation I can't code out of as of yet! Consider the graphic below. The cyan nodes represent the shortest path found so far, the violet nodes represent inspected nodes, and the dark gray nodes represent a wall.
From what I know of the A* algorithm, to calculate a path for the situation below, it...
Starts at "start" and checks if the upper node is available. It is. It calculates its G and H scores. It does the same to the right, lower, and left nodes.
Finds that the right node has the lowest F score (F = G + H). It then repeats the same method as step #1 to find the next node, marking the "start" node as the parent.
Continues this pattern and lands on the node in the center of the "C trap," as this node has the lowest F score so far.
Discovers that the upper, right, lower, and left nodes are all closed. A* then marks this node as closed and starts over, understanding to leave this node alone.
What happens immediately after #4? Would A* re-examine the G and H scores along the same path, skipping that newly-closed node? Also, when drawing this scenario with this SWF, it indicates that more nodes are discovered to make up for the trap, as suggested here. Why?
What you seem to be missing is that A* is not like a single unit moving along the grid, and backtracking when it hits a wall; it's more like an observer looking at various nodes around the graph, always considering next the node "most likely" to be in the shortest path.
So, step 4 above never happens. A* doesn't mark a node as closed when it determines it can't be part of the best-path - it simply puts every single node it vists in the closed list, as soon as it visits it. And it doesn't ever "start over" - it's always looking at the next "most likely" node, where "most likely" means the front of the priority queue ordered by f(x).
So, in your example above, A* will jump to one of the blue-nodes next, since they are all in the open list, and all have the same f(x) value. Though it could technically consider any one of them next, if you use the correct tie-breaking criteria, it will take one of the two that are closest to the end.

Node layering in Graphviz

I'm creating a graph using Graphviz (compiled with neato). This graph contains many overlapping nodes which is perfectly fine. However, there is a group of large nodes which I prefer to always be on top of other small nodes - even-though I prefer to define the large nodes first in the graph (which makes them get painted at the very bottom).
Any way I can force this?
Edit:
Here's a small example, just to clarify what I mean:
graph G {
node [style=filled,fillcolor=black];
BigNode [fillcolor=skyblue,shape=Msquare];
node [style=filled,fillcolor=red,shape=circle];
edge [style=invis]
1 -- BigNode[len=0.5];
2 -- BigNode[len=1];
}
I'd like for BigNode to be painted over node 1.
I did find one (sort of) solution...
I found that if you postpone only the node definition to the end, even if you defined edges for this node earlier, it will be painted top-most.
I realize this contradicts what I defined earlier, but this was the only possible solution in this case and it was the one I eventually had to use.
In my short example, you would do this:
graph G {
node[style=filled,fillcolor=black];
// Definition of BigNode moved to the end of the file
/*BigNode [fillcolor=skyblue,shape=Msquare];*/
node[style=filled,fillcolor=red,shape=circle];
edge[style=invis]
1 -- BigNode[len=0.5];
2 -- BigNode[len=1];
// Defined after already defining edges for BigNode
BigNode [fillcolor=skyblue,shape=Msquare];
}
In the resulting graph, BigNode is painted over node 1
I don't think it's possible. The official neato guide talks about node layering on pages 6 through 9. It looks like the most you can do is adjust the length of edges and pin down nodes: you can't actually control how nodes layer over each other.

Clarification of Graph Terminology

I'm working on a homework question and I've got the answer, but I'm not sure if the terminology I'm using is correct and I was hoping if someone can clarify. Here's the situation:
I have a graph in the shape of a rectangle of size M x N. The node at (0, 0) has no incoming edges. All other nodes have incoming edges from the north, northwest, and west, unless they are on the top or left-most side, in which case the diagonal incoming edge does not exist and either the edge from the west or north does not exist.
So if starting at node (0, 0) and following any path to its finish, it will end at node (m, n). I am now asked to define what type of graph this is. Is this a directed acyclic graph?
Yep, it is, because each edge has a direction (hence directed) and there are no cycles - once you leave any node, there's no way to get back to it by following the edges of the graph (hence acyclic). See http://en.wikipedia.org/wiki/Directed_acyclic_graph for more info.

Resources