Table of Contents
Table of Contents
Overview
Parking Lot System
Features
Home Backend Development Golang Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK

Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK

Oct 27, 2024 pm 08:31 PM

Since I’ve been working with Go for quite some time, I thought it would be a fun challenge to implement a few classic low-level design solutions in it.

When designing an elevator system, one crucial aspect is how to decide which floor to service next, especially when the elevator has multiple requests. Go’s straightforward syntax and performance make it ideal for modeling such systems, so I set out to create basic implementations of FCFS (First Come First Serve), SSTF (Shortest Seek Time First), SCAN, and LOOK algorithms.

1. First Come First Serve (FCFS)

I started with the simplest approach: service requests in the order they’re received. It’s easy to implement but can be inefficient if the requests are spread out across floors, leading to more travel time.

func FCFS(currentFloor int, requests []int) []int {
    path := []int{}
    for _, floor := range requests {
        path = append(path, floor)
    }
    return path
}
Copy after login

In FCFS, the elevator simply moves to each requested floor in the given order.

2. Shortest Seek Time First (SSTF)

SSTF tries to minimize travel by choosing the closest requested floor next. This reduces travel time but can lead to "starvation" for far-off requests if new closer requests keep coming.

func SSTF(currentFloor int, requests []int) []int {
    path := []int{}
    remaining := append([]int{}, requests...)

    for len(remaining) > 0 {
        closestIdx := 0
        minDistance := abs(currentFloor - remaining[0])

        for i, floor := range remaining {
            distance := abs(currentFloor - floor)
            if distance < minDistance {
                closestIdx = i
                minDistance = distance
            }
        }

        currentFloor = remaining[closestIdx]
        path = append(path, currentFloor)
        remaining = append(remaining[:closestIdx], remaining[closestIdx+1:]...)
    }
    return path
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
Copy after login

This function finds the closest floor to the current floor each time, updating the elevator’s position after each move.

3. SCAN (Elevator Algorithm)

In SCAN, the elevator moves in one direction, servicing all requests in that direction until it reaches the end, then reverses. This approach is more fair than SSTF because it reduces starvation.

func SCAN(currentFloor, maxFloor int, requests []int) []int {
    path := []int{}
    up := []int{}
    down := []int{}

    for _, floor := range requests {
        if floor >= currentFloor {
            up = append(up, floor)
        } else {
            down = append(down, floor)
        }
    }

    sort.Ints(up)
    sort.Sort(sort.Reverse(sort.IntSlice(down)))

    path = append(path, up...)
    path = append(path, down...)
    return path
}
Copy after login

This function splits requests into floors above and below the current position. It serves all floors upwards, then downwards.

4. LOOK

LOOK is a slight variation of SCAN. Instead of going all the way to the end, the elevator reverses direction at the last request in each direction. It saves time by stopping where the requests end, not at the physical limits.

func LOOK(currentFloor int, requests []int) []int {
    path := []int{}
    up := []int{}
    down := []int{}

    for _, floor := range requests {
        if floor >= currentFloor {
            up = append(up, floor)
        } else {
            down = append(down, floor)
        }
    }

    sort.Ints(up)
    sort.Sort(sort.Reverse(sort.IntSlice(down)))

    path = append(path, up...)
    path = append(path, down...)
    return path
}
Copy after login

Similar to SCAN, this approach only moves as far as the last request in each direction.

Each algorithm has its trade-offs:

  • FCFS: Simple but can be inefficient.
  • SSTF: Optimizes for closest floors but can starve far-off requests.
  • SCAN: Fairer and efficient, minimizing direction changes.
  • LOOK: Saves additional time by stopping at the last request.

The right choice depends on the specific requirements for efficiency, fairness, and response time in the system.

For full implementation using LOOK algorithm, refer to my github repo:

Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK thesaltree / low-level-design-golang

Low level system design problems solutions in Golang

Low-Level System Design in Go

Welcome to the Low-Level System Design in Go repository! This repository contains various low-level system design problems and their solutions implemented in Go. The primary aim is to demonstrate the design and architecture of systems through practical examples.

Table of Contents

  • Overview
  • Parking Lot System
  • Elevator System

Overview

Low-level system design involves understanding the core concepts of system architecture and designing scalable, maintainable, and efficient systems. This repository will try to cover solutions of various problems and scenarios using Go.

Parking Lot System

The first project in this repository is a Parking Lot System. This system simulates a parking lot where vehicles can be parked and unparked. It demonstrates:

  • Singleton design pattern for managing the parking lot instance.
  • Handling different types of vehicles (e.g., cars, trucks).
  • Parking space management across multiple floors.
  • Payment processing for parked vehicles.

Features

  • Add and remove vehicles from the…


View on GitHub


The above is the detailed content of Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

See all articles