2013-10-17 4 views
1

데이터 구조로 함수 종속성을 나타내는 방법을 생각하고 있습니다.함수 종속성을 나타내는 데이터 구조

기능적 종속성 (데이터베이스의 관계 스키마)은 속성 세트를 속성 세트로 맵핑합니다. 예 : -에서 {A, B}> {C, D} 속성 C와 D는 가

내가 여기 네 가지 경우를 생각할 수 A와 B

에 기능 의존 :

  1. {A} -> {A} -> {B, C} (단일 속성은 하나의 속성을 의미 함) {B} (단일 속성 두 개)
  2. {A, B} {A, B} -> {C, D} (속성 집합은 속성 집합을 의미 함)
- (4) - 내가 생각

class attribute 
{ 
    string name; 
    set<attribute*> dependent_on; 
} 

이 (1)가 아니라 (2)와 같은 함수 종속과 함께 작동합니다 :

내 첫 번째 방법은 간단한 두 멤버 클래스이었다. 저는 (3)을 분해 할 수는 있지만 실제로 그러한 분류로 (2)와 (4)를 표현할 방법이 없습니다.

나는 C D는 B에 따라 작동하는지 정보를 유지하기 위해, 그래서는 속성 세트가 속성 세트에 매핑되는 경우 attributegroup 같은 클래스를 만들어야 할 것입니다. 예컨대 :

class attributegroup 
{ 
    set<attribute*> members; 
    set<attribute*> dependent_on; 
} 

그래서 실제로 나는 단지 하나 개의 구성원이있는 attributegroup 단순히 하나의 속성을 나타낼 수 있습니다. 그러나 이것이 이것이 최선의 방법이라고 생각하지 않습니다.

어떤 도움/생각에 감사드립니다 :) 내가 속성에 종속성을 저장하지 않을

+0

당신이 제안한 접근법에 대한 구체적인 관심사는 무엇입니까? –

+0

나는 특별한 걱정거리가 없습니다. 어쩌면 아마도 더 좋은 것이 있다고 생각합니다. 필자의 접근 방식으로 모든'attributegroup'을 반복해야하는 알고리즘을 구현할 때 @Dieter Lücking의 접근 방식과 마찬가지로'set '이된다. –

+1

OK - 다음 질문 : 모든 '속박 집단'을 조사 할 필요없이 우리가 무엇을하려 하는가? 나중에 사용할 알고리즘이 무엇인지 알아야합니다.이를 통해 사용할 수있는 데이터 구조가 어떻게 생겼는지 말할 수 있습니다. –

답변

1

:

include <iostream> 
#include <map> 
#include <vector> 

struct Attribute; 
typedef std::vector<Attribute> Attributes; 
struct Attribute { 
    char name; 
    operator Attributes() const { return Attributes{ 1, *this }; } 
}; 

inline bool operator < (const Attribute& a, const Attribute& b) { 
    return a.name < b.name; 
} 

inline bool operator < (const Attributes& a, const Attributes& b) { 
    return std::lexicographical_compare(
     a.begin(), a.end(), 
     b.begin(), b.end()); 
} 

inline std::ostream& operator << (std::ostream& stream, const Attributes& attributes) { 
    for(const auto& a: attributes) { 
     std::cout << a.name; 
    } 
    return stream; 
} 

typedef std::multimap<Attributes, Attributes> AttributeDependencies; 
typedef AttributeDependencies::value_type AttributeDependency; 

int main(int argc, const char* argv[]) { 
    Attribute a {'A'}; 
    Attribute b {'B'}; 
    Attribute c {'C'}; 
    Attribute d {'D'}; 

    AttributeDependencies dpendencies; 
    dpendencies.insert(AttributeDependency(a, b)); 
    dpendencies.insert(AttributeDependency(Attributes{a, b}, c)); 
    dpendencies.insert(AttributeDependency(a, Attributes{b, c})); 
    dpendencies.insert(AttributeDependency(Attributes{a, b}, Attributes{c, d})); 
    for(const auto& d: dpendencies) { 
     std::cout << '{' << d.first << "} -> {" << d.second << "}\n"; 
    } 
    return 0; 
} 

참고 : 성병 : 맵이 바로 용기가 될 수 있지만 표준 : multimap에 적합 예제 데이터.

+0

글쎄, 실제로 구현의 이런 종류의 생각 (비록 std :: set over std :: vector 중복을 피하기 위해). 하지만 모든 종속성 (O (n^2)이 될 것인가 또는 뭔가 빠졌는가?)을 반복해야하는 알고리즘을 구현할 때 런타임과 관련하여 약간의 우려가있었습니다. 그래서 대부분의 사람들이 생각하지 못하는 일종의 "별난"데이터 구조가 필요했습니다. –

관련 문제