Is there a way possible in python to find the number of node which are connected to two Leaf in a full binary tree - binary-tree

I am not a computer programmer but I need this idea for one of my projects. I would explain what I am trying to achieve:
These are two pictures picture picture. Can we write a code to find the number of nodes whose both children are the terminal node? If yes then I would spend the time to form the algorithm or I may try to achieve it in some other way.
If achieved, the answer to my problem would be 3 (1+2), based on above pictures.
NOTE : 1) This are Full Binary tree. 2) There are many more trees. 3) I am Using Python

Related

Data structure to represent a graph

Having a couple of cities and their locations I want to create a data structure that would represent a graph like this. This graph represent all possible paths that can be taken in order to visit every city only once:
My question is, since this is probably a very common problem, is there an algorithm or already made data structure to represent this? The programming language is not important (although I would prefer java).
Your problem seems very close to the traveling salesman problem, a classic among the classics.
As you did intuite, the graph that will represent all the possible solutions is indeed a tree (the paths from the root to any of its leaf should represent a solution).
From there, you can ask yourself several questions:
Is the first city that I'll visit an important piece of information, or is it only the order that matters ? For instance, is London-Warsaw-Berlin-Lidz equivalent to Warsaw-Berlin-Lidz-London ?
Usually, we consider these solutions as being equivalent to solve a TSP, but it might not be the case for you.
Did you see the link between a potential solution to the TSP and a permutation ? Actually, what you're looking for is a way (and the data structure that goes with it) to generate all the permutations of a given set(your set of cities).
With these two points in mind, we can think about a way to generate such a tree. A good strategy to work with trees is to think recursively.
We have a partial solution, meaning the k first cities. Then, the next possible city can be any among the n-k cities remaining. That gives the following pseudo-code.
get_all_permutations(TreeNode node, Set<int>not_visited){
for (city in not_visited){
new_set = copy(not_visited);
new_set.remove(city);
new_node = new TreeNode();
new_node.city = city;
node.add_child(new_node);
get_all_permutations(new_node, new_set);
}
}
This will build the tree recursively.
Depending on your answer to the first point I mentioned (about the importance of the first city), you might want to assign a city to the root node, or not.
Some good points to look in, if you want to go further with this kind of problem/thinking, are enumeration algorithms, and recursive algorithms. They're generally a good option when your goal is to enumerate all the elements of a set. But they're also generally an inefficient way to solve problems (for example, in the case of the TSP, solving using this algorithm results in a very inefficient approach. There are some much much better ones).
This tree is bad. There are redundant data in it. For instance connection between nodes 2 and 4 occurs three times in the tree. You want a "structure" that automatically gives the solution to your problem, so that it's easier for you, but that's not how problem solving works. Input data is one set of data, output data is another set of data, and they could appear similar, but they can also be quite different.
One simple matrix with one triangle empty and the other containing data should have all the information you need. Coordinates of the matrix are nodes, cells are distances. This is your input data.
What you do with this matrix in your code is a different matter. Maybe you want to write all possible paths. Then write them. Use input data and your code to produce output data.
What you are looking for is actually a generator of all permutations. If you keep one city fixed as the first one (London, in your diagram), then you need to generate all permutations of the list of all your remaining nodes (Warsaw, Łódź, Berlin).
Often such an algorithm is done recursively by looping over all elements, taking it out and doing this recursively for the remaining elements. Often libraries are use to achieve this, e. g. itertools.permutations in Python.
Each permutation generated this way should then be put in the resulting graph you originally wanted. For this you can use any graph-representation you would like, e. g. a nested dictionary structure:
{ a: { b: { c: d,
d: c },
c: { b: d,
d, b },
d: { b: c,
c: b } } }

create decision tree from data

I'm trying to create decision tree from data. I'm using the tree for guess-the-animal-game kind of application. User answers questions with yes/no and program guesses the answer. This program is for homework.
I don't know how to create decision tree from data. I have no way of knowing what will be the root node. Data will be different every time. I can't do it by hand. My data is like this:
Animal1: property1, property3, property5
Animal2: property2, property3, property5, property6
Animal3: property1, property6
etc.
I searched stackoverflow and i found ID3 and C4.5 algorithms. But i don't know if i should use them.
Can someone direct me, what algorithm should i use, to build decision tree in this situation?
I searched stackoverflow and i found ID3 and C4.5 algorithms. But i
don't know if i should use them.
Yes, you should. They are very commonly used decision trees, and have some nice open source implementations for them. (Weka's J48 is an example implementation of C4.5)
If you need to implement something from scratch, implementing a simple decision tree is fairly simple, and is done iteratively:
Let the set of labled samples be S, with set of properties P={p1,p2,...,pk}
Choose a property pi
Split S to two sets S1,S2 - S1 holds pi, and S2 do not. Create two children for the current node, and move S1 and S2 to them respectively
Repeat for S'=S1, S'=S2 for each of the subsets of samples, if they are not empty.
Some pointers:
At each iteration you basically split the current data to 2 subsets, the samples that hold pi, and the data that does not. You then create two new nodes, which are the current node's children, and repeat the process for each of them, each with the relevant subset of data.
A smart algorithm chooses the property pi (in step 2) in a way that minimizes the tree's height as much as it can (finding the best solution is NP-Hard, but there are greedy approaches to minimize entropy, for example).
After the tree is created, some pruning to it is done, in order to avoid overfitting.
A simple extension of this algorithm is using multiple decision trees that work seperately - this is called Random Forests, and is empirically getting pretty good results usually.

Graph search algorithm

I was blocked on the following question, can you please share your light on it?
one more: how to avoid the loop?
I believe the only sensible solution, assuming you don't have any information on the number of nodes in the network, is to take random left/right decisions at each node. Your route will form a random walk over the network and will eventually (after enough time steps) visit all nodes including the one with the hotel.
The number of steps required to get to the hotel will obviously depend on the size of the network but I believe it will be polynomial in the N, where N is the initial (but unknown) shortest path length between your starting point and the hotel. Without proof, I suggest teh average number of steps to solve for a network will scale as N^2.
Any deterministic path may run the risk of failing to visit one or more nodes.
Just found a counter example so the guess below is wrong.
I just tried a few examples - and did not try to explicitly construct a counter example - but it looks like alternating between going left and going right will visit all nodes without getting stuck in a loop. This may be the consequence of the graph being finite, 3-regular and planar. But I am very skeptic because I was unable to find this property mentioned anywhere and a handful of examples is far from a proof.

Binary tree, deleting item and reconnecting node

I'm learning data structures and found out that for binary search trees, there are two ways to reconnect node when you delete item. Are those two ways (below) correct?
Link to the image to see it non-resized
Yes, they are. Note that you could also do the "mirror image" version of each way, so it's actually 4 ways in total.
In fact, there are quite few ways that would produce a valid binary tree. All you need to take care of is that the left child of a node is less than the node itself, and the right child is more. However the ways you have listed are the simplest ones that are typically used (unless it's a balanced tree and you need to rebalance it).
The two methods look correct. The first method does re-balance the tree while the second simply does the connect.

branch and bound

Can someone explain the branch and bound search technique for me? I need to find a path with the smallest cost from any start node to an end node of any random graph using branch and bound search algorithm.
The basic idea of B & B is:
When solving an optimisation problem ("Find an X satisfying criteria Y so as to minimise the cost f(X)"), you build a solution piece by piece -- at any point in time, you have a partial solution, which has a cost.
If the nature of the problem is such that the cost of a partial solution can only stay the same or go up as you continue adding pieces to it, then you know that there's no point continuing to add pieces to a partial solution if there's already a full solution with lower cost. In this case, you can abandon (or "prune", or "fathom") further processing of this partial solution.
Many problems have the latter property, making B & B a widely applicable algorithm technique.
The process of searching for solutions can be represented by a search tree, where the root node represents the starting point where no decisions have been made, and each edge leading from a node represents a decision about something to be included in a partial solution. Each node is a partial solution comprising the decisions made (edges) from the root to that node.
Example: if we want to solve a Sudoku puzzle, the root node would represent the board with just the originally supplied numbers filled in; there might be 9 edges from this root, each representing the decision to assign a number 1-9 to the top-left cell. Each of those 9 partial solution nodes could have 8 branches, representing the valid assignments to the cell at position (1, 2), and so on. Usually, each edge represents a recursion step in a program.
With B & B, in the best case a good solution is found early, meaning that unpromising areas of the search tree can be pruned near the root; but in the worst case, the entire tree of valid solutions will be generated. For this reason B & B is usually only used to solve problems for which no faster algorithm is known (such as NP-hard problems).
This link provides a graphical representation of concepts related to B & B.
This link provides an explanation of the algorithm and sample C# code in a downloadable zip file.
Hope this helps.
There are a lot of references about branch and bound algorithms in the web.
here you can find some theoretical explanation.
whereas the code in C# is here
Fantastic answer #j_random_hacker !!!!
See pg 439 (example 18.2) in Papadimitriou and Steiglitz, Combinatorial Optimization.
This book is a classic, and it discusses your exact problem.

Resources