0

I am confused with Divide and Conquer (the technique we apply to solve a problem using recursion) and Induction.

Like if I am sorting an array using recursion, I will divide the whole array.

Something like below:

void sortv(vector<int> &v){

        if(v.size()==1)
          { return; } 

       int num=v.back(); 
        v.pop_back(); 
         sortv(v); 
        insert(v,num); 
      }

So, when I am calling sortArray() inside sort array after decreasing one element from array I am assuming that, that call will sort that much part of the array and now I just have to insert the last element at the correct position.

Am I using divide and conquer strategy which says break the problem into smaller sub problems and approach and you should know how the sub-problems solution are related to each other or It is induction which says assume that your function can solve for the smaller problem!

Or are both of them are same?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Welcome to SO! I'm afraid I don't follow the logic in the code. How is this divide and conquer? It doesn't seem like this would sort the array, and if `insert` is a linear walk, then the algorithm is O(n^2), not D&C. – ggorlen Sep 15 '20 at 16:57
  • insert is a recursive walk @ggorlen! – Parth Panchal Sep 15 '20 at 16:59
  • Please show the code for it as a [mcve], otherwise I have no idea what its complexity is. Already, the algorithm is linear without `insert` because you're stepping through each element so it fails as D&C which is going to be chopping something into halves typically. The classical D&C would be a binary search or mergesort. – ggorlen Sep 15 '20 at 17:02
  • void sortv(vector &v) { if(v.size()==1) { return; } int num=v.back(); v.pop_back(); sortv(v); insert(v,num); } – Parth Panchal Sep 15 '20 at 17:07
  • Please edit your post to include that--it's unreadable here. Also, since it appears you're writing C++, you might as well provide a working [mcve] so there's no guesswork on my part about what your code even is. – ggorlen Sep 15 '20 at 17:08
  • I did edit my post please help me claasify which step is induction and if i am using divide and conquer. @ggorlen – Parth Panchal Sep 15 '20 at 17:13
  • Sorry, I don't see a complete code snippet I can make sense of. I don't see any D&C or induction as I said. – ggorlen Sep 15 '20 at 17:22
  • https://stackoverflow.com/a/63325843/12280589 – Parth Panchal Sep 15 '20 at 17:30
  • The above link might help @ggorlen – Parth Panchal Sep 15 '20 at 17:30
  • I see. They're somewhat awkwardly trying to apply a mathematical proof notion to recursion. OK, fine, call it induction, but it's generally called a "base case" and "recursive case" because a function isn't a proof. I'm not sure what gain you get in writing a loop like this and calling it induction. Either way, there's no D&C here because nothing is being divided in half. What exactly is your goal here? – ggorlen Sep 15 '20 at 17:33
  • Okay so divide and conquer always means diving in half? – Parth Panchal Sep 15 '20 at 17:35
  • Even if i am sperating each element in array and then solving the problem this is not divide and conquer? – Parth Panchal Sep 15 '20 at 17:36
  • Yes. Your algorithm is moving through an array one element at a time, like a loop. That's not [D&C](https://en.wikipedia.org/wiki/Divide_and_conquer_algorithm), that's just looping, only with recursion and a lot of unnecessary work. – ggorlen Sep 15 '20 at 17:37
  • Okay I got it! Thanks! I just have one last question How are recursion and induction related, I mean how to think indcutively to craft a recursive solution? – Parth Panchal Sep 15 '20 at 17:40
  • As a non-mathematician, I think "induction" is a rather silly lens to use to look at problems through, but maybe it helps some mathematical algorithms along. I prefer to look at it in terms of "what's the base case" and "what's the recursive case" only. If you're recursively traversing and reordering a JSON-based tree structure, for example, the idea of thinking about induction seems goofy to me. If it helps you to think of "recursive case" as "induction" then go ahead, though. – ggorlen Sep 15 '20 at 17:54
  • Re, "divide and conquer always means dividing in half? " No, not always exactly in half. But it typically means, dividing the problem into two or more _similar_ sub-problems and then combining the results. Your example sorts a list of N elements by dividing the problem into two _different_ sub-problems: (1) "sort a list of N-1 elements," and (2) "insert the Nth element into the sorted list." – Solomon Slow Sep 15 '20 at 18:20
  • A more appropriate example of divide an conquer would be (1) divide the list into two smaller lists, A and B, (2) sort list A, (3) sort list B, (4) merge list A with list B to get the final result. The similarity of steps (2) and (3) is what makes it "divide and conquer." – Solomon Slow Sep 15 '20 at 18:23
  • Thank you @solomon show, Now I understand that whenever we are doing same operation on all the divided subproblems and combining the solution it is D&C!! – Parth Panchal Sep 15 '20 at 18:40

0 Answers0