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:

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));
}
}
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:
- From station S1, j-1
- 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:
- From station S2, j-1
- 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>
Time Complexity: O(NUM_STATION), where NUM_STATION = number of stations
Auxiliary Space: O(1)

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>
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.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem