Skip to content

Commit 457df01

Browse files
committed
mew question added
1 parent 3c893c6 commit 457df01

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ remainder.
115115
Then we take the remainder and quotient from bottom to top and assign them alphabets corresponding to their
116116
values.
117117

118+
### For Greedy
119+
- Used in case of optimization problems (maximize of minimize something)
120+
- When to use heaps vs when to use sorting: When the problem requires just finding the min or max, we can use sorting, but if after finding something is to be inserted again for which in case of sorting it will take O(n) time as it requires finding the place, heaps are better as they take only O(logn) time to do the same thing.
121+
- In order to represent nodes using bits etc, (eg: huffman coding), we use trees.
122+
- Huffman codes or optimal merge patterns where something is to be minimized, always choose the maximum
123+
value to be at the top of the tree with min edge length (or path to be traversed) and min at bottom with
124+
max edge or path to be traversed to minimize work.
118125

119126
# Topic0: Programming Questions
120127

@@ -279,6 +286,13 @@ values.
279286
- [Find the smallest window in the string containing all characters of another string](/strings/question11.c)
280287
- [Find first non-repeating characters from a stream of characters](/strings/question12.c)
281288

289+
### Greedy
290+
291+
- [Make a program for greedy knapsack problem](/greedy/question1.c)
292+
- [Make a program to implement huffman encoding](/greedy/question2.c)
293+
- [Make a program to sequence given jobs with deadlines to maximize profits](/greedy/question3.c)
294+
- [Optimal merge patterns](/greedy/question4.c)
295+
282296
## Some important concepts to solve algos better
283297

284298
- For extreme values refer to limits.h constants given by C
@@ -355,6 +369,12 @@ Eg: SSMMMAAARRT => S2M3A3R2T1
355369
- Two strings are anagrams if they have same no of characters and they are composed of the same letters.
356370
Even if there are repetitions, they are going to be same.
357371
- Ideal approach of writing a program is return from a functiona and keep strings at a single place and not scattered
372+
- Greedy method and DP are two programing paradigms which can be used to solve optimization problems
373+
- Greedy method fractions are allowed
374+
- For huffman coding to work letters must not be uniformly distributed
375+
- Spanning tree is min number of edges present in the graph such that all nodes are connected. Span tree is always subgraph of the main graph and cannot contain edges which are not present in the main graph
376+
- Number of edges incident on the node is degree of node in undirected graphs. In case of directed, there is no degree but in degree and out degree
377+
- Kirchoff theorem is used to find spanning tree of non-weighted undirected simple graph
358378

359379
# C Programming - Topic1: Introduction
360380

greedy/question1.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Program for greedy knapsack problem.
3+
4+
METHOD:
5+
In this problem different weights are given along with the profit associated to each weight (if full weight
6+
is used). Now, a bag is given with a max capacity. We have to put objects in the bag such that profit
7+
is maximum.
8+
9+
The approach is to find the profit by weight ratio corresponding to each object, then sort the list.
10+
Now from the list keep filling the bag with the object completely with the max profit by weight ratio.
11+
Once the bag is full enough such that it cannot accomodate the next weight fully, put the fraction of
12+
that weight such that the bag is full and add the fraction of profit to the total profit so far.
13+
14+
Time complexity: O(nlogn) //since sorting is the dominant term
15+
Space complexity: O(n) //since we will be storing profit by weight ratio in array
16+
17+
We can also use max heap in this case and everytime we will have to delete max and max heapify n times
18+
which will give the same time complexity
19+
*/
20+
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
24+
int main(){
25+
26+
return 0;
27+
}

greedy/question2.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Program to implement Huffman encoding
3+
4+
METHOD:
5+
In case of huffman encoding the condition is that elements should not be uniformly distributed.
6+
There the element that is repeating most number of times, we represent it using least number of bits
7+
and the element that is repeating least number of times, we use max bits. Therefore in this case we can
8+
represent this thing using tree.
9+
The algo is to make a min heap out of the given elements (as we are required to find min each time) and also
10+
insertion is to be done (therefore sorting is not used).
11+
12+
Now each time min is extracted twice. A new node is made which will contain the value equal to the sum
13+
of the two mins (freq). The left node smaller one will be the left child of the newly created node and the
14+
bigger of the two will be the right child. Like this we keep repeating this process for n-1 elements
15+
to build the tree.
16+
Now we can fix one pattern that is making left edge of each tree as 0 and right as 1 or vice versa down
17+
to the bottom and then we traverse for each node to generate the huffman codes.
18+
19+
Note that in this each node will have its own unique pattern which wont be present in any of the nodes.
20+
Also note that node which is smaller has to be on the left and each time there is a possibility that not
21+
always there will be a tree, many a times there can be forests also and then you combine two search forests.
22+
This may happen because least two values will be different from the current sequence
23+
24+
Time complexity: O(nlogn) //finding min n times (twice) and inserting the sum back to the tree O(logn) which
25+
is done n-1 times again
26+
Space complexity: O(n) //tree
27+
*/

greedy/question3.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Make a program to sequence given jobs with deadlines to maximize profits
3+
4+
Here some jobs will be given along with some profits associated to each job if the job is done in
5+
given deadline.
6+
METHOD:
7+
First we find the job with the max deadline. That is the max slot that will be available to us. Therefore
8+
array will be of that size. Now, we arrange them in decreasing order of profits. Once that is done we pick
9+
max profit job and place it as far away as possible such that it is meeting its deadline. Like this we
10+
keep scanning the empty slot to place a job as far as possible such that it meets its deadline. Like this
11+
we maximize profit.
12+
Note: if the deadline is very very high as compared to the no. of jobs, always limit the array size
13+
with the number of jobs given. So compare both.
14+
Time complexity: O(n^2) //in worst case we will end up scanning all the cells in the array
15+
Space complexity: O(n) //array size or O(max deadline)
16+
*/

greedy/question4.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Optimal merge patterns
3+
4+
In this some files with sorted number of records are given. We need to make an algo such that
5+
there is min record movement when merging them in sorted order into single file
6+
7+
Same algo as that of Huffman codes. File with lowest records is moved more so is kept at the bottom
8+
away from the root. Files with higher number of records are placed closer to the root i.e. merged
9+
later
10+
11+
Time complexity: O(nlogn)
12+
13+
*/

stacks-and-queues/divide-and-conquor

Whitespace-only changes.

0 commit comments

Comments
 (0)