The 3NF decomposition C ++ implementation with lossless connection and function dependency is not damaged.

Source: Internet
Author: User

The 3NF decomposition C ++ implementation with lossless connection and function dependency is not damaged.

Bytes ---------------------------------------------------------------------------------------------------------

This article welcomes reprint, reprint Please attach link http://blog.csdn.net/iemyxie/article/details/41246163

Bytes ---------------------------------------------------------------------------------------------------------


The database paradigm is undoubtedly very important for databases (nonsense ..) The main content of this article is c ++'s 3NF decomposition that achieves lossless connections and maintains function dependencies.

Each paradigm has been introduced in the previous article and will not be described here.

What is the third paradigm?

The third paradigm (3NF) requires that a database table does not contain information about non-primary keywords already contained in other tables, that is, the transfer dependency is eliminated.

Algorithm pseudocode

Input: relational mode R and function dependency set F on R
Output: The 3NF lossless connection of R and the decomposition Result of maintaining function dependency
The procedure is as follows:
1. Calculate the extremely small coverage of F Fm
Right Minimization
Left Minimization
Minimum number of rules
2. Obtain the Regular Expression of F to overwrite Fc.
Right part of FD with the same left part in merge Fm
3. The Result is left empty.
For (each fd x-> Y in Fc) do
If (Result contains the relational mode Ri so that Ri is included in XY) then
Result = Result-Ri 1_xy
Else if (Result does not contain Ri so that XY is included in Ri) then
Result = Result invalid XY
If (the relational mode in Result does not contain any code in R) then
Add a code in R to Result
R ^ '= A | A ε R, A does not appear in F
If (R' is not empty) then

Add R' to Result
Return Result

Algorithm C ++ implementation (algorithm subject comes from @ DarkSword)

# Include <iostream> # include <string> # include <algorithm> # include <vector> # include <map> # include <stdio. h> using namespace std; string R; // link mode vector <pair <string, string> F; // function dependency set (FD) vector <string> subset; // all subsets of relational mode R char * temp; // evaluate the auxiliary variable vector of all subsets <string> candidate_key; // all candidate keys vector <string> super_key; // all the superkeys
Bool _ des (string s1, string s2) {// checks whether each element of s2 exists in s1 sort (s1.begin (), s1.end (); sort (s2.begin (), s2.end (); return vertex des (s1.begin (), s1.end (), s2.begin (), s2.end (); // The vertex des function is based on an ordered set, so first sort} bool _ equal (string s1, string s2) {// determine whether the two sets are the same sort (s1.begin (), s1.end (); sort (s2.begin (), s2.end (); s1.erase (unique (s1.begin (), s1.end (), s1.end ()); // After deduplication, determine if the sorting is equal. s2.erase (unique (s2.begin (), s2.end (), S2.end (); return s1 = s2;} string get_attribute_closure (const string & X, const vector <pair <string, string> & F) {// return the closure string ans (X) of attribute set X; // initialize ans string temp; bool * vis = new bool [F. size ()]; fill (vis, vis + F. size (), 0); do {temp = ans; for (int I = 0; I! = F. size (); ++ I) {if (! Vis [I] & _ pair des (ans, F [I]. first) {vis [I] = 1; ans + = F [I]. second ;}}while (temp! = Ans); // terminate the loop delete [] vis; vis = NULL if ans does not change any; // delete the repeated sort (ans. begin (), ans. end (); ans. erase (unique (ans. begin (), ans. end (), ans. end (); return ans;} void _ all_subset (int pos, int cnt, int num) {// auxiliary function of get_all_subset () if (num <= 0) {temp [cnt] = '\ 0'; subset. push_back (temp); return;} temp [cnt] = R [pos]; _ all_subset (pos + 1, cnt + 1, num-1); _ all_subset (pos + 1, cnt, num-1);} void get_all_subset (const string & R ){ // Evaluate all subsets of relational mode R and save them in subset. clear (); temp = NULL; temp = new char [R. size ()]; _ all_subset (0, 0, R. length (); delete [] temp; temp = NULL;} bool is_candidate_key (const string & s) {// determine if s is a candidate key for (int I = 0; I! = Candidate_key.size (); ++ I) if (_ includes (s, candidate_key [I]) // if s contains a known candidate key, s is not the candidate key return false; return true;} bool cmp_length (const string & s1, const string & s2) {// return s1.length () <s2.length ();} void get_candidate_key (const string & R, const vector <pair <string, string> & F) {// evaluate relational mode R all F-based candidate keys get_all_subset (R); sort (subset. begin (), subset. end (), cmp_length); candidate_key.clear (); Super_key.clear (); for (int I = 0; I! = Subset. size (); ++ I) {if (_ includes (get_attribute_closure (subset [I], F), R) {super_key.push_back (subset [I]); if (is_candidate_key (subset [I]) candidate_key.push_back (subset [I]) ;}} typedef vector <pair <string, string> vpss; vpss get_minimum_rely (const vpss & F) {// return the dependency set vpss G (F) of F ); // make the right side of each FD in G a single property for (int I = 0; I! = G. size (); ++ I) {if (G [I]. second. length ()> 1) {string f = G [I]. first, s = G [I]. second, temp; G [I]. second = s [0]; for (int j = 1; j <s. length (); ++ j) {temp = s [j]; G. push_back (make_pair (f, temp) ;}} int MAXN = 0; for (int I = 0; I! = G. size (); ++ I) if (G [I]. first. length ()> MAXN) MAXN = G [I]. first. length (); bool * del = new bool [MAXN]; // remove the left-side redundancy attribute in each FD of G for (int I = 0; I! = G. size (); ++ I) {if (G [I]. first. length ()> 1) {fill (del, del + G [I]. first. length (), 0); for (int j = 0; j! = G [I]. first. length (); ++ j) {// for the I-th FD, determine whether the j-th attribute of first can be eliminated; string temp; del [j] = 1; for (int k = 0; k! = G [I]. first. length (); ++ k) if (! Del [k]) temp + = G [I]. first [k]; if (! _ Includes (get_attribute_closure (temp, G), G [I]. second) // del [j] = 0;} string temp; for (int j = 0; j! = G [I]. first. length (); ++ j) if (! Del [j]) temp + = G [I]. first [j]; G [I]. first = temp ;}} delete [] del; del = NULL; // The sort (G. begin (), G. end (); G. erase (unique (G. begin (), G. end (), G. end (); // eliminate redundant FD vpss ans in G; for (int I = 0; I! = G. size (); ++ I) {// determine whether the I-th FD is redundant vpss temp (G); temp. erase (temp. begin () + I); if (! _ Includes (get_attribute_closure (G [I]. first, temp), G [I]. second) // The I FD is not redundant ans. push_back (G [I]);} return ans;} vector <string> split_to_3nf (const string & R, const vector <pair <string, string> & F) {vector <pair <string, string> FF = get_minimum_rely (F ); // Save the minimum dependency set of F to FF // merge the FD with the same left to map <string, string> mp; for (int I = 0; I! = FF. size (); ++ I) {if (mp. find (FF [I]. first) = mp. end () mp [FF [I]. first] = FF [I]. second; else mp [FF [I]. first] + = FF [I]. second;} FF. resize (mp. size (); int id = 0; map <string, string >:: iterator It; for (It = mp. begin (); It! = Mp. end (); ++ It) {FF [id]. first = It-> first; FF [id ++]. second = It-> second;} // each FD x-> y forms a pattern xy vector <string> P; for (int I = 0; I! = FF. size (); ++ I) P. push_back (FF [I]. first + FF [I]. second); get_candidate_key (R, F); // obtain the R candidate key. // In the constructed mode set, if each mode does not contain the R candidate key, put the candidate key as a mode into the mode set // The resulting mode set is a decomposition of the relational mode R, and this decomposition is both lossless decomposition and can maintain FD. for (int I = 0; I! = Candidate_key.size (); ++ I) {int flag = 0; for (int j = 0; j! = P. size (); ++ j) {if (_ includes (P [j], candidate_key [I]) {flag = 1; break;} if (! Flag) P. push_back (candidate_key [I]);} sort (P. begin (), P. end (); P. erase (unique (P. begin (), P. end (), P. end (); return P;} void init () {// initialize R = ""; F. clear ();} void inputR () {// enter the relational mode R cout <"Enter the relational mode R:" <endl; cin> R ;} void inputF () {// input function dependency set F int n; string temp; cout <"Enter the number of function dependencies:" <endl; cin> n; cout <"enter" <n <"function dependencies: (the input format is a-> B AB-> c)" <endl; for (int I = 0; I <n; ++ I) {pair <string, string> ps; cin> tem P; int j; for (j = 0; j! = Temp. length (); ++ j) {// read ps. first if (temp [j]! = '-') {If (temp [j] = '>') break; ps. first + = temp [j] ;}} ps. second. assign (temp, j + 1, string: npos); // read ps. second F. push_back (ps); // read ps} int main () {freopen ("in.txt", "r", stdin); init (); inputR (); inputF (); vector <string> ans = split_to_3nf (R, F); cout <"lossless decomposition of relational mode R and dependency into 3NF mode set, as follows: "<endl; for (int I = 0; I! = Ans. size (); ++ I) cout <ans [I] <endl; return 0 ;}

PS: You can run the code in DEV instead of using VC6.0.


Test data:

Input sample
Abcde
2
A-> B
C-> d
 
Abc
2
A-> B
Bc->
 
Abcdef
4
AB-> ef
D-> e
E-> f
Cd-> ef
 
Abcdef
2
AB-> cd
Bc-> ef
 
Abcdef
4
AB-> ef
C-> d
D-> e
Bc-> de
 
Output sample
 
AB
Ace
Cd
 
AB
Abc
 
Abcd
Abe
De
Ef
 
Abcd
Bcef
 
Abc
Abef
Cd
De

Bytes -------------------------------------------------------------------------------------------------------------

This article welcomes reprint, reprint Please attach link http://blog.csdn.net/iemyxie/article/details/41246163

Bytes -------------------------------------------------------------------------------------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.