Questions tagged [clrs]

CLRS refers to the textbook "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald R. Rivest, and Clifford Stein. It is a standard textbook in algorithms and data structures.

170 questions
1
vote
1 answer

estimating my code big-O, naive pattern matching

I am trying to solve exercise 32.1-2 from the CLRS Book, which is about string algorithms, naive pattern search Suppose that all characters in the pattern P are different. Show how to accelerate NAIVE-STRING-MATCHER to run in time O(n) on an…
Mohamed Kira
  • 417
  • 2
  • 5
  • 14
1
vote
1 answer

Adjacency list indegree

I was solving this CLRS problem, which asked to find out the indegree of every vertex of a graph G(V,E). I found out the solution to be O(|E|) as we only have to scan through all the edges to find out the degrees of all vertices. But most of the…
Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63
1
vote
1 answer

Elaboration on the uniform randomness of selection of a key

Consider this question:- Efficiently picking a random element from a chained hash table? And consider it's first answer. It suggests a method to select a key in a uniformly random fashion. That, however, is unclear to me. The first step would take …
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
1
vote
4 answers

Deleting a node A[i] from a Max-Heap

CLRS Exercise: 6.5-8 The operation HEAP-DELETE(A,i) deletes the item in node i from heap A. Give an implementation of HEAP-DELETE that runs in O(lg n) time for an n-element max-heap. I wonder if the algorithm is wrong for the input…
sagar_jeevan
  • 761
  • 5
  • 16
  • 34
1
vote
2 answers

Collisions with hashing (CLRS)

I am trying to solve this problem (CLRS, 3rd edition, exercise 11.2-1): Suppose we use a hash function h to hash n distinct keys into an array of length m. Assuming simple uniform hashing, what is the expected number of collisions? The correct…
John Davis
  • 21
  • 1
  • 3
1
vote
1 answer

Selection sort CLRS - Correctness of the reasoning

I'm a self-taught computer science student. Now I'm reading CLRS and I did the 2.2-2 exercise, it's about Selection Sort. First array subscript is 1. The pseudocode that I wrote is: FOR i=1 to A.length - 1 FOR j=i+1 to A.length IF A[j]…
Lucas
  • 11
  • 2
1
vote
1 answer

Is heap sort supposed to be very slow on MATLAB?

I wrote a heap sort function in MATLAB and it works fine, except that when the length of input is greater or equal to 1000, it can take a long time (e.g. the length of 1000 takes half a second). I'm not sure if it's that MATLAB doesn't run very fast…
1
vote
1 answer

What is considered a leaf in red black trees?

I am studying red black trees from CLRS. I have 2 questions about the part where properties of red-black trees are discussed. The passage from CLRS is as follows: A red-black tree is a binary tree that satisfies the following red-black properties:…
Ralph
  • 2,959
  • 9
  • 26
  • 49
1
vote
1 answer

Is my transition function correct (String matching with finite automata)

I am learning String matching with finite automata from CLRS . I am solving some exercise problems .For the exercise problem 32.3-1 , Construct the string-matching automaton for the pattern P = aabab and illustrate its operation on the text…
MdKamil
  • 41
  • 3
  • 10
1
vote
2 answers

What is the relation/difference between worst case time complexity of an algorithm and its upper bound?

What is the relation/difference between worst case time complexity of an algorithm and its upper bound?
1
vote
2 answers

Why Dijkstra Algorithm is relaxing adjacent edges to the vertices already in the Shortest path tree?

In the DIJKSTRA pseudo-code in chapter 24 page 658 CLRS Third Edition, in the inner loop, while relaxing adjacent edges from the new added vertex why is the relaxing allowed on the edges already dequed from the queue and added to Shortest Path to…
Sohail Khan
  • 279
  • 1
  • 3
  • 10
1
vote
3 answers

How to fix an UnboundLocalError caused due to a recursive function call in Python?

I tried to convert the pseudo-code for the Maximum-Subarray problem given in Introduction to Algorithms By CLRS into a full-fledged working code in Python. Code: def cross(A,low,mid,high): left_sum = -float("inf") s = 0 i = mid …
Suyash Shetty
  • 513
  • 3
  • 8
  • 17
1
vote
1 answer

Explain how nC2 is Θ(n^2)

In the book CLRS, in page 69 it's said nC2 is Θ(n^2) in the unit divide and conquer(U - 4). Can anyone expalin how this result is true?
lethal
  • 39
  • 2
  • 9
1
vote
1 answer

How do I prove theta(log n)=o(log n)?

I'm solving a question from CLRS where we need to prove that (ceil(lg lg n))! is polynomially bounded. Let g(n)=(ceil(lg lg n))! lg(g(n))=lg((ceil(lg lg n))!) =theta(ceil(lg lg n) * lg (ceil(lg lg n))) [since lg(n!)=theta(n * lg n) …
Garima
  • 53
  • 5
1
vote
1 answer

How to implement a compact linked list with array?

Here is the question of exercise CLRS 10.3-4 I am trying to solve It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is…
Unbound
  • 243
  • 2
  • 12