CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Exercise 6
  • 4. Marks
  • Recursion & Backtracking
  • Graph Traversal
  • Exercise 7
  • 3. walk
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lab

Lab 09 - Backtracking

PreviousLab 08 - C PreprocessorNextLab 10 - Struct and Wrap up

Last updated 5 months ago

Slides:

Exercise 6

4. Marks

Construct a cumulative frequency table.

Recursion & Backtracking

It seems (erroneously) that recursion is always about "going backwards". but it is not always the case.

A problem-solving paradigm (Usually used in AI):

  • Suppose we have an initial state S.

  • Our goal is to find all states where a terminating condition p holds.

  • Upon entering each current state X, we first check if p is true. If it is, we collect X as a valid result.

  • Else, we explore the actions that can be performed at state X to transition to a different state Y.

  • For each new state Y generated this way, we enter it and repeat the process.

  • After exhausting all possible actions, we return to the previous state.

And the steps can be summarized as:

  1. Define goal(S)

  2. Define condition for goal(S)

  3. Define state representation

  4. Define actions and transitions (Usually it is recursion)

  5. Invoke function with initial state

Backtracking is searching by brute force.

The difficult part is for us to find "initial state S, goal, terminating condition, actions that can be performed to transition, return to the previous state".

  1. At every stage, check: is k = n? (This is our terminating condition)

    1. If yes, then this is a valid sequence, we will print it and return (Collect a valid result and backtrack to the previous state)

    2. If no, then we have 2 choices to proceed to the next stage:

      1. Append 0 to the end of s; or

      2. Append 1 to the end of s

void print_binary(long k, long n) {
    // Is [s, k] a goal state?
    if (k == n) {
        // Yes, collect the result.
        cs1010_println_string("");
        // Backtrack to the previous state.
        return;
    }
    // No, explore the possible actions.
    // Action 1: append 0.
    // Transition 1: [s, k] -> [s0, k + 1].
    putchar('0');
    print_binary(k + 1, n);
    // Action 2: append 1.
    // Transition 2: [s, k] -> [s1, k + 1].
    putchar('1');
    print_binary(k + 1, n);
}
int main() {
    long n = cs1010_read_long();
    print_binary(0, n); // Initial state: ["", 0].
    return 0;
}

However, this code will miss printing the digits infront in the after backtracking. There are two methods to solve this:

Graph Traversal

Taking the Depth-First-Search as an example. Given that two vertices v and u, we wish to determine whether they are connected by a path.

Identify the following

  1. Current Stage: The current vertex x (initially v)

  2. Terminating Condition: x=u

  3. State Transitions: Move to any unvisited neighbour of x

Exercise 7

3. walk

We can practice this kind of thinking using one of the past year PE questions .

We start our sequence s=∅s=\emptys=∅ and number of digits k = 0

Change the representation to whole number. See

Use an array to store the appended characters. See

A genius way to solve this problem is to use combinatorics! Legit genius! But only implementing nCrnCrnCr directly is not correct! (The factorial will easily exceed the space limit of long). Try the step-by-step solution.

5. Stone
5. Stone
136KB
Lab9.pdf
pdf
Lab 9 Slides
3. Group*