[Solved]: Studying Skiena. War Story: What’s Past is Prolog

Problem Detail: I am reading The Algorithm Design Manual, 2nd Edition. The book gives an example task and then explains how to solve it step by step. (The task and solution is detailed here) But I don’t follow one step from the solution. That step doesn’t contain enough details for me so I don’t understand it. I’m asking if someone can explain it. Below I rewrote the task in a shorter form than in the book: Given n ordered strings (lets call them rules) of m characters. The strings are indexed starting from 1 (not 0) The goal is to construct the trie with the minimum possible number of edges. The leaves of the resulting trie (each leaf represents a string) have to be in the same exact order as the given collection of strings. We are free to pick an arbitrary character position on each step of constructing the trie (e. g., we are free to start constructing the trie from character position 2 as opposed to starting from 1). Thanks to this ability we can minimize the built trie. Lets call picking a character – probing. Example: From the four rules below it’s possible to build different tries depending on the order of picking characters from the strings. S1: (a,a,a) S2: (b,a,a) S3: (c,b,b) S4: (d,b,b) tries The book says that

Probing at the p-th position, 1 <= p <= m, partitioned the rules into runs R1,…,Rr, where each rule in a given run Rx = Si,…,Sj had the same character value of Si[p]. Since the rules were ordered, each node in the subtree must represent the root of a run of consecutive rules, so there were only ${{n}choose{2}}$ possible nodes to choose from for this tree… … The rules in each run must be consecutive, so there are only ${{n}choose{2}}$ possible runs to worry about.

My question: How does the fact the rules are consecutive lead to the inference that there are ${{n}choose{2}}$ possible nodes/runs after probing at the p-th position?

Asked By : Nik

Answered By : Yuval Filmus

Each (non-trivial) run is given by two (different) indices, marking the beginning and end of the run, so if the sequence is of length $n$, there are $binom{n}{2}$ possible runs. If we only consider maximal runs, there are at most $n$ (or even less, if we don’t consider trivial runs).
Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/23908  Ask a Question  Download Related Notes/Documents