Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[BUG]: methodpop in PrimMST.js not working properly #1298

Closed
Labels
bugSomething isn't working
@paulinegarelli

Description

@paulinegarelli

Description

InGraphs/PrimMST.js, the method_shiftDown of classPriorityQueue does not give the correct output when a node must be shifted to a left-child leaf without a right-child "brother", which results in a wrong behavior of methodpop.

Expected Behavior

An example of a tree that produces the bug is given by the following array of nodes denoted by their priority:
queue = [0, 1, 2]

ie

  1. root node of priority 0
  2. left leaf of priority 1
  3. right leaf of priority 0

If we call methodpop on this queue, we expect to get the new tree:
queue = [1, 2]

ie

  1. root node of priority 1
  2. left leaf of priority 2

Actual Behavior

In the case described above, we will get as output a tree represented by:
queue = [2, 1]

ie

  1. root node of priority 2
  2. left leaf of priority 1

This violates the properties of the heap (we have 2 > 1 but a parent node should never have a bigger value than any of its children).

Steps to reproduce (if applicable)

  1. Add an export statement for class PriorityQueue in PrimMST.js

export { PriorityQueue }

  1. Add a test filePrimMST.test.js containing the following code
import { PriorityQueue} from '../PrimMST.js'test('Bug in pop method', () => {    // create queue    const queue = new PriorityQueue()    queue.push(0, 0)    queue.push(1, 1)    queue.push(2, 2)    // create expected queue    const expectedQueue = new PriorityQueue()    expectedQueue.push(1, 1)    expectedQueue.push(2, 2)    // result from popping element from the queue    queue.pop()    expect(queue).toEqual(expectedQueue)})
  1. Run the tests for PrimMST

npm test -- PrimMST.test.js

Additional information

This is due to the fact that in_shiftDown, the children's priorities are computed under a singletry {} catch {} statement, thus the priority of the left child is set toInfinity if the right child does not exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp