Clarification of an atomic value example and its right definition - xpath

Here I found the following example of an atomic value: J K. Rowling.
But in the file.xml I saw that the author element, whose value is J K. Rowling, has a parent, its parent should be the book element. Why is "J K. Rowling" an atomic value?
The definition of an atomic value is (from w3schools): Atomic values are nodes with no children or parent. If this definition is not correct, what is the right one?
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

Related

Finding certain arrangements of all 2-combinatons for a given list

Given a list L of an even number (2k) of elements, I'm looking for an algorithm to produce a list of 2k-1 sublists with the following properties:
each sublist includes exactly k 2-combinations (pairs where the order does not matter) of elements from L,
each sublist includes every elements from L exactly once, and
the union of all elements from all sublists is exactly the set of all possible 2-combinations of the elements from L.
For example, if the input list is L = [a, b, c, d], we have k = 2 with 3 sublists, each including 2 pairs. A possible solution would look like [[ab, cd], [ac, bd], [ad, bc]]. If we ignore the ordering for all elements in the lists (think of all lists as sets), it turns out that this is also the only solution for k = 2.
My aim now is not only to find a single solution but all possible solutions. As the number of involved combinations grows pretty quickly, it would be nice to have all results be constructed in a clever way instead of generating a huge list of candidates and removing the elements from it that don't satisfy the given properties. Such a naïve algorithm could look like the following:
Find the set C of all 2-combinations for L.
Find the set D of all k-combinations for C.
Choose all sets from D that union equals L, call the new set D'.
Find the set E of all (2k-1)-combinations for D'.
Choose all sets from E that union is the set C, and let the new set be the final output.
This algorithm is easy to implement but it's incredibly slow for bigger input lists. So is there a way to construct the result list more efficently?
Edit: Here is the result for L = [a,b,c,d,e,f] with k = 3, calculated by the above algorithm:
[[[ab,cd,ef],[ac,be,df],[ad,bf,ce],[ae,bd,cf],[af,bc,de]],
[[ab,cd,ef],[ac,bf,de],[ad,be,cf],[ae,bc,df],[af,bd,ce]],
[[ab,ce,df],[ac,bd,ef],[ad,be,cf],[ae,bf,cd],[af,bc,de]],
[[ab,ce,df],[ac,bf,de],[ad,bc,ef],[ae,bd,cf],[af,be,cd]],
[[ab,cf,de],[ac,bd,ef],[ad,bf,ce],[ae,bc,df],[af,be,cd]],
[[ab,cf,de],[ac,be,df],[ad,bc,ef],[ae,bf,cd],[af,bd,ce]]]
All properties are satisfied:
each sublist has k = 3 2-combinations,
each sublist only includes each element once, and
the union of all 2k-1 = 5 sublists for one solution is exactly the set of all possible 2-combinations for L.
Edit 2: Based on user58697's answer, I improved the calculation algorithm by using the round-robin tournament scheduling:
Let S be the result set, starting with an empty set, and P be the set of all permutations of L.
Repeat the following until P is empty:
Select an arbitrary permutation from P
Perform full RRT scheduling for this permutation. In each round, the arrangement of elements from L forms a permutation of L. Remove all these 2k permutations from P.
Add the resulting schedule to S.
Remove all lists from S if the union of their sublists has duplicate elements (i.e. doesn't add up to all 2-combinations of L).
This algorithm is much more performant than the first one. I was able to calculate the number of results for k = 4 as 960 and k = 5 as 67200. The fact that there doesn't seem to be an OEIS result for this sequence makes me wonder if the numbers are actually correct, though, i.e. if the algorithm is producing the complete solution set.
It is a round-robin tournament scheduling:
A pair is a match,
A list is a round (each team plays with some other team)
A set of list is an entire tournament (each team plays each other team exactly once).
Take a look here.
This was an interesting question. In the process of answering it (basically after writing the program included below, and looking up the sequence on OEIS), I learned that the problem has a name and rich theory: what you want is to generate all 1-factorizations of the complete graph K2k.
Let's first restate the problem in that language:
You are given a number k, and a list (set) L of size 2k. We can view L as the vertex set of a complete graph K2k.
For example, with k=3, L could be {a, b, c, d, e, f}
A 1-factor (aka perfect matching) is a partition of L into unordered pairs (sets of size 2). That is, it is a set of k pairs, whose disjoint union is L.
For example, ab-cd-ef is a 1-factor of L = {a, b, c, d, e, f}. This means that a is matched with b, c is matched with d, and e is matched with f. This way, L has been partitioned into three sets {a, b}, {c, d}, and {e, f}, whose union is L.
Let S (called C in the question) denote the set of all pairs of elements of L. (In terms of the complete graph, if L is its vertex set, S is its edge set.) Note that S contains (2k choose 2) = k(2k-1) pairs. So for k = 0, 1, 2, 3, 4, 5, 6…, S has size 0, 1, 6, 15, 28, 45, 66….
For example, S = {ab, ac, ad, ae, af, bc, bd, be, bf, cd, ce, cf, de, df, ef} for our L above (k = 3, so |S| = k(2k-1) = 15).
A 1-factorization is a partition of S into sets, each of which is itself a 1-factor (perfect matching). Note that as each of these matchings has k pairs, and S has size k(2k-1), the partition has size 2k-1 (i.e., is made of 2k-1 matchings).
For example, this is a 1-factorization: {ab-cd-ef, ac-be-df, ad-bf-ce, ae-bd-cf, af-bc-de}
In other words, every element of S (every pair) occurs in exactly one element of the 1-factorization, and every element of L occurs exactly once in each element of the 1-factorization.
The problem asks to generate all 1-factorizations.
Let M denote the set of all 1-factors (all perfect matchings) of L. It is easy to prove that M contains (2k)!/(k!2^k) = 1×3×5×…×(2k-1) matchings. For k = 0, 1, 2, 3, 4, 5, 6…, the size of M is 1, 1, 3, 15, 105, 945, 10395….
For example, for our L above, M = {ab-cd-ef, ab-ce-df, ab-cf-de, ac-bd-ef, ac-be-df, ac-bf-de, ad-bc-ef, ad-be-cf, ad-bf-ce, ae-bc-df, ae-bd-cf, ae-bf-cd, af-bc-de, af-bd-ce, af-be-cd} (For k=3 this number 15 is the same as the number of pairs, but this is just a coincidence as you can from the other numbers: this number grows much faster than the number of pairs.)
M is easy to generate:
def perfect_matchings(l):
if len(l) == 0:
yield []
for i in range(1, len(l)):
first_pair = l[0] + l[i]
for matching in perfect_matchings(l[1:i] + l[i+1:]):
yield [first_pair] + matching
For example, calling perfect_matchings('abcdef') yields the 15 elements ['ab', 'cd', 'ef'], ['ab', 'ce', 'df'], ['ab', 'cf', 'de'], ['ac', 'bd', 'ef'], ['ac', 'be', 'df'], ['ac', 'bf', 'de'], ['ad', 'bc', 'ef'], ['ad', 'be', 'cf'], ['ad', 'bf', 'ce'], ['ae', 'bc', 'df'], ['ae', 'bd', 'cf'], ['ae', 'bf', 'cd'], ['af', 'bc', 'de'], ['af', 'bd', 'ce'], ['af', 'be', 'cd'] as expected.
By definition, a 1-factorization is a partition of S into elements from M. Or equivalently, any (2k-1) disjoint elements of M form a 1-factorization. This lends itself to a straightforward backtracking algorithm:
start with an empty list (partial factorization)
for each matching from the list of perfect matchings, try adding it to the current partial factorization, i.e. check whether it's disjoint (it should not contain any pair already used)
if fine, add it to the partial factorization, and try extending
In code:
matching_list = []
pair_used = defaultdict(lambda: False)
known_matchings = [] # Populate this list using perfect_matchings()
def extend_matching_list(r, need):
"""Finds ways of extending the matching list by `need`, using matchings r onwards."""
if need == 0:
use_result(matching_list)
return
for i in range(r, len(known_matchings)):
matching = known_matchings[i]
conflict = any(pair_used[pair] for pair in matching)
if conflict:
continue # Can't use this matching. Some of its pairs have already appeared.
# Else, use this matching in the current matching list.
for pair in matching:
pair_used[pair] = True
matching_list.append(matching)
extend_matching_list(i + 1, need - 1)
matching_list.pop()
for pair in matching:
pair_used[pair] = False
If you call it with extend_matching_list(0, len(l) - 1) (after populating known_matchings), it generates all 1-factorizations. I've put the full program that does this here. With k=4 (specifically, the list 'abcdefgh'), it outputs 6240 1-factorizations; the full output is here.
It was at this point that I fed the sequence 1, 6, 6240 into OEIS, and discovered OEIS A000438, sequence 1, 1, 6, 6240, 1225566720, 252282619805368320,…. It shows that for k=6, the number of solutions ≈2.5×1017 means that we can give up hope of generating all solutions. Even for k=5, the ≈1 billion solutions (recall that we're trying to find 2k-1=9 disjoint sets out of the |M|=945 matchings) will require some carefully optimized programs.
The first optimization (which, embarrassingly, I only realized later by looking closely at trace output for k=4) is that (under natural lexicographic numbering) the index of the first matching chosen in the partition cannot be greater than the number of matchings for k-1. This is because the lexicographically first element of S (like "ab") occurs only in those matchings, and if we start later than this one we'll never find it again in any other matching.
The second optimization comes from the fact that the bottleneck of a backtracking program is usually the testing for whether a current candidate is admissible. We need to test disjointness efficiently: whether a given matching (in our partial factorization) is disjoint with the union of all previous matchings. (Whether any of its k pairs is one of the pairs already covered by earlier matchings.) For k=5, it turns out that the size of S, which is (2k choose 2) = 45, is less than 64, so we can compactly represent a matching (which is after all a subset of S) in a 64-bit integer type: if we number the pairs as 0 to 44, then any matching can be represented by an integer having 1s in the positions corresponding to elements it contains. Then testing for disjointness is a simple bitwise operation on integers: we just check whether the bitwise-AND of the current candidate matching and the cumulative union (bitwise-OR) of previous matchings in our partial factorization is zero.
A C++ program that does this is here, and just the backtracking part (specialized for k=5) does not need any C++ features so it's extracted out as a C program here. It runs in about 4–5 hours on my laptop, and finds all 1225566720 1-factorizations.
Another way to look at this problem is to say that two elements of M have an edge between them if they intersect (have a pair (element of S) in common), and that we're looking for all maximum independent set in M. Again, the simplest way to solve that problem would still probably be backtracking (we'd write the same program).
Our programs can be made quite a lot more efficient by exploiting the symmetry in our problem: for example we could pick any matching as our first 1-factor in the 1-factorization (and then generate the rest by relabelling, being careful not to avoid duplicates). This is how the number of 1-factorizations for K12 (the current record) was calculated.
A note on the wisdom of generating all solutions
In The Art of Computer Programming Volume 4A, at the end of section 7.2.1.2 Generating All Permutations, Knuth has this important piece of advice:
Think twice before you permute. We have seen several attractive algorithms for permutation generation in this section, but many algorithms are known by which permutations that are optimum for particular purposes can be found without running through all possibilities. For example, […] the best way to arrange records on a sequential storage […] takes only O(n log n) steps. […] the assignment problem, which asks how to permute the columns of a square matrix so that the sum of the diagonal elements is maximized […] can be solved in at most O(n3) operations, so it would be foolish to use a method of order n! unless n is extremely small. Even in cases like the traveling salesrep problem, when no efficient algorithm is known, we can usually find a much better approach than to examine every possible solution. Permutation generation is best used when there is good reason to look at each permutation individually.
This is what seems to have happened here (from the comments below the question):
I wanted to calculate all solutions to run different attribute metrics on these and find an optional match […]. As the number of results seems to grow quicker than expected, this is impractical.
Generally, if you're trying to "generate all solutions" and you don't have a very good reason for looking at each one (and one almost never does), there are many other approaches that are preferable, ranging from directly trying to solve an optimization problem, to generating random solutions and looking at them, or generating solutions from some subset (which is what you seem to have done).
Further reading
Following up references from OEIS led to a rich history and theory.
On 1-factorizations of the complete graph and the relationship to round robin schedules, Gelling (M. A. Thesis), 1973
On the number of 1-factorizations of the complete graph, Charles C Lindner, Eric Mendelsohn, Alexander Rosa (1974?) -- this shows that the number of nonisomorphic 1-factorizations on K2n goes to infinity as n goes to infinity.
E. Mendelsohn and A. Rosa. On some properties of 1-factorizations of complete graphs. Congr. Numer, 24 (1979): 739–752
E. Mendelsohn and A. Rosa. One factorizations of the complete graph: A survey. Journal of Graph Theory, 9 (1985): 43–65 (As long ago as 1985, this exact question was studied well-enough to need a survey!)
Via papers of Dinitiz:
D. K. Garnick and J. H. Dinitz, On the number of one-factorizations of the complete graph on 12 points, Congressus Numerantium, 94 (1993), pp. 159-168. They announced they were computing the number of nonisomorphic 1-factorizations of K12. Their algorithm was basically backtracking.
Jeffrey H. Dinitz, David K. Garnick, Brendan D. McKay: There are 526,915,620 nonisomorphic one-factorizations of K12 (also here), Journal of Combinatorial Designs 2 (1994), pp. 273 - 285: They completed the computation, and reported the numbers they found for K12 (526,915,620 nonisomorphic, 252,282,619,805,368,320 total).
Various One-Factorizations of Complete Graphs by Gopal, Kothapalli, Venkaiah, Subramanian (2007). A paper that is relevant to this question, and has many useful references.
W. D. Wallis, Introduction to Combinatorial Designs, Second Edition (2007). Chapter 10 is "One-Factorizations", Chapter 11 is "Applications of One-Factorizations". Both are very relevant and have many useful references.
Charles J. Colbourn and Jeffrey H. Dinitz, Handbook of Combinatorial Designs, Second Edition (2007). A goldmine. See chapters VI.3 Balanced Tournament Designs, VI.51 Scheduling a Tournament, VII.5 Factorizations of Graphs (including its sections 5.4 Enumeration and Tables, 5.5 Some 1-Factorizations of Complete Graphs), VII.6 Computational Methods in Design Theory (6.2 Exhaustive Search). This last chapter references:
[715] How K12 was calculated ("orderly algorithm"), a backtracking -- the Dinitz-Garnick-McKay paper mentioned above
[725] “Contains, among many other subjects related to factorization, a fast algorithm for finding 1-factorizations of K2n.” ("Room squares and related designs", J. H. Dinitz and S. R. Stinson)
[1270] (P. Kaski and P. R. J. Östergård, One-factorizations of regular graphs of order 12, Electron. J. Comb. 12, Research Paper 2, 25 pp. (2005))
[1271] “Contains the 1-factorizations of complete graphs up to order 10 in electronic form.” (P. Kaski and P. R. J. Östergård, Classification Algorithms for Codes and Designs, Springer, Berlin, 2006.)
[1860] “A survey on perfect 1-factorizations of K2n” (E. S. Seah, Perfect one-factorizations of the complete graph—A survey, Bull. Inst. Combin. Appl. 1 (1991) 59–70)
[2107] “A survey of 1-factorizations of complete graphs including most of the material of this chapter.” W. D. Wallis, One-factorizations of complete graphs, in Dinitz and Stinson (ed), Contemporary Design Theory, 1992
[2108] “A book on 1-factorizations of graphs.” W. D. Wallis, "One-Factorizations", Kluwer, Dordrecht, 1997
Some other stuff:
*Factors and Factorizations of Graphs by Jin Akiyama and Mikio Kano (2007). This looks like a great book. “Frank Harary predicted that graph theory will grow so much that each chapter of his book Graph Theory will eventually expand to become a book on its own. He was right. This book is an expansion of his Chapter 9, Factorization.” There's not much about this particular topic (1-factorizations of complete graphs), but there is a proof in Chapter 4 (Theorem 4.1.1) that K2n always has a 1-factorization.
Papers on special types of 1-factorizations:
[Symmetry Groups Of] Some Perfect 1-Factorizations Of Complete Graphs, B. A. Anderson, 1977 (1973). Considers 1-factorizations that are in fact "perfect", having the property that the union of any two 1-factors (matchings) is a Hamiltonian cycle. (There's one up to isomorphism for K2k k ≤ 5, and two for K12.)
On 4-semiregular 1-factorizations of complete graphs and complete bipartite graphs.
Low Density MDS Codes and Factors of Complete Graphs -- also about perfect 1-factorizations
Self-invariant 1-Factorizations of Complete Graphs and Finite Bol Loops of Exponent 2
See also OEIS index entry for [sequences related to tournaments].
AMS feature column: Mathematics and Sports (April 2010) -- despite the overly broad name, is quite related.

xpath expression for selecting all leaf nodes

I stumbled across the following XPATH expression applied on a certain root node:
.//*[not(child::*)]
child::* selects all the children nodes of the current node.
.//* selects all nodes under the current node (including their children)
As a result, I would intuitively say that the expression selects.. all leaf nodes? ie. nodes that do not have any more children.
That is, not(child::*) actually verifies the number of children to be 0.
Let's apply this expression on the root node of the following tree:
<root>
<A>
<C/>
<D/>
<E>
<F/>
<G>
<H/>
</G>
</E>
</A>
<B>
<I>
<J/>
<K/>
</I>
</B>
</root>
Am I correct when I say that my expression selects C D F H J K?
Yes, you are correct. The expression selects nodes with no child, a.k.a. leaf node. You can test it with an XPath tester like this one.

In an acyclic graph (E,V), find paths which sum up to 0

Acyclic graph(tree) (E,V) given. Find all paths (path is a sequence of neighbouring nodes), where nodes on the path sum to 0.
Brute force approach would be to generate all pairs of nodes, for each pair check if the nodes' values sum to 0. This takes O(N^3) time and O(N) space complexity. Please suggest faster solution.
You can do this in O(n^2) time by running a depth first search for each choice of start node.
The depth first search computes the distance for each node from the root start node. A path is found whenever the distance is 0.
There are n nodes, and each DFS takes O(n) so the total running time is O(n^2).
It is hard to do much better in general because if all your nodes have weight 0, then you need to output O(n^2) answers.
Python code
import networkx as nx
G=nx.Graph()
G.add_edge(0,1)
G.add_edge(1,2)
G.add_edge(1,3)
W=[1,0,-1,-1]
def dfs(G,n,dist,parent):
"""Depth first search and yield nodes where sum is 0."""
dist += W[n]
if dist==0:
yield n
for n2 in G[n]:
if n2!=parent:
for e in dfs(G,n2,dist,n):
yield e
for start in G:
for n in dfs(G,start,0,None):
print start,n
Note that this returns 2 entries for each path 0->2 and 2->0, and also returns paths 1->1 if a node has zero weight.
You can remove these extra cases by only outputting an answer if start < n.
Counting solutions
If you just want to know the number of solutions you can do this in O(nlogn) and O(n) space by splitting the tree into smaller pieces.
Choose a node in the centre of the graph ( O(n) to find it, although I suspect that picking a random node will work well in practice in the same way as quicksort works well)
Find all paths of zero weight that include this node by running a DFS for each neighbour and storing the distances in a dictionary mapping distance to count of nodes with that distance. Comparing the dictionaries allows us to find paths of zero weight. O(n)
Now repeat this algorithm for the subgraphs rooted at each child
For a binary tree we will have 2 subgraphs of size at most n/2, so the second stage will take about the same number of operations, and similarly each stage takes O(n) until the subgraphs contain a single node. There will be O(logn) stages, so overall the complexity is O(nlogn).
For a non-binary tree there are more subgraphs, but they are also smaller, so each stage will take O(n) as before, but we should bottom out faster, so non-binary trees should also be O(nlogn). (Also doing step2 is slightly more complicated but can still be done in O(n))
An O(n) solution using the following code in Python. As it is a tree, we don't need to check for visited nodes.
from collections import defaultdict
class Node(object):
def __init__(self, id, weight):
self.id = id
self.weight = weight
self.children = []
def __repr__(self):
return '<Node {0}: {1}>'.format(self.id, self.weight)
class Solver(object):
def __init__(self):
self.sums = defaultdict(list)
self.path = []
self.solutions = []
def dfs(self, depth, node, acc):
self.path.append(node)
key = acc + node.weight
for x in self.sums[key]:
self.solutions.append(self.path[x+1:])
self.sums[key].append(depth)
for child in node.children:
self.dfs(depth + 1, child, acc + node.weight)
self.sums[key].pop()
self.path.pop()
def run(self, root):
self.sums[0].append(-1)
self.dfs(0, root, 0)
return self.solutions
nodes = [
Node('A', 5),
Node('B', 1),
Node('C', 2),
Node('D', -3),
Node('E', 1),
Node('F', 2),
Node('G', 0),
Node('H', -8),
]
i = 0
while i < len(nodes) - 1:
nodes[i].children.append(nodes[i+1])
i += 1
s = Solver()
solutions = s.run(nodes[0])
for x in solutions:
print x
The output is:
[<Node B: 1>, <Node C: 2>, <Node D: -3>]
[<Node C: 2>, <Node D: -3>, <Node E: 1>]
[<Node D: -3>, <Node E: 1>, <Node F: 2>]
[<Node D: -3>, <Node E: 1>, <Node F: 2>, <Node G: 0>]
[<Node G: 0>]
[<Node A: 5>, <Node B: 1>, <Node C: 2>, <Node D: -3>, <Node E: 1>, <Node F: 2>, <Node G: 0>, <Node H: -8>]
I'm assuming the dictionary methods to add, delete and access a key are O(1). If they aren't, you could use some hash to have it on average.
Explanation: in any path (..., n_i, ..., n_j, ...), we have that the sum of all weights from n_i to n_j is equal to n_j.acc - n_(i-1).acc. So we just have to find two nodes with the same accumulated sum. We use hash to find it in O(1).
Of course you can adapt it to only count the number of paths. You just have to sum the size of self.sums (and you can remove self.path).

Finding Maximum Matching Topcoder

This is an algoritm problem from Topcoder SRM 566 Div2.
The problem can viewed here.
For those not having topcoder account the problem is described below:
Penguin Pals is a match making service that matches penguins to new friends, using the following procedure:
Each penguin is asked a single question: "Do you prefer the color blue, or the color red?"
All penguins are arranged so that they stand on a circle, equally spaced.
The organizers draw some straight lines, connecting some pairs of penguins. Each penguin may only be connected to at most one other penguin. Two penguins cannot be connected if they prefer a different color.
Each penguin who is connected to some other penguin follows the line to find their match.
The only problem with the above system was that it allowed penguins to collide if two lines crossed each other. Therefore, a new additional rule was adopted: no two lines may cross. Penguin Pals now has some penguins arranged on a circle (after step 2 of the above procedure). They need to know the maximum number of pairs of penguins they can create.
You are given a String colors whose i-th character represents the prefered color of the i-th penguin (0-based index) in the circular arrangement. The i-th character is 'R' if the i-th penguin prefers red and 'B' if the i-th penguin prefers blue. Return the maximum number of matched pairs that can be formed.
Example:
"RRBRBRBB"
Returns: 3
"BBBBB"
Returns: 2
"RRRBRBRBRBRB"
Returns: 5
My Approach:
Call the string s of length n. (Note that 0th and n-1th index are consecutive).
I used a recursive function recurse(string s,int i,int j)
which is as follows:
int recurse(string s,int i,int j)
{
if(i>=j)
return 0;
if(s[i]==s[j])
return(1+recurse(s,i+1,j-1));
else return max(recurse(s,i,j-1),recurse(s,i+1,j));
}
I start from i=0 and j=n-1, as they both will be consecutive if they are equal, call the the function with (i+1,j-1) and if not take both the possibilities and call the function recurse(s,i,j-1) and recurse(s,i+1,j) and will take the maximum of these two.
I called this function for every possible starting pair i.e.
for input "RRRBRRBB".
I called the function recurse() with inputs:
s="RRRBRRBB" i=0 j=n-1
s="RRBRRBBR" i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
s="RBRRBBRR" i=0 j=n-1 (the same operation)
and so on until all the cases are covered.
But i got WA, and couldn't identify the flaw in my approach why it couldn't work.
To correct you solution you should do following into each recursively call:
s="RRRBRRBB" i=0 j=n-1
s="RRBRRBBR" i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
s="RBRRBBRR" i=0 j=n-1 (the same operation)
and so on until all the cases are covered.
But I feels TLE on this case.
Solution:
This is simple problem.
1) Remove all pairs from string where s[i] == s[(i+1) % n], and calculate count. (i from 0 to n-1).
2) Iterate #1 till your string not converted to "RBRBRBRB...RB" or "BRBRBRBRBR...BR", for this special casing result (length / 2) - 1;
It's probably worthwhile mentioning that the expected solutions are documented on the Problem Set Analysis page for SRM566.

Is my Matlab code correctly implementing this given algorithm?

I am getting some wrong results and I couldn't locate any mistake in my code, so I was thinking if any of you can figure out if I am implementing this Binomial-Lattice algorithm correctly or not. Here's what I am getting as results and what I expect from as my results:
Actual Results:
I start with [S0,K,sigma,r,T,nColumn]=[1.5295e+009,6e+008,0.0023,0.12,20,15] and I get p=32.5955 , price=-6.0e+18 and BLOV_lattice as shown in figure 1.
Expected Results:
p is probability, so it should not be greater than 1. Even if I increase the nColumn to 1000, still the p is greater than 1 in the above actual results.
price should come out to be same as S0 , the number I start with in the first column, after backward induction i.e. there should be backwards-compatibility.
Figure 1: BLOV_lattice
My Matlab Code is:
function [price,BLOV_lattice]=BLOV_general(S0,K,sigma,r,T,nColumn)
% BLOV stands for Binomial Lattice Option Valuation
%% Constant parameters
del_T=T./nColumn; % where n is the number of columns in binomial lattice
u=exp(sigma.*sqrt(del_T));
d=1./u;
p=(exp(r.*del_T)-d)./(u-d);
a=exp(-r.*del_T);
%% Initializing the lattice
Stree=zeros(nColumn+1,nColumn+1);
BLOV_lattice=zeros(nColumn+1,nColumn+1);
%% Developing the lattice
%# Forward induction
for i=0:nColumn
for j=0:i
Stree(j+1,i+1)=S0.*(u.^j)*(d.^(i-j));
end
end
for i=0:nColumn
BLOV_lattice(i+1,nColumn+1)=max(Stree(i+1,nColumn+1)-K,0);
end
%# Backward induction
for i=nColumn:-1:1
for j=0:i-1
BLOV_lattice(j+1,i)=a.*(((1-p).*BLOV_lattice(j+1,i+1))+(p.*BLOV_lattice(j+2,i+1)));
end
end
price=BLOV_lattice(1,1);
%% Converting the lattice of upper traingular matrix to a tree format
N = size(BLOV_lattice,1); %# The size of the rows and columns in BLOV_lattice
BLOV_lattice = full(spdiags(spdiags(BLOV_lattice),(1-N):2:(N-1),zeros(2*N-1,N)));
References:
Cox, John C., Stephen A. Ross, and Mark Rubinstein. 1979. "Option Pricing: A Simplified Approach." Journal of Financial Economics 7: 229-263.
E. Georgiadis, "Binomial Options Pricing Has No Closed-Form Solution". Algorithmic Finance Forthcoming (2011).
Richard J. Rendleman, Jr. and Brit J. Bartter. 1979. "Two-State Option Pricing". Journal of Finance 24: 1093-1110. doi:10.2307/2327237
As far as I can see, your formulation of p as p=(exp(r.*del_T)-d)./(u-d) is not defined (clearly as such) anywhere in your references.
From your code it's not so straightforward to deduce what kind of options you are trying to valuate.
Closest I can get is to interpret that p (in your case) simply boils down to be p= (1- d)/ (u- d), which with your parameters will be 0.49934. (At least a reasonable value to be interpreted as probability!)

Resources