Find electric circuit with given resistance - algorithm

I need some help with the following problem:
Given a set of resistances, need to construct circuit with given resistance (i.e. we choose some resistors and construct circuit). Only parallel and sequential connection are allowed. So, the formal definition of such circuit is the following:
Circuit = Resistance | (Sequential (Circuit) (Circuit a)) |
(Parallel (Circuit) (Circuit))
The total number of circuits with N unlabeled resistors (where all resistors are used) is A000084 (Thanks Axel Kemper). But in my case resistors are labeled and I don't know how to check all circuits efficiently.
Number of resistors is about 15, is it possible to solve this problem?
UPD. Resistors may have different resistance. And of course, some resistances can't be achieved, in such case we just say that there is no solutions.

Integer sequence A000084 lists the Number of series-parallel networks with n unlabeled edges. Also called yoke-chains by Cayley and MacMahon. MacMahon's paper is online.
The first 15 elements of the sequence:
1, 2, 4, 10, 24, 66, 180, 522, 1532, 4624, 14136, 43930, 137908, 437502, 1399068
If the resistors have different resistance values, they are not "unlabeled".
The number of different overall-resistances is less than the number of networks.
Looking at the numbers, brute-force enumeration is probably feasible for moderate values of n.
It is not possible to match every conceivable total resistance exactly. As mentioned in a comment: The number of 15 resistors might be too small to reach the required value. Other example: If all 15 restors have 1 ohm each, the total resistance cannot be smaller than 1/15 ohm.
Look on page 70 of Analytic Combinatorics to find an illustration of the equivalence between a tree, a bracketed expression and a series-parallel graph:
Like mentioned in one of the comments, a search procedure like A* could be used to search the space of possible trees. The tree representation of the series-parallel network is also useful to determine the source-to-sink resistance with a simple recursive function.

Related

Effective clustering algorithm

I need help (preferably a full algorithm, but any hint or reference will be appreciated) with the following algorithmic problem:
We have a set of N elements. I can define a distance between any two elements, which satisfies the metric conditions. I need to group these elements into disjoint subsets (each element belonging to exactly one subset) according to the following rules:
The maximum distance between any two elements in each subset does not exceed specified threshold.
The number of the subsets is as small as possible.
If there is more than one possible grouping satisfying conditions (1) and (2), the maximum distance between any two elements in each subset should be as small as possible.
Example:
Assume we have the following points on a number axis: 1, 11, 12, 13, 23. The distance is simple the the difference between the points. Our distance threshold is 10. The two possible grouping satisfying conditions (1) and (2) are: (1, 11), (12), (13, 23) or (1), (11, 12, 13), (23). However, the condition (3) says that the latter grouping is the correct one.
In 1 dimensional data, sort your data, and divide into the desired number of bins, then move bin boundaries to optimize.
It gets more interesting in higher dimensionality. There, the problem will be NP hard. So finding the optimum will be expensive. You can indeed use clustering here: use complete-linkage clustering. For a O(n²) and O(n) memory approach, try CLINK. But in my experience, you will need to run this algorithm several times, on shuffled data, to get a good solution.

Determin Depth of Huffman Tree using Input Character pattern (or Frequency)?

I'd like to as a variation on this question regarding Huffman tree building. Is there anyway to calculate the depth of a Huffman tree from the input (or frequency), without drawing tree.
if there is no quick way, How the answer of that question was found? Specific Example is : 10-Input Symbol with Frequency 1 to 10 is 5.
If you are looking for an equation to take the frequencies and give you the depth, then no, no such equation exists. The proof is that there exist sets of frequencies on which you will have arbitrary choices to make in applying the Huffman algorithm that result in different depth trees! So there isn't even a unique answer to "What is the depth of the Huffman tree?" for some sets of frequencies.
A simple example is the set of frequencies 1, 1, 2, and 2, which can give a depth of 2 or 3 depending on which minimum frequencies are paired when applying the Huffman algorithm.
The only way to get the answer is to apply the Huffman algorithm. You can take some shortcuts to get just the depth, since you won't be using the tree at the end. But you will be effectively building the tree no matter what.
You might be able to approximate the depth, or at least put bounds on it, with an entropy equation. In some special cases the bounds may be restrictive enough to give you the exact depth. E.g. if all of the frequencies are equal, then you can calculate the depth to be the ceiling of the log base 2 of the number of symbols.
A cool example that shows that a simple entropy bound won't be strong enough to get the exact answer is when you use the Fibonacci sequence for the frequencies. This assures that the depth of the tree is the number of symbols minus one. So the frequencies 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, and 610 will result in a depth of 14 bits even though the entropy of the lowest frequency symbol is 10.64 bits.

Minimum # of resistors algorithm

I'm trying to design a simple algorithm that takes a vector of standard resistor values along with an input of a desired resistance value and then goes through series and parallel combinations to figure out the minimum number of standard resistors require to achieve that equivalent resistance doing so by any combination of series and parallel resistors, whichever takes the least.
Anyone got any ideas? If I wanted parallel only or series only it would be a lot easier, but not sure how to combine the two for minimum total number of resistors.
FYI if you don't know total series R = S1 + S2 + ...+ SN and Total parallel R = (1/S1 + 1/S2 + ... + 1/SN)^-1
Perhaps a genetic algorithm would be best? I don't know the calculation for the big-O notation for this but it looks exponential: O(cⁿ).
I found this comment on another site's post, it's the number of variations that can be attained with resistors of different values (ie brute force):
Networks with 1 resistors: 1
Networks with 2 resistors: 2
Networks with 3 resistors: 10
Networks with 4 resistors: 68
Networks with 5 resistors: 558
Networks with 6 resistors: 5186
Networks with 7 resistors: 53805
A genetic algorithm would avoid brute force, possibly allowing you to come to an answer much sooner. Unfortunately, it cannot guarantee answers with the minimal amount of resistors. It is likely to find close equivalent resistor values with much less work, and it can be weight so that it favours the fewest possible resistors.
I will keep researching this and post anything else I find.
Create an object to hold a resistance value, plus two resistances from which it came, plus the operation used to obtain the value from the two previous values (series or parallel).
Use some collection data structure like a Set or an ArrayList to hold resistance objects. Your set S1 initially contains just the resistors you have (networks of 1 resistor). Now create a set S2 which is all combinations (series or parallel) of an element of S1 with an element of S1. S3 is combinations of S1 and S2. S4 is combinations of S1 and S3, plus combinations of S2 and S2. Continue until you have a member of Sk which is within tolerance (1%, 5%, or 10%, say) of your target value. The resulting resistance object can be unwrapped one step at a time to find the way it was built up.
One other thing you need to consider is how the tolerances combine. Errors will propagate, so you may need 1% resistors to start in order to achieve the resistance you want at the end to a 5% tolerance, say.

Efficient algorithm for eliminating nodes in "graph"?

Suppose I have a a graph with 2^N - 1 nodes, numbered 1 to 2^N - 1. Node i "depends on" node j if all the bits in the binary representation of j that are 1, are also 1 in the binary representation of i. So, for instance, if N=3, then node 7 depends on all other nodes. Node 6 depends on nodes 4 and 2.
The problem is eliminating nodes. I can eliminate a node if no other nodes depend on it. No nodes depend on 7; so I can eliminate 7. After eliminating 7, I can eliminate 6, 5, and 3, etc. What I'd like is to find an efficient algorithm for listing all the possible unique elimination paths. (that is, 7-6-5 is the same as 7-5-6, so we only need to list one of the two). I have a dumb algorithm already, but I think there must be a better way.
I have three related questions:
Does this problem have a general name?
What's the best way to solve it?
Is there a general formula for the number of unique elimination paths?
Edit: I should note that a node cannot depend on itself, by definition.
Edit2: Let S = {s_1, s_2, s_3,...,s_m} be the set of all m valid elimination paths. s_i and s_j are "equivalent" (for my purposes) iff the two eliminations s_i and s_j would lead to the same graph after elimination. I suppose to be clearer I could say that what I want is the set of all unique graphs resulting from valid elimination steps.
Edit3: Note that elimination paths may be different lengths. For N=2, the 5 valid elimination paths are (),(3),(3,2),(3,1),(3,2,1). For N=3, there are 19 unique paths.
Edit4: Re: my application - the application is in statistics. Given N factors, there are 2^N - 1 possible terms in statistical model (see http://en.wikipedia.org/wiki/Analysis_of_variance#ANOVA_for_multiple_factors) that can contain the main effects (the factors alone) and various (2,3,... way) interactions between the factors. But an interaction can only be present in a model if all sub-interactions (or main effects) are present. For three factors a, b, and c, for example, the 3 way interaction a:b:c can only be in present if all the constituent two-way interactions (a:b, a:c, b:c) are present (and likewise for the two-ways). Thus, the model a + b + c + a:b + a:b:c would not be allowed. I'm looking for a quick way to generate all valid models.
It seems easier to think about this in terms of sets: you are looking for families of subsets of {1, ..., N} such that for each set in the family also all its subsets are present. Each such family is determined by the inclusion-wise maximal sets, which must be overlapping. Families of pairwise overlapping sets are called Sperner families. So you are looking for Sperner families, plus the union of all the subsets in the family. Possibly known algorithms for enumerating Sperner families or antichains in general are useful; without knowing what you actually want to do with them, it's hard to tell.
Thanks to #FalkHüffner's answer, I saw that what I wanted to do was equivalent to finding monotonic Boolean functions for N arguments. If you look at the figure on the Wikipedia page for Dedekind numbers (http://en.wikipedia.org/wiki/Dedekind_number) the figure expresses the problem graphically. There is an algorithm for generating monotonic Boolean functions (http://www.mathpages.com/home/kmath094.htm) and it is quite simple to construct.
For my purposes, I use the algorithm, then eliminate the first column and last row of the resulting binary arrays. Starting from the top row down, each row has a 1 in the ith column if one can eliminate the ith node.
Thanks!
You can build a "heap", in which at depth X are all the nodes with X zeros in their binary representation.
Then, starting from the bottom layer, connect each item to a random parent at the layer above, until you get a single-component graph.
Note that this graph is a tree, i.e., each node except for the root has exactly one parent.
Then, traverse the tree (starting from the root) and count the total number of paths in it.
UPDATE:
The method above is bad, because you cannot just pick a random parent for a given item - you have a limited number of items from which you can pick a "legal" parent... But I'm leaving this method here for other people to give their opinion (perhaps it is not "that bad").
In any case, why don't you take your graph, extract a spanning-tree (you can use Prim algorithm or Kruskal algorithm for finding a minimal-spanning-tree), and then count the number of paths in it?

Crossover operator for permutations

i'm trying to solve the problem of crossover in genetic algorithm on my permutations.
Let's say I have two permutations of 20 integers. I want to crossover them to get two children. Parents have the same integers inside, but the order is different.
Example:
Parent1:
5 12 60 50 42 21 530 999 112 234 15 152 601 750 442 221 30 969 113 134
Parent2:
12 750 42 113 530 112 5 23415 60 152 601 999 442 221 50 30 969 134 21
Let it be that way - how can I get children of these two?
What you are looking for is ordered crossover. There is an explanation for the Travelling Salesman Problem here.
Here is some Java code that implements the partially mapped crossover (PMX) variant.
The choice of crossover depends on whether the order or the absolute position of the integers is important to the fitness. In HeuristicLab (C#) we have implemented several popular ones found in the literature which include: OrderCrossover (2 variants), OrderBasedCrossover, PartiallyMatchedCrossover, CyclicCrossover (2 variants), EdgeRecombinationCrossover (ERX), MaximalPreservativeCrossover, PositionBasedCrossover and UniformLikeCrossover. Their implementation can be found together with reference to a scientific source in the HeuristicLab.Encodings.PermutationEncoding plugin. The ERX makes sense only for the TSP or TSP-like problems. The CX is position-based, the PMX is partly position partly order based, but more towards position. The OX is solely order based.
Beware that our implementations assume a continous numbered permutation with integers from 0 to N-1. You have to map them to this range first.
According to my research and implementations of genetic operators. Many types of crossover operators exist for the order coding (i.e. repetition of genes not allowed, like in TSP). In general, I like to think that there are two main families:
The ERX-family
A list of neighborhood is used to store the neighbors of each node in both parents. Then, The child is generated using only the list. ERX is known to be more respectful and alleles transmitting, which basically means that the links between genes are not likely to be broken.
Examples of ERX-like operators include: Edge Recombination (ERX), Edge-2, Edge-3, Edge-4, and Generalized Partition Crossover (GPX).
OX-like crossovers
Two crossover points are chosen. Then, the genes between the points are swapped between the two parents. Since repetitions are not allowed, each crossover proposes a technique to avoid/eliminate repetitions. These crossover operators are more disruptive than ERX.
Example of OX-like crossovers: Order crossover (OX), Maximal Preservative Crossover (MPX), and Partial-Mapped Crossover (PMX).
The first family (ERX) performs better in plain genetic algorithms. While the second family is more suited in a hybrid genetic algorithm or memetic algorithm (use of local search). This paper explains it in details.
In Traveling Salesrep Problem (TSP), you want the order to visit a list of cities, and you want to visit each city exactly once. If you encode the cities directly in the genome, then a naive crossover or mutation will often generate an invalid itinerary.
I once came up with a novel approach to solving this problem: Instead of encoding the solution directly in the genome, I instead encoded a transformation that would re-order a canonical list of values.
Given the genome [1, 2, 4, 3, 2, 4, 1, 3], you'd start with the list of cities in some arbitrary order, say alphabetical:
Atlanta
Boston
Chicago
Denver
You'd then you'd take each pair of values from the genome and swap the cities in those positions. So, for the genome above, you'd swap those in 1 and 2, and then those in 4 and 3, and then those in 2 and 4, and finally those in 1 and 3. You'd end up with:
Denver
Chicago
Boston
Atlanta
With this technique, you can use any type of crossover or mutation operation and still always get a valid tour. If the genome is long enough, then entire solution space can be explored.
I've used this for TSP and other optimization problems with lots of success.

Resources