Priority Queue in PHP

Priority Queue in PHP

Today it's time to publish the code of a simple Priority Queue in PHP, but with an important update: the ability to change the priority to one element inside the queue.

This feature came across when I had to develop Dijkstra's Algorithm in PHP. The cost difference of Dijkstra's Algorithm in terms of Big O notation with a Priority Queue is simply amazing, so I couldn't do anything else; I had to implement it.  

The implementation of a method to change the priority of an element isn't difficult at all. It must be found, modified and reordered; that's pretty much it. But this, apparently very easy, must also be very efficient, and that's a bit more complicated. ;-)

In PHP, finding an element in a multi-level array is not very efficient (Cost O(n) if you use 'array_search'). This is, for me, unacceptable. The cost of finding an element should be O(1), and that's quite more complicated.

What I do to achieve this is to maintain an internal pointer called hashmap (like in Java). In this pointer I save a hash of the element, and a pointer to its current position. The difficult part is to have this pointer 'always' updated, and that's something I achieve by updating it while it is being up or down-heaped.

Using this technique, the change_priority method cost is, in the worst case, the cost of the up and down-heap operations: O(log n).

Let me know if you like this implementation of a Priority Queue in PHP.

And of course, if you want to use this Priority Queue in your PHP projects, feel free to download the code from github: https://github.com/oscarpascualbakker/priority-queue

To view or add a comment, sign in

More articles by Óscar Pascual Bakker

  • Asset Tokenization: Reinventing Ownership and Investment

    🇪🇸 ¿Quieres leer este artículo en español? In a fast-moving world, the term "Asset Tokenization" is gaining…

  • How to Start Programming in Solidity

    🇪🇸 ¿Quieres leer este artículo en español? At a recent talk in Vic, Catalonia, Spain, I found myself surrounded by…

  • Blockchain-Based Loyalty Program

    🇪🇸 ¿Quieres leer este artículo en español? The world of blockchain technology has opened up a myriad of possibilities…

  • What Is a Smart Contract and How Does It Work?

    🇪🇸 ¿Quieres leer este artículo en español? In today's digital world, blockchain technology is revolutionizing the way…

    2 Comments
  • What is Blockchain?

    🇪🇸 ¿Quieres leer este artículo en español? The term "blockchain" has become a buzzword in technology, finance, and…

  • Fiat-Shamir "Zero Knowledge Proof"

    ¿Quieres leer el artículo en español? It's a true pleasure (and also a matter of pride) to share a development of the…

  • Bloom filter in PHP

    ¿Prefieres leerlo en castellano? https://www.oscarpascual.

  • Merkle Tree o Árbol de Merkle en PHP

    En este artículo voy a hablar de la implementación en PHP de un «Merkle Tree», un árbol de Merkle, una implementación…

  • Dijkstra's algorithm in PHP

    The first time I heard about Dijkstra I was studying at the University. Dijkstra's Algorithm, by the way, was the first…

    4 Comments
  • Cola de Prioridad en PHP 7

    ¿Quién me iba a decir que iba a usar esto algún día para un proyecto profesional? Una Cola de Prioridad es una…

    2 Comments

Explore content categories