c# – Sentinel nodes in a 2-3 tree

I want to implement a 2-3 tree in C# language, where every leaf has a unique key. The keys are from a given class that implements the IComparable interface (the specific class is unknown, and it’s should work for every class which implements the interface).

I want to use two sentinel nodes to represent plus and minus infinity, but I can’t figure out the best way to do it, that won’t be too complex.

I thought about creating a class which will implement the IComparable interface, and use it as two leaves in the tree, but I am not sure if that’s the best thing to do or not.

minimum number of edges that should be added to an undirected graph to make it a tree

Your first guess is correct. Sometimes there is more than one way to write the same solution.

Clearly, if there are $k$ connected components you’ll need exactly $k-1$ edges to connect them (without forming any cycle).

On the other hand, a tree with $n$ nodes must have exactly $n-1$ edges, so if the graph is acyclic and already has $m$ edges, then it is missing $n-1-m$ edges.

Regarding the example: the given graph has $n=10$ vertices, $m=6$ edges, and $k=4$ connected components (not 3), so the answer is $3=k-1=n-m-1$. The sets of vertices in each connected component are ${1,2,8}, {3}, {4, 6, 10}$, and ${5, 7, 9}$.

induction – Every AVL tree can be colored to be a red-black tree

I want to prove any AVL tree can be turnt into a red-black tree by coloring nodes appropriately.
Let $h$ be the height of a subtree of an AVL tree.
It is given that such a coloring is constrained by these cases:

  1. $h$ odd $implies$ black height $=$ $frac{h}{2} + 1$, root node black
  2. $h$ even $implies$ black height $=$ $frac{h+1}{2}$, root node red

After that the root node is colored black.


I’m trying to prove this inductively. Let’s start with the base case $h=1$. Then there is only one node (the root node) and it gets colored black (using case 2) which yields a valid red-black tree.

Now suppose the statement is true for some $h geq 1$. Then for any node $u$ in the AVL tree, the height difference between their children is less than $1$. That is, for an AVL tree of height $h+1$ either both subtrees of the root node have height $h$ or one has height $h-1$.

By the induction hypothesis we know how to color the subtree of height $h$, depending on the parity of $h$. I’m unsure if I should use strong induction instead because it is not given in the hypothesis how to color a subtree of height $h-1$.

If we would know how to color both subtrees, then consider the following cases:

  1. $h+1$ is even
    • one subtree has height $h$, the other height $h-1$
    • both subtrees have height $h$
  2. $h+1$ is odd
    • one subtree has height $h$, the other height $h-1$
    • both subtrees have height $h$

For case 1.1 we would get
$$
begin{align*}
quad & h+1 &text{even} \
implies quad & h &text{odd} \
implies quad & text{black height} = frac{h}{2} + 1 \
implies quad & h-1 &text{even} \
implies quad & text{black height} = frac{(h-1)+1}{2} = frac{h}{2}
end{align*}
$$

So their black heights differ by $1$. How would I take that into consideration?

time complexity – How to index a tree to allow efficient search for paths?

I feel this question must be pretty elementary, but as a CS noob I don’t know how to search for the answer. I may not even be phrasing the question correctly.

So let me describe a hypothetical use case. Say my program stores a chess game (including variations) as a tree, with each node representing the state of the game after a move. My question is, given any two nodes which are guaranteed to be in the tree, what is the most efficient way to find the path (a sequence of intervening nodes) from one to the other? I’m mostly concerned with time complexity, although I’d like to save space as well (not sure how to express myself more precisely).

Walking the whole tree is probably not it. I’m thinking that maybe each node can be assigned an informative address, perhaps an array. One scheme would be to let a child’s address be one element longer than its parent’s, where the extra element uniquely identifies the child. So if the first node has address (0), its children would have (0, 0), (0, 1), … and the children of (0, 1) would have addresses like (0, 1, 0), (0, 1, 1), and so on. With this scheme the nodes on the path between any two nodes can be directly read off from their addresses.

With a tall tree though, the addresses can get quite long. Not a real concern for a chess program, but this way of indexing a tree just feels inefficient and inelegant to me. Is there a better way to assign addresses? Or is there a completely different approach to this problem?

dynamic programming – Longest path on a full tree

Given a full tree $ T = (V, E, w) $ I need to find the path with maximum length from root $\ s $ to any of the leaves.

I was thinking I could use some sort of BFS. Because I’m looking for maximum length path, I must go through all of the edges of the tree and I will start at the vertex $ s $. So I’ll use a dictionary $ lengths = {} $ where each vertex in the $ lengths $ dictionary is a key and its value is the total length from $ s $ to that vertex. Then I’ll just choose a the leaf with the highest value. From what I’ve seen online the solution to the problem is actually using Shortest path for DAG and multiply lengths by $ -1 $ and the multiply back once algorithm finish. So not sure if my solution is ok?

Thanks,

Alternatives for Tree structure [duplicate]

Possible Duplicate:
What's the best way to view a deep hierarchy?

In a desktop application what are the innovative, alternate methods to a tree structure?

Provide me some example links to go through.

Christmas tree made up only with HTML and CSS

body {
  background-color: #9fd8ee;
}

.tree {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.triangle-one {
  width: 0;
  height: 0;
  border-left: 60px solid transparent;
  border-right: 60px solid transparent;
  border-bottom: 100px solid rgb(20, 97, 27);
  margin-top: -69px;
}

.triangle-two {
  width: 0;
  height: 0;
  border-left: 90px solid transparent;
  border-right: 90px solid transparent;
  border-bottom: 150px solid rgb(20, 97, 27);
  margin-top: -40px;
}

.triangle-three {
  width: 0;
  height: 0;
  border-left: 120px solid transparent;
  border-right: 120px solid transparent;
  border-bottom: 200px solid rgb(20, 97, 27);
  margin-top: -80px;
}

.triangle-four {
  width: 0;
  height: 0;
  border-left: 150px solid transparent;
  border-right: 150px solid transparent;
  border-bottom: 250px solid rgb(20, 97, 27);
  margin-top: -120px;
}

.rectangle {
  height: 50px;
  width: 60px;
  background-color: maroon;
}

.globe {
  border-radius: 50%;
  width: 25px;
  height: 25px;
  position: absolute;
}

.glpos-1 {
  background: red;
  top: 175px;
  left: 630px;
}

.glpos-2 {
  background: blue;
  top: 225px;
  left: 655px;
}

.glpos-3 {
  background: yellow;
  top: 315px;
  left: 610px;
}

.glpos-4 {
  background: orange;
  top: 375px;
  left: 680px;
}

.glpos-5 {
  background: cyan;
  top: 425px;
  left: 640px;
}

.glpos-6 {
  background: red;
  top: 535px;
  left: 700px;
}

.glpos-7 {
  background: blue;
  top: 510px;
  left: 590px;
}

.light {
  border-radius: 50%;
  width: 10px;
  height: 10px;
  background: yellow;
  animation: example 1s infinite;
  position: absolute;
}

.lipos-1 {
  top: 260px;
  left: 620px;
}

.lipos-2 {
  top: 300px;
  left: 710px;
}

.lipos-3 {
  top: 410px;
  left: 590px;
}

.lipos-4 {
  top: 510px;
  left: 730px;
}

.lipos-5 {
  top: 160px;
  left: 666px;
}

.lipos-6 {
  top: 417px;
  left: 700px;
}

.lipos-7 {
  top: 480px;
  left: 610px;
}

@keyframes example {
  50% {
    box-shadow: 1px 1px 10px #ccc, -2px 1px 10px #ccc, 0 -1px 10px #ccc;
  }
}

.star {
  margin: 50px 0;
  position: relative;
  display: block;
  color: yellow;
  width: 0px;
  height: 0px;
  border-right: 100px solid transparent;
  border-bottom: 70px solid yellow;
  border-left: 100px solid transparent;
  transform: rotate(35deg) scale(0.75);
}
.star:before {
  border-bottom: 80px solid yellow;
  border-left: 30px solid transparent;
  border-right: 30px solid transparent;
  position: absolute;
  height: 0;
  width: 0;
  top: -45px;
  left: -65px;
  display: block;
  content: '';
  transform: rotate(-35deg);
}
.star:after {
  position: absolute;
  display: block;
  color: yellow;
  top: 3px;
  left: -105px;
  width: 0px;
  height: 0px;
  border-right: 100px solid transparent;
  border-bottom: 70px solid yellow;
  border-left: 100px solid transparent;
  transform: rotate(-70deg);
  content: '';
}
<!DOCTYPE html>
<html>
  <head>
    <title>Christmas tree</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" type="text/css" href="https://codereview.stackexchange.com/style.css" />
  </head>
  <body>
    <div class="tree">
      <div class="star"></div>
      <div class="triangle-one"></div>
      <div class="triangle-two"></div>
      <div class="triangle-three"></div>
      <div class="triangle-four"></div>
      <div class="rectangle"></div>
    </div>
    <div class="globe glpos-1"></div>
    <div class="globe glpos-2"></div>
    <div class="globe glpos-3"></div>
    <div class="globe glpos-4"></div>
    <div class="globe glpos-5"></div>
    <div class="globe glpos-6"></div>
    <div class="globe glpos-7"></div>

    <div class="light lipos-1"></div>
    <div class="light lipos-2"></div>
    <div class="light lipos-3"></div>
    <div class="light lipos-4"></div>
    <div class="light lipos-5"></div>
    <div class="light lipos-6"></div>
    <div class="light lipos-7"></div>
  </body>
</html>

graphs – Unsure why (or whether?) a certain algorithm correctly computes a Minimum spanning tree

CLRS problem 23-4 part c gives an algorithm that may or may not compute a minimum spanning tree. Given some connected undirected graph G, we have

MAYBE-MST-C(G,w)
  T = empty set
  for each edge e in G, taken in arbitrary order
    T = T U {e}
    if T has a cycle c
      let e' be a maximum-weight edge on c
      T = T - {e'}
  return T

I’ve found several other solutions (here, here and here) that claim that this procedure succeeds in finding an MST.

I can see that this results in a spanning tree. It’s spanning because adding every edge e would certainly span G, and removing parts of cycles e’ certainly doesn’t affect that. It’s a tree because any cycle that is added is guaranteed to be immediately removed.

I’m having a hard time seeing why it’s minimal (or perhaps it actually isn’t?). One of the solutions uses the result of exercise 23.1-5:

if $G$ and has some cycle $c$, then
there exists some MST of G that does not include the edge $e’$ with the
largest weight on that cycle $c$.

While this result superficially resembles the operation performed in MAYBE-MST-C, upon closer inspection, it’s not so obvious that it helps, since in the algorithm, T is not guaranteed to span G until the very end, and also the exercise result only claims that an MST exists when omitting one e’; it doesn’t say we need to remove all e’. What if the MST that the exercise guarantees requires us to remove the first e’, but keep the second? Further, upon removing e’, how are we sure that the edges needed to compete the MST have not been removed in previous iterations of the loop?

Best algorithm (Time Complexity) to find Minimum spanning tree of an complete, positive weighted, undirected, graph

Suppose that we have a complete undirected positive weighted graph G={V,E} and I want to find one MST of it. My problem is what is the best (in time complexity) algorithm for it? The best prime complexity I have found is O(V^2) The best Kruskal complexity is O(E.Log(E))=~O(V^2. Log(V)) any idea for lower time complexity? I would like to find lower than quadratic in the number of Vertex.

algorithms – Depth two tree CBQ rate limiting

Context: I’m operating a service with 5 accounts simultaneously servicing tens of millions of users. The accounts using the service have extreme bursts due to various campaigns by each account and the incoming requests from one account can suddenly go up 100x. All our queries are about equally expensive, so it makes sense to do rate limiting locally on each backend server instance, since the limits then automatically scale with the number of nodes started by an autoscaler. What I want to do is give each account a fixed percentage of the total capacity, but allow the accounts to burst up to the full capacity available.

Question: Is there’s a simple rate limiting algorithm that can deal with this? We’re talking HTTP queries here and not network bandwidth. Every router out there usually supports traffic shaping with CBQ which with a depth 2 tree would do exactly what I want, but there doesn’t seem to exists any rate limiter libraries that can do this. I don’t need a full CBQ algorithm that supports tree hierarchies of unlimited depth, since I only need the root node and its children. Are there any simple algorithms for this? My guess is that a token bucket algorithm exist for this where one request is one token, but one would need multiple buckets.