Showing posts with label example. Show all posts
Showing posts with label example. Show all posts
Tuesday, February 17, 2015
Huffman Coding Algorithm With Example
Huffman coding algorithm was invented by David Huffman in 1952. It is an algorithm which works with integer length codes. A Huffman tree represents Huffman codes for the character that might appear in a text file. Unlike to ASCII or Unicode, Huffman code uses different number of bits to encode letters. If the number of occurrence of any character is more, we use fewer numbers of bits. Huffman coding is a method for the construction of minimum redundancy codes.
Also Read: Kruskal’s Algorithm for Finding Minimum Cost Spanning Tree
Huffman tree can be achieved by using compression technique. Data compression have lot of advantages such as it minimizes cost, time, bandwidth, storage space for transmitting data from one place to another.
In regular text file each character would take up 1 byte (8 bits) i.e. there are 16 characters (including white spaces and punctuations) which normally take up 16 bytes. In the ASCII code there are 256 characters and this leads to the use of 8 bits to represent each character but in any test file we do not have use all 256 characters. For example, in any English language text, generally the character ‘e’ appears more than the character ‘z’. To achieve compression, we can often use a shorter bit string to represent more frequently occurring characters. We do not have to represent all 256 characters, unless they all appear in the document. The data encoding schemes broadly categorized in two categories.
Fixed Length Encoding Scheme
Fixed length encoding scheme compresses our data by packing it into the minimum number of bits i.e. needed to represent all possible values of our data. The fixed length code can store maximum 224,000 bits data.
Variable Length Encoding Scheme
In variable length encoding scheme we map source symbol to variable number of bits. It allows source to be compressed and decompressed with zero error.
Construction of Huffman Code
A greedy algorithm constructs an optimal prefix code called Huffman code. The algorithm builds the tree T corresponding to the optimal code in a bottom-up manner. It begins with a set of |C| leaves (C is the number of characters) and perform |C| - 1 ‘merging’ operations to create the final tree. In the Huffman algorithm ‘n’ denotes the number of set of characters, z denotes the parent node and x & y are the left & right child of z respectively.
Also Read: Dijkstra Algorithm for Finding Shortest Path of a Graph
Huffman (C)
1. n=|C|
2. Q=C
3. for i=1 to n-1
4. do
5. z=allocate_Node()
6. x=left[z]=Extract_Min(Q)
7. y=right[z]=Extract_Min(Q)
8. f[z]=f[x]+f[y]
9. Insert(Q,z)
10. return Extract_Min(Q)
Analysis
The Q is initialized as a priority queue with the character C.
Q=C can be performed by using Build_Heap in O(n) time.
For loop takes (|n|-1) times because each heap operation requires O(log n) time.
Hence the total running time of Huffman code on the set of n characters is O(n log n).
Method
The following general procedure is applied for construction a Huffman tree:
Search for the two nodes having the lowest frequency, which are not yet assigned to a parent node.
Couple these nodes together to a new interior node.
Add both the frequencies and assign this value to the new interior node.
The procedure has to be repeated until all nodes are combined together in a root node.
Huffman Coding Example
Construct a Huffman tree by using these nodes.
| Value | A | B | C | D | E | F |
| Frequency | 5 | 25 | 7 | 15 | 4 | 12 |
Solution:
Step 1: According to the Huffman coding we arrange all the elements (values) in ascending order of the frequencies.
| Value | E | A | C | F | D | B |
| Frequency | 4 | 5 | 7 | 12 | 15 | 25 |
Step 2: Insert first two elements which have smaller frequency.

| Value | C | EA | F | D | B |
| Frequency | 7 | 9 | 12 | 15 | 25 |
Step 3: Taking next smaller number and insert it at correct place.

| Value | F | D | CEA | B |
| Frequency | 12 | 15 | 16 | 25 |
Step 4: Next elements are F and D so we construct another subtree for F and D.


| Value | CEA | B | FD |
| Frequency | 16 | 25 | 27 |
Step 5: Taking next value having smaller frequency then add it with CEA and insert it at correct place.


| Value | FD | CEAB |
| Frequency | 27 | 41 |
Step 6: We have only two values hence we can combined by adding them.

Huffman Tree
| Value | FDCEAB |
| Frequency | 68 |
Now the list contains only one element i.e. FDCEAB having frequency 68 and this element (value) becomes the root of the Huffman tree.
If you find anything missing or incorrect in above tutorial then please mention it by commenting below.
Saturday, February 14, 2015
Cascading parameters example in Pentaho CDE
Hi Guys,
It is quite easy to learn about cascading parameters in pentaho CDE dash boarding.
Example below teach you how to give cascading parameters on CDE dashboard.
Environment :
C-Tools 13.09
Pentaho BA Server 4.8 stableb
DB : postgreSQL foodmart ( jasperservers database)
IDEA of cascading parameters:









:-)
It is quite easy to learn about cascading parameters in pentaho CDE dash boarding.
Example below teach you how to give cascading parameters on CDE dashboard.
Environment :
C-Tools 13.09
Pentaho BA Server 4.8 stableb
DB : postgreSQL foodmart ( jasperservers database)
IDEA of cascading parameters:

Example:
1) Lay out section
* Design your lay out for keeping the parameters on dasbharod.
* Sample reference design of layout.

2) Components section
* You need to define 3 selects for 3 parameters
* You need to define 3 parameters
* Observer the Sample worked out image below
*

* Define 2 parameters for country and state(paramCountry and paramState)
* Define 3 Selects for displaying the selection of parameters on dashboard.
( CountrySelectComponent, StateSelectComponent, CitySelectComponent)
Parameters
1. ParamCountry

i.e, Name= paramCountry and Propertyvalue=USA(default value)
2. paramState
Name=paramState and Propertyvalue=OR(default value)
NOTE : you need NOT to create parameter for city in this case.
Select Components
* Select components are used to display the data coming from sequel query on dashboard as drop down list at the respected place holders.
1) CountrySelectComponent

Name: CountrySelecte Component
Parameter: paramCouentry (you should give param name otherwise, it wont work)
Datasource: give the query name (will discussion in Datasource selection)
HtmlObject : countrySelect
2)StateSelectComponent

Name: StateSelectComponent
Parameter : paramState
Listners : paramCountry
Parameters : Arg(paramCountry) and val(paramCountryP)
Datasource : queryState
HtmlObject : stateSelect
3) CitySelectComponent
* You need add two listners and two parameters at Listerns and parameters properties
Name;CitySelectComponent
Parameter : No need to give any parameters name here as we are not using parameter for city sleelct
Listners : paramState,paramCountry
Parameters :
Arg(paramCountry) and val(paramCountry)
Arg(paramState) and val(paramState)
Datasource : queryCity
HtmlObject : citySelect
3) Data Sources section:

Write queries for 3 selects
1) queryCountry(above image)
Name: queryCountry
Driver: org.postgresql.Driver
User name : postgres
Password : postgres
URL : jdbc:postgresql://localhost:6062/foodmart (5432 is the default port number for postgreSQL)
Query: SELECT DISTINCT country from customer
Parameters : paramCountry(arg) and paramCountry(val)
2) queryState(image is not shown in post refere queryCountry image)
Name: queryState
Driver: org.postgresql.Driver
User name : postgres
Password : postgres
URL : jdbc:postgresql://localhost:6062/foodmart (5432 is the default port number for postgreSQL)
Query: SELECT DISTINCT state_province from customer where country=${paramCountry}
Parameters : paramCountry(arg) and paramCountry(val)
3) queryCity(image is not shown in post refere queryCountry image)
Name: queryCity
Driver: org.postgresql.Driver
User name : postgres
Password : postgres
URL : jdbc:postgresql://localhost:6062/foodmart (5432 is the default port number for postgreSQL)
Query: SELECT DISTINCT city from customer WHERE country=${paramCountry} AND state_province=${paramState}
Parameters : paramCountry(arg) and paramCountry(val)
paramState(arg) and paramState(val)
Save the dashboard and see the preview.
Final output:
1) Test 1

2) Test2

Thank you for reading this post :) :)
for queries and suggestions : sadakar.1988@gmail.com
SOURCE CODE OF THIS EXAMPLE:
https://drive.google.com/file/d/0BymV_QP4TGBEZGhpb1hySDBoSEE/edit?usp=sharing
Thursday, February 5, 2015
PhoneGap creating forms and validating forms example using jquerymobile
Hi friends,
I am going to explain you how to create forms and validate them according to input.
Title: Creating forms using jquerymobile and validating them and sending request to server and getting some response from server.
Here is the project structure :
colors.css
{color:#FF0000;font-weight:bold;I am going to explain you how to create forms and validate them according to input.
Title: Creating forms using jquerymobile and validating them and sending request to server and getting some response from server.
Here is the project structure :
colors.css
label
.missing}
wait.gif
Add the following image
index.html
<!
DOCTYPE html> <
html> <head> <title>New Claim</title> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" /><link rel="stylesheet" href="css/colors.css"><script src="http://code.jquery.com/jquery-1.4.4.min.js"></script><script src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script><script> // Global declarations - assignments made in $(document).ready() belowvar hdrMainvar = null;var contentMainVar = null;var ftrMainVar = null;var contentTransitionVar = null;var stateLabelVar = null;var whatLabelVar = null;var stateVar = null;var whatVar = null;var form1var = null;var confirmationVar = null;var contentDialogVar = null;var hdrConfirmationVar = null; var contentConfirmationVar = null;var ftrConfirmationVar = null;var inputMapVar = null;// Constantsvar MISSING = "missing";var EMPTY = "";var NO_STATE = "ZZ";</script></
head> <
body> <!-- Page starts here -->
<
div data-role="page" data-theme="a" id="page1"><div data-role="header" id="hdrMain" name="hdrMain" data-nobackbtn="true"><h1>Simple Registration</h1></div><div data-role="content" id="contentMain" name="contentMain"> <form id="form1"><div id="shipDiv" data-role="fieldcontain"><label for="title">Title*</label> <input id="title" name="title_r" type="text" /></div><div id="fnameDiv" data-role="fieldcontain"><label for="fname" id="fnameLabel" name="fnameLabel">First name*</label> <input id="fname" name="fname_r" type="text" /></div><div id="lnameDiv" data-role="fieldcontain"><label for="lname">Last name*</label> <input id="lname" name="lname_r" type="text" /></div><div id="emailDiv" data-role="fieldcontain"><label for="email">Email*</label> <input id="email" name="email_r" type="text" /></div><div id="phoneDiv" data-role="fieldcontain"><label for="phone">Phone</label> <input id="phone" name="phone" type="text"/></div><div id="streetDiv" data-role="fieldcontain"><label for="street">Street address*</label> <input id="street" name="street_r" type="text" /></div><div id="cityDiv" data-role="fieldcontain"><label for="city">City*</label> <input id="city" name="city_r" type="text" /></div><div id="stateDiv" data-role="fieldcontain"><label id="stateLabel" for="state">State*</label> <select id="state" name="state_r" tabindex="2"><option value="ZZ">Please select a state</option><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select></div><div id="zipDiv" data-role="fieldcontain"><label for="zip">Postal code*</label> <input id="zip" name="zip_r" type="text"/></div><div id="whatDiv" data-role="fieldcontain"><label id="whatLabel" for="what">Please explain what happened*:</label><textarea cols="40" rows="8" id="what" name="what_r"></textarea></div><div id="submitDiv" data-role="fieldcontain"> <input type="submit" value="Submit Claim" data-inline="true"/></div></form></div><!-- contentMain --><div data-role="footer" id="ftrMain" name="ftrMain"></div><div align="CENTER" data-role="content" id="contentDialog" name="contentDialog"> <div>Please fill in all required fields before submitting the form.</div><a id="buttonOK" name="buttonOK" href="#page1" data-role="button" data-inline="true">OK</a></div> <!-- contentDialog --><!-- contentTransition is displayed after the form is submitted until a response is received back. --><div data-role="content" id="contentTransition" name="contentTransition"> <div align="CENTER"><h4>Your claim has been sent. Please wait.</h4></div><div align="CENTER"><img id="spin" name="spin" src="img/wait.gif"/></div></div> <!-- contentTransition --><!-- Although stored within page1 div tag, hdrConfirmation, contentConfirmation and ftrConfirmation represent a self contained confirmation page --><div data-role="header" id="hdrConfirmation" name="hdrConfirmation" data-nobackbtn="true"><h1>Claim Created</h1></div><div data-role="content" id="contentConfirmation" name="contentConfirmation" align="center"> <p>A new claim has been created based on data you have submitted.</p> <p>Your confirmation number is: <span id="confirmation" name="confirmation"></span> </p></div><!-- contentConfirmation --> <div data-role="footer" id="ftrConfirmation" name="ftrConfirmation"></div> <script>$(document).ready(
function() {// Assign global variableshdrMainVar = $(
#hdrMain);contentMainVar = $(
#contentMain);ftrMainVar = $(
#ftrMain);contentTransitionVar = $(
#contentTransition);stateLabelVar = $(
#stateLabel);whatLabelVar = $(
#whatLabel);stateVar = $(
#state);whatVar = $(
#what);form1Var = $(
#form1);confirmationVar = $(
#confirmation);contentDialogVar = $(
#contentDialog);hdrConfirmationVar = $(
#hdrConfirmation);contentConfirmationVar = $(
#contentConfirmation);ftrConfirmationVar = $(
#ftrConfirmation); inputMapVar = $(
input[name*="_r"]);hideContentDialog();
hideContentTransition();
hideConfirmation();
});
$(
#buttonOK).click(function() {hideContentDialog();
showMain();
return false; });
$(
#form1).submit(function() {var err = false;// Hide the Main contenthideMain();
// Reset the previously highlighted form elementsstateLabelVar.removeClass(MISSING);
whatLabelVar.removeClass(MISSING);
inputMapVar.each(
function(index){ $(
this).prev().removeClass(MISSING); });
// Perform form validationinputMapVar.each(
function(index){ if($(this).val()==null || $(this).val()==EMPTY){ $(
this).prev().addClass(MISSING); err =
true;}
});
if(stateVar.val()==NO_STATE){ stateLabelVar.addClass(MISSING);
err =
true;}
if(whatVar.val()==null||whatVar.val()==EMPTY){ whatLabelVar.addClass(MISSING);
err =
true;}
// If validation fails, show Dialog contentif(err == true){ showContentDialog();
return false;}
// If validation passes, show Transition contentshowContentTransition();
// Submit the form$.post(
"requestProcessor.php", form1Var.serialize(), function(data){confirmationVar.text(data);
hideContentTransition();
showConfirmation();
});
return false; });
function hideMain(){hdrMainVar.hide();
contentMainVar.hide();
ftrMainVar.hide();
}
function showMain(){hdrMainVar.show();
contentMainVar.show();
ftrMainVar.show();
}
function hideContentTransition(){contentTransitionVar.hide();
}
function showContentTransition(){contentTransitionVar.show();
}
function hideContentDialog(){contentDialogVar.hide();
}
function showContentDialog(){contentDialogVar.show();
}
function hideConfirmation(){hdrConfirmationVar.hide();
contentConfirmationVar.hide();
ftrConfirmationVar.hide();
}
function showConfirmation(){hdrConfirmationVar.show();
contentConfirmationVar.show();
ftrConfirmationVar.show();
}
</script></
div> <!-- page1 --><!-- Page ends here -->
</
body></
html>OutPut
Labels:
and,
creating,
example,
forms,
jquerymobile,
phonegap,
using,
validating
Subscribe to:
Posts (Atom)


