Sardinas–Patterson algorithm - algorithm

Is the code C = {00, 11, 0101, 111, 1010, 100100, 0110} uniquely decodeable?
My answer is no, because according to Sardinas–Patterson algorithm:
C1 = {1}
C2 = {1, 11, 010, 00100}
So C2 AND C = {11}, so C is not a uniquely decodable code.
I am wondering am I right about this?

You are correct that this code is not uniquely decodable.
Consider the string 111111, this can be parsed as 11 11 11 or as 111 111.

Related

How do i determine given code is decodable or not? [duplicate]

I'm trying to solve a Huffman Coding problem, but I'm not completely sure I understand the topic completely. I am trying to figure out if the following are is a valid Huffman Code:
A: 0
B: 01
C: 11
D: 110
E: 111
What I'm thinking is that it is not valid, because A, or 1, would infringe on B, or 01. I'm not positive though. Could someone enlighten me on this?
Edit: I'm sorry I meant to type A as 0 and not 1.
No. A Huffman code is a prefix code, which means that no code can be a prefix of any other code. In your example, A is a prefix of B, and C is a prefix of both D and E.
A valid prefix code would be:
A: 0
B: 10
C: 11
That's as far as you can go with codes of length 1, 2, and 2. Any other codes would be a prefix of those. It is not possible to have a prefix code with lengths 1, 2, 2, 3, and 3.
This is a valid prefix code for five symbols:
A: 0
B: 10
C: 110
D: 1110
E: 1111
as is this:
A: 00
B: 01
C: 10
D: 110
E: 111

Find all possible combination of numbers from a string of number while maintaining the original order

Say if you have string: 1234
What I need is to generate all possible number combinations from this string, but the original order of the string must be maintained.
Also there must be at least 2 numbers in each result.
In the case of 1234 the output would be an list of lists
output list:
list: 1, 2, 3, 4
list: 12, 3, 4
list: 123, 4 --Note a list containing only 1234 is not valid
list: 1, 23, 4
list: 1, 234
list: 1, 2, 34
list: 12, 34
Also note that the numbers in each of the result list is always ordered the same way as the original string 1234. So a list with combination 34, 21 or 213, 4 is not valid.
The only approach I can think of:
Start at the 1st index (1)
Append the next number to form a new number(12)
Then recursively pass in the newly formed number to append the next number (123)
After the 1st index is done, we start all over again with the second index (2)
...
However with my approach I don't know how I can generate the 12, 34 combination
Any help is greatly appreciated!
1 | 2 | 3 | 4 | 5
| | | |
d1 d2 d3 d4
Suppose the origin string has length 5, and we divide this string by adding a divider d1, d2 ,d3 and d4 which can be empty.
when d1, d2 ,d3 ,d4 are all empty, we get 12345.
when d2, d3, d4 are empty, we get 1, 2345
when none of d1, d2, d3, d4 are empty, we get 1,2,3,4,5
for every divider we added, it has two options: visible or gone,
so the total possible count is 2^(n-1) - 1 when n is the length of the original string
Then, the next problem we will solve is to iterate all the possibilities:
use a binary value to represent the divider:
for (int i = 1; i <= pow(2, n - 1); i++) {
// i = 1, 0b0001, d4 is visible, we get 1234,5
// i = 2, 0b0010, d3 is visible, we get 123,45
// i = 3, 0b0011, d3 and d4 is visible, we get 123,4,5
// i = 4, 0b0100, d2 is visible, we get 12,345
// i = 5, 0b0101, d2 and d4 is visible, we get 12, 34, 5
// i = 6, 0b0110, d2 and d3 is visible, we get 12, 3, 45
// i = 7, 0b0111, d2,d3,d4 is visible, we get 12,3,4,5
// go on...
}
Hope help...
You can use similar approach to get the output you need.
Modify it as it is simple traversing.
String str="1234";
StringBuilder sb=new StringBuilder(str);
int l=str.length();
for(int i=0;i<l;i++){
int j=i+1;
if(i!=0)
j=i+2;
for(;j<l+1;j++){
sb.insert(j, ',');
System.out.println(sb);
sb.deleteCharAt(j);
}
sb=new StringBuilder(str);
sb.insert(i+1, ',');
}
It produces the following output
1,234
12,34
123,4
1234,
1,2,34
1,23,4
12,3,4

Print the row and column wise sorted 2 D matrix in a sorted order

Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.
Example:
Input:
mat[][] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50},
};
Output:
(Elements of matrix in sorted order)
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50
I am unable to figure out how to do this.But according to me we can put the 2 D matrix in one matrix and apply the sort function.But i am in a need of space optimized code.
Using a Heap would be a good idea here.
Please refer to the following for a very similar question:
http://www.geeksforgeeks.org/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array-set-1/
Thought the problem in the link above is different, the same approach could be used for the problem you specify. Instead of looping k times as the link explains, you need to visit all elements in the matrix i.e you should loop till the heap is empty.

Shannon Fano algorithm

I tried to implement the algorithm according to the example:
"Shannon Fano"
According to this decision, I have to get A = 11, B = 101, C = 100, D = 00, E = 011, F = 010. But i get A = 11 B = 101 C = 100 D = 01 E = 001 F = 000
This my code:
Input parametrs: frequencies = [50, 39, 18, 49, 35, 24] и chars = [A, B, C, D, E, F]
OnClick: SearchTree(' ',' ', 1, charCount,Memo1);
procedure SearchTree(branch:char; full_branch:string; start_pos:integer; end_pos:integer; memo:TMemo);
var
dS:real;
i, m, S:integer;
c_branch:string;
x,y,j:integer;
begin
if (branch<>' ') then c_branch := full_branch + branch
else c_branch := '';
if (start_pos = end_pos) then
begin
memo.Lines.Add(chars[start_pos]+ ' = ' + c_branch);
exit;
end;
x:=0; y:=0;
i:=start_pos-1; j:=end_pos;
repeat
Inc(i);
x:=x+frequencies[i];
while ((x>=y) and (i<>j))do
begin
y:=y+frequencies[j];
Dec(j);
end;
m:=i;
until (i=j);
SearchTree('1', c_branch, start_pos, m,memo);
SearchTree('0', c_branch, m+1, end_pos,memo);
end;
Prompt if I understood the algorithm and what is my problem?
I will not try to decipher your code, but for what it's worth, what you are doing here is not the way I have understood Shannon-Fano encoding.
I must admit right away that I have never personally coded one (opting for Huffman encoding instead, which offers as good as or better compression for all input data at the sacrifice of some complexity).
Here's how I believe Shannon-Fano codes should be constructed from your sample data:
Given character frequencies:
A B C D E F
50, 39, 18, 49, 35, 24 = 215 (ideal distribution = 215 / 2 = 107.5 to each start bit)
Now sort the frequencies:
A D B E F C
50, 49, 39, 35, 24, 18 = 215 (ideal distribution = 215 / 2 = 107.5 to each start bit)
Now find the split point in this list that gives the least amount of "error" (waste):
50 | 49 39 35 24 18 -- error (distance to ideal 107.5) = 57.5
50 49 | 39 35 24 18 -- error (distance to ideal 107.5) = 8.5
50 49 39 | 35 24 18 -- error (distance to ideal 107.5) = 30.5
So the best split point at the first level is between 49(D) and 39(B), which in turn means that we have AD on the left-hand branch and BEFC on the right-hand branch.
Since there happens to be only two characters left on the left-hand branch we have the encoding for those directly:
Assuming left is 1 and right is zero, A becomes 11 and D becomes 10.
All the remaining character encodings (BEFC) then begin with zero.
You now repeat this process recursively with the remaining list in the same way until there are at most two entries in the list, and you are done.

Find the index of a item in a sorted list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We have a generated list:
1. 003
2. 012
3. 021
4. 030
5. 102
6. 111
7. 120
8. 201
9. 210
10. 300
(numbers are from 0 to 3 and their sum is 3)
How to find in what place is a combination without counting them??
Ex. 201 -> index=8
Thanks in advance.
If digits of your number are ABC, then index is:
ndx = A * (8 - A + 1) / 2 + B + 1;
For example, for value ABC=201, we will have:
ndx = 2 * (8 - 2 + 1) / 2 + 0 + 1 = 8;
Really, value 201 has index 8.
Not a complete answer, but I think this is a good start.
If you view each digit as two binary digits, you get:
1. 003 00 00 11
2. 012 00 01 10
3. 021 00 10 01
4. 030 00 11 00
5. 102 01 00 10
6. 111 01 01 01
7. 120 01 10 00
8. 201 10 00 01
9. 210 10 01 00
10. 300 11 00 00
If you ignore the right hand column of digits, then the first seven items (values 003 through 120) are the binary representations of the numbers 0 through 6.
The next two items have values 8 and 9, and the last is 12.
So, we can convert the number to a rough index with:
ix = 4*first_digit + second_digit
And then adjust:
if (first_digit < 2)
ix = ix + 1
else if (first_digit == 3)
ix = ix - 2
I'm not happy with the conditional there. Is there a mathematical way to make this translation:
0 => 1
1 => 1
2 => 0
3 => -2
Right, the comments under the question I have been making are assuming you want to go directly from the current value to the index, without performing a search. That is to say, making some inspection of the digits of the entry and translating that to a 1-indexed number.
Note, this answer is directional and incomplete, just shows the way I would approach the problem.
Looking at your example, if we treat each entry as composed of 3 digits, (z_i, y_i, x_i), then you get the following sequences:
003; z=0, y=0, x=3
012; z=0, y=1, x=2
021; z=0, y=2, x=1
030; z=0, y=3, x=0
102; z=1, y=0, x=2
111; z=1, y=1, x=1
120; z=1, y=2, x=0
201; z=2, y=0, x=1
210; z=2, y=1, x=0
300; z=3, y=0, x=0
If the max digit is k (=3), then:
x_i = 3, 2, 1, 0, 2, 1, 0, 1, 0, 0 = k, k-1, ..., 0, k-1, ... 0, ......, 0
y_i = 0, 1, 2, 3, 0, 1, 2, 0, 1, 0 = 0, 1, ..., k, 0, ..., k-1, ......, 0
z_i = 0, 0, 0, 0, 1, 1, 1, 2, 2, 3 = k+1 x 0, k x 1, ......., 1 x k
As you can see, the y_i digit goes up in sequence repetitively, knocking the z_i up at the end of each completion.
If you had more digits, the pattern gets more complicated, but still follows a similar pattern.
For k=4:
0004
0013
0022
0031
0040
0103
0112
0121
0130
0202
0211
0220
0301
0310
0400
1003
1012
1021
1030
1102
1111
1120
1201
1210
1300
2002
2011
2020
2101
2110
2200
3001
3010
3100
4000
The total entries can be seen from the first or last column, it is the triangle number of the triangle number of k+1, in the case of k=4. For k=3, it's just the triangle of k+1.
Not having it worked it out, but that pattern might indicate successive summations as the number of digits increases.
There is a pattern still:
k=3:
k=4:
k=5:
Or in general for the total number of entries in the sequence of length k:
This knowledge helps give us a hand in finding the scalars for the first digit, and the rest of the problem is effectively a sub problem for k-1. Defeating me at the moment...
Use Collections.binarySearch.
Check http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List, T)

Resources