Open In App

Transportation Problem | Set 2 (NorthWest Corner Method)

Improve
Improve
Like Article
Like
Save
Share
Report

An introduction to Transportation problem has been discussed in the previous article, in this article, finding the initial basic feasible solution using the NorthWest Corner Cell Method will be discussed. 
 

Explanation: Given three sources O1, O2 and O3 and four destinations D1, D2, D3 and D4. For the sources O1, O2 and O3, the supply is 300, 400 and 500 respectively. The destinations D1, D2, D3 and D4 have demands 250, 350, 400 and 200 respectively. 
Solution: According to North West Corner method, (O1, D1) has to be the starting point i.e. the north-west corner of the table. Each and every value in the cell is considered as the cost per transportation. Compare the demand for column D1 and supply from the source O1 and allocate the minimum of two to the cell (O1, D1) as shown in the figure. 
The demand for Column D1 is completed so the entire column D1 will be canceled. The supply from the source O1 remains 300 – 250 = 50

Now from the remaining table i.e. excluding column D1, check the north-west corner i.e. (O1, D2) and allocate the minimum among the supply for the respective column and the rows. The supply from O1 is 50 which is less than the demand for D2 (i.e. 350), so allocate 50 to the cell (O1, D2). Since the supply from row O1 is completed cancel the row O1. The demand for column D2 remain 350 – 50 = 300
 

From the remaining table the north-west corner cell is (O2, D2). The minimum among the supply from source O2 (i.e 400) and demand for column D2 (i.e 300) is 300, so allocate 300 to the cell (O2, D2). The demand for the column D2 is completed so cancel the column and the remaining supply from source O2 is 400 – 300 = 100
 

Now from remaining table find the north-west corner i.e. (O2, D3) and compare the O2 supply (i.e. 100) and the demand for D2 (i.e. 400) and allocate the smaller (i.e. 100) to the cell (O2, D2). The supply from O2 is completed so cancel the row O2. The remaining demand for column D3 remains 400 – 100 = 300
 

Proceeding in the same way, the final values of the cells will be: 
 

Note: In the last remaining cell the demand for the respective columns and rows are equal which was cell (O3, D4). In this case, the supply from O3 and the demand for D4 was 200 which was allocated to this cell. At last, nothing remained for any row or column. 
Now just multiply the allocated value with the respective cell value (i.e. the cost) and add all of them to get the basic solution i.e. (250 * 3) + (50 * 1) + (300 * 6) + (100 * 5) + (300 * 3) + (200 * 2) = 4400

Below is the implementation of the above approach

C++




#include <iostream>
#include <vector>
 
int main() {
    std::vector<std::vector<int>> grid = {{3, 1, 7, 4}, {2, 6, 5, 9}, {8, 3, 3, 2}}; // table
    std::vector<int> supply = {300, 400, 500}; // supply
    std::vector<int> demand = {250, 350, 400, 200}; // demand
 
    int startR = 0; // start row
    int startC = 0; // start col
    int ans = 0;
    // loop runs until it reaches the bottom right corner
    while (startR != grid.size() && startC != grid[0].size()) {
        // if demand is greater than supply
        if (supply[startR] <= demand[startC]) {
            ans += supply[startR] * grid[startR][startC];
            // subtract the value of supply from the demand
            demand[startC] -= supply[startR];
            startR += 1;
        }
        // if supply is greater than demand
        else {
            ans += demand[startC] * grid[startR][startC];
            // subtract the value of demand from the supply
            supply[startR] -= demand[startC];
            startC += 1;
        }
    }
 
    std::cout << "The initial feasible basic solution is " << ans << std::endl;
 
    return 0;
}


Java




public class GFG {
    public static void main(String[] args)
    {
        int[][] grid = new int[][] {
            { 3, 1, 7, 4 }, { 2, 6, 5, 9 }, { 8, 3, 3, 2 }
        }; // table
 
        int[] supply
            = new int[] { 300, 400, 500 }; // supply
        int[] demand
            = new int[] { 250, 350, 400, 200 }; // demand
 
        int startR = 0; // start row
        int startC = 0; // start col
        int ans = 0;
 
        // Loop runs until it reaches the bottom right
        // corner
        while (startR != grid.length
               && startC != grid[0].length) {
 
            // If demand is greater than supply
            if (supply[startR] <= demand[startC]) {
                ans += supply[startR]
                       * grid[startR][startC];
 
                // Subtract the value of supply from the
                // demand
                demand[startC] -= supply[startR];
                startR++;
            }
 
            // If supply is greater than demand
            else {
                ans += demand[startC]
                       * grid[startR][startC];
 
                // Subtract the value of demand from the
                // supply
                supply[startR] -= demand[startC];
                startC++;
            }
        }
 
        System.out.println(
            "The initial feasible basic solution is "
            + ans);
    }
}


Python3




grid = [[3, 1, 7, 4], [2, 6, 5, 9], [8, 3, 3, 2]]  # table
supply = [300, 400, 500# supply
demand = [250, 350, 400, 200# demand
 
startR = 0  # start row
startC = 0  # start col
ans = 0
# loop runs until it reaches the bottom right corner
while(startR != len(grid) and startC != len(grid[0])):
    # if demand is greater than supply
    if(supply[startR] <= demand[startC]):
        ans += supply[startR] * grid[startR][startC]
        # subtract the value of supply from the demand
        demand[startC] -= supply[startR]
        startR += 1
    # if supply is greater than demand
    else:
        ans += demand[startC] * grid[startR][startC]
        # subtract the value of demand from the supply
        supply[startR] -= demand[startC]
        startC += 1
 
print("The initial feasible basic solution is ", ans)


C#




using System;
 
class GFG {
  public static void Main(string[] args)
  {
 
    int[, ] grid = new int[3, 4] {
      { 3, 1, 7, 4 }, { 2, 6, 5, 9 }, { 8, 3, 3, 2 }
    }; // table
    int[] supply
      = new int[3] { 300, 400, 500 }; // supply
    int[] demand
      = new int[4] { 250, 350, 400, 200 }; // demand
 
    int startR = 0; // start row
    int startC = 0; // start col
    int ans = 0;
 
    // loop runs until it reaches the bottom right
    // corner
    while (startR != grid.GetLength(0)
           && startC != grid.GetLength(1))
    {
       
      // if demand is greater than supply
      if (supply[startR] <= demand[startC]) {
        ans += supply[startR]
          * grid[startR, startC];
 
        // subtract the value of supply from the
        // demand
        demand[startC] -= supply[startR];
        startR++;
      }
       
      // if supply is greater than demand
      else {
        ans += demand[startC]
          * grid[startR, startC];
 
        // subtract the value of demand from the
        // supply
        supply[startR] -= demand[startC];
        startC++;
      }
    }
 
    Console.WriteLine(
      "The initial feasible basic solution is "
      + ans);
  }
}


Javascript




let grid = [[3, 1, 7, 4], [2, 6, 5, 9], [8, 3, 3, 2]]; // table
let supply = [300, 400, 500]; // supply
let demand = [250, 350, 400, 200]; // demand
 
let startR = 0; // start row
let startC = 0; // start col
let ans = 0;
 
// loop runs until it reaches the bottom right corner
while (startR !== grid.length && startC !== grid[0].length)
{
 
  // if demand is greater than supply
  if (supply[startR] <= demand[startC])
  {
    ans += supply[startR] * grid[startR][startC];
     
    // subtract the value of supply from the demand
    demand[startC] -= supply[startR];
    startR += 1;
  }
   
  // if supply is greater than demand
  else
  {
    ans += demand[startC] * grid[startR][startC];
     
    // subtract the value of demand from the supply
    supply[startR] -= demand[startC];
    startC += 1;
  }
}
 
console.log("The initial feasible basic solution is " + ans);


Output

The initial feasible basic solution is  4400




Last Updated : 09 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads