Assignment 6 - Graph Algorithms
Assignment 6 - Graph Algorithms
Total Marks: 60
You are playing an easier variation of the board game with your friend. There is an N∗M grid,
depicting the world map. Each cell of the grid is either 1 or 0 where 1 denotes that there is
land on this cell, while 0 denotes water.
In a turn, a player can capture an uncaptured cell that has land, and keep capturing
neighbouring cells that share a side (4-adjacent) with it if they are on land. A cell once
captured by a player can't be captured again by another player. The game continues till no
uncaptured cell has land. Each player wants to be in control of as many cells of land as
possible in total when the game finishes. Find the maximum number of cells of land that you
can capture if you play second, and both players play optimally.
Input:
● N+1 lines of input.
● First line will contain 2 space separated integers N and M denoting the size of the
grid.
● Each of the next N lines contains a binary string of length M depicting the map.
Output: The output is the number of cells captured by the second player, if both players play
optimally. Output in a single number.
Constraints:
● Each character in the grid is either 0 or 1
● There's at least 1 land on the grid
Example:
Input:
44
1001
0110
0110
1001
Output:
2
Explanation:
Step1: Friend={(2,2),(2,3),(3,2),(3,3)} comprising 4 lands.
Step2: You={(1)} comprising 1 land.
Step3: Friend = {(1, 4)} comprising 1 land.
Step4: You={(4, 1)} comprising 1 land.
Step 5: Friend={(4, 4)} comprising 1 land.
Lands occupied by you = 1+1 = 2
1. Given a graph G and a minimum spanning tree T, suppose that we decrease the
weight of one of the edges in T. Show that T is still a minimum spanning tree for G.
More formally, let T be a minimum spanning tree for G with edge weights given by
weight function w. Choose one edge (x, y) ∈ T and a positive number k, and define
the weight function w’ by
w’(u, v) = w(u, v), if (u, v) ≠ (x, y), or w(u, v) − k, if (u, v) = (x, y).
Show that T is a minimum spanning tree for G with edge weights given by w’. (10)
2. Suppose we change the while loop of Dijkstra’s algorithm (described in page -- in the
textbook by Cormen et al. ) from while Q≠ϕ to while |Q| > 1. This change causes the
while loop to execute |V| -1 times instead of |V | times. Is this proposed algorithm
correct? (10)
3. Run the Floyd-Warshall algorithm on the weighted, directed graph of figure below.
Show the matrix D(k) that results for each iteration of the outer loop. (10)
Submission Instructions:
For question 1, submit the C-code which takes the name of the input file as command line
argument and prints output on the terminal. For question 2, submit a writeup in PDF format
answering the questions. Upload a zip file containing both.