Infinity War

Student Handout


The purpose of this assignment is to practice your understanding of graphs as well as various graph manipulations and algorithms.

You are not required to watch Avengers: Infinity War to understand the assignment.

How are the Infinity War assignment files structured?

The assignment consists of the following files:

  • Files to edit: ForgeStormBreaker.java, LocateTitan.java, MindStoneNeighborNeurons.java, UseTimeStone.java, and PredictThanosSnap.java.
  • Each file contains a task, where each task contains 1-2 problems to be implemented.
  • Files to utilize: StdRandom.java, StdOut.java, and StdIn.java.
Editable files:
  • The assignment encourages the use of text files as inputs and outputs for each task.
  • An example structure of the input and output files (as well as corresponding preconditions) are described for each task below.
  • Each input test case is a text file containing a representation of a graph.
  • The problems to implement in each file are described in further detail for each task below.
  • In the editable file, students must read the input test case, construct a graph using the input, implement a solution for the problem described, and finally write the result into a new output file.
  • Some of the output test cases are provided, while the others are hidden.
Using this Student Handout:
  • Each editable file is decribed below. There is a navigation bar for every file, with "Title", "Task", and "Context" sections.
  • The "Title" section contains the title of the editable file.
  • The "Task" section displays the problems to be solved for that file, as well as a detailed example.
    • The "Task" section for each file MUST be read to solve the problems corresponding to the file.
  • The "Context" section contains details regarding how the task relates to Avengers: Infinity War.
    • The "Context" section for each file is NOT required to be read and does NOT contain essential information to solve the problem.
  • The "Heplful Tips and Guidance" section is RECOMMENDED and contains a lot of useful information!
  • The "Backstory of Thanos" and "Characters and Locations" sections below are also NOT required to solve any problem in this assignment.
  • The "Credits" section contains the MLA Citations for the Marvel resources and other intellectual property used to create this assignment.
Download the Starter Code here:

The following graph creator tool is very helpful in visualizing test cases and debugging your code!



Executing and Debugging:

  • You can run your program through an IDE of your choice (ex: VSCode) or you can use the Terminal to compile and execute.
  • We suggest running through VSCode because it will give you the option to debug.
  • If you choose the Terminal, from InfinityWar directory/folder:
    • Compile all files: javac -d bin src/avengers/*.java
    • Run ForgeStormBreaker: java -cp bin avengers.ForgeStormBreaker forgestormbreaker.in forgestormbreaker.out
    • Here, forgestormbreaker.in and forgestormbreaker.out are the input and output files, respectively.
    • The other files can be ran in a similar way in the Terminal.
    • Details for compiling and running are found in the begining each file.

Implementation Details:

  • In each given Java class, you will read from a given input file (passed in as a command line argument), and write to a given output file (passed in as a command line argument).
  • DO NOT change the names of any of the given Java files, or the project structure itself (do not change directory names or create new directories).
  • DO NOT remove the package statement from any of the given input files.
  • YOU MAY (and should) create your own classes in the src/avengers folder.
  • YOU MAY import “java.util.*”.
  • Make sure any new classes have a package statement: package avengers;
  • The classes that you create MAY NOT have spaces in their names.
  • In order to grade a problem, we run the corresponding Java class and verify the output file. This means you have full freedom in your project structure, as long as our provided classes output the correct answer to the correct output file. Take this opportunity to practice your project design skills, and write clean code that avoids redundancy.

StdIn, StdOut, and StdRandom:

  • StdIn, StdOut, and StdRandom are classes from the code libraries described and implemented in the textbook Algorithms, 4th ed. written by Robert Sedgewick and Kevin Wayne.
  • Use StdIn.setFile(fileName) to set the current input file you want to read from.
  • You can now use methods like StdIn.readInt(), StdIn.readString() and StdIn.readLine() to operate on the input file as if it was standard input.
  • The methods StdIn.readInt() and StdIn.readString() actually leave the newline character unread, so if you use StdIn.readLine() after one of these methods, it will read this character rather than the next line. If you want to read the next line with StdIn.readLine(), you will need to call StdIn.readLine() once to read the newline character and then again to read the next line. StdIn.readInt() and StdIn.readString() ignore spaces and newlines by default.
  • Use StdOut.setFile(fileName) to set the current output file you want to write to. It creates the file if it doesn’t already exist.
  • You can now use methods like StdOut.print() and StdOut.println() to operate on the output file as if it was standard output.
  • The autograder ignores empty lines and extra spaces that your output files may have.
  • Use StdRandom.setSeed(seed) to initialize a random number generator using the given seed.
  • Use StdRandom.uniform() to generate a random number from 0 to 1, using the seed.

Java Collections Data Structures:

  • ArrayList
    • Definition: an ordered array-like structure with no size limit, as it automatically resizes.
    • You can initialize an empty ArrayList named “name” which holds objects of type “Type” with ArrayList name = new ArrayList();
    • For example, an ArrayList of integers named “arrList” is initialized with ArrayList arrList = new ArrayList<>();
    • You can add a new element of type “Type” to the end of your ArrayList in average case O(1) time with name.add(newElement);
    • You can get the element at some index of your ArrayList in O(1) time with name.get(index);
    • You can set some index to some new element in O(1) time with name.set(index, newElement);
    • You can check if the ArrayList contains some element (returns a boolean) in O(n) time with name.contains(element)
  • HashMap
    • Definition: an unordered data structure which stores and retrieves key value pairs
    • You can initialize an empty HashMap named “name” that maps objects of type “Key” to objects of type “Value” with HashMap name = new HashMap<>();
    • For example, a HashMap named “map” which maps strings to integers is initialized with HashMap map = new HashMap<>();
    • You can add a new key value pair, or update an existing key with a new value in average case O(1) time with name.put(key, value);
    • You can check if the HashMap contains some key in average case O(1) time (returns a boolean) with name.containsKey(key)
    • You can check the value of some key in the HashMap in average case O(1) time with name.get(key)
    • You can iterate over all the keys in the HashMap with for (Key key : name.keySet()) where Key is the type of keys in the HashMap.
  • HashSet
    • Definition: an unordered data structure which only stores keys.
    • You can initialize an empty HashSet named “name” that stores objects of type “Key” with HashSet name = new HashSet<>();
    • For example, a HashSet named “set” which stores strings is initialized with HashSet set = new HashSet<>();
    • You can add a new key to the hash set in average case O(1) time with name.add(key)
    • You can check if a key exists in the hash set in average case O(1) time (returns a boolean) with name.contains(key)
    • You can remove a key in average case O(1) time with name.remove(key)
    • You can iterate over all the keys in the HashSet with for (Key key : name) where Key is the type of keys in the HashSet.
  • LinkedList
    • Definition: a list of nodes that have pointers to adjacent nodes. The Java Implementation is a doubly-linked list.
    • You can initialize an empty LinkedList named “name” that stores objects of type “Element” with: LinkedList name = new LinkedList<>();
    • For example, a LinkedList named “list” which stores strings is initialized with: LinkedList list = new LinkedList<>();
    • You can add a new element to the end of the linked list with: name.add(element);
    • You can add a new element at a specific index in the linked list (this operation is O(n) in the worst case) with: name.add(index, element);
    • You can check if an element exists in the linked list (this operation is O(n) in the worst case) with: name.contains(element);
    • You can remove a specific element (this operation is O(n) in the worst case) with: name.remove(element);
    • You can remove an element at a specific index (this operation is O(n) in the worst case) with: name.remove(index);
    • You can retrieve the element at a specific index (this operation is O(n) in the worst case) with: name.get(index);
    • You can iterate over all the elements in the LinkedList with: for (Element element : name) where Element is the type of elements in the LinkedList.
  • Dequeue
    • Definition: a double-ended queue that is a combination of a Stack and a Queue.
    • You can initialize an empty ArrayDeque named “name” that stores objects of type “Element” with: ArrayDeque name = new ArrayDeque<>();
    • You can add at the end with name.addLast(element); and at the start with name.addFirst(element). Also, peekFirst() and peekLast() let you see the elements.
    • You can remove from the end with name.removeLast(); and remove from the beginning with name.removeFirst(). name.contains(x) determines if x is in name.

In the MCU (Marvel Cinematic Universe), right after the Big Bang occurred there were 6 singularities contained within Infinity Stones that were spread across the infant universe.

Infinity Stones:
  • Space Stone: Allows users to teleport.
  • Time Stone: Allows users to travel through and manipulate time.
  • Reality Stone: Allows users to alter the surroundings around them.
  • Power Stone: Allows users increase their strength and control energy.
  • Soul Stone: Allows users to control the essence of other individuals.
  • Mind Stone: Allows users to increase their intelligence and control other minds.
Infinity Stones

Before the events of Avengers: Infinity War, a titan named Thanos witnessed the death of his home planet due to overpopulation. Reeling from his experience, Thanos made it his life mission to exterminate half of all life in the universe to delay overpopulation on other worlds like his.

Thanos determined that using the 6 Infinity Stones which he could hold in his Infinity Gauntlet, a simple snap of his fingers would painlessly eliminate 50% of all life, and he can finally rest in peace victorious.

Locations:

  • NYC: New York City on Earth.
  • Asgard: Homeworld of the Asgardians (demi-God like warriors who look over Asgard, Earth, and other locations).
  • Titan: Homeworld of the Titans (advanced beings who nearly went extinct due to inequality and shortages on their planet).
  • Wakanda: An African Stronghold on Earth.
  • Knowhere: An abandoned, ruleless mining location in Space.
  • Nidavellir: Home of the Dwarves, origin of many Asgardian weapons such as Thor’s hammer (Mjolnir), the Stormbreaker, and Thanos’ Infinity Gauntlet.

Main Avengers:

  • Thor: King of Asgard, known as the God of Thunder. Can control lightning and electricity, but lost his hammer when his evil sister Hela destroyed it.
  • Captain America: A serum-enhanced super soldier.
  • Iron Man: A scientific genius who invented his Iron-Man suit and many forms of nanotech and advanced weaponry.
  • Hulk: A genius scientist who, because of exposure to gamma radiation, typically transforms into a monster when enraged or agitated known as the Hulk. However, he is unable to transform into the Hulk during the film but still helps the Avengers.
  • Hawkeye: A master marksman who has insanely good accuracy.
  • Black Widow: A highly trained spy and infiltration agent.
  • Scarlet Witch: A girl who was experimented on using the Mind Stone and thus can harness magic and engage in hypnosis and telekinesis.

Gaurdians of the Galaxy:

  • Starlord: The half-human, half-Celestial leader of the Guardians who was abducted from Earth as a child and raised by a group of alien thieves and smugglers called the Ravagers.
  • Mantis: A girl with empathic powers who was previously saved by the Guardians.
  • Gamora: An orphan from an alien world and was subsequently raised by Thanos, and is seeking redemption for her past crimes.
  • Nebula: An adopted daughter of Thanos who was raised with Gamora as siblings.
  • Drax: A warrior in search of vengeance against Thanos for killing his family.
  • Rocket: A genetically-engineered raccoon-based bounty hunter, mercenary, and master of weapons and battle tactics.
  • Groot: A tree-like humanoid who can grow branch-like limbs to attack its enemies.

Other Characters:

  • Black Panther: The king of the African nation of Wakanda who gained his enhanced strength by ingesting the Heart-Shaped Herb.
  • Dr. Strange: A former neurosurgeon who, after a car accident that led to a journey of healing, discovered the hidden world of magic and alternate dimensions and became a Master of the Mystic Arts (basically a lead magician).
  • Loki: Thor’s brother who is killed early on in the film.
  • Elitri: King of the Dwarves of Nidavellir, and weaponsmith for the Asgardians.
  • Winter Soldier: An enhanced soldier and Rogers' ally and best friend who reemerged as a brainwashed assassin after being thought to have been killed in action during World War II.
  • Spiderman: A teenager and Stark's protégé who received spider-like abilities after being bitten by a radioactive spider.
  • War Machine: An officer in the U.S. Air Force and Avenger who operate the War Machine armor that is basically an Iron Man suit augmented with extra weapons.
  • Ebony Maw: A telekinetic alien who is very loyal to Thanos.
  • Cull Obsidian: The most powerful of Thanos’ children; functions as a tank like the Hulk.
  • Proxima Midnight: An expert combatant and owned a spear created from a star trapped in a singularity by Thanos himself.
  • Corvus Glave: Husband of Proxima Midnight, is immortal as long as his blade is intact.

MLA Citation for Algorithms, 4th ed., which was used for StdIn, StdOut, and StdRandom:

Sedgewick, Robert, and Kevin Wayne. Algorithms, 4th ed., Addison-Wesley Professional, 2011. Web.

MLA Citation for MCU:

"Marvel Cinematic Universe." Marvel Studios, LLC, 2023.

MLA Citation for Avengers: Infinity War:

Russo, Anthony, director. Avengers: Infinity War. Marvel Studios, 2018.

MLA Citation for Marvel Entertainment:

Marvel Entertainment, LLC, 2023.

MLA Citation for Marvel Studios:

Marvel Studios, LLC, 2023.

MLA Citation for Marvel Website:

"Marvel.com | The Official Site for Marvel Movies, Characters, Comics, TV" Marvel Entertainment, LLC, 2023, https://www.marvel.com/. Accessed 04 Dec 2023.

MLA Citation for Disney Website:

"The Walt Disney Company." The Walt Disney Company, Disney, 2023, www.thewaltdisneycompany.com.

MLA Citation for Marvel Universe Entertainment Youtube Channel (used for video clips):

"Marvel Universe Entertainment." YouTube, YouTube, 04 Dec 2023, www.youtube.com/@Marvel-Universe-Entertainment.

MLA Citation for Wikipedia Page for the MCU:

"Marvel Cinematic Universe." Wikipedia, Wikimedia Foundation, 04 Dec. 2023, en.wikipedia.org/wiki/Marvel_Cinematic_Universe.

MLA Citation for Marvel Fandom Wikipedia:

Marvel Cinematic Universe Wiki." Fandom, 04 Dec. 2023, marvelcinematicuniverse.fandom.com.





ForgeStormBreaker

Steps for this task:

  • Traverse the adjacency matrix in the input file into a 2D array.
  • Print the sum of elements in the 2D array into the ouput file.


Example:

  • Input file:
    • forgeExampleInp
    • The first line is the number of rows, and the second line is the number of columns in the 2D array.
    • The rest of the input file contains the 2D array data.
  • Output file:
    • forgeExampleOut
    • The sum of the elements in the 2D array is (1 + 2 + 3) + (4 + 5 + 6) + (7 + 8 + 9) = 45.


Preconditions

  • The numbers in the 2D array are guaranteed to be integers.
  • The dimensions of the 2D array may not be the same.

The film begins with Thanos destroying Thor’s homeworld (Asgard) after acquiring the Power and Space Stones.


A defeated Hulk is sent to NYC to warn Dr. Strange and Iron Man about the impending arrival of Thanos and his army (the Black Order) to retrieve the Time Stone. His ship arrives soon and a battle ensues as Iron Man, Spiderman, and Dr. Strange battles Ebony Maw and Cull Obsidian (two of Thanos’ “children”).

After Cull Obsidian dies, Dr. Strange is captured onto Maw’s ship, and Iron Man and Spiderman latch onto it as it departs Earth. Iron Man blows a hole in the ship and Maw is shot into space.

Meanwhile, the Guardians of the Galaxy (hereby referred to as “Guardians”) rescue Thor and the group splits, where Thor, Rocket, and Groot (a tree-human hybrid) go to Nidavellir to forge Thor’s Stormbreaker, a weapon capable of destroying Thanos.


To forge the Stormbreaker, Thor has to endure the power of a Neutron Star near Nidavellir. The star’s plasma rushes through the opening that Thor is inside, as depicted in the image below.


Fig. 1. Screenshot from Avengers: Infinity War, directed by Anthony Russo and Joe Russo, 2018


In physics, flux is defined as the amount of fluid (or field strength) flowing through an area.




In the image above, the box with the matrix of numbers shows the flux intensity of the sun’s plasma through the opening that Thor is inside. Taking the sum of all the numbers reveals the total flux of the plasma through the opening.
Thus, the flux would be (3 + 4 + 5 + 1 + 2) + (12 - 1 + 7 + 2 + 8) + (11 - 6 + 3 + 5 + 2) = 58.


Task: given a 2D array where the values are the flux intensity data of the Neutron Star, calculate the total flux that Thor has to endure. Write this number into the output file.




LocateTitan

Steps for the task:

  • Create the adjacency matrix using the input file. Lets call this 2D array matrix[][].
    Here, matrix[i][j] contains the edge cost from vertex i to vertex j.
  • Store the functionality values from the input file into an array. Lets call this 1D array functionalities[].
    Here, functionalities[x] represents the functionality of vertex x.
  • Divide each value in the adjacency matrix with the functionality values of vertex i and vertex j.
    Specifically, for every element in the adjacency matrix, divide matrix[i][j] by functionalities[i]*functionalities[j].
  • Suppose there are n vertices. Use Dilkstra’s algorithm to find the minimum cost from vertex 0 to vertex n-1.
  • Print the minimum cost to the output file.


Example:

  • Input File:
    • forgeExampleInp
    • The first line is the number of vertices. In this example, there are 6 vertices. Note that the vertex numbers START with 0.
    • The next 6 lines give a functionality value for each vertex. Note that the number of lines depends on the number of vertices.
    • The next 6 lines have 6 values each, representing the edge weight in the adjacency matrix. Again, the number of lines and values depends on the number of vertices.
  • Graph from Input:
    • forgeExampleInp
    • The circles are vertices and each contain the vertex number. The squares contain edge weights.
    • The numbers in white outside the vertices are the functionality values for the associated vertex.
    • Ex: Vertex 0 and Vertex 1 have functionality values 0.5 and 0.3, and the the weight of the edge connecting them is 1.
  • Matrix after division:
    • forgeExampleInp
    • Here, divide each value in the matrix by the functionality value represented by the indices.
    • Ex: The edge cost between vertex 1 and vertex 4 is 7, since matrix[1][4] = 7.
    • Since functionalities[1] = 0.3 and functionalities[4] = 0.9, then we calculate 7/(0.3*0.9) = 25.925.
    • Finally, we typecast this to an integer and update matrix[1][4] to be 25.
    • DO THIS for every element in the adjacency matrix!
  • Graph after division:
    • forgeExampleInp
    • Note how the edge weights are modified by the division described above.
    • The vertex numbers are still unchanged! Only the edge weights change.
  • Graph using Dijkstra's:
    • forgeExampleInp
    • Using Dijkstra's algorithm, we calculate the LENGTH of the shortest path from vertex 0 to vertex 5.
    • We use vertex 5 as the destination since we need the FINAL vertex in the graph of n vertices, which would be vertex n-1 = 6-1 = 5.
    • Feel free to USE the pseudocode of Dijkstra's given below for implementation!
  • Output:
    • forgeExampleInp
    • The output is the LENGTH of the shortest path, which is 6 + 25 + 11 = 42.


Pseudocode for Dijkstra's:

Preconditions:

  • The functionality values are guaranteed to be between 0 and 1.
  • The values in the input adjacency matrix are gauranteed to be positive integers.
  • The graph is guaranteed to be undirected.
  • The graph is guaranteed to be connected.

On Earth, Vision (who has the Mind Stone contained in his head) is protected by the Scarlet Witch (Wanda Maximoff) against more of Thanos’ forces as they try to acquire the Mind Stone.

They are joined by Captain America (Steve Rogers), Falcon, and Black Widow (Natasha Romanoff).


Meanwhile, the Guardians who did not go to Nidavellir (basically Peter Quill, Gamora, Drax, and Mantis) head to Knowhere to prevent Thanos from acquiring the Reality Stone.

However, this fails as Thanos has already gained the Reality Stone and uses it to trick the Guardians into an illusion, and captures Gamora (Thanos’ daughter) since she knows the location of the Soul Stone.

Thanos tortures his daughter Nebula until Gamora reveals that the Soul Stone is in Vormir. Afterwards, Thanos and Gamora depart there while Nebula begs the Guardians to meet her on Titan, Thanos’ homeworld.

After hijacking the Q-ship from Earth, Dr. Strange, Iron Man, and Spider Man decide to go to Titan. In the MCU, interstellar travel happens via wormholes (Einstein-Rosen Bridges).

Fig. 2. Screenshot from Avengers: Infinity War, directed by Anthony Russo and Joe Russo, 2018

Suppose that the route from Earth to Titan can be represented by a Weighted Undirected Graph where the vertices are wormhole generators and the edges are the wormholes themselves. A wormhole is created by two wormhole generators, so the wormhole would be a “path” between these generators.

Some important points:

  • There is no direct wormhole that can extend from Earth to Titan, so going from Earth to Titan requires intermediate “stops” at different wormhole generators.
  • For instance, a hypothetical path could be from a generator on Earth to a generator on Planet X, and from the generator on Planet X to a generator on Titan.
  • Each edge (Wormhole) would have an associated weight, which is the energy cost of creating the wormhole that the particular edge represents.
  • Each vertex (Wormhole Generator) would also have a weight, and this number (a double data type) represents how functional that generator is. Due to Thanos’ conquest so far across the universe, not all of the wormhole generators would be 100% functional.
  • The Wormhole Generators would be identified by cardinal numbers (vertex 1 would be generator 1, vertex 2 would be generator 2, etc).
  • SUPPOSE THERE ARE n vertices IN THE GRAPH. ASSUME THAT GENERATOR #0 (vertex 0, the source) IS EARTH AND GENERATOR #(n-1) (vertex n-1, the destination) IS TITAN.

Division of Energy Costs:

  • Right now, the adjacency matrix only shows the energy cost of each edge. However, we also need to factor in how each generator is not fully functional.
  • Thus, if the total cost acknowledges the energy cost of each wormhole and the functionalities of the generators creating the wormhole, then the less functional the generators are, the higher the total cost of creating and using the wormhole would be.
  • Thus, if we want the adjacency matrix to hold the total cost of each wormhole, we can DIVIDE the energy cost by the functionality of BOTH generators that the wormhole connects.
  • Hence, once you read the input in and create your adjacency matrix, change EVERY edge weight (energy cost) by DIVIDING it by the functionality of BOTH vertices (generators) that the edge points to. Then, typecast this number to an integer (this is done to avoid precision errors).

Task: Use Dijkstra's Algorithm to find the minimum cost for a path between Earth (generator #0) and Titan (generator #(n-1)), using the updated energy costs.




MindStoneNeighborNeurons

Steps for the task:

  • Create a graph using the "set of edges" representation in the input file.
    All the vertices have a varying out-degree EXCEPT a single vertex which has out-degree 0.
  • Find the vertex with an out-degree of 0.
  • Identify the vertices that neighbor (have an edge) this vertex.
  • Print these neighbors to the output file.

Example:

  • Input File:
    • forgeExampleInp
    • The first number is the number of vertices. In this case, there are n = 17 vertices.
    • The next n lines are STRINGS representing the vertex names.
    • The first number is the number of edges. In this case, there are m = 16 edges.
    • The next m lines list the vertices involved in each edge. For example, there is an edge between vertices "a" and "d".
  • Graph:
    • forgeExampleInp
    • In the graph, each vertex is a NEURON and each edge is a SYNAPSE (see "Context" section for more information).
    • The graph shows how the vertices are connected, with the name of each vertex beside it.
    • In this case, the only vertex with an out-degree of 0 is vertex "x".
    • The neighbors of "x" are the vertices "d", "e", "i", and "l".
  • Output File:
    • forgeExampleInp
    • We print "d", "e", "i", and "l" into the output file.
    • MAKE SURE to print each vertex name in a new line!

Preconditions:

  • The names of the vertices may not be single letters or characters (instead, they may be alphanumeric strings with multiple ASCII characters).
  • There is gauranteed to be only ONE vertex with an out-degree of 0.
  • The graph is gauranteed to be directed and connected.

Iron Man, Dr. Strange, and Spiderman arrive at Titan but are ambushed by the Guardians who think they are part of the Black Order. After some tension, they plan to work together to kill Thanos.

Meanwhile, to protect Vision, the Avengers on Earth head to Wakanda, a country in East Africa which has the technology to remove the Mind Stone in Vision’s head without killing him in the process.

A Wakandan named Shuri attempts to figure out how to remove the Mind Stone from Vision’s head.

Fig. 3. Screenshot from Avengers: Infinity War, directed by Anthony Russo and Joe Russo, 2018

Extracting the Mind Stone from Vision without killing him requires the detection of a specific set of neurons that CONNECT to the Mind Stone. Given a Directed Graph that contains vertices (neurons) and edges (synapses), the MindStoneNeighborNeurons method returns a list of neurons that connect to the Mind Stone.

In the brain, cranial nerves usually have dendrites and an axon. A synapse occurs between a dendrite and an axon. The dendrites receive information from other nerves, and the axon transmits information to other nerves. As such, information always flows from dendrites of a nerve, and then into the axon of that nerve. That is why the graph is directed!

For this method, you can assume that the neurons have multiple dendrites and only one axon. You can also assume that a nerve’s axon always connects to ONLY 1 dendrite of another nerve (or it connects to the Mind Stone).

Also, the Mind Stone does not have any axons, but it does receive information from other nerves. As such, the Mind Stone always has an out-degree of 0. Use this to your advantage!

Task: given a Set of Edges representing Vision’s Neural Network, identify all of the vertices that connect to the Mind Stone. List the names of these neurons in the output file.




UseTimeStone

Steps for the task:

  • Create the adjacency matrix using the input file.
    Each vertex is an event, each path with connecting vertices is a timeline. The vertices are numbered from 0, and each vertex has an associated value called the EU value.
  • Determine the TOTAL number of UNIQUE timelines.
    This would be the total number of unique PATHS through the graph.
  • The input file gives a threshold EU. Determine the number of timelines (paths) whose EU is greater than or equal to the threshold EU.
    The EU of a timeline (path) is the SUM of the EUs of all the events (vertices) in that timeline (so all the vertices in the path).
  • Print the results from the previous two steps to the output file, in SEPARATE lines.

Example:

  • Input File:
    • forgeExampleInp
    • The first line is the threshold EU, which has been defined earlier.
    • The next line is the number of vertices.
    • Suppose there are n vertices. Then, the next n lines show each vertex number and the associated EU value of that vertex. For example, vertex 2 has an EU value of 3.
    • The next n lines each contain n values, which form the adjacency matrix.
  • Graph:
    • forgeExampleInp
    • Here, each circle is a vertex, and the corresponding EU value of the vertex is listed nearby.
  • Paths and EU Sum:
    • forgeExampleInp
    • Remember, the EU of a timeline is the sum of EUs of all the vertices in the timeline.
    • The example graph has paths 0, 0-1, 0-1-2, 0-1-3, 0-1-3-5, 0-1-3-5-6, 0-1-3-5-7, etc. There are 12 total paths (timelines).
    • The EU of these paths are shown in the image.
    • In the example, the threshold EU is 5. There are only 4 timelines whose EU is greater than or equal to 5.
  • Output File:
    • forgeExampleInp
    • There are 12 total paths (timelines), and 4 which have an EU greater than or equal to the threshold.
    • Thus, we print 12 and 4 into the output file, in separate lines.

Preconditions:

  • The graph is gauranteed to be connected.
  • The graph is gauranteed to be directed.
  • The graph is gauranteed to be acyclic.
  • The EUs are gauranteed to be integers.

On Titan, the Guardians plan out with Iron Man and Spiderman regarding an action scheme to stop Thanos. Meanwhile, using his Time Stone, Dr. Strange tries to predict the possible future outcomes, discovering that out of around 14 million, they win only one.

Dr. Strange uses his Time Stone to view possible alternate realities.

Fig. 4. Screenshot from Avengers: Infinity War, directed by Anthony Russo and Joe Russo, 2018

  • When Dr. Strange calculates all the possible futures, the network of events that can potentially occur can be represented by a Directed Graph.
  • The value that Dr. Strange obtained would basically be the number of possible paths through this graph - in other words, the number of possible timelines.
  • Think of each timeline as a “path” through the graph where each vertex represents an event that could potentially occur.
  • For instance, starting from Event 0 in the graph below, there are 6 possible timelines that could occur.
  • These timelines are 0, 0-1, 0-1-4, 0-1-2, 0-1-2-3, and 0-3.

Task: First determine the number of possible timelines in the graph, starting from the first event (event 0, in the above example). Print this value into the output file.

Each “event” also has an expected utility (EU) which is a number that shows how “desirable” the event is.

  • The EU of a timeline would be the SUM of the EUs of all the events in that timeline (meaning all the vertices in the path).
  • In Avengers: Infinity War, Dr. Strange only sees 1 timeline where the Avengers win against Thanos.
  • We want to determine the number of timelines starting from event 0 where the EU of the timeline is at least the EU of the Avengers winning!
  • The EU of the Avengers winning is given as the threshold EU.

Task: given an input threshold EU value, next determine the number of timelines starting from event 0 where the total EU is GREATER than OR equal to the threshold. Print this value into the output file in a NEW line!




PredictThanosSnap

Steps for this task:

  • Create the adjacency matrix using the input file.
    Each vertex represents a person, and each edge represents a social connection. The vertices are numbered from 0.
  • Use the StdRandom.uniform() function and the given pseudocode to determine which vertices to delete.
  • Delete the selected vertices (this can be done directly on the adjacency matrix).
  • Determine if the resulting graph is connected.
    A graph is connected with there is a path from each vertex to every other vertex.
  • Print true to the output file if the graph is connected, false otherwise.

Example:

  • Input File:
    • forgeExampleInp
    • The first line contains a seed, which is a number used by StdRandom to generate random numbers. The data type is LONG (not int)!
    • The next line is the number of vertices.
    • Suppose there are n vertices. Then, the next n lines each have n values representing the adjacency matrix.
  • Input Graph:
    • forgeExampleInp
    • Notice in the graph how all the vertices are connected, and they are numbered from 0 to n-1.
  • Graph after deletion:
    • forgeExampleInp
    • Using StdRandom.setSeed(seed) and StdRandom.uniform(), we select vertices 1, 2, 4, and 6 in this example to delete.
    • Once the deletion happens, we see that the resulting graph is NO longer connected!
    • This is because the undeleted vertices are not connected to each other.
    • Specifically, 0 and 3 are seperated from the rest of the undeleted vertices 5 and 7.
  • Output File:
    • forgeExampleInp
    • We output true or false depending on if the graph AFTER the random deletion is connected or not.

Seed pseudocode:

  • forgeExampleInp
  • We use StdRandom.getSeed(seed) to set and use the given seed from the input file.
  • We use StdRandom.uniform() to generate a random number between 0 and 1. This number depends on the seed.
  • We then iterate through all the vertices to choose which ones to delete.
  • Thus, for a certain vertex, we generate a random number, and if it is less than or equal to 0.5, we delete that vertex.

Preconditions:

  • The graph is gauranteed to be undirected.
  • The INPUT graph is gauranteed to be connected.

On Vormir, Thanos sacrifices Gamora’s soul for the Soul Stone by throwing her off a cliff.

Thanos arrives on Titan and after an intense showdown, Dr. Strange gives up the Time Stone to Thanos who was about to kill Iron Man using the 4 Infinity Stones he already had in his gauntlet.


The Wakandans hold a fierce battle against the Black Order at Wakanda.


Eventually, Thor, Rocket, and Groot arrive in Wakanda to assist in defeating the Black Order.


Finally, Thanos shows up in Wakanda to take the Mind Stone. Using his Infinity Stones, Thanos easily blocks and incapacitates many of the characters fighting him there including Captain America, Black Panther, and Bruce Banner.

Using all of her power, Wanda destroys the Mind Stone (reluctantly killing Vision in the process) before Thanos reaches her.

However, using the Time Stone, Thanos reversed time until Vision was still alive, and then grabs the Mind Stone in Vision’s head and kills him.

A final attack by Thor has him dig his Strombreaker through Thanos’ chest. However, Thanos snaps his fingers and teleports away, causing half of all life to disintegrate shortly afterward.

Fig. 5. Screenshot from Avengers: Infinity War, directed by Anthony Russo and Joe Russo, 2018

  • Thanos’ snap would have had an immeasurable impact on planets such as Earth.
  • Eliminating half of the population without notice would lead to unprecedented accidents, crashes, power outages, and loss of governmental control.
  • In the PredictThanosSnap method, we work with a social network (an Adjacency Matrix) where each vertex represents a person.
  • We want to find out if half of the people (vertices) are removed, are the remaining people still able to contact each other?
  • In other words, if half of the vertices are removed, is the graph still connected?
  • Because Thanos’ snap was impartial and unbiased, we use a random() function to determine which vertices to eliminate.
  • In essence, each person has a 50% probability of disappearing, and you will use a seed so that the randomization yields the same results in the autograder as well as your program.

Example - before Snap:

Example - after Snap:

The social network in the example is not connected since person 6 has no way of contacting person 1 and person 3 (since person 4 disappears).

Task: Return a boolean (true or false) which indicates if the graph is still connected after the removal.