Having tasks that can be ran individually or “in trees”, and each task can be blocking or non-blocking

I’m creating a framework that needs to execute certain tasks, I’ll use building a car as an example for this. So, an individual task could be something like weld metal or screw bolt. But there are also collections of tasks, I call them jobs. Each job can have X amount of tasks, so combining these two tasks could get us a job build an engine:

build an engine +-- weld metal +-- screw bolts 

Additionally, jobs can have sub-jobs:

build a car +-- build an engine |   +-- weld metal |   +-- screw bolts +-- build a frame |   +-- weld metal |   +-- screw bolts |   +-- paint +-- combine engine and frame 

So basically each node of my tree is a job, and each leaf is a task. This is easy enough, here’s what my classes would look like:

class Task:     def do(self):         raise NotImplementedError('abstract method')  class Job(Task):     def __init__(self):         self.tasks = []      def do(self):         for task in self.tasks:             task.do() 

And now I’d just subclass WeldMetal and ScrewBolts from Task.

The problem arrives when we want to parallellise this. I need to somehow tell each task (and job) whether it should block the execution, or run in a separate thread and not block it. Sometimes my jobs also need results from other jobs, while those jobs may run in parallel to each other. For example in the above car building:

  • weld metal must block, since you can’t screw before the metal is welded.
  • build an engine and build a frame can be ran in their own threads (and within those threads the weld metal and screw bolts must block the particular thread).
  • combine engine and frame must wait for build an engine and build a frame to finish.

Honestly I’m not too sure where to start, initially I thought of using ThreadPoolExecutor in Job.do(), but I’m not sure how to block only some jobs and not others, and another issue is that sometimes a task must be ran “alone” without even being inside of any job, i.e. WeldMetal(block=False).do() must be valid.

I ended up using this at first:

class Task:     def do(self):         raise NotImplementedError('abstract method')      def run(self):         if self.block:             self.do()         else:             threading.Thread(target=self.do).start() 

And this works for most cases, but the issue comes when combine engine and frame needs to wait for both build an engine and build a frame to finish.

Having tasks that can be ran individually or “in trees”, and each task can be blocking or non-blocking

I’m creating a framework that needs to execute certain tasks, I’ll use building a car as an example for this. So, an individual task could be something like weld metal or screw bolt. But there are also collections of tasks, I call them jobs. Each job can have X amount of tasks, so combining these two tasks could get us a job build an engine:

build an engine +-- weld metal +-- screw bolts 

Additionally, jobs can have sub-jobs:

build a car +-- build an engine |   +-- weld metal |   +-- screw bolts +-- build a frame |   +-- weld metal |   +-- screw bolts |   +-- paint +-- combine engine and frame 

So basically each node of my tree is a job, and each leaf is a task. This is easy enough, here’s what my classes would look like:

class Task:     def do(self):         raise NotImplementedError('abstract method')  class Job(Task):     def __init__(self):         self.tasks = []      def do(self):         for task in self.tasks:             task.do() 

And now I’d just subclass WeldMetal and ScrewBolts from Task.

The problem arrives when we want to parallellise this. I need to somehow tell each task (and job) whether it should block the execution, or run in a separate thread and not block it. Sometimes my jobs also need results from other jobs, while those jobs may run in parallel to each other. For example in the above car building:

  • weld metal must block, since you can’t screw before the metal is welded.
  • build an engine and build a frame can be ran in their own threads (and within those threads the weld metal and screw bolts must block the particular thread).
  • combine engine and frame must wait for build an engine and build a frame to finish.

Honestly I’m not too sure where to start, initially I thought of using ThreadPoolExecutor in Job.do(), but I’m not sure how to block only some jobs and not others, and another issue is that sometimes a task must be ran “alone” without even being inside of any job, i.e. WeldMetal(block=False).do() must be valid.

I ended up using this at first:

class Task:     def do(self):         raise NotImplementedError('abstract method')      def run(self):         if self.block:             self.do()         else:             threading.Thread(target=self.do).start() 

And this works for most cases, but the issue comes when combine engine and frame needs to wait for both build an engine and build a frame to finish.

Randomly built binary search trees

In Introduction to Algorithms (CLRS) 3rd Edition, page 299, the section attempts to prove:

The expected height of a randomly built binary search tree on $ n$ distinct keys is $ O(\lg n)$ .

We define “randomly built binary search tree on $ n$ keys” as:

a binary search tree that arises from inserting the keys in random order into an initially empty tree, where each of the $ n!$ permutations of the input keys is equally likely.

In the proof, we defined some random variables:

Let $ X_n$ denotes the height of a randomly built binary search tree on $ n$ keys and the exponential height $ Y_n = 2^{X_n}$ . Of the $ n$ keys, we choose one key as the root of the tree, and we let $ R_n$ denotes the random variable that holds the position that this key would occupy if the set of keys were sorted (also know as ‘rank’ in the text). If we know that $ R_n=i$ , it follows that $ Y_n=2\cdot max(Y_{i-1},Y_{n-1})$ .

We also define indicator random variables $ Z_{n,1}, Z_{n,2}, …, Z_{n,n}$ , $ Z_{n,i} = I\{R_n=i\}$ .

\begin{equation} \begin{split} E[Y_n] & = E\Big[\sum_{i=1}^{n} Z_{n,i} (2\cdot max(Y_{i-1}, Y_{n-i}))\Big] \ & = \sum_{i=1}^{n} E[Z_{n,i} (2\cdot max(Y_{i-1}, Y_{n-i}))] && \text{(by linearity of expectation)}\ & = \sum_{i=1}^{n} E[Z_{n,i}] E[(2\cdot max(Y_{i-1}, Y_{n-i}))] && \text{(by independence)}\ \end{split} \end{equation}

I would like to ask why is the last equality correct? In other words, why can we assume independence of $ Z_{n,i}$ and $ max(Y_{i-1},Y_{n-i})$ ?

In the proof, there was a brief explanation:

Having chosen $ R_n = i$ , the left subtree (whose ranks are less than $ i$ . This subtree is just like any other randomly built binary search tree on $ i-1$ keys. Other than the number of keys it contains, this subtree’s structure is not affected at all by the choice of $ R_n=i$ , and hence the random variables $ Y_{i-1}$ and $ Z_{n,i}$ are independent. Likewise, the right subtree, whose exponential height is $ Y_{n-i}$ , is randomly built on the $ n-i$ keys whose ranks are greater than $ i$ .

However, since $ R_n$ does affect the number of keys $ Y_{i-1}$ and $ Y_{n-i}$ contain (as acknowledged by the explanation above), wouldn’t it mean that $ Y_{i-1}$ and $ Y_{n-i}$ are dependent on $ Z_{n,i}$ ?

A note regarding similar questions posted on CS stackexchange

I have read the answers for the following questions which are very similar to mine:

  1. Proof that a randomly built binary search tree has logarithmic height
  2. Average height of a BST with n Nodes.
  3. Randomized BST height analysis : How $ Z_{n,i}$ and $ Y_{k-1}$ are independent?

For 1 & 2, the question was a more general one which asked for an explanation for the entire proof. Also, the answer for both questions did not attempt to justify the independence and simply used the result.

For 3, the question was based on a MIT lecture, https://www.youtube.com/watch?v=vgELyZ9LXX4 and it lacked some of the details which I have included above. Also, the answer was also not clear.

How to merge a lot of trees into one single graph?

I have a few different trees, which resemble what the AST that compilers often deal with.

For example:

tree 1

( (a, b), (c, d) )

Imagine that each tree split represents the function “add”, then tree 1 simply says: add a and b, then add c and d, in the end, add the sum of these 2 sums as the final result.

tree 2

( ( (a, b), (c, d) ), (e, f) )

obviously, I can merge tree 1 into tree 2, because tree 2 is simply constructed by adding a sibling (e, f) to tree 1.

By doing so, I don’t need to re-visit a, b, c , d twice, I can simply add e and f, and add the result to the result of tree 1, to get the result of tree 2.

If I have a lot of these kind of trees, with overlapping (redundant sub-subtrees), is there an algorithm that can automatically create a graph that covers all the computation in the most efficient way ?

PS: all the trees share the same set of leaf,in this case, a b c d e f. Some trees are taller (deeper), some are shallower.

PS2: tree are not necessarily binary trees. A tree could have multiple

PS3: there could be a tree like ( (a,b), (g,h) ), i can still be partially merged with tree 1

Applying functions to leaves of nested list structure, when these leaves are more complex expression trees

Is there a way to apply a function h to the following nested list

{{a, b}, {c, d}, {{d, e }, {f, g}}}

where this should should become

{h[a], h[c], {h[d], h[f]}}

h is applied to the first element of each (deepest nested) sublist, replacing this sublist. The nesting is never deeper than the example displayed above. I.e. the expression tree for the list has at most depth 3.

The rest of the list structure pattern is preserved.

Here, a, c, d and f are not atoms. They are again expressions with heads. The heads are however not list-heads.

As an example, consider

{{u[a], u[b]}, {u[v[c]], d}, {{d, e}, {f, g}}}

applying h to this should yield:

{h[u[a]], h[u[v[c]]], {h[d], h[f]}}

In other words, h is applied to the leaves" of the list expression given above, where theseleaves” are more complicated expressions.

Can the pre-order traversal of two different trees be the same even thought they are different?

This question pretty much explains that they can, but does not show any examples of there being two different trees with the same pre-order traversal.

It is also mentioned that the in-order traversal of two different trees can be the same though they are structurally the same. Is there an example of this?

What is a polynomial-time algorithm for determining whether two trees, with colored nodes, are isomorphic or not

Provide any polynomial-time algorithm (even a large degree polynomial) which determines whether two rooted colored trees are isomorphic to each-other or not.

For example, consider the following two trees:

trees

Example trees T and U are isomorphic.
An isomorphism (bijection) is described in the table below:

  T          U   1          2   2          4   3          1   4          5   5          3   "white"    "green"   "blue"     "white"      

Below are some things to know about the problem:

  • Nodes are colored
  • edges are not colored.
  • Nodes are free to be any color. Adjacent nodes are allowed to be the same color.
  • which node is the root node of each tree cannot be changed.
  • children are un-ordered.
  • the tree is not necessarily a binary tree. a node could have 3 children, 4 children, 5, etc…

Formally, a colored tree is a tuple (VS, ES, root, color_set, color_map) such that:

  • VS is the vertex set
  • ES is the edge set
  • (VS, ES) is a undirected tree
  • root is a element of VS
  • color_set is a set of objects called “colors”
  • color_map is a mapping from VS to color_set
  • every element of color_set appears in the range of color_map at least once. That is, every color is applied to at least one node.

colored trees T and U are isomorphic if and only if there exists a bijection, PHI from the vertex set of T, VT, to the vertex set of U, VU such that:

  • the root of one tree is matched to the root of the other tree
  • for all nodes v, w in VT, {v, w} is an edge in tree T if and only if {PHI(v), PHI(w)} is an edge in tree U
  • for all nodes v, w in VT, v and w are the same color in tree T if and only if PHI(v), PHI(w) are the same color in tree U

Can I use trees and other climbable objects to get on Tenser’s Floating Disk?

I have been creating Tenser’s floating disk next to trees and climbing them to get on the disk. I usually do this just so I can rain literal fire on bad guys. I did this twice and I was asked to do skill checks. I’m fine with it, because I can understand that it’s a pretty cheese tactic.

My motive was that I can better avoid damage this way. I don’t move the disk around or anything, it just stays next to my makeshift ladder. Is this a legal usage of the Tenser’s floating disk spell?

Pair sum binary trees

Given a binary tree and an integer S, print all the pair of nodes whose sum equals S. Assume binary tree contains all unique elements. Note : In a pair, print the smaller element first. Order of different pair doesn’t matter.

Input format : Line 1 : Elements in level order form (separated by space) (If any node does not have left or right child, take -1 in its place)

Line 2 : Integer S

Output Format : Each pair in different line (pair elements separated by space)

Constraints : 1 <= N <= 1000 Sample Input 1: 5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -1 15 Sample Output 1: 5 10 6 9

My approach is as following :-

public class Solution {

/* Binary Tree Node class * * class BinaryTreeNode { T data; BinaryTreeNode left; BinaryTreeNode right;

    public BinaryTreeNode(T data) {         this.data = data;     } } */  public static void nodesSumToS(BinaryTreeNode<Integer> root, int sum) {     // Write your code here     if(root==null)         return;     nodesSumToS(root.left, sum);     if(root.left.data==sum-root.data) {         System.out.println(root.left.data +" "+ root.data);     }      nodesSumToS(root.right, sum);      if(root.right.data==sum-root.data) {         System.out.println(root.data +" "+ root.right.data);     }        return;  } 

}

Please tell what am i doing wrong and what is the correct approach for same

I have been doing this since long but i am not correct

This is not a homework problem i found this on web while searching questions on the above topic..

The most important thing is that this is a java program so please tell the code in java only…

Replace method using raw SQL with Expression trees and Reflection

I’m working with a large enterprise SQL Server database with many tables (dozens) that are used mainly as lookups to populate dropdown lists, etc. These tables all follow the convention of having (table)Name as the string value of the item. For example the State table has a StateName column with values like California, Florida, etc.

There is an old method used to load data for a dropdown that uses raw SQL, it looks like this:

    public async Task<IEnumerable<string>> GetLookupOptions(string table)     {         List<string> values = new List<string>();          using (var command = _context.Database.GetDbConnection().CreateCommand())         {             command.CommandText = $  "SELECT {table}Name FROM {table} WHERE IsApproved = 1";              _context.Database.OpenConnection();              using (var result = await command.ExecuteReaderAsync())             {                 do                 {                     while (result.Read())                     {                         values.Add(result.GetString(0));                     }                 } while (result.NextResult());             }         }          return values;     } 

This isn’t testable using InMemoryDatabase and only works against an actual database connection. I have rewritten it using Reflection and Expressions to query the DbContext.DbSet<T> based on the table name provided, like this:

    public IEnumerable<string> GetLookupOptions(string table)     {         // Get the Type for State and then replace its name with whatever entity this is querying.         // This is hacky but it's used so the AssemblyQualifiedName will always have correct         // version info.         Type t = typeof(State);         t = Type.GetType(t.AssemblyQualifiedName.Replace("State", table));          // Get lambda used to filter <table> where IsApproved is true.         object whereLamda = this.GetType()             .GetMethod(nameof(CreateWhereExpression), BindingFlags.NonPublic | BindingFlags.Instance)             .MakeGenericMethod(t)             .Invoke(this, new object[0]);          // Get lambda used to select <table>Name from <table>.         object selectLamda = this.GetType()             .GetMethod(nameof(CreateSelectExpression), BindingFlags.NonPublic | BindingFlags.Instance)             .MakeGenericMethod(t)             .Invoke(this, new object[] { table });          // Get the DbSet<T> for the <table>.         object set = _context.GetType()             .GetMethod("Set")             .MakeGenericMethod(t)             .Invoke(_context, new object [0]);          IEnumerable<MethodInfo> whereMethods = typeof(Enumerable)             .GetMethods().Where(m => m.Name == nameof(Enumerable.Where));          // Apply Where() method to DbSet.         object filteredApproved = whereMethods             .ElementAt(0)             .MakeGenericMethod(t)             .Invoke(set, new object[] { set, whereLamda });          IEnumerable<MethodInfo> selectMethods = typeof(Enumerable)             .GetMethods().Where(m => m.Name == nameof(Enumerable.Select));          // Apply Select() method to filtered query.         object r = selectMethods             .ElementAt(0)             .MakeGenericMethod(t, typeof(string))             .Invoke(filteredApproved, new object[] { filteredApproved, selectLamda });          return r as IEnumerable<string>;     }      private Func<T, string> CreateSelectExpression<T>(string tableName)     {         var param = Expression.Parameter(typeof(T));         var expr = Expression.Property(param, $  "{tableName}Name");          return Expression.Lambda<Func<T, string>>(expr, param).Compile();     }      private Func<T, bool> CreateWhereExpression<T>()     {         var param = Expression.Parameter(typeof(T));         var expr = Expression.Equal(             Expression.Property(param, "IsApproved"),             Expression.Constant(true));          return Expression.Lambda<Func<T, bool>>(expr, param).Compile();     } 

Currently working, but has issues:

  • Doesn’t run async like the old method does, looking for a “correct” way to implement this.
  • Getting the correct Where and Select methods from Reflection, I don’t like relying on ElementAt(). Both methods have overloads with 2 parameters.
  • Any and all other problems. Have I created a monster?