function dependency set closure and attribute set for relationship normalization X closure for function dependency set F

Source: Internet
Author: User
Tags bool closure int size

Recently learning database principle, the relationship normalization introduced several algorithms, the most basic and most important is to find the attribute set X about a function dependency set F closure.

/*8 Week of Kung Fu a database basic principles of schooling, and soon to test, so 5-1 holiday also not planning to go out to play, in the school review, rest and so on. In the process of review, it was suddenly found that the difference between the closure of a function-dependent set and the closure of a collection of attributes was rarely introduced. I read it several times before I understood it, so I would like to add it to my friends who have the same doubts. */

/* This is in 2016 Wuyi Labor Day modified, haha, I am a good programmer in China, Labour on Labor to get through @@facesymbol@@ ̄▽ ̄) ~*. Before the closure of the package does not adopt a recursive way, the process is very cumbersome, not worth recommending, so the deletion of the previous paragraph of the Code. Adopt recursion, more close to the definition, the idea is clearer. In addition, the code is streamlined by streamlining and optimizing the code, removing unnecessary functions, or switching to better ideas. */

First of all, the closure of the function dependency set

----------------------------------------------------Split Line------------------------------------------------------------------ -------------------------

Closed Packets with function dependencies

Definition: If f is a function dependency set of the relationship mode R (U), we refer to F and all the set of functions dependent by F logic as the closure of F, denoted as f+.

What is "logically implied by F"?


Logical implication of function dependencies:

Definition: There is a relationship mode R (U) and its function dependency set F, if any one of R satisfies the F relationship R function dependent X→Y is established, then said F logic implication x→y, or x→y can be introduced by F.

Example: Relational schema r= (a,b,c), function dependency set f={a→b,b→c}, F logic implication a→c


Therefore, the function depends on the closure of the set F, the element is also a function of dependence (that is, x→y), the function of the dependency set F of the closure includes F itself, plus can be deduced from F through the Armstong axiom, a new function dependence.

namely: f+={x→y| X→y∈f∨ "Apply Armstong axiom derived from F any X→y"}


In order to determine whether the function relies on x->y in f+, only the f+ can be calculated. Because F+ is a collection of function dependencies derived from F based on the Armstrong Axiom. Therefore, in principle, the f+ can be calculated as long as the inference rules in the Armstrong axiom system are followed. However, the calculation of the closure f+ is a very troublesome thing, because the problem of computing f+ is a NP-complete problem, even if f={x->a1, X->a2, ..., X->an,}, you need to calculate the f+ of the O (2n) function dependencies, so when n is larger, the actual calculation f+ is not feasible. Even though the elements of F are not long, the elements in f+ can be many. In addition, there are many redundant information in the closure f+. In fact, judging whether a function depends on x->y in f+, it is completely unnecessary to calculate the closure f+


An important theorem: X->y belongs to f+, which is equivalent to Y belonging to the closure of X about F. X, Y are all properties in F.


So what is attribute X's closure on F?

----------------------------------------------------Split Line------------------------------------------------------------------ -------------------------

2. Attribute set X (x∈u) Closure of a function dependency set F on U x_f^+

(1) Definition: Set the relationship mode R (u,f), U for its set of properties, F for its function dependency set, it is said that in all the Armstrong axioms from the function of F-dependent X→ai, the Ai's attribute set is the attribute of x closure.

That is, the attribute set X is about the closure of a function-dependent collection, where the elements are attributes (rather than function dependencies), which are derived from F according to the Armstrong Axiom.


Well, to fully understand, add what is Armstrong Axiom:

Armstrong Axiom 
 1, theorem: if U is the property of the relationship mode R, F is a set of function dependencies on U, set X, Y, Z, W are subsets of R, R (u,f) is:
        F1 (reflexive): If X≥y (table x contains Y), then X→Y is the implication of F; (F1 ' : x→x)
        F2 (augmented): If X→Y is implied by F, then Xz→yz is the implication of F, (F2 ': xz→y)
        F3 (transitive): If x→y,y→z is implied by F, then x→z is implied by F;
        F4 (Pseudo-additive): If X→y,w≥z ( The table w contains z) is implied by F, then Xw→yz is the implication of F,
        F5 (pseudo-transmission): If X→y,yw→z is F, then xw→z is the implication of F,
        F6 (synthesis): If x→y,x→z is implied by F, then X→yz is the implication of F;
        F7 ( Decomposition): If x→y,z≤y (table Z is contained in Y) is implied by F, then X→z is the implication of F.
        the function-dependent inference rule f1∽f7 is correct.
 2. Armstrong Axiom:
inference Rules F1, F2, F3 collectively Armstrong axioms, F4∽f7
can be pushed by F1, F2 and F3, which is the inference part of Armstrong Axiom.

(2) Calculation: attribute set X about function dependency F property closure x+. Set the entire attribute set of the relationship mode R to U, and the function dependency set on U is a subset of F,u for x, and the attribute closure x+ of x about F is computed.

Here's how:

① initial X (0) =ф,x (1) =x;

② if x (0) ≠x (1), place x (0) =x (1), otherwise turn ④;

③ is dependent on each function in F for y→z, if y x (1), X (1) =x (1) ∪z, turn ②;

④ ends, i.e. x (1) is x+.

Let's use an example to illustrate the computational process of a property closure.

Example 1: There is a relationship mode R (u,f), where u={a,b,c,d,e},f={ab→c,b→d,c→e,ce→b,ac→b}, calculated (AB) +.

Solution: By the above algorithm:

First time: ①x (0) =ф,x (1) =ab;

② due to x (0) ≠ab, place x (0) =ab;

③ search F for each function dependent, get ab→c and B→d for ab,b x (1), set X (1) =AB∪C∪D=ABCD;

Second time: ②x (0) ≠ABCD, then X (0) =abcd;

③ find C→e and Ac→b, place X (1) =abcd∪e∪b=abcde;

Third time: ②x (0) ≠abcde, then X (0) =abcde;

③ Find Ce→b, place x (1) =abcde∪b=abcde;

Fourth time: ②x (0) =x (1), turn ④;

④ end, i.e. x (1) = (AB) +=abcde.

For simplicity, each attribute is represented by a character, and the program is as follows:


Property set X (X∈u) for closure of a function dependency set F on U x_f^+//Input: The function dependency F, property set X (x∈u)//output: X about f u,u x_f^+ #include <iostream> #

Include <string> using namespace std;	
The struct functiondependence//function relies on the {string x;//determinant factor string Y;

};
	void Init (functiondependence fd[],int N) {//function dependency initialization int i;
	string x, Y; 
	cout<< "Please enter the function dependency in F (the determinants are left, the determinants are on the right)" <<endl;
		Input function Dependency set F for (i=0;i<n;i++) {cin>>x>>y; Fd[i].
		X=x; Fd[i].	
	Y=y; 
	} cout<< "function depends on set F:";
	cout<< "f={"; for (i=0;i<n;i++) {//displays known function dependency set F Cout<<fd[i]. x<< "," <<fd[i ".
		Y	
	if (i<n-1) cout<< ","; 
} cout<< "}" <<endl;
	the bool IsIn (string f,string zz)//can determine whether all the factors in F are in X, but this may result in duplicate {bool flag1=false;
	int Len1=f.length ();
	int Len2=zz.length ();
	int k=0,t=0,count1=0;
				for (k=0;k<len1;k++) {for (t=0;t<len2;t++) {if (F[k]==zz[t]) {//flag1=true;break;
			count1++;
	}}} if (Count1==len1) {flag1=true;} else Flag1=false;
return FLAG1;
	} string Cutandsort (string mm)//removes the resulting closure from the repeating element and sorts {int size=mm.length ();
	int kk=0,ii=0;;
	string closure; The int a[200]={0};//is used to record the number of occurrences of each proposition for (kk=0;kk<size;kk++) {a[(int) mm[kk]]++;//cast type, the number of times to store each factor} for (II=0;II&L
		t;200;ii++) {if (a[ii]>=1)//cout<< (char) II;
	closure+= (char) II;
} return closure;
	} string X_fn (functiondependence fd[],int n,string &xx) {string yy=xx; for (int i=0;i<n;i++) {if (IsIn (Fd[i]). X,YY) ==true) {Xx+=fd[i].
		Y
	}} yy=cutandsort (yy);	
	Xx=cutandsort (XX);
	cout<< "yy=" <<yy;
	cout<< "xx=" <<xx<<endl; 
if (xx!=yy) {X_FN (FD,N,XX);//recursive} return xx;
	} void FD (Functiondependence fd[],int N) {//input X string xx;

	int i; 
	cout<< "\ n Please enter attribute set x:";
	cin>> xx;
	cout<< "\ n" <<xx<< "closure on F: {";
	
	The closure cout<<x_fn (FD,N,XX) about F is obtained.
cout<< "}\n";
	} int main () {int N; cout<< "Please enter the group that the function depends on in FNumber: ";
	
	cin>>n;
	Functiondependence Fd[n];	
	Init (Fd,n);
	FD (Fd,n);
	FD (Fd,n);
	 
	FD (Fd,n);
return 0;  }






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.