Minimizing cost of a given sequence by partitoning [closed]

Given a sequence of positive integers of size N(let) divide it into at most K(K > N/C) disjoint parts/subsequences in order to minimize the "cost" of the entire sequence.

Partitions cannot overlap, for example [1,2,3,4,5] can be divided into [1,2], [3,4] and [5] but not [1,3] and [2,4,5].

The cost of a subsequence is computed as the number of repeated integers in it. The cost of the entire sequence is computed as the sum of costs of all the subsequences and a fixed positive integer cost C times the number of partitions/divisions of the original sequence.

How should I go about determining the position and number of partitions to minimize the total cost?

Some more examples:

The given list = [1,2,3,1] Without any partitions, its cost will be 2 + C, as 1 occurs two times and the original sequence is counted as one partition.

[1,1,2,1,2] Without any partitions, its cost will be 5, as 1 occurs three times and 2 occurs two times. If we divided the subsequence like so [1,1,2],[1,2] then the cost becomes 2 + 2*C, where C is the cost of partitioning.

I have actually solved the problem for the case of C = 1, but am having problems generalizing it to higher values of C.

For C = 1 it makes sense to partition the sequence while traversing it from one direction as soon as a repetition occurs as the cost of a single repetition is 2 whereas the cost of partitioning is 1.

I’m trying to solve it in nlog(n) complexity ideally or at most a fast n^2.

Difficulty in few steps in proof of “Amortized cost of $\text{Find-Set}$ operation is $\Theta(\alpha(n))$”assuming union by rank, path compression

I was reading the section of data structures for disjoint sets from the text CLRS I faced difficulty in understanding few steps in the proof of the lemma as given in the question title. Here we assume we follow union by rank and path compression heuristics. Before we move into our target lemma a few definitions and lemma is required as a prerequisites for the target lemma.


The prerequisites:

$ $ level(x)=\max\{k:rank[p[x]]\geq A_k(rank[x])\}$ $ $ $ iter(x)=\max\{i:rank[p[x]]\geq A_{level(x)}^{(i)}(rank[x])\}$ $ $ $ \phi_q(x) = \begin{cases} \alpha(n).rank[x] &\quad\text{if $ x$ is a root or $ rank[x]=0$ }\ (\alpha(n)-level(x)).rank[x]-iter(x) &\quad\text{if $ x$ is not a root and $ rank[x]\geq1$ }\ \end{cases}$ $

Lemma 21.9: Let $ x$ be a node that is not a root, and suppose that the $ q$ th operation is either a $ \text{Link}$ or $ \text{Find-Set}$ . Then after the $ q$ th operation, $ \phi_q(х) \leq \phi_{q-1}(х)$ . Moreover, if $ rank[x] \geq 1$ and either $ level(x)$ or $ iter(x)$ changes due to the $ q$ th operation, then $ \phi_q(х) < \phi_{q-1}(х) – 1$ . That is, $ x$ ‘s potential cannot increase, and if it has positive rank and either $ level(x)$ or $ iter(x)$ changes, then $ x$ ‘s potential drops by at least $ 1$ .


Now in the proof below I marks the steps where I face problem

Lemma 21.12: The amortized cost of each $ \text{Find-Set}$ operation is $ \Theta(\alpha(n))$ .

Proof: Suppose that the $ q$ th operation is a $ \text{Find-Set}$ and that the find path contains $ s$ nodes. The actual cost of the $ \text{Find-Set}$ operation is $ O(s)$ . We shall show that no node’s potential increases due to the $ \text{Find-Set}$ and that at least $ \max\{0,s – (\alpha(n) + 2)\}$ nodes on the find path have their potential decrease by at least $ 1$ .

To see that no node’s potential increases, we first appeal to Lemma 21.9 for all nodes other than the root. If $ x$ is the root, then its potential is $ \alpha(n) . rank[x]$ , which does not change.

Now we show that at least $ \max\{0,s – (\alpha(n) + 2)\}$ nodes have their potential decrease by at least $ 1$ . Let $ x$ be a node on the find path such that $ rank[x] > 0$ and $ x$ is followed somewhere on the find path by another node $ у$ that is not a root, where $ level(y) = level(x)$ just before the $ \text{Find-Set}$ operation. (Node $ у$ need not immediately follow $ x$ on the find path.) $ \require{color}\colorbox{yellow}{All but at most $ \alpha(n) + 2$ nodes on the find path satisfy these constraints on $ x$ .}$ $ \require{color}\colorbox{yellow}{Those that do not satisfy them are the firstnode on the find path (if it has rank $ 0$ ),}$ $ \require{color}\colorbox{yellow}{ the last node on the path (i.e., the root), and the last node $ w$ on the path for which}$ $ \require{color}\colorbox{yellow}{ $ level(w) = k$ , for each $ k = 0,1,2,…, \alpha(n) – 1$ .}$

Let us fix such a node $ x$ , and we shall show that $ x$ ‘s potential decreases by at least $ 1$ . Let $ k = level(x) = level(y)$ . Just prior to the path compression caused by the $ \text{Find-Set}$ , we have

$ rank[p[x]] \geq A_k^{(iter(x)}(rank[x])$ (by definition of $ iter(x)$ ) ,

$ rank[p[y]] \geq A_k(rank[y])$ (by definition of $ level(y)$ ,

$ rank[y] > rank[p[x]]$ (by Corollary 21.5 and because $ у$ follows $ x$ on the find path)

Putting these inequalities together and letting $ i$ be the value of $ iter(x)$ before path compression, we have

$ rank[p[y]] \geq A_k(rank[y]) \geq A_k(rank[p[x]])$ (because $ A_k(j)$ is strictly increasing) $ > A_k(A_k^{(iter(x)}(rank[x])) = A_k^{(i+1)}(rank[x])$ .

Because path compression will make $ x$ and $ у$ have the same parent, we know that after path compression, $ rank[p[x]] = rank[p[y]]$ and that the path compression does not decrease $ rank[p[y]]$ . Since $ rank[x]$ does not change, after path compression we have that $ \require{color}\colorbox{pink}{$ rank[p[x]]\geq A_k^{(i+1)}(rank[x])$ . Thus, path compression will cause either $ iter(x)$ to }$ $ \require{color}\colorbox{pink}{increase (to atleast $ i + 1$ ) or $ level(x)$ to increase (which occurs if $ iter(x)$ increases}$ $ \require{color}\colorbox{pink}{to at least $ rank[x] + 1$ ). In either case,by Lemma 21.9, we have $ \phi_q(х) \leq \phi_{q-1}(х) – 1$ .}$ $ \require{color}\colorbox{pink}{Hence, $ x$ ‘s potential decreases by at least $ 1$ .}$

The amortized cost of the $ \text{Find-Set}$ operation is the actual cost plus the change in potential. The actual cost is $ O(s)$ , and we have shown that the total potential decreases by at least $ \max\{0,s – (\alpha(n) + 2)\}$ . The amortized cost, therefore, is at most $ O(s) — (s — (\alpha(n) + 2)) = O(s) — s + 0(\alpha(n)) = O(\alpha(n))$ , since we can scale up the units of potential to dominate the constant hidden in $ О (s)$ . ■


In the proof above I could not get the mathematics behind the statements highlighted in yellow and pink. Can anyone help me out?

Essence of the cost benifit obtained by using “markings” in Fibonacci Heaps (by using a mathematical approach)

The following excerpts are from the section Fibonacci Heap from the text Introduction to Algorithms by Cormen et. al


The authors deal with a notion of marking the nodes of Fibonacci Heaps with the background that they are used to bound the amortized running time of the $ \text{Decrease-Key}$ or $ \text{Delete}$ algorithm, but not much intuition is given behind their use of it.

What things shall go bad if we do not use markings ? (or) use $ \text{Cacading-Cut}$ when the number of children lost from a node is not just $ 2$ but possibly more ?

The excerpt corresponding to this is as follows:

We use the mark fields to obtain the desired time bounds. They record a little piece of the history of each node. Suppose that the following events have happened to node $ x$ :

  1. at some time, $ x$ was a root,
  2. then $ x$ was linked to another node,
  3. then two children of $ x$ were removed by cuts.

As soon as the second child has been lost, we cut $ x$ from its parent, making it a new root. The field $ mark[x]$ is true if steps $ 1$ and $ 2$ have occurred and one child of $ x$ has been cut. The Cut procedure, therefore, clears $ mark[x]$ in line $ 4$ , since it performs step $ 1$ . (We can now see why line $ 3$ of $ \text{Fib-Heap-Link}$ clears $ mark[y]$ : node $ у$ is being linked to another node, and so step $ 2$ is being performed. The next time a child of $ у$ is cut, $ mark[y]$ will be set to $ \text{TRUE}$ .)


[The intuition of why to use the marking in the way stated in italics portion of the block above was made clear to me by the lucid answer here, but I still do not get the cost benefit which we get using markings what shall possibly go wrong if we do not use markings, the answer here talks about the benefit but no mathematics is used for the counter example given]


The entire corresponding portion of the text can be found here for quick reference.

Does a supernatural Follower cost more BP than a mortal?

I’m a bit unsure on how much to have one of my players spend on a supernatural follower of his.

In the rules it states that:

Followers are assumed to have 3 in all Attributes, but the Follower gains 4 skill points for each BP spent to reflect the skills they use to help the character.

And that

This Gift, unlike Bonds, can include magical beings or even other gods at times, but are usually mortals who simply follow the character.

I’m understanding how to build the character itself with the points the player has to spend, but what I’m really unsure of is: does it cost more to gain a supernatural ally (like a kitsune, succubi, …) instead of a mortal?

Thus any explanation from a book I don’t have or from another source is welcome.

How much does it cost to hire a Hireling?

In Dungeon World, you can hire NPCs to accompany the party and provide assistance. These NPCs vary in loyalty and skill. However, I don’t see any chart or table detailing how much the NPCs are expected to charge per-session for their services. What is a fair/reasonable/balanced price? Does it scale off the skill of the NPC?

How much does Antivenom cost in Pathfinder?

Antivenom is a very potent substance very much worth carrying around if you think that a possibility of encountering a certain monster is real and can get your hands on a vial or two. For example, I’d like my character to have Antivenom against Phase Spiders.

Creating this item is a complex process that requires a dose of target venom. How much can such a thing cost? I think I remember seeing a formula based on monster CR, but I didn’t happen to find it.

Pricing with flat gp cost abilities?

How do I calculate the cost of upgrading a piece of equipment with a flat gp cost ability? It costs the same amount to add, for example, the Expeditious ability to a set of +1 armor as it would to add it to a set of +3 armor. Expeditious, disregarding its price, counts as a +2 equivalent ability.

This, to me, is fine for factoring into the maximum effective armor bonus a piece of equipment can have. However, if the Expeditious ability were to factor into the price for further upgrades to the armor, such as upgrading the +1 Expeditious armor to +3 Expeditious armor, it would create a price discrepancy of over 40% compared to simply forging a new set of armor, enhancing it to +3, then adding Expeditious to it, and that price gap only gets wider as the enhancement bonuses get higher.

Is this price gap intentional, or do flat cost abilities not affect the price of upgrades?