Open In App

Circular Tour using JavaScript

The circular tour problem is a classic algorithmic challenge encountered in the context of travelling between gas stations in the circular route.

Given two arrays representing the amount of gas available at each station and the fuel cost to travel from one station to the next, the task is to find the starting gas station from which one can complete the tour without running out of fuel.

Below are the approaches to implementing the circular tour using JavaScript which are as follows:

Brute-force Approach

In the brute-force approach, we iterate through each possible starting point in the tour and check if we can complete the tour by visiting all the gas stations without running out of fuel.

Example: To demonsrtate creating a circular tour brute- force approach in JavaScript.

function GFG(gas, cost) {
    const n = gas.length;
    for (let start = 0; start < n; start++) 
    {
        let petrol = 0;
        let i = start;
        let count = 0;
        while (count < n) {
            petrol += gas[i] - cost[i];
            if (petrol < 0)
                break;
            i = (i + 1) % n;
            count++;
        }
        if (count === n) {
            return start;
        }
    }
    return -1;
}
const gas = [4, 3, 5, 2];
const cost = [5, 4, 3, 4];
console.log(GFG(gas, cost));

Output :

-1

Time Complexity: O(n^2)

Space Complexity: O(1)

Optimized Approach using Circular Linked List

In this approach, we create a circular linked list where each node represents the gas station. We iterate through the linked list only once maintaining the total gas available and total fuel required to reach the next station. If at any point the total gas becomes less than the total fuel required we reset the starting point to next station and continue.

Syntax:

class ListNode {
constructor(gas, cost) {
this.gas = gas; // Gas available at this station
this.cost = cost; // Fuel cost to travel to next station
this.next = null; // Pointer to the next station
}
}
function findCircularTour(gasStation) {
// gasStation: head node of circular linked list representing gas stations
// Returns the index of the starting gas station if a circular tour is possible otherwise returns -1
}

Example: To demonsrtate creating a circular tour using circular linked list in JavaScript.

class ListNode {
    constructor(gas, cost) {
        this.gas = gas;
        this.cost = cost;
        this.next = null;
    }
}
function GFG(head) {
    if (!head) return -1;
    let start = head;
    let current = head;
    let totalGas = 0;
    let totalCost = 0;

    do {
        totalGas += current.gas;
        totalCost += current.cost;
        if (totalGas < totalCost)
        {
            start = current.next;
            totalGas = 0;
            totalCost = 0;
        }
        current = current.next;
    } while (current !== head);

    return totalGas >=
        totalCost ? findIndex(head, start) : -1;
}
function findIndex(head, target) {
    let index = 0;
    let current = head;
    while (current !== target) {
        current = current.next;
        index++;
    }
    return index;
}

const station1 = new ListNode(4, 5);
const station2 = new ListNode(3, 4);
const station3 = new ListNode(5, 3);
const station4 = new ListNode(2, 4);
station1.next = station2;
station2.next = station3;
station3.next = station4;
station4.next = station1;
console.log(GFG(station1));

Output:

2
Article Tags :