Sample solution

advertisement
Sample solution #19
7-2 Multiset
The following algorithm will work both for a multiset or distinct set of elements.
Step 0: Suppose the elements are numbered from 1 to n. Initially the elements are
stored in an array A with indices 1 to m, in the sorted order. Output A from 1 to m.
Step 1: Pick the largest i such that A[i] < A[i+1]; if no such i exists, exit with completion.
Step 2: Pick the least element A[j] in A[i+1..m] such that A[j]>A[i].
Step 3: Swap A[i] and A[j] and then reverse the subarray A[i+1..m].
Step 4: Output A from 1 to m; goto Step 1.
If we have an algorithm which works for distinct elements only, and suppose a, b, c, …,
are identical, we may replace these numbers by (a, 1), (b, 2), (c, 3), …, and apply that
algorithm. For each permutation generated by the algorithm, we throw away those
permutations, where (b, 2) appears before (a, 1), etc. This idea can be implemented as
follows:
First, we can consider the all the numbers indistinguishable by storing all the numbers in
a 2-dimentional array input[n][2]. For duplicate numbers in the multiset, we assign them
an order by adding additional number sequence (1, 2, 3) in input[n][1].Now, we are able
to construct all permutations of the multiset as the way we did for {1, … , n}. The only
difference is that we have to eliminate all the duplicate permutations which has
duplicate numbers out of order. This can be done by comparing the their additional
number sequence in the 2-dimentional array.
The modified version of process_solution is as follows: Use a 2-dimentional array
input[n][2] to store each number, if a number n is unique we put a 0 in input[n][1];
process_solution (int a[], int k)
{
for (int i =1; i < = k; i++){
if(a[i][1] != 0){
for (int j = i +1; j < k; j++){
if (a[j][0] == a[i][0] && a[j][1] > a[i][1]) return;
}//end for
}//end if
}//end for
for(int m = 1; m < = k; m++ ) printf(“%d”, a[m]);
}
7-5
Given two undirected graph G = (V, E) and H = (U, D), we decide if there exists a
subset U’ of U such that |V| = |U’| and there is a one-to-one mapping f : V -> U', such
that f(E) is a subset of D. We may describe this problem as CSP:
Variables: X = { f(v) | v in V }
Domain of variables: U
Constraints:
(a) all values of f(v) are distinct (i.e., one-to-one mapping)
(b) for each edge (a, b) of E, (f(a) f(b)) is in D.
If we have an algorithm to solve the above problem, we may use this algorithm to solve
the Hamiltonian cycle problem and the clique problem. Let A(G, H) be the algorithm to
solve the subgraph isomorphism problem. To decide if G has a Hamiltonian cycle, we
call A(G, C), where C is a cycle of |V| nodes. To decide G has a clique of k notes, we
call A(G, K), where K is a complete graph of k nodes. That is, the Hamiltonian cycle
problem and the clique problem are the special case of the subgraph isomorphism
problem. If we have an algorithm to decide the k-clique problem, we may use the
algorithm to apply to the complement graph of G to decide if G has an independent set
of k nodes.
Download