How many ways can five items be arranged in a row if two particular items may not be adjacent?
There are $5!$ ways of arranging items in a line.
From these arrangements, we must subtract those in which two particular items are adjacent. Place those two items in a box. That gives us four objects to arrange, the box and the other three objects. The objects can be arranged in a row in $4!$ ways. The two objects in the box can be arranged in $2!$ ways. Hence, the number of inadmissible arrangements is $4!2!$.
Therefore, the number of admissible arrangements is $5! - 4!2!$.
How many ways can five of nine items be arranged if two items can't be next to each other.
Your approach is sound. Your counts for the case neither item $A$ nor item $B$ is correct. However, your count for only item $A$ is incorrect.
If only item $A$ is used, then item $B$ is not used, so we must select four of the other $9 - 2 = 7$ objects in addition to item $A$. The five selected objects can be arranged in $5!$ ways. Hence, the number of arrangements containing only item $A$ is actually
$$\binom{1}{1}\binom{1}{0}\binom{7}{4}5!$$
By symmetry, the same number of arrangements contain only item $B$.
The two cases in which exactly one item is present can be handled simultaneously. Choose one of $A$ and $B$, four of the remaining seven objects, and arrange the selected objects in $5!$ ways, which gives
$$\binom{2}{1}\binom{7}{4}5!$$
For the case in which both item $A$ and item $B$ are selected, we must select three of the other seven objects to be in the arrangement. Once those five objects have been selected, they can be arranged in $5! - 4!2!$ ways without placing items $A$ and $B$ next to each other. Hence, there are
$$\binom{2}{2}\binom{7}{3}[5! - 4!2!]$$
ways to select five objects including $A$ and $B$ and arrange them so that $A$ and $B$ are not adjacent.
Adding the three cases gives
$$\binom{2}{0}\binom{7}{5}5! + \binom{2}{1}\binom{7}{4}5! + \binom{2}{2}\binom{7}{3}[5! - 4!2!]$$
where the first term is the number of admissible arrangements in which neither $A$ nor $B$ is selected, the second term is the number of admissible arrangements in which exactly one of $A$ or $B$ is selected, and the third term is the number of admissible arrangements in which both $A$ and $B$ are selected.
All the books can be arranged in $(2+3+2)!=7!$ ways
There are $3$ branches, three units of books: $\{$History$\}$,$\{$Geography$\}$,$\{$Science$\}$- Arranging branches $=3!$ ways.
Arranging the books within the branches:
History: $2!$
Geography: $3!$
Science:$2!$
Total $=3!(2!\times3!\times2!)=144$ ways
Best Answer
We solve a simpler problem first. How many ways are there to arrange $n$ objects so that $k$ are visible when seen from the left? Denote this number by $f_k(n)$, then clearly $f_k(k)=1$ and $f_1(n)=(n-1)!$ since exactly one of the objects can be covered.
After this we obtain $f_{k+1}(n+1)=f_k(n)+(n)f_{k+1}(n)$.
To see this take a configuration with $n+1$ objects and remove the smallest one of those objects, If the smallest object was at the beginning then we shall get an arrangement with $k$ visible items and $n$ objects. Conversely given an arrangement with $k$ visible items and $n$ objects we can obtain one with $n+1$ objects and $k+1$ visible items by placing an item smaller than the rest at the beginning.
If the smallest item was not at the beginning we can take it away, this will give us a configuration with $n$ items and $k$ objects. Moroever there are $n$ configurations which take us to the same exact configuration (depending on the position of the smallest object).
Since $f_k(n)$ has the same recurrence as the stirling numbers of the first kind we conclude $f_k(n)=S^{(k)}_n$.
Finally notice that the problem when there are two sides can be solved classifying on the position of the largest item (this is because the largest item cuts the arrangement into two spots.
Using this let $g_{j,i}(n)$ be the number of ways to arrange $n$ objects so that $j$ are visible from the left and $i$ are visible from the right, we obtain the recursion $g_{j,i}(n)=\sum_{k=1}^nS^{(j-1)}_{k-1}S^{(i-1)}_{n-k}$