Addition Of Sparse Matrix



Perform addition of two sparse matrix Sparse matrix is a matrix populated primarily with zeros.If a sparse matrix is represented by using normal method i.e. by using two dimensional array say m[r][c] then too many memory locations will be used only for storing the zero values.In order to avoid this,only non-zero values are stored in two dimensional array say s[m][3],where m is = total non-zero values + 1.

Read full article from Addition Of Sparse Matrix


Memory Layout of C Programs | GeeksforGeeks



A typical memory representation of C program consists of following sections. 1. Text segment 1. Text Segment: A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions. As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells,

Read full article from Memory Layout of C Programs | GeeksforGeeks


Pure Functions | GeeksforGeeks



A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. The only result of calling a pure function is the return value. Examples of pure functions are strlen(), pow(), sqrt() etc. Examples of impure functions are printf(), rand(), time(), etc.
If a function is known as pure to compiler then Loop optimization and subexpression elimination can be applied to it. In GCC, we can mark functions as pure using the “pure” attribute.
__attribute__ ((pure)) return-type fun-name(arguments1, …)
{
    /* function body */
}



If “strlen()” function is not marked as pure function then compiler will invoke the “strlen()” function with each iteration of the loop, and if function is marked as pure function then compiler knows that value of “strlen()” function will be same for each call, that’s why compiler optimizes the for loop and generates code like following.

Marking function as pure says that the hypothetical function “my_strlen()” is safe to call fewer times than the program says.
Read full article from Pure Functions | GeeksforGeeks

The Ubiquitous Binary Search | Set 1 | GeeksforGeeks



The Ubiquitous Binary Search | Set 1 We all aware of binary search algorithm. Binary search is easiest difficult algorithm to get it right. I present some interesting problems that I collected on binary search. There were some requests on binary search. I request you to honor the code, “I sincerely attempt to solve the problem and ensure there are no corner cases”. After reading each problem minimize the browser and try solving it. Problem Statement: Given a sorted array of N distinct elements. Find a key in the array using least number of comparisons.

Read full article from The Ubiquitous Binary Search | Set 1 | GeeksforGeeks


org.apache.lucene.codecs.lucene49 (Lucene 4.9.0 API)





Segments Filesegments.gen, segments_NStores information about a commit point
A collection of segmentInfo objects with methods for operating on those segments in relation to the file system.
The active segments in the index are stored in the segment info file, segments_N. There may be one or more segments_N files in the index; however, the one with the largest generation is the active one (when older segments_N files are present it's because they temporarily cannot be deleted, or, a writer is in the process of committing, or a custom IndexDeletionPolicy is in use). This file lists each segment by name and has details about the codec and generation of deletes.
There is also a file segments.gen. This file contains the current generation (the _N in segments_N) of the index. This is used only as a fallback in case the current generation cannot be accurately determined by directory listing alone (as is the case for some NFS clients with time-based directory cache expiration). This file simply contains an Int32 version header (FORMAT_SEGMENTS_GEN_CURRENT), followed by the generation recorded as Int64, written twice.

Segment Info.siStores metadata about a segment
Deprecated. 
Only for reading old 4.0-4.5 segments, and supporting IndexWriter.addIndexes
Compound File.cfs, .cfeAn optional "virtual" file consisting of all the other index files for systems that frequently run out of file handles.


  • .cfs: An optional "virtual" file consisting of all the other index files for systems that frequently run out of file handles.
  • .cfe: The "virtual" compound file's entry table holding all entries in the corresponding .cfs file.

Fields.fnmStores information about the fields
Field names are stored in the field info file, with suffix .fnm.
FieldInfos (.fnm) --> Header,FieldsCount, <FieldName,FieldNumber, FieldBits,DocValuesBits,DocValuesGen,Attributes> FieldsCount,Footer

Field Index.fdxContains pointers to field data

Lock File

The write lock, which is stored in the index directory by default, is named "write.lock". If the lock directory is different from the index directory then the write lock will be named "XXXX-write.lock" where XXXX is a unique prefix derived from the full path to the index directory. When this file is present, a writer is currently modifying the index (adding or removing documents). This lock file ensures that only one writer is modifying the index at a time.



  • In version 4.8, checksum footers were added to the end of each index file for improved data integrity. Specifically, the last 8 bytes of every index file contain the zlib-crc32 checksum of the file.

Read full article from org.apache.lucene.codecs.lucene49 (Lucene 4.9.0 API)

Lucene学习总结之一:全文检索的基本原理 - 觉先 - 博客园



由于从字符串到文件的映射是文件到字符串映射的反向过程,于是保存这种信息的索引称为反向索引

inverted index
每个字符串都指向包含此字符串的文档(Document)链表,此文档链表称为倒排表(Posting List)。

第二步:将原文档传给分次组件(Tokenizer)。

分词组件(Tokenizer)会做以下几件事情(此过程称为Tokenize)
1. 将文档分成一个一个单独的单词。
2. 去除标点符号。
3. 去除停词(Stop word)
所谓停词(Stop word)就是一种语言中最普通的一些单词,由于没有特别的意义,因而大多数情况下不能成为搜索的关键词,因而创建索引时,这种词会被去掉而减少索引的大小。
英语中挺词(Stop word)如:“the”,“a”,“this”等。
对于每一种语言的分词组件(Tokenizer),都有一个停词(stop word)集合。
经过分词(Tokenizer)后得到的结果称为词元(Token)

第三步:将得到的词元(Token)传给语言处理组件(Linguistic Processor)。

语言处理组件(linguistic processor)主要是对得到的词元(Token)做一些同语言相关的处理。
对于英语,语言处理组件(Linguistic Processor)一般做以下几点:
1. 变为小写(Lowercase)
2. 将单词缩减为词根形式,如“cars”到“car”等。这种操作称为:stemming
3. 将单词转变为词根形式,如“drove”到“drive”等。这种操作称为:lemmatization

Stemming 和 lemmatization的异同:
  • 相同之处:Stemming和lemmatization都要使词汇成为词根形式。
  • 两者的方式不同:
    • Stemming采用的是“缩减”的方式:“cars”到“car”,“driving”到“drive”。
    • Lemmatization采用的是“转变”的方式:“drove”到“drove”,“driving”到“drive”。
  • 两者的算法不同:
    • Stemming主要是采取某种固定的算法来做这种缩减,如去除“s”,去除“ing”加“e”,将“ational”变为“ate”,将“tional”变为“tion”。
    • Lemmatization主要是采用保存某种字典的方式做这种转变。比如字典中有“driving”到“drive”,“drove”到“drive”,“am, is, are”到“be”的映射,做转变时,只要查字典就可以了。
  • Stemming和lemmatization不是互斥关系,是有交集的,有的词利用这两种方式都能达到相同的转换。
语言处理组件(linguistic processor)的结果称为词(Term)

第四步:将得到的词(Term)传给索引组件(Indexer)。

索引组件(Indexer)主要做以下几件事情:
1. 利用得到的词(Term)创建一个字典。
2. 对字典按字母顺序进行排序。
3. 合并相同的词(Term)成为文档倒排(Posting List)链表。
postinglist
在此表中,有几个定义:
  • Document Frequency 即文档频次,表示总共有多少文件包含此词(Term)。
  • Frequency 即词频率,表示此文件中包含了几个此词(Term)。

第二步:对查询语句进行词法分析,语法分析,及语言处理。

语法树1

第三步:搜索索引,得到符合语法树的文档。

此步骤有分几小步:
  1. 首先,在反向索引表中,分别找出包含lucene,learn,hadoop的文档链表。
  2. 其次,对包含lucene,learn的链表进行合并操作,得到既包含lucene又包含learn的文档链表。
  3. 然后,将此链表与hadoop的文档链表进行差操作,去除包含hadoop的文档,从而得到既包含lucene又包含learn而且不包含hadoop的文档链表。
  4. 此文档链表就是我们要找的文档

第四步:根据得到的文档和查询语句的相关性,对结果进行排序。

找出词(Term)对文档的重要性的过程称为计算词的权重(Term weight)的过程。
计算词的权重(term weight)有两个参数,第一个是词(Term),第二个是文档(Document)。
词的权重(Term weight)表示此词(Term)在此文档中的重要程度,越重要的词(Term)有越大的权重(Term weight),因而在计算文档之间的相关性中将发挥更大的作用。
判断词(Term)之间的关系从而得到文档相关性的过程应用一种叫做向量空间模型的算法(Vector Space Model)

1. 计算权重(Term weight)的过程。

  • Term Frequency (tf):即此Term在此文档中出现了多少次。tf 越大说明越重要。
  • Document Frequency (df):即有多少文档包含次Term。df 越大说明越不重要。
image
image

2. 判断Term之间的关系从而得到文档相关性的过程,也即向量空间模型的算法(VSM)。

我们把文档看作一系列词(Term),每一个词(Term)都有一个权重(Term weight),不同的词(Term)根据自己在文档中的权重来影响文档相关性的打分计算。
于是我们把所有此文档中词(term)的权重(term weight) 看作一个向量。
Document = {term1, term2, …… ,term N}
Document Vector = {weight1, weight2, …… ,weight N}
同样我们把查询语句看作一个简单的文档,也用向量来表示。
Query = {term1, term 2, …… , term N}
Query Vector = {weight1, weight2, …… , weight N}
我们把所有搜索出的文档向量及查询向量放到一个N维空间中,每个词(term)是一维。
vsm
我们认为两个向量之间的夹角越小,相关性越大。
所以我们计算夹角的余弦值作为相关性的打分,夹角越小,余弦值越大,打分越高,相关性越大。
有人可能会问,查询语句一般是很短的,包含的词(Term)是很少的,因而查询向量的维数很小,而文档很长,包含词(Term)很多,文档向量维数很大。你的图中两者维数怎么都是N呢?
在这里,既然要放到相同的向量空间,自然维数是相同的,不同时,取二者的并集,如果不含某个词(Term)时,则权重(Term Weight)为0。

相关性打分公式如下:
image
clip_image016
1. 索引过程:
1) 有一系列被索引文件
2) 被索引文件经过语法分析和语言处理形成一系列词(Term)
3) 经过索引创建形成词典和反向索引表。
4) 通过索引存储将索引写入硬盘。
2. 搜索过程:
a) 用户输入查询语句。
b) 对查询语句经过语法分析和语言分析得到一系列词(Term)
c) 通过语法分析得到一个查询树。
d) 通过索引存储将索引读入到内存。
e) 利用查询树搜索索引,从而得到每个词(Term)的文档链表,对文档链表进行交,差,并得到结果文档。
f) 将搜索到的结果文档对查询的相关性进行排序。
g) 返回查询结果给用户。
Read full article from Lucene学习总结之一:全文检索的基本原理 - 觉先 - 博客园

Lucene学习总结之二:Lucene的总体架构 - 觉先 - 博客园



  • 索引过程如下:
    • 创建一个IndexWriter用来写索引文件,它有几个参数,INDEX_DIR就是索引文件所存放的位置,Analyzer便是用来对文档进行词法分析和语言处理的。
    • 创建一个Document代表我们要索引的文档。
    • 将不同的Field加入到文档中。我们知道,一篇文档有多种信息,如题目,作者,修改时间,内容等。不同类型的信息用不同的Field来表示,在本例子中,一共有两类信息进行了索引,一个是文件路径,一个是文件内容。其中FileReader的SRC_FILE就表示要索引的源文件。
    • IndexWriter调用函数addDocument将索引写到索引文件夹中。
  • 搜索过程如下:
    • IndexReader将磁盘上的索引信息读入到内存,INDEX_DIR就是索引文件存放的位置。
    • 创建IndexSearcher准备进行搜索。
    • 创建Analyer用来对查询语句进行词法分析和语言处理。
    • 创建QueryParser用来对查询语句进行语法分析。
    • QueryParser调用parser进行语法分析,形成查询语法树,放到Query中。
    • IndexSearcher调用search对查询语法树Query进行搜索,得到结果TopScoreDocCollector
clip_image008
  • Lucene的analysis模块主要负责词法分析及语言处理而形成Term
  • Lucene的index模块主要负责索引的创建,里面有IndexWriter
  • Lucene的store模块主要负责索引的读写。
  • Lucene的QueryParser主要负责语法分析。
  • Lucene的search模块主要负责对索引的搜索。
  • Lucene的similarity模块主要负责对相关性打分的实现。
Read full article from Lucene学习总结之二:Lucene的总体架构 - 觉先 - 博客园

Little and Big Endian Mystery | GeeksforGeeks



What are these? Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first. Suppose integer is stored as 4 bytes (For those who are using DOS based compilers such as C++ 3.0 , integer is 2 bytes) then a variable x with value 0×01234567 will be stored as following.

Read full article from Little and Big Endian Mystery | GeeksforGeeks


Find the Number Occurring Odd Number of Times | GeeksforGeeks



Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.

Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3

Algorithm:
Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences.


Position of rightmost set bit | GeeksforGeeks



Position of rightmost set bit Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer. I/P 18, Binary Representation 010010 O/P 2 I/P 19, Binary Representation 010011 O/P 1 Let I/P be 12 (1100) Algorithm: (Example 18(010010)) 1. Take two's complement of the given no as all bits are reverted except the first '1' from right to left (10111) 2 Do an bit-wise & with original no, this will return no with the required one only (00010) 3 Take the log2 of the no, you will get position -1 (1) 4 Add 1 (2) Program:

Read full article from Position of rightmost set bit | GeeksforGeeks


Write one line C function to find whether a no is power of two | GeeksforGeeks



bool isPowerOfTwo (int x)
{
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x&(x-1)));
}

Read full article from Write one line C function to find whether a no is power of two | GeeksforGeeks


Efficient way to multiply with 7 | GeeksforGeeks



We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n).


Program:

# include<stdio.h>
 
int multiplyBySeven(unsigned int n)
    /* Note the inner bracket here. This is needed
       because precedence of '-' operator is higher
       than '<<' */
    return ((n<<3) - n);
}

Read full article from Efficient way to multiply with 7 | GeeksforGeeks


Adder (electronics) - Wikipedia, the free encyclopedia



In electronics , an adder or summer is a digital circuit that performs addition of numbers. In many computers and other kinds of processors, adders are used not only in the arithmetic logic unit (s), but also in other parts of the processor, where they are used to calculate addresses, table indices, and similar operations. Although adders can be constructed for many numerical representations, such as binary-coded decimal or excess-3 , the most common adders operate on binary numbers. In cases where two's complement or ones' complement is being used to represent negative numbers,

Read full article from Adder (electronics) - Wikipedia, the free encyclopedia


XOR swap algorithm - Wikipedia, the free encyclopedia



(February 2012) Using the XOR swap algorithm to exchange nibbles between variables without the use of temporary storage In computer programming , the XOR swap is an algorithm that uses the XOR bitwise operation to swap values of distinct variables having the same data type without using a temporary variable. "Distinct" means that the variables are stored at different memory addresses; the actual values of the variables do not have to be different. Contents Conventional swapping requires the use of a temporary storage variable. Using the XOR swap algorithm, however,

Read full article from XOR swap algorithm - Wikipedia, the free encyclopedia


How does default virtual behavior differ in C++ and Java ? | GeeksforGeeks



Default virtual behavior of methods is opposite in C++ and Java: In C++, class member methods are non-virtual by default. They can be made virtual by using virtual keyword. For example, Base::show() is non-virtual in following program and program prints “Base::show() called”. #include using namespace std; class Base { public: // non-virtual by default void show() { cout<<"Base::show() called"; } }; class Derived: public Base { public: void show() { cout<<"Derived::show() called"; } }; int main() { Derived d; Base &b = d; b.show(); getchar(); return 0;

Read full article from How does default virtual behavior differ in C++ and Java ? | GeeksforGeeks


Comma operator should be used carefully | GeeksforGeeks



In C and C++, comma is the last operator in precedence table. So comma should be carefully used on right side of an assignment expression. For example, one might expect the output as b = 10 in below program. But program prints b = 20 as assignment has higher precedence over comma and the statement “b = 20, a” becomes equivalent to “(b = 20), a”.

#include<stdio.h>
int main()
{
  int a = 10, b;
  b = 20, a;   // b = 20
  printf(" b = %d ", b);
  getchar();
  return 0;
}

Putting a bracket with comma makes b = a (or 10).

#include<stdio.h>
int main()
{
  int a = 10, b;
  b = (20, a); // b = a
  printf(" b = %d ", b);
  getchar();
  return 0;
}

Read full article from Comma operator should be used carefully | GeeksforGeeks


Time Complexity of building a heap | GeeksforGeeks



BUILD-HEAP(A)       heapsize := size(A);       for i := floor(heapsize/2) downto 1           do HEAPIFY(A, i);       end for   END  

What is the worst case time complexity of the above algo?
Although the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity.

http://www.cse.iitk.ac.in/users/sbaswana/Courses/ESO211/heap.pdf/

http://www.cs.sfu.ca/CourseCentral/307/petra/2009/SLN_2.pdf


Read full article from Time Complexity of building a heap | GeeksforGeeks


Applications of Queue Data Structure | GeeksforGeeks



Queue is used when things don’t have to be processed immediatly, but have to be processed in First In First Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios.

1) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.


Read full article from Applications of Queue Data Structure | GeeksforGeeks


Applications of Heap Data Structure | GeeksforGeeks



Priority Queues: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also in O(logn) time which is a O(n) operation in Binary Heap. Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s algorithm.

Order statistics: The Heap data structure can be used to efficiently find the kth smallest (or largest) element in an array. See method 4 and 6 of this post for details.


Read full article from Applications of Heap Data Structure | GeeksforGeeks


Stability in sorting algorithms | GeeksforGeeks



A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.

However, any given sorting algo which is not stable can be modified to be stable. There can be sorting algo specific ways to make it stable, but in general, any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison operation so that the comparison of two keys considers position as a factor for objects with equal keys.


Read full article from Stability in sorting algorithms | GeeksforGeeks


When does the worst case of Quicksort occur? | GeeksforGeeks



The answer depends on strategy for choosing pivot. In early versions of Quick Sort where leftmost (or rightmost) element is chosen as pivot, the worst occurs in following cases.

1) Array is already sorted in same order.
2) Array is already sorted in reverse order.
3) All elements are same (special case of case 1 and 2)

Since these cases are very common use cases, the problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot. With these modifications, the worst case of Quick sort has less chances to occur, but worst case can still occur if the input array is such that the maximum (or minimum) element is always chosen as pivot.


Read full article from When does the worst case of Quicksort occur? | GeeksforGeeks


Access specifier of methods in interfaces | GeeksforGeeks



In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized.

Read full article from Access specifier of methods in interfaces | GeeksforGeeks


Critical Section | GeeksforGeeks



Critical Section:

In simple terms a critical section is group of instructions/statements or region of code that need to be executed atomically (read this post for atomicity), such as accessing a resource (file, input or output port, global data, etc.).

In concurrent programming, if one thread tries to change the value of shared data at the same time as another thread tries to read the value (i.e. data race across threads), the result is unpredictable.

The access to such shared variable (shared memory, shared files, shared port, etc…) to be synchronized. Few programming languages have built in support for synchronization.

It is critical to understand the importance of race condition while writing kernel mode programming (a device driver, kernel thread, etc.). since the programmer can directly access and modifying kernel data structures.

A simple solution to critical section can be thought as shown below,

acquireLock();  Process Critical Section  releaseLock();

A thread must acquire a lock prior to executing critical section. The lock can be acquired by only one thread. There are various ways to implement locks in the above pseudo code.


Read full article from Critical Section | GeeksforGeeks


Dynamic Programming | Set 2 (Optimal Substructure Property) | GeeksforGeeks



As we discussed in Set 1, following are the two main properties of a problem that suggest that the given problem can be solved using Dynamic programming.

1) Overlapping Subproblems
2) Optimal Substructure

We have already discussed Overlapping Subproblem property in the Set 1. Let us discuss Optimal Substructure property here.

2) Optimal Substructure: A given problems has Optimal Substructure Property if optimal solution of the given problem can be obtained by using optimal solutions of its subproblems.
For example the shortest path problem has following optimal substructure property: If a node x lies in the shortest path from a source node u to destination node v then the shortest path from u to v is combination of shortest path from u to x and shortest path from x to v. The standard All Pair Shortest Path algorithms like Floyd–Warshall and Bellman–Ford are typical examples of Dynamic Programming.
On the other hand the Longest path problem doesn’t have the Optimal Substructure property. Here by Longest Path we mean longest simple path (path without cycle) between two nodes. Consider the following unweighted graph given in the CLRS book. There are two longest paths from q to t: q -> r ->t and q ->s->t. Unlike shortest paths, these longest paths do not have the optimal substructure property. For example, the longest path q->r->t is not a combination of longest path from q to r and longest path from r to t, because the longest path from q to r is q->s->t->r.


Read full article from Dynamic Programming | Set 2 (Optimal Substructure Property) | GeeksforGeeks


What does 'Space Complexity' mean? | GeeksforGeeks



Auxiliary Space is the extra space or temporary space used by an algorithm.

Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input.

For example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criteria than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort and Heap Sort use O(1) auxiliary space. Space complexity of all these sorting algorithms is O(n) though.


Read full article from What does 'Space Complexity' mean? | GeeksforGeeks


Average of a stream of numbers | GeeksforGeeks



Difficulty Level: Rookie Given a stream of numbers, print average (or mean) of the stream at every point. For example, let us consider the stream as 10, 20, 30, 40, 50, 60, … Average of 1 numbers is 10.00 Average of 2 numbers is 15.00 Average of 3 numbers is 20.00 Average of 4 numbers is 25.00 Average of 5 numbers is 30.00 Average of 6 numbers is 35.00 .................. To print mean of a stream, we need to find out how to find average when a new number is being added to the stream. To do this, all we need is count of numbers seen so far in the stream, previous average and new number.

Read full article from Average of a stream of numbers | GeeksforGeeks


Copy Constructor in Java | GeeksforGeeks



Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

Read full article from Copy Constructor in Java | GeeksforGeeks


Accessing Grandparent's member in Java | GeeksforGeeks



Directly accessing Grandparent’s member in Java: Predict the output of following Java program. // filename Main.java class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { System.out.println("Parent's Print()"); } } class Child extends Parent { public void Print() { super.super.Print(); // Trying to access Grandparent's Print() System.out.println("Child's Print()"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.Print(); } } Output:

Read full article from Accessing Grandparent’s member in Java | GeeksforGeeks


Dynamic Programming | Set 21 (Variations of LIS) | GeeksforGeeks



We have discussed Dynamic Programming solution for Longest Increasing Subsequence problem in this post and a O(nLogn) solution in this post. Following are commonly asked variations of the standard LIS problem . 1. Building Bridges: Consider a 2-D map with a horizontal river passing through its center. There are n cities on the southern bank with x-coordinates a(1) … a(n) and n cities on the northern bank with x-coordinates b(1) … b(n). You want to connect as many north-south pairs of cities as possible with bridges such that no two bridges cross. When connecting cities,

Read full article from Dynamic Programming | Set 21 (Variations of LIS) | GeeksforGeeks


HttpClient 4 Tutorial



. Thanks for visiting! This is a comprehensive guide to using Apache HttpClient 4 – from starting out to advanced configuration and best practices. 1. Client Basics Learn the basics of sending a Request, receiving and interpreting the Response and configure the Client for basic usage. The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is. I usually post about Dev stuff on Google+ - you can follow me there: New eBook REST Services with Spring Join more than 4,000 engineers!

Read full article from HttpClient 4 Tutorial


HttpClient 4 Cookbook



1. Overview This cookbook shows how to use the Apache HttpClient 4 in a variety of examples and usecases. The focus is on HttpClient 4.3.x and above, so some of the examples may not work with the older versions of the API. The format of the cookbook is example focused and practical – no extraneous details and explanations necessary. If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main HttpClient tutorial . 2. Cookbook CloseableHttpResponse response = instance.execute(new HttpGet("http://www.google.com")); assertThat(response.

Read full article from HttpClient 4 Cookbook


Android - How to send gzipped JSON in HTTP request - Arnab Chakraborty



Android - How to Send Gzipped JSON in HTTP Request If you have ever developed an Android application which sends and consumes large amount of JSON data without gzipping then this post is probably for you. This is a very simple optimization technique that you can implement which vastly reduces the network latency and also benefits the users as well as internet. The idea is to transfer less data over the network and once you do that, it improves the speed of your application and helps users by reducing their mobile data usage (i.e saves $$$ in fact). module offers a way to achieve this,

Read full article from Android - How to send gzipped JSON in HTTP request - Arnab Chakraborty


Servlet request input decompression filter | Oracle Community



Please enter a title. You can not post a blank message. Please type your message and try again. This discussion is archived 2 Replies Latest reply : May 10, 2013 1:58 AM by 1008130 1008130 May 10, 2013 12:51 AM I would like to find/write a http servlet filter to decompress gziped input from a client. If the client makes a request with the header *"Content-Encoding"* set to *"gzip"* then I would like the input to be decompressed by the filter. I have been able to write a filter that wraps the request if the content type is gzip.

Read full article from Servlet request input decompression filter | Oracle Community


JavaTechniques » Compressing Data Sent Over a Socket



The java.util.zip package, introduced in JDK 1.3, provides a Java interface to the widely used ZLIB compression algorithms. The core of the package is a pair of classes, Inflator and Deflator , that wrap a native library that compresses and decompresses blocks of data. Two pairs of stream classes, ZipInputStream GZIPOutputStream , use Inflator and Deflator to read and write compressed data in the ubiquitous zip and gzip formats. The stream classes in java.util.zip work well for working with compressed files and similarly bounded data. They are, however,

Read full article from JavaTechniques » Compressing Data Sent Over a Socket


GZip Servlet Filter



Get all my free tips & tutorials! Connect with me, or sign up for my news letter or RSS feed , and get all my tips that help you become a more skilled and efficient developer. Newsletter Why GZip Compress Content? GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser smaller. This speeds up the download. This is especially beneficial for mobile phones where internet bandwidth may be limited. GZip compressing content adds a CPU overhead on the server and browser, but it is still speeding up the total page load compared to not GZip compressing.

Read full article from GZip Servlet Filter


Microsoft .NET & C#: ASP.NET WebServices two-way (Response and Request) compression - a general solution



Wednesday, July 1, 2009 Objective I will present a general solution to the two-way compression of WebServices" issue. The issue can be critical in a complicated SOA solution where a lot of data is passed not only from the server to the client but also from the client to the server. The solution is a part of a Data Service component I currently work on. Motivation On one hand, the application server can compress the HTTP data sent to the client "out-of-the-box", just enable "HTTP Compression". While such solution seems attractive, it does not provide two-way compression.

Read full article from Microsoft .NET & C#: ASP.NET WebServices two-way (Response and Request) compression - a general solution


Eclipse: TCP/IP Monitor view | JavaBlog.fr / Java.lu



This article presents a tool in Eclispe IDE to monitor the exchanges between a client (browser) and your server. In fact, the TCP/IP Monitor is a simple server that monitors all the requests and the responses (TCP/IP activity) between a Web browser and server. It is very great tool to analyze the complexe request/response (like JSON). So, TCP monitor eclipse plugin allows the testing of web applications by enabling the capture and analysis of messages sent and received from a port and a specific host. The monitoring tool TCP / IP can save messages to a log file, which can then be analyzed.

Read full article from Eclipse: TCP/IP Monitor view | JavaBlog.fr / Java.lu


Java/Web: GZIP compression with Http Filter, GZIPFilter, GZIPResponseStream, GZIPResponseWrapper | JavaBlog.fr / Java.lu



10 Jul Hi, The bandwidth is an important concern of the production management of a website. A recognized method of all browsers and systems to drastically reduce the bandwidth usage is the Zip compression of data sent on the fly, decompressed without loss and displayed by the browser. In this article, we will discuss about the configuration of gzip compression in the client-server exchanges of Web based application. The protocol used is called GZIP. Side browsers, most (99%) browsers can decode gzipped content. The header of such content is ‘Content-Encoding: gzip’,

Read full article from Java/Web: GZIP compression with Http Filter, GZIPFilter, GZIPResponseStream, GZIPResponseWrapper | JavaBlog.fr / Java.lu


How to Configure SLF4J with Different Logger Implementations | Javalobby



How to Configure SLF4J with Different Logger Implementations 08.21.2013 slf4j  library as your Java application logging API layer. Here I will show few examples on how to use and configure it with different loggers. You can think of  slf4j  as an Java interface, and then you would need an implementation (ONLY ONE) at runtime to provide the actual logging details, such as writing to STDOUT or to a file etc. Each logging implementation (or called binding) would obviously have their own way of configuring the log output, but your application will remain agnostic and always use the same  org.

Read full article from How to Configure SLF4J with Different Logger Implementations | Javalobby


[TopTalent.in] How Tech companies Like Their Résumés | GeeksforGeeks



[TopTalent.in] How Tech companies Like Their Résumés Have all the skills require to be a great Software Engineer? Here are 9 tips to make your résumé look just like how the Tech Companies want them to be. After all, your résumé is the most powerful ship to a perfect job, suitable for your skills. 1. The Opening: If your résumé starts with “Objective: To utilize my knowledge, skills and abilities as a Software Engineer” then you can definitely bid the job a farewell. 2. Long = Boring: Unless you have 10+ years of experience, your résumé’s length cannot exceed one page.

Read full article from [TopTalent.in] How Tech companies Like Their Résumés | GeeksforGeeks


java - Returning 10 most frequently used words in a document in O(n) - Stack Overflow



First create Radix Tree(or just trie) and count occurrences of each word, create a Min Heap of size 10, then scan the radix tree

Read full article from java - Returning 10 most frequently used words in a document in O(n) - Stack Overflow


Radix tree - Wikipedia, the free encyclopedia



In computer science, a radix tree (also patricia trie or radix trie or compact prefix tree) is a space-optimized trie data structure where each node with only one child is merged with its parent. The result is that every internal node has up to the number of children of the radix r of the radix trie, where r is a positive integer and a power x of 2, having x ≥ 1. Unlike in regular tries, edges can be labeled with sequences of elements as well as single elements. This makes them much more efficient for small sets (especially if the strings are long) and for sets of strings that share long prefixes.

Unlike regular trees (where whole keys are compared en masse from their beginning up to the point of inequality), the key at each node is compared chunk-of-bits by chunk-of-bits, where the quantity of bits in that chunk at that node is the radix r of the radix trie. When the r is 2, the radix trie is binary (i.e., compare that node's 1-bit portion of the key), which minimizes sparseness at the expense of maximizing trie depth—i.e., maximizing up to conflation of nondiverging bit-strings in the key. When r is an integer power of 2 greater or equal to 4, then the radix trie is an r-ary trie, which lessens the depth of the radix trie at the expense of potential sparseness.


Read full article from Radix tree - Wikipedia, the free encyclopedia


Significance of Pascal's Identity | GeeksforGeeks



We know the Pascal’s Identity very well, i.e. n c r = n-1 c r + n-1 c r-1 A curious reader might have observed that Pascal’s Identity is instrumental in establishing recursive relation in solving binomial coefficients. It is quite easy to prove the above identity using simple algebra. Here I’m trying to explain it’s practical significance. Recap from counting techniques, n c r means selecting r elements from n elements. Let us pick a special element k from these n elements, we left with (n – 1) elements.

Read full article from Significance of Pascal’s Identity | GeeksforGeeks


URL Encoding



RFC 1738: Uniform Resource Locators (URL) specification The specification for URLs ( RFC 1738 , Dec. '94) poses a problem, in that it limits the use of allowed characters in URLs to only a limited subset of the US-ASCII character set: "...Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL." HTML, on the other hand,

Read full article from URL Encoding


[TopTalent.in] Dhananjay Sathe Talks About His GSoC Experience And How To Hack It | GeeksforGeeks



[TopTalent.in] Dhananjay Sathe Talks About His GSoC Experience And How To Hack It Google Summer of Code (GSoC) is the brain child of the Google founders Larry Page and Sergey Brin and was first held in the summer of 2005 with an aim to provide some of the most challenging open source projects to the  top talent . Not so surprisingly it is one of the most sought after programs and some of the best organizations in the world have now opened up their doors for brilliant students from different parts of the world to collaborate and contribute on their projects.

Read full article from [TopTalent.in] Dhananjay Sathe Talks About His GSoC Experience And How To Hack It | GeeksforGeeks


Efficient program to calculate e^x | GeeksforGeeks



Efficient program to calculate e^x The value of Exponential Function e^x can be expressed using following Taylor Series . e^x = 1 + x/1! + x^2/2! + x^3/3! + ...... How to efficiently calculate the sum of above series? The series can be re-written as e^x = 1 + (x/1) (1 + (x/2) (1 + (x/3) (........) ) ) Let the sum needs to be calculated for n terms, we can calculate sum using following loop. for (i = n - 1, sum = 1; i > 0; --i ) sum = 1 + x * sum / i; Following is implementation of the above idea. // Efficient program to calculate e raise to the power x #include

Read full article from Efficient program to calculate e^x | GeeksforGeeks


Abstract Classes in Java | GeeksforGeeks



Abstract Classes in Java In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract. // An example abstract class in Java abstract class Shape { int color; // An abstract function (like a pure virtual function in C++) abstract void draw(); } Following are some important observations about abstract classes in Java. 1) Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.

Read full article from Abstract Classes in Java | GeeksforGeeks


Closest Pair: A Plane Sweep Algorithm



In section 3 , we explored an algorithm for determining the closest pair of a set of points in the plane. We used a divide-and-conquer approach which we generalized from one-dimension in order to solve the problem. Here we will discuss another O(nlogn) that attacks the problem in a different way. Once again, we are given a set S of n points in the plane, but this time we shall attempt to use the plane sweep technique. We sweep a vertical line across the set from left to right keeping track of the closest pair seen so far. We shall describe an O(nlogn) algorithm,

Read full article from Closest Pair: A Plane Sweep Algorithm


Write a function to delete a Linked List | GeeksforGeeks



Write a function to delete a Linked List Algorithm: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted. Implementation: #include #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to delete the entire linked list */ void deleteList(struct node** head_ref) { /* deref head_ref to get the real head */ struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; free(current);

Read full article from Write a function to delete a Linked List | GeeksforGeeks


Given a linked list which is sorted, how will you insert in sorted way | GeeksforGeeks



Algorithm: Let input linked list is sorted in increasing order. 1) If Linked list is empty then make the node as head and return it. 2) If value of the node to be inserted is smaller than value of head node then insert the node at start and make it head. 3) In a loop, find the appropriate node after which the input node (let 9) is to be inserted. To find the appropriate node start from head, keep moving until you reach a node GN (10 in the below diagram) who's value is greater than the input node. The node just before GN is the appropriate node (7).

Read full article from Given a linked list which is sorted, how will you insert in sorted way | GeeksforGeeks


Write a function to reverse a linked list | GeeksforGeeks



Iterative Method Iterate trough the linked list. In loop, change next to prev, prev to current and current to next. Implementation of Iterative Method #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to reverse the linked list */ static void reverse(struct node** head_ref) { struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev;

Read full article from Write a function to reverse a linked list | GeeksforGeeks


Reverse a Linked List in groups of given size | GeeksforGeeks



Reverse a Linked List in groups of given size Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Inputs: 1->2->3->4->5->6->7->80->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL. Algorithm: reverse(head, k) 1) Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev. See this post for reversing a linked list.

Read full article from Reverse a Linked List in groups of given size | GeeksforGeeks


Merge Sort for Linked Lists | GeeksforGeeks



Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let head be the first node of the linked list to be sorted and headRef be the pointer to head. Note that we need a reference to head in MergeSort() as the below implementation changes next links to sort the linked lists (not data at the nodes), so head node has to be changed if the data at original head is not the smallest value in linked list.

Read full article from Merge Sort for Linked Lists | GeeksforGeeks


Merge two sorted linked lists | GeeksforGeeks



Merge two sorted linked lists Write a SortedMerge() function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order. SortedMerge() should return the new list. The new list should be made by splicing together the nodes of the first two lists. For example if the first linked list a is 5->10->15 and the other linked list b is 2->3->20, then SortedMerge() should return a pointer to the head node of the merged list 2->3->5->10->15->20. There are many cases to deal with: either ‘a’ or ‘b’ may be empty,

Read full article from Merge two sorted linked lists | GeeksforGeeks


Delete alternate nodes of a Linked List | GeeksforGeeks



Delete alternate nodes of a Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Method 1 (Iterative) Keep track of previous of the node to be deleted. First change the next link of previous node and then free the memory allocated for the node. #include #include /* A linked list node */ struct node { int data; struct node *next; };

Read full article from Delete alternate nodes of a Linked List | GeeksforGeeks


Compressor Head - YouTube




Read full article from Compressor Head - YouTube


operating system - Can't understand Belady's anomaly? - Stack Overflow



The reason that when using FIFO, increasing the number of pages can increase the fault rate in some access patterns, is because when you have more pages, recently requested pages can remain at the bottom of the FIFO queue longer.

Consider the third time that "3" is requested in the wikipedia example here: http://en.wikipedia.org/wiki/Belady%27s_anomaly


Read full article from operating system - Can't understand Belady's anomaly? - Stack Overflow


A mini guide for implementing serializable interface in java - How To Do In Java



by Lokesh + · November 21, 2012 We all know what Serializable interface guarantees i.e. ability to serialize the classes. This interface recommends you to use serialVersioUID also. Now, even if you use both in your classes, do know what can break your design even now?? Lets identify the future changes in your class which will be compatible and others which will prove incompatible. Sections in this post:

Read full article from A mini guide for implementing serializable interface in java - How To Do In Java


How to do deep cloning using in memory serialization in java - How To Do In Java



that easiest way of deep cloning (with some performance overhead) is Serialization. It involves serializing the object to bytes and from bytes to object again.

 public SerializableClass deepCopy() throws Exception
    {
        //Serialization of object
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(this);
 
        //De-serialization of object
        ByteArrayInputStream bis = new   ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bis);
        SerializableClass copied = (SerializableClass) in.readObject();
 
        //Verify that object is not corrupt
 
        //validateNameParts(fName);
        //validateNameParts(lName);
 
        return copied;
    }
Read full article from How to do deep cloning using in memory serialization in java - How To Do In Java

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts