-2

I have a school project, where I have to write a program in c#. I think I'm on the right track but I stuck on this problem.

In this section of the program I have to write a backtracking algorithm. I have classes(English, Physics, Maths etc.) and all the classes last for a specific time. (For example 1h, 2h, 3h, 5h etc.). Also, I have maxClassHours which shows how much time one can spend at school this day. The algorithm should collect all the possible combinations of the classes which fill this maxClassHours as much as possible.

It's important that I can only sit a given class once! So I can only have one Maths lesson.

For example the maxClassHours is 5 so I can spend 5 hours sitting in different classes.

Let's say I have these classes:

  • Maths - 2h
  • Physics - 1h
  • Music - 2h
  • Dance - 5h
  • P.E. - 3h
  • Geography - 5h
  • English - 3h
  • French - 1h
  • Science - 7h
  • Art - 1h

The goal is to find all the possible combinations (using backtrack algorithm) which fills this 5 hours.

I hope it make sense... Thank you for the help.

EDIT: I would like to understand how backtracking algorithm can be applied on this problem.

EDIT2: I tried to work on it for hours, but I couldn't make significant progress, that's why I'm asking for some help...

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
nethuszar
  • 225
  • 3
  • 13
  • 2
    What exactly is your question? (If it's "Will the SO community do my school project for me?", I'm afraid the answer is no.) – Gareth McCaughan Apr 29 '16 at 14:34
  • And where is your code? What are you stuck with? – Enigmativity Apr 29 '16 at 14:35
  • Help me start with this. I would like to understand how backtracking algorithm can be applied on this problem. – nethuszar Apr 29 '16 at 14:36
  • "Will the SO community do my school project for me?" I'm not asking for this. – nethuszar Apr 29 '16 at 14:37
  • @nethuszar - You need to do your own research on this - try Google. When you have an approach to try then write some code, and when you're stuck then come ask a question. That's how we work here. – Enigmativity Apr 29 '16 at 14:38
  • A backtracking algorithm works by searching a solution tree in a depth-first manner, and for every leaf node hit the algorithm backtracks to the most recent untraversed branch and continues there. – Enigmativity Apr 29 '16 at 14:41
  • I'm not using trees this case. The classes are stored in a List. – nethuszar Apr 29 '16 at 14:42
  • Somebody put posting on hold so I can't answer, but can edit your posting. See my comments above. – jdweng Apr 29 '16 at 15:34
  • @nethuszar - Yes, backtracking searches a tree using program flow. There isn't an actual tree data structure. – Enigmativity Apr 30 '16 at 00:03

1 Answers1

2

Try this thought process:

  1. Choose a class
  2. Is my total time still less than the max?
    • If so, choose another class
    • If not, remove the last added class and add a different one in its place

For example:

Max is 5h

| Class List                       | Total Time |
|----------------------------------|------------|
| Math                             | 2h         |
| Math, Physics                    | 3h         |
| Math, Physics, Music             | 5h (save)  |
| Math, Physics, Dance             | 8h         |
| Math, Physics, P.E.              | 6h         |
| Math, Physics, Geography         | 8h         |
| Math, Physics, English           | 6h         |
| Math, Physics, French            | 4h         |
| Math, Physics, French, Science   | 11h        |
| Math, Physics, French, Art       | 5h (save)  |
| Math, Music                      | 4h         |
| ...                              | ...        |
Zach Olivare
  • 3,805
  • 3
  • 32
  • 45
  • Thank you for the answer. I have something like this, but the problem is that I have to find all the possible combinations. So finding a good combination works for me but finding all of them... – nethuszar Apr 29 '16 at 14:52
  • 1
    Why don't you post what you have so far? That way we can give more specific help. – Zach Olivare Apr 29 '16 at 14:58
  • It's not in English + I don't think it's that good. My algorithm do something like: Start with the first class and if that's smaller or equal than the maxTime then I add it. Check the second class in the list if the first class time + this class time is <= than the maxTime then I add it. . . . But this gives me only one solution I need all of them. From maths we know that if I have 10 classes I can have 2^10 combinations which CAN be a good solution for this problem. I just need some help to think over this problem, sounds too complex to me... – nethuszar Apr 29 '16 at 15:06
  • Please note the last part of my explanation above `If not, remove the last added class and add a different one in its place`. Once you find a combination which is greater than or equal to 5h, you need to remove the last class that you added, and then beginning adding classes in its place until you find another combination greater than or equal to 5h. This is precisely what I demonstrate in my example. This type of problem lends itself to a recursive solution, and can be somewhat more complex to do without recursion. Good luck! – Zach Olivare Apr 29 '16 at 15:30