tags:

views:

68

answers:

2

Hi All,

This question is similar to Question i had asked before.

Suppose a library.

The library has many of Books(say B(1 to n)). (n =number of Books)

Each book can have many Chapters (say C(1 to k)) . (k= number of Chapters)

Every Chapter can have number of Lessons (say L(1 to j)) .. (j = number of Lessons)

Every Lessons can have number of Topics (say T(1 to i)) .. (i = number of Topics ) ... ...

Now suppose if I want to create a list such that it has "all" entries like

Book 1 Chapter 1 Lesson 1 Topic 1 ...    

Book 1 Chapter 1 Lesson 1 Topic 2 ...  

Book 1 Chapter 1 Lesson 1 Topic 3 ...  

Book 2 Chapter 1 Lesson 1 Topic 1 ...

Book 2 Chapter 1 Lesson 1 Topic 2 ...

Book 2 Chapter 1 Lesson 1 Topic 3 ...

Book 2 Chapter 1 Lesson 1 Topic 4 ...

Book 2 Chapter 2 Lesson 1 Topic 1 ...

Book 2 Chapter 2 Lesson 1 Topic 2 ...

Book 2 Chapter 2 Lesson 1 Topic 3 ...
.....

Book (x1) Chapter (x2) Lesson (x3) Topic (x4) ... 


  where  1 <= x1 <= n, 1 <= x2 <= k, 1<= x3 <= j, 1< = x4 < = i

(Above example shows "Book 1" with 1 chapter 1 lesson 3 topics and "Book 2" with 2 chapters with "lesson 1" and 4 topics in chapter 1 and "lesson 1" and 3 topics in chapter 2. ) How can this list be efficiently generated?

Also, the entry "Book (x1) Chapter (x2) Lesson (x3) Topic (x4)" is not limited to Topic (4 variables).
It can vary too. Grow or shrink .
eg: Topics can have Questions,Questions can have Sub Questions. (based on user selection.)

Note: This is purely academic

Does this problem fall in the NP class of problems?

Any programming language ..algorithm :)

Thanks All

+1  A: 

This is a very common situation, addressed by both the Relational Db model and, for example, XML.

Datastructures cannot be NP problems, but a algorithm applied to your library might be.

If you make the number of levels variable, it will require some meta-programming.

Henk Holterman
+1  A: 

Does this problem fall in the NP class of problems?

No, but generating all combinations has exponential time complexity in the depth of the section system (book-chapter-etc).

If you you have a way of determining if the current structure (e.g. chapter) should have children, you can do the following:

// The int value is the number of children
Tuple<string, int> GetChildStructure(string parentStructure) {
    if(parentStructure == "Library") return new Tuple<string, int>("Book", 3);
    if(parentStructure == "Book") return new Tuple<string, int>("Chapter", 2);
    // ...
    // You can get these from a config file/DB/etc
    return null;
}

void BuildStructure(Structure parent) {
   var childStruct = GetChildStructure(parent.StructName);
   if(childStruct != null) {
      for(int i=1; i <= childStruct.Item2; i++) {
         Structure child = new Structure(childStruct.Item1,  i);
         BuildStructure(child);
      }
   }
}

void Main() {
    var lib = new Structure("Library", 1);
    BuildStructure(lib);
}

You should also check this post out.

Mau