Avengers: Endgame time heist decoded by a Programmer- Part 1

Dishant SharmaDishant Sharma
3 min read

To write this blog I re-watched some parts of Endgame and Gosh!! What a movie it was. I still get goosebumps every single time I watch it. Truly a masterpiece. So unless you are living under a rock, you should know that in the endgame the Avengers travel back to different points/timelines in the past to retrieve the Infinity Stones. They use Quantum Realm to collect the Infinity Stones and bring them back to the present. So which algorithms they would have used to pull this off? Well, clues lie in the cap's speech right before the time heist begins. He says :

6 stones, 3 teams, 1 shot. Most of us are going somewhere we know that doesn't mean we should know what to expect. Be careful. Look out for each other.

Let's break Cap's motivational speech into two parts :

PART- 1: 6 stones, 3 teams, 1 shot!!

Avengers had to find 6 stones in different timelines. So, let's assume we have an array of type timeline. Every timeline has a member variable that denotes the number of stones in it and another variable that contains stones in that timeline. For example:

timline 2013Asgard{
    int numberOfStones
    String[] stonesName
}

To find all 6 stones, the first approach is that the whole team traverses each timeline, one by one, together. Like iterating a timeline array in a brute-force manner to find infinity stones. This is a classic case of Linear Search and the time complexity of the linear search is O(n). Now, remember that Thanos arrived at Avengers HQ right after their heist. Although the avengers didn't know this when they were plotting the heist. But they knew that "Dread It, Run From It, Destiny Still Arrives". So they wanted to find all stones ASAP and needed something better than O(n).

Also, there could have been some fallout in one timeline then they would have to look for a stone in multiple timelines. Something similar happened when Ant-Man and Iron Man went to collect Tesseract / Space stone and Hulk happened. So this could have been cascaded into infinite timelines. Who Knows ๐Ÿ˜‚. But we know that the linear search behaves very poorly when input size increases.

So Avengers, specifically Iron Man, would have decided to use Divide and Conquer algorithm. Its strategy involves :

  • Divide the big problem into smaller subproblems.

    To collect 6 stones, they formed 3 teams. Each team will solve only the mission given to them.

  • Solve the subproblems independently.

    Each team goes to its assigned timelines to collect stones. The timelines shown in the endgame's plot are also depicted exactly like this. Multiple sub-plots occur simultaneously in different timelines.

  • Combine the solutions of the subproblems to solve the original problem.

    Each team brought its assigned stones. So finally they had 6 stones.

Well now that the Avengers have all 6 stones let's look at the below pseudo-code of divide and conquer quickly before Hulk does the snap thingy.

function splitTheProblem(problem)
    if problem is small enough to solve then
        solve the problem directly //Base case
    else
        divide problem into subproblems //Like we find mid of an
                                        // array in binary search. 
        splitTheProblem(subproblem)     // Recursive call
        Add up the solutions of subproblems to solve problem

Now you know how useful the divide and conquer algorithm is. Also now that our heroes have all 6 infinity stones let's explore Quantum Realm in Part 2 of this blog with some more algorithms. Stay tuned!!.

Wakanda Forever!! ๐Ÿ™…โ€โ™‚๏ธ๐Ÿ™…โ€โ™‚๏ธ

Disclaimer: This article is an attempt by the author to explain few particular stuff using a non-conventional approach. You might find similarities with other concepts as well. Those are not in the scope of this article. This article is for informational and educational purposes only. The views and opinions expressed in this blog are the personal experiences of the writer. It should not be taken as a definitive representation of the film or its creators. Please watch the film and form your own opinions.

1
Subscribe to my newsletter

Read articles from Dishant Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dishant Sharma
Dishant Sharma