Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Assembly Line Scheduling | DP-34
Next article icon

Assembly Line Scheduling | DP-34

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Assembly line scheduling is a manufacturing problem. In automobile industries assembly lines are used to transfer parts from one station to another station.

- Manufacturing of large items like car, trucks etc. generally undergoes through multiple stations, where each station is responsible for assembling particular part only. Entire product be ready after it goes through predefined n stations in sequence. 

- Manufacturing of car may be done through several stages like engine fitting, coloring, light fitting, fixing of controlling system, gates, seats and many other things.

-The particular task is carried out at the station dedicated to that task only. Based on the requirement there may be more than one assembly line.

-In case of two assembly lines if the load at station j at assembly 1 is very high, then components are transfer to station of assembly line 2 the converse is also true. This technique helps to speed ups the manufacturing process.

 -The time to transfer partial product from one station to next station on the same assembly line is negligible. During rush factory may transfer partially completed auto from one assembly line to another, complete the manufacturing as quickly as possible.

Assembly line scheduling is a problem in operations management that involves determining the optimal sequence of tasks or operations on an assembly line to minimize production costs or maximize efficiency. This problem can be solved using various data structures and algorithms. One common approach is dynamic programming, which involves breaking the problem down into smaller sub-problems and solving them recursively.

The following is an overview of the steps involved in solving an assembly line scheduling problem using dynamic programming:

  • Define the problem: The first step is to define the problem, including the number of tasks or operations involved, the time required to perform each task on each assembly line, and the cost or efficiency associated with each task.
  • Define the sub-problems: Next, we need to define the sub-problems by breaking down the problem into smaller pieces. In assembly line scheduling, this involves determining the optimal sequence of tasks for each station along the assembly line.
  • Define the recurrence relation: The recurrence relation defines the relationship between the sub-problems and the overall problem. In assembly line scheduling, the recurrence relation involves computing the minimum cost or maximum efficiency of the assembly line by considering the cost or efficiency of the previous station and the time required to transition to the next station.
  • Solve the sub-problems: To solve the sub-problems, we can use a table or matrix to store the minimum cost or maximum efficiency of each station. We can then use this table to determine the optimal sequence of tasks for the entire assembly line.
  • Trace the optimal path: Finally, we can trace the optimal path through the table or matrix to determine the sequence of tasks that minimizes production costs or maximizes efficiency.

A car factory has two assembly lines, each with n stations. A station is denoted by Si,j where i is either 1 or 2 and indicates the assembly line the station is on, and j indicates the number of the station. The time taken per station is denoted by ai,j. Each station is dedicated to some sort of work like engine fitting, body fitting, painting, and so on. So, a car chassis must pass through each of the n stations in order before exiting the factory. The parallel stations of the two assembly lines perform the same task. After it passes through station Si,j, it will continue to station Si,j+1 unless it decides to transfer to the other line. Continuing on the same line incurs no extra cost, but transferring from line i at station j - 1 to station j on the other line takes time ti,j. Each assembly line takes an entry time ei and exit time xi which may be different for the two lines. Give an algorithm for computing the minimum time it will take to build a car chassis.

The below figure presents the problem in a clear picture: 
 

Assembly Line Scheduling Problem

The following information can be extracted from the problem statement to make it simpler: 

  • Two assembly lines, 1 and 2, each with stations from 1 to n.
  • A car chassis must pass through all stations from 1 to n in order(in any of the two assembly lines). i.e. it cannot jump from station i to station j if they are not at one move distance.
  • The car chassis can move one station forward in the same line, or one station diagonally in the other line. It incurs an extra cost ti, j to move to station j from line i. No cost is incurred for movement in same line.
  • The time taken in station j on line i is ai, j.
  • Si, j represents a station j on line i.

Breaking the problem into smaller sub-problems: 
We can easily find the ith factorial if (i-1)th factorial is known. Can we apply the similar funda here? 
If the minimum time taken by the chassis to leave station Si, j-1 is known, the minimum time taken to leave station Si, j can be calculated quickly by combining ai, j and ti, j.
T1(j) indicates the minimum time taken by the car chassis to leave station j on assembly line 1.
T2(j) indicates the minimum time taken by the car chassis to leave station j on assembly line 2.

Base cases: 
The entry time ei comes into picture only when the car chassis enters the car factory.
Time taken to leave the first station in line 1 is given by: 
T1(1) = Entry time in Line 1 + Time spent in station S1,1 
T1(1) = e1 + a1,1 
Similarly, time taken to leave the first station in line 2 is given by: 
T2(1) = e2 + a2,1

Recursive Relations: 
If we look at the problem statement, it quickly boils down to the below observations: 
The car chassis at station S1,j can come either from station S1, j-1 or station S2, j-1.

Case #1: Its previous station is S1, j-1 
The minimum time to leave station S1,j is given by: 
T1(j) = Minimum time taken to leave station S1, j-1 + Time spent in station S1, j 
T1(j) = T1(j-1) + a1, j

Case #2: Its previous station is S2, j-1 
The minimum time to leave station S1, j is given by: 
T1(j) = Minimum time taken to leave station S2, j-1 + Extra cost incurred to change the assembly line + Time spent in station S1, j 
T1(j) = T2(j-1) + t2, j + a1, j

The minimum time T1(j) is given by the minimum of the two obtained in cases #1 and #2. 
T1(j) = min((T1(j-1) + a1, j), (T2(j-1) + t2, j + a1, j)) 

Similarly, the minimum time to reach station S2, j is given by: 
T2(j) = min((T2(j-1) + a2, j), (T1(j-1) + t1, j + a2, j))

The total minimum time taken by the car chassis to come out of the factory is given by: 
Tmin = min(Time taken to leave station Si,n + Time taken to exit the car factory) 
Tmin = min(T1(n) + x1, T2(n) + x2)

Assembly line Scheduling Using  Recursion:

C++
#include <iostream> #include <vector> using namespace std;  int fun(vector<vector<int> > a, vector<vector<int> > t,         int cl, int cs, int x1, int x2, int n) {     // base case     if (cs == n - 1) {         if (cl == 0) { // exiting from (current) line =0             return x1;         }         else // exiting from line 2             return x2;     }     // continue on same line     int same         = fun(a, t, cl, cs + 1, x1, x2, n) + a[cl][cs + 1];     // continue on different line     int diff = fun(a, t, !cl, cs + 1, x1, x2, n)                + a[!cl][cs + 1] + t[cl][cs + 1];      return min(same, diff); } int main() {     int n = 4; // number of statin     vector<vector<int> > a         = { { 4, 5, 3, 2 }, { 2, 10, 1, 4 } };     vector<vector<int> > t         = { { 0, 7, 4, 5 }, { 0, 9, 2, 8 } };      int e1 = 10;     int e2 = 12;     int x1 = 18;     int x2 = 7;     // entry from 1st line     int x = fun(a, t, 0, 0, x1, x2, n) + e1 + a[0][0];     // entry from 2nd line     int y = fun(a, t, 1, 0, x1, x2, n) + e2 + a[1][0];     cout << min(x, y) << endl; } 
Python3
def fun(a, t, cl, cs, x1, x2, n):     # base case     if cs == n - 1:         if cl == 0:  # exiting from (current) line =0             return x1         else:  # exiting from line 2             return x2     # continue on same line     same = fun(a, t, cl, cs + 1, x1, x2, n) + a[cl][cs + 1]     # continue on different line     diff = fun(a, t, not cl, cs + 1, x1, x2, n) + a[not cl][cs + 1] + t[cl][cs + 1]     return min(same, diff)   n = 4  # number of stations a = [[4, 5, 3, 2], [2, 10, 1, 4]]  # time taken at each station t = [[0, 7, 4, 5], [0, 9, 2, 8]]  # time taken to switch lines e1 = 10  # time taken to enter first line e2 = 12  # time taken to enter second line x1 = 18  # time taken to exit first line x2 = 7  # time taken to exit second line  # entry from 1st line x = fun(a, t, 0, 0, x1, x2, n) + e1 + a[0][0] # entry from 2nd line y = fun(a, t, 1, 0, x1, x2, n) + e2 + a[1][0]  print(min(x, y)) 
Java
import java.util.*;  public class AssemblyLineScheduling {      // Recursive function to calculate minimum time required     // to exit from the factory     public static int fun(ArrayList<ArrayList<Integer> > a,                           ArrayList<ArrayList<Integer> > t,                           int cl, int cs, int x1, int x2,                           int n)     {          // base case         if (cs == n - 1) {             if (cl == 0) { // exiting from (current) line =0                 return x1;             }             else { // exiting from line 2                 return x2;             }         }          // continue on same line         int same = fun(a, t, cl, cs + 1, x1, x2, n)                    + a.get(cl).get(cs + 1);          // continue on different line         int diff             = fun(a, t, cl == 0 ? 1 : 0, cs + 1, x1, x2, n)               + a.get(cl == 0 ? 1 : 0).get(cs + 1)               + t.get(cl).get(cs + 1);          return Math.min(same, diff);     }      // Main function     public static void main(String[] args)     {          int n = 4; // number of stations          // Create and initialize ArrayList of ArrayLists for         // a and t         ArrayList<ArrayList<Integer> > a             = new ArrayList<>();         ArrayList<ArrayList<Integer> > t             = new ArrayList<>();          // Add elements to ArrayList of ArrayLists a and t         a.add(new ArrayList<Integer>(             Arrays.asList(4, 5, 3, 2)));         a.add(new ArrayList<Integer>(             Arrays.asList(2, 10, 1, 4)));          t.add(new ArrayList<Integer>(             Arrays.asList(0, 7, 4, 5)));         t.add(new ArrayList<Integer>(             Arrays.asList(0, 9, 2, 8)));          int e1 = 10;         int e2 = 12;         int x1 = 18;         int x2 = 7;          // Entry from 1st line         int x = fun(a, t, 0, 0, x1, x2, n) + e1                 + a.get(0).get(0);          // Entry from 2nd line         int y = fun(a, t, 1, 0, x1, x2, n) + e2                 + a.get(1).get(0);          // Print minimum time required to exit from the         // factory         System.out.println(Math.min(x, y));     } } 
JavaScript
// Function to calculate minimum time required // to exit from the factory function fun(a, t, cl, cs, x1, x2, n) {  // base case if (cs == n - 1) { if (cl == 0) { // exiting from (current) line =0 return x1; } else { // exiting from line 2 return x2; } }  // continue on same line let same = fun(a, t, cl, cs + 1, x1, x2, n) + a[cl][cs + 1];  // continue on different line let diff = fun(a, t, cl == 0 ? 1 : 0, cs + 1, x1, x2, n) + a[cl == 0 ? 1 : 0][cs + 1] + t[cl][cs + 1];  return Math.min(same, diff); }  // Main function function main() {  const n = 4; // number of stations  // Create and initialize arrays of arrays for a and t const a = [ [4, 5, 3, 2], [2, 10, 1, 4] ]; const t = [ [0, 7, 4, 5], [0, 9, 2, 8] ];  const e1 = 10; const e2 = 12; const x1 = 18; const x2 = 7;  // Entry from 1st line const x = fun(a, t, 0, 0, x1, x2, n) + e1 + a[0][0];  // Entry from 2nd line const y = fun(a, t, 1, 0, x1, x2, n) + e2 + a[1][0];  // Print minimum time required to exit from the factory console.log(Math.min(x, y)); }  // Call main function main(); 
C#
//C# equivalent of the above Java code using System; using System.Collections.Generic;  public class AssemblyLineScheduling {     // Recursive function to calculate minimum time required     // to exit from the factory     public static int fun(List<List<int>> a,         List<List<int>> t,         int cl, int cs, int x1, int x2,         int n)     {         // base case         if (cs == n - 1)         {             if (cl == 0) // exiting from (current) line =0             {                 return x1;             }             else // exiting from line 2             {                 return x2;             }         }          // continue on same line         int same = fun(a, t, cl, cs + 1, x1, x2, n)              + a[cl][cs + 1];          // continue on different line         int diff = fun(a, t, cl == 0 ? 1 : 0, cs + 1, x1, x2, n)             + a[cl == 0 ? 1 : 0][cs + 1]             + t[cl][cs + 1];          return Math.Min(same, diff);     }      // Main function     public static void Main(string[] args)     {         int n = 4; // number of stations          // Create and initialize List of Lists for         // a and t         List<List<int>> a = new List<List<int>>();         List<List<int>> t = new List<List<int>>();          // Add elements to List of Lists a and t         a.Add(new List<int>(new int[] { 4, 5, 3, 2 }));         a.Add(new List<int>(new int[] { 2, 10, 1, 4 }));          t.Add(new List<int>(new int[] { 0, 7, 4, 5 }));         t.Add(new List<int>(new int[] { 0, 9, 2, 8 }));          int e1 = 10;         int e2 = 12;         int x1 = 18;         int x2 = 7;          // Entry from 1st line         int x = fun(a, t, 0, 0, x1, x2, n) + e1             + a[0][0];          // Entry from 2nd line         int y = fun(a, t, 1, 0, x1, x2, n) + e2             + a[1][0];          // Print minimum time required to exit from the         // factory         Console.WriteLine(Math.Min(x, y));     } } 

Output
35

Time Complexity: O(2^n) where n = number of stations
Auxiliary Space: O(n) as the recursion depth of the function is proportional to n, so the space required by the function call stack is also O(n)

Why dynamic programming? 
The above recursion exhibits overlapping sub-problems. There are two ways to reach station S1, j: 

  1. From station S1, j-1
  2. From station S2, j-1

So, to find the minimum time to leave station S1, j the minimum time to leave the previous two stations must be calculated(as explained in above recursion).

Similarly, there are two ways to reach station S2, j: 

  1. From station S2, j-1
  2. From station S1, j-1

Please note that the minimum times to leave stations S1, j-1 and S2, j-1 have already been calculated.
So, we need two tables to store the partial results calculated for each station in an assembly line. The table will be filled in a bottom-up fashion.

Note: 
In this post, the word “leave” has been used in place of “reach” to avoid confusion. Since the car chassis must spend a fixed time in each station, the word leave suits better.

Implementation: 

C++
// A C++ program to find minimum possible  // time by the car chassis to complete  #include <bits/stdc++.h> using namespace std; #define NUM_LINE 2  #define NUM_STATION 4   // Utility function to find a minimum of two numbers  int min(int a, int b) {      return a < b ? a : b;  }   int carAssembly(int a[][NUM_STATION],                  int t[][NUM_STATION],                  int *e, int *x)  {      int T1[NUM_STATION], T2[NUM_STATION], i;       // time taken to leave first station in line 1      T1[0] = e[0] + a[0][0];           // time taken to leave first station in line 2      T2[0] = e[1] + a[1][0];       // Fill tables T1[] and T2[] using the      // above given recursive relations      for (i = 1; i < NUM_STATION; ++i)      {          T1[i] = min(T1[i - 1] + a[0][i],                      T2[i - 1] + t[1][i] + a[0][i]);          T2[i] = min(T2[i - 1] + a[1][i],                     T1[i - 1] + t[0][i] + a[1][i]);      }       // Consider exit times and return minimum      return min(T1[NUM_STATION - 1] + x[0],                 T2[NUM_STATION - 1] + x[1]);  }   // Driver Code int main()  {      int a[][NUM_STATION] = {{4, 5, 3, 2},                              {2, 10, 1, 4}};      int t[][NUM_STATION] = {{0, 7, 4, 5},                              {0, 9, 2, 8}};      int e[] = {10, 12}, x[] = {18, 7};       cout << carAssembly(a, t, e, x);       return 0;  }   // This is code is contributed by rathbhupendra 
C
// A C program to find minimum possible time by the car chassis to complete #include <stdio.h> #define NUM_LINE 2 #define NUM_STATION 4  // Utility function to find minimum of two numbers int min(int a, int b) { return a < b ? a : b; }  int carAssembly(int a[][NUM_STATION], int t[][NUM_STATION], int *e, int *x) {     int T1[NUM_STATION], T2[NUM_STATION], i;      T1[0] = e[0] + a[0][0]; // time taken to leave first station in line 1     T2[0] = e[1] + a[1][0]; // time taken to leave first station in line 2      // Fill tables T1[] and T2[] using the above given recursive relations     for (i = 1; i < NUM_STATION; ++i)     {         T1[i] = min(T1[i-1] + a[0][i], T2[i-1] + t[1][i] + a[0][i]);         T2[i] = min(T2[i-1] + a[1][i], T1[i-1] + t[0][i] + a[1][i]);     }      // Consider exit times and return minimum     return min(T1[NUM_STATION-1] + x[0], T2[NUM_STATION-1] + x[1]); }  int main() {     int a[][NUM_STATION] = {{4, 5, 3, 2},                 {2, 10, 1, 4}};     int t[][NUM_STATION] = {{0, 7, 4, 5},                 {0, 9, 2, 8}};     int e[] = {10, 12}, x[] = {18, 7};      printf("%d", carAssembly(a, t, e, x));      return 0; } 
Java
// A java program to find minimum possible // time by the car chassis to complete import java.io.*;  class GFG  {     static int NUM_LINE = 2;     static int NUM_STATION = 4;          // Utility function to find minimum of two numbers     static int min(int a, int b)      {          return a < b ? a : b;               }          static int carAssembly(int a[][], int t[][], int e[], int x[])     {         int T1[]= new int [NUM_STATION];         int T2[] =new int[NUM_STATION] ;         int i;              // time taken to leave first station in line 1         T1[0] = e[0] + a[0][0];                   // time taken to leave first station in line 2         T2[0] = e[1] + a[1][0];              // Fill tables T1[] and T2[] using          // the above given recursive relations         for (i = 1; i < NUM_STATION; ++i)         {             T1[i] = min(T1[i - 1] + a[0][i],                      T2[i - 1] + t[1][i] + a[0][i]);             T2[i] = min(T2[i - 1] + a[1][i],                      T1[i - 1] + t[0][i] + a[1][i]);         }              // Consider exit times and return minimum         return min(T1[NUM_STATION-1] + x[0],                      T2[NUM_STATION-1] + x[1]);     }               // Driver code     public static void main (String[] args)      {         int a[][] = {{4, 5, 3, 2},                     {2, 10, 1, 4}};         int t[][] = {{0, 7, 4, 5},                     {0, 9, 2, 8}};         int e[] = {10, 12}, x[] = {18, 7};              System.out.println(carAssembly(a, t, e, x));              } } // This code is contributed by vt_m 
Python3
# Python program to find minimum possible  # time by the car chassis to complete  def carAssembly (a, t, e, x):          NUM_STATION = len(a[0])     T1 = [0 for i in range(NUM_STATION)]     T2 = [0 for i in range(NUM_STATION)]          T1[0] = e[0] + a[0][0] # time taken to leave                            # first station in line 1     T2[0] = e[1] + a[1][0] # time taken to leave                            # first station in line 2      # Fill tables T1[] and T2[] using     # above given recursive relations     for i in range(1, NUM_STATION):         T1[i] = min(T1[i-1] + a[0][i],                     T2[i-1] + t[1][i] + a[0][i])         T2[i] = min(T2[i-1] + a[1][i],                     T1[i-1] + t[0][i] + a[1][i] )      # consider exit times and return minimum     return min(T1[NUM_STATION - 1] + x[0],                T2[NUM_STATION - 1] + x[1])  a = [[4, 5, 3, 2],      [2, 10, 1, 4]] t = [[0, 7, 4, 5],      [0, 9, 2, 8]] e = [10, 12] x = [18, 7]  print(carAssembly(a, t, e, x))  # This code is contributed by Soumen Ghosh 
C#
// A C# program to find minimum possible // time by the car chassis to complete using System;  class GFG {          static int NUM_STATION = 4;          // Utility function to find minimum      // of two numbers     static int min(int a, int b)      {          return a < b ? a : b;               }          static int carAssembly(int [,]a, int [,]t,                              int []e, int []x)     {         int []T1= new int [NUM_STATION];         int []T2 =new int[NUM_STATION] ;         int i;              // time taken to leave first station         // in line 1         T1[0] = e[0] + a[0,0];                   // time taken to leave first station         // in line 2         T2[0] = e[1] + a[1,0];              // Fill tables T1[] and T2[] using          // the above given recursive relations         for (i = 1; i < NUM_STATION; ++i)         {             T1[i] = min(T1[i - 1] + a[0,i],                    T2[i - 1] + t[1,i] + a[0,i]);             T2[i] = min(T2[i - 1] + a[1,i],                    T1[i - 1] + t[0,i] + a[1,i]);         }              // Consider exit times and return         // minimum         return min(T1[NUM_STATION-1] + x[0],                      T2[NUM_STATION-1] + x[1]);     }          // Driver code     public static void Main ()      {         int [,]a = { {4, 5, 3, 2},                      {2, 10, 1, 4} };                               int [,]t = { {0, 7, 4, 5},                      {0, 9, 2, 8} };                               int []e = {10, 12};         int []x = {18, 7};              Console.Write(carAssembly(a, t, e, x));           } }  // This code is contributed by nitin mittal. 
PHP
<?php // A PHP program to find minimum // possible time by the car chassis // to complete  $NUM_LINE = 2; $NUM_STATION = 4;  // Utility function to find  // minimum of two numbers function carAssembly($a, $t,                       $e, $x) {     global $NUM_LINE,            $NUM_STATION;     $T1 = array();      $T2 = array();     $i;      $T1[0] = $e[0] + $a[0][0]; // time taken to leave                                 // first station in line 1     $T2[0] = $e[1] + $a[1][0]; // time taken to leave                                 // first station in line 2      // Fill tables T1[] and T2[]      // using the above given     // recursive relations     for ($i = 1;           $i < $NUM_STATION; ++$i)     {         $T1[$i] = min($T1[$i - 1] + $a[0][$i],                        $T2[$i - 1] + $t[1][$i] +                                      $a[0][$i]);         $T2[$i] = min($T2[$i - 1] + $a[1][$i],                        $T1[$i - 1] + $t[0][$i] +                                      $a[1][$i]);     }      // Consider exit times      // and return minimum     return min($T1[$NUM_STATION - 1] + $x[0],                 $T2[$NUM_STATION - 1] + $x[1]); }  // Driver Code $a = array(array(4, 5, 3, 2),            array(2, 10, 1, 4)); $t = array(array(0, 7, 4, 5),            array(0, 9, 2, 8)); $e = array(10, 12); $x = array(18, 7);  echo carAssembly($a, $t, $e, $x);  // This code is contributed // by anuj_67. ?> 
JavaScript
<script>  // A JavaScript program to find minimum possible // time by the car chassis to complete  const NUM_LINE = 2; const NUM_STATION = 4;  // Utility function to find a minimum of two numbers function min(a, b) {     return a < b ? a : b; }  function carAssembly(a, t, e, x) {     let T1 = new Array(NUM_STATION);      let T2 = new Array(NUM_STATION);     let i;      // time taken to leave first station in line 1     T1[0] = e[0] + a[0][0];          // time taken to leave first station in line 2     T2[0] = e[1] + a[1][0];      // Fill tables T1[] and T2[] using the     // above given recursive relations     for (i = 1; i < NUM_STATION; ++i)     {         T1[i] = min(T1[i - 1] + a[0][i],                     T2[i - 1] + t[1][i] + a[0][i]);         T2[i] = min(T2[i - 1] + a[1][i],                     T1[i - 1] + t[0][i] + a[1][i]);     }      // Consider exit times and return minimum     return min(T1[NUM_STATION - 1] + x[0],             T2[NUM_STATION - 1] + x[1]); }  // Driver Code     let a = [[4, 5, 3, 2],                             [2, 10, 1, 4]];     let t = [[0, 7, 4, 5],                             [0, 9, 2, 8]];     let e = [10, 12], x = [18, 7];      document.write(carAssembly(a, t, e, x));   // This code is contributed by Surbhi Tyagi.  </script> 

Output
 

Time Complexity: O(NUM_STATION), where NUM_STATION = number of stations
Auxiliary Space: O(1)

Assembly Line Scheduling Problem Solution

The bold line shows the path covered by the car chassis for given input values. We need only the last two values in the auxiliary arrays. So instead of creating two arrays, we can use two variables.

C++
// A space optimized solution for // assembly line scheduling  #include <bits/stdc++.h>  using namespace std;   int carAssembly(int a[][4],                  int t[][4],                  int *e, int *x)  {      int first, second, i;      // Time taken to leave first      // station in line 1      first = e[0] + a[0][0];           // Time taken to leave first      // station in line 2      second = e[1] + a[1][0];       // Fill tables T1[] and T2[] using the      // above given recursive relations      for(i = 1; i < 4; ++i)      {          int up =  min(first + a[0][i],                       second + t[1][i] +                                a[0][i]);          int down = min(second + a[1][i],                          first + t[0][i] +                                  a[1][i]);          first = up;         second = down;     }       // Consider exit times and      // return minimum      return min(first + x[0],                second + x[1]);  }   // Driver Code  int main()  {      int a[][4] = { { 4, 5, 3, 2 },                     { 2, 10, 1, 4 } };      int t[][4] = { { 0, 7, 4, 5 },                     { 0, 9, 2, 8 } };      int e[] = { 10, 12 }, x[] = { 18, 7 };       cout << carAssembly(a, t, e, x);       return 0;  }   // This code is contributed by chitrasingla2001 
Java
// A space optimized solution for assembly line scheduling public class AssemblyLine {     public static void main(String[] args) {         int a[][] = {{4, 5, 3, 2},                 {2, 10, 1, 4}};         int t[][] = {{0, 7, 4, 5},                 {0, 9, 2, 8}};         int e[] = {10, 12}, x[] = {18, 7};           System.out.println(carAssembleTime(a, t, e, x));     }       public static int carAssembleTime(int a[][], int t[][],                                         int e[], int x[]) {         int n = a[0].length;                  // time taken to leave first station in line 1           int first = e[0] + a[0][0];          // time taken to leave first station in line 2         int second = e[1] + a[1][0];                   for (int i = 1; i < n; i++) {             int up = Math.min(first + a[0][i],                     second + t[1][i] + a[0][i]),                     down = Math.min(second + a[1][i],                             first + t[0][i] + a[1][i]);             first = up;             second = down;         }           first += x[0];         second += x[1];           return Math.min(first, second);     } } 
Python3
# A space optimized solution for assembly # line scheduling in Python3 def carAssembleTime(a, t, e, x):          n = len(a[0])      # Time taken to leave first station     # in line 1     first = e[0] + a[0][0]      # Time taken to leave first station      # in line 2     second = e[1] + a[1][0]      for i in range(1, n):         up = min(first + a[0][i],                   second + t[1][i] + a[0][i])         down = min(second + a[1][i],                     first + t[0][i] + a[1][i])                       first, second = up, down       first += x[0]      second += x[1]       return min(first, second)  # Driver Code a = [ [ 4, 5, 3, 2 ], [ 2, 10, 1, 4 ] ]  t = [ [ 0, 7, 4, 5 ], [ 0, 9, 2, 8 ] ] e = [ 10, 12 ]  x = [ 18, 7 ]       print(carAssembleTime(a, t, e, x))  # This code is contributed by Prateek Gupta  
C#
// A space optimized solution for  // assembly line scheduling using System;  class GFG{      static int carAssembleTime(int[,] a, int[,] t,                             int[] e, int[] x) {     int n = a.GetLength(1);          // Time taken to leave first station in line 1       int first = e[0] + a[0, 0];      // Time taken to leave first station in line 2     int second = e[1] + a[1, 0];            for(int i = 1; i < n; i++)     {         int up = Math.Min(first + a[0, i],                second + t[1, i] + a[0, i]),             down = Math.Min(second + a[1, i],                    first + t[0, i] + a[1, i]);                first = up;         second = down;     }      first += x[0];     second += x[1];      return Math.Min(first, second); }  // Driver Code static void Main()  {     int[,] a = { { 4, 5, 3, 2 },                  { 2, 10, 1, 4 } };     int[,] t = { { 0, 7, 4, 5 },                  { 0, 9, 2, 8 } };     int[] e = { 10, 12 }, x = { 18, 7 };          Console.WriteLine(carAssembleTime(a, t, e, x)); } }  // This code is contributed by divyeshrabadiya07 
JavaScript
<script> // A space optimized solution for assembly line scheduling      function carAssembleTime(a , t , e , x) {         var n = a[0].length;          // time taken to leave first station in line 1         var first = e[0] + a[0][0];          // time taken to leave first station in line 2         var second = e[1] + a[1][0];          for (var i = 1; i < n; i++) {             var up = Math.min(first + a[0][i], second + t[1][i] + a[0][i]),                     down = Math.min(second + a[1][i], first + t[0][i] + a[1][i]);             first = up;             second = down;         }          first += x[0];         second += x[1];          return Math.min(first, second);         }        var a = [ [ 4, 5, 3, 2 ], [ 2, 10, 1, 4 ] ];         var t = [ [ 0, 7, 4, 5 ], [ 0, 9, 2, 8 ] ];         var e = [ 10, 12 ], x = [ 18, 7 ];          document.write(carAssembleTime(a, t, e, x));      // This code is contributed by gauravrajput1 </script> 

Output
35

Time Complexity: O(n), where n = number of stations
Auxiliary Space: O(1)

Exercise: 
Extend the above algorithm to print the path covered by the car chassis in the factory.

References: 
Introduction to Algorithms 3rd Edition by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
This article is compiled by Aashish Barnwal.
 


Next Article
Assembly Line Scheduling | DP-34

K

kartik
Improve
Article Tags :
  • DSA
  • Dynamic Programming
Practice Tags :
  • Dynamic Programming

Similar Reads

    Basic Blocks in Compiler Design
    Basic Block is a straight line code sequence that has no branches in and out branches except to the entry and at the end respectively. Basic Block is a set of statements that always executes one after other, in a sequence. The first task is to partition a sequence of three-address codes into basic b
    3 min read
    8086 program for selection sort
    Problem - Write an assembly language program in 8086 microprocessor to sort a given array of n numbers using Selection Sort. Assumptions - The number of elements in the array is stored at offset 500. The array starts from offset 501. Example - Algorithm - We first find the smallest number in the arr
    3 min read
    Round Robin Scheduling Algorithm with Different Arrival Time
    Round Robin Scheduling is one of the most popular CPU scheduling algorithms used in operating systems. This algorithm is designed to handle processes efficiently by assigning a fixed time slice or quantum to each process. However, when processes arrive at different times, the scheduling becomes slig
    15+ min read
    Phases of a Assembler
    The Assembler is a program that converts assembly language into machine language that can be executed by a computer. The Assembler operates in two main phases: Analysis Phase and Synthesis Phase. The Analysis Phase validates the syntax of the code, checks for errors, and creates a symbol table. The
    4 min read
    Operating Systems | Set 12
    Following questions have been asked in GATE CS 2007 exam. 1) Consider a disk pack with 16 surfaces, 128 tracks per surface and 256 sectors per track. 512 bytes of data are stored in a bit serial manner in a sector. The capacity of the disk pack and the number of bits required to specify a particular
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences