Efficient Array-Backed Priority Queue Implementation

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

A priority queue is a data structure that allows for efficient access to the minimum (or maximum) element in a collection. It is commonly used in algorithms such as Dijkstra's shortest path algorithm and Huffman coding. In this challenge, we will implement a priority queue backed by an array.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/arrays_strings("`Arrays Strings`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/arrays_strings -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/variables_data_types -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/strings -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/type_conversion -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/conditional_statements -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/for_loops -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/lists -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/tuples -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/function_definition -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/importing_modules -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/standard_libraries -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/classes_objects -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/constructor -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/polymorphism -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/encapsulation -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/os_system -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} python/build_in_functions -.-> lab-268805{{"`Efficient Array-Backed Priority Queue Implementation`"}} end

Priority Queue

Problem

Implement a priority queue backed by an array. The priority queue should support the following methods:

  • insert: insert a new element into the priority queue
  • extract_min: remove and return the minimum element from the priority queue
  • decrease_key: decrease the key of a given element in the priority queue

Requirements

To implement the priority queue, we need to meet the following requirements:

  • The methods supported by the priority queue should be insert, extract_min, and decrease_key.
  • There won't be any duplicate keys in the priority queue.
  • We don't need to validate inputs.
  • The priority queue should fit into memory.

Example Usage

Here are some examples of how to use the priority queue methods:

insert

  • insert general case: insert a new node into the priority queue.

extract_min

  • extract_min from an empty list: return None.
  • extract_min general case: remove and return the minimum node from the priority queue.

decrease_key

  • decrease_key an invalid key: return None.
  • decrease_key general case: decrease the key of a given node in the priority queue.

Summary

In this challenge, we implemented a priority queue backed by an array. The priority queue supports insert, extract_min, and decrease_key methods. We ensured that there are no duplicate keys in the priority queue, and the inputs don't need to be validated. The priority queue fits into memory.

Other Algorithm Tutorials you may like