Questions tagged [min-heap]
181 questions
1
vote
2 answers
PriorityQueue implementation in Java with support for changePriority operation
I require an implementation of a Priority queue that allows decrease priority operation to allow for an efficient implementation for Prim's and Dijkstra's algorithm.
I've coded up a minHeap implementation using a HashMap to store the indices of…

Karmah24
- 485
- 1
- 5
- 14
1
vote
1 answer
Prim’s MST for Adjacency List Representation
I was trying to implement Prim's algorithm using min Heap.
here is the link I was referring to
https://www.geeksforgeeks.org/prims-mst-for-adjacency-list-representation-greedy-algo-6/
I was wondering why can we use a vector and sort it using…

cgDude
- 93
- 1
- 8
1
vote
0 answers
Dijkstra's Algorithm implementation with binary heaps
I've been trying to implement Dijkstra's algorithm with binary heaps for my algorithms class.The goal is to find the length of the minimum spanning tree found through Dijkstra. I've worked with before, but using Priority queues. I find I'm getting…

Bumbling Benjamin
- 11
- 2
1
vote
0 answers
Modifying an element in min-heap and rebuild heap in sublinear time
Suppose I build a min heap:
std::vector h;
...
std::make_heap(h.begin(), h.end(), std::greater());
Then I modify an arbitrary heap element:
h[7] = 0;
The heap property is then broken after this modification so I cannot rely on…

Andreas
- 33
- 3
1
vote
1 answer
Checking if a vector is a min heap using recursion
I'm trying to write a program that checks if a vector is a min heap. I've been looking at code from here. I understand why they use 2*i+2 to compare to n, since there's a tipping point with the index where values in the vector/array (mine uses a…
user8503635
1
vote
1 answer
Find timestamp given K best candidates
So I was asked a weird inversion of the K best candidates problem. The normal problem is as follows.
Given a list of 'votes' which are tuples of timestamps and candidates like below:
(111111, Clinton)
(111111, Bush)
...
Return the top K candidates…

SpaceDandy
- 11
- 2
1
vote
0 answers
error: Process returned -1073741819 (0xC0000005) C something to do with pointers?
I'm fairly new to code::blocks and how it works but I'm using it for an assignment at uni, basically, its implementing dijkstra's algorithm using ADTs and adjacency lists, the code works and I get an output but I get the error
Process returned…

namarok
- 11
- 2
1
vote
1 answer
convert a tree into a heap using minimum number of changes
Given a k-ary tree, i want to convert it into a min-heap with minimum number of changes. Change is defined as relabelling a node.
one solution i have found is that, i can try a dp solution of changing a nodes value or not changing. But its going to…

Ensei Tankado
- 11
- 1
1
vote
3 answers
O(1) and O(log n), are they same thing?
There is a question asking whether taking a minimum element from a min-heap is o(logn) time, I thought it was false because it takes constant time because the minimum element is on the top. But the answer is true and the instructor's explanation is…

J. C
- 89
- 11
1
vote
1 answer
Check if a tree is a min heap
How do I get a predicate in prolog to return a value?
I need to find a node of a tree and also check if its a min heap or not.
I'm guessing it goes something like this:-
getnode(tree(_, node, _), node).
My code so far is this
minheap(tree(L, Node,…

John Keaton
- 51
- 8
1
vote
1 answer
Is there a better way to find shortest path between nodes in Min Heap
I have written a small snippet, to find the shortest path between two nodes in a Min Heap.
public int shortestPath(int i, int j)
{
int path = 0;
int hi = Math.max(i, j);
int lo = i + j - hi;
while (hi > lo)
{
hi = hi /…

daviddecoding
- 142
- 1
- 12
1
vote
1 answer
How min heap is used here to solve this
I would like to know how min heap is used here to solve the following problem.
What I thought to solve it is to use hashtable and save the counts of the numbers. but I don't know how to use the min heap to contiune solving the problem.
Given a…

ahmed andre
- 71
- 1
- 9
1
vote
3 answers
Min heap in javascript
Hey I tried to implement a min heap in javascript, but i had a question regarding the algorithm for remove min. I'm using an array to represent the heap internally. When I'm percolating downwards, what should be the stopping condition? In my code I…

teaflavored
- 440
- 4
- 9
1
vote
1 answer
Create Huffman Code Tree from min-heap C++
Say you have a C++ program that must read in text from a given .txt file. The program will:
compute number of occurrences of each char in the file, then each unique char (and its frequency) will be stored as a new tree node
The program then builds…

bama_programmer
- 185
- 1
- 1
- 11
1
vote
1 answer
I have to create a FUNCTION in C++ to CHECK whether the array A is a Min Heap or not? If it is Min Heap then return true otherwise return false
bool isMinHeap(int A[],int size)
{
for(int i=1; i<=size; i++)
{
if((A[i]<=A[2i]) && (A[i]<=A[2i+1]))
t=1;
else
{
t=0;
break;
}
}
if(t==1)
return true;
…

Umair Mahmood
- 63
- 1
- 9