-2

I'm trying to implement following picture.

enter image description here

The logic this illustrates is as follows: compare the first entity and last entity of a block and if they are different, divide it into two blocks. Then, compare the first and last entity of the divided block. Repeat it until we find two entities that are the same.

I've newly started to program and I just learned recursive logic, stacks and queues. I'm trying to implement it with DFS but I'm not sure how I can divide it into two and repeat. Can you help me to find a keyword to Google it? Or is there any matching data structure I can use?

I wrote this code, but this doesn't seem to work.

def getBln(idx1, idx2):
  pass 

#DFS
def videoRcsv():
  if getBln(idx1, idx2) == True:
    break
  else:
    videoRcsv(idx1, idx2/2),videoRcsv(idx2/2, idx2)

def main():
  pass

main():
ggorlen
  • 44,755
  • 7
  • 76
  • 106
z2ouu
  • 41
  • 1
  • 10
  • I don't think you'd need to use an explicit stack or queue for this. You'd likely want to use recursion, which would make use of the language's call-stack. – Carcigenicate Aug 16 '21 at 13:56
  • Yeah, I understand that I need to use DFS in recursive way. But I'm not sure how to make node of tree – z2ouu Aug 16 '21 at 14:00
  • It's not really clear what this algorithm is supposed to accomplish, exactly. I feel like the cart (solution) is put before the horse (clear problem description/specification). If you're trying to find two equal elements in a list, this won't work, because the elements might be in different halves of the main list. The overall divide & conquer paradigm is often used for searching and sorting, but it's not clear what's being searched for in this case. Some input examples with expected output might go a ways to explaining the goal. – ggorlen Aug 16 '21 at 20:28
  • What is actually the expected *output*? Your code has no `return` anywhere, nor any attempt to print anything... Can you edit your question and provide the code that *creates* the initial tree, and then add what would be the expected output (not an image, but Python syntax) for that example tree? – trincot Aug 16 '21 at 20:33

2 Answers2

0

DFS is not relevant here, Use recursion to implement the picture. Think about the easiest case (a condition in which the function returns the answer - 2 entities that are either equal or not) and continue from that point.

Elena Alexeenko
  • 100
  • 4
  • 13
-1

why don't you use loop ?

    int l = 0; // first block
    
    int r = idx2; // index of last block
       
    while( l < r ){
          if( blocks[l++] == blocks[r--] ){
              // do smth
          }
    }
Akbar Odilov
  • 174
  • 1
  • 7
  • How would this handle the recursive calls on two separate sub-trees? In other words, this would split array `blocks` into `blocks'` and `blocks''`. Which sub-blocks does `l` and `r` correspond to? How would it process sub-sub blocks of those sub-blocks? – ggorlen Aug 16 '21 at 20:21