Tuesday, March 10, 2020

Tuesday, 10 March 2020

Hashing

-Hashing is a method / technique used for storing and retrieving keys in a rapid manner.
-Hashing is also used to index and retrieve items in a database because it is faster to find item
using shorter hashed key than to find it using the original value.
-The idea of hashing is to distribute the entries (key/value pairs) across an array of buckets. Given a key, the algorithm computes an index that suggests where the entry can be found.

Hash Table

-Hash table is a table (array) where we store the original string, the index of the table
is the hashed key while the value is the original string.
-The size of hash table is usually several orders of magnitude lower than the total number
of possible string, so several string might have a same-hashed key.
Image result for hash table













Hash Function

-There are many ways to hash a string into a key. The following are some of the important
methods for constructing hash functions.

1) Mid-square
Steps to be taken :
a) Square the value of the key. Make it like (V2).
b) Extract the middle r bits of the result obtained in the first step.

Illustration :

Function : h(V) = s
V = key
s = the hash key obtained by selecting r bits from V2.

2) Division
Steps to be taken :
a) Divide the string / identifier by using the modulus operator.
b) Its the most simple method of hashing an integer x.

Illustration : 

Function: h(c) = c mod R
c = key
R = the value which is used to divide the key, usually a prime number or the table size
or the size of the memory used.

3) Folding 
Steps to be taken :
a) Divide the key value into a number of parts where each part has the same number of digits except
the last part which may have lesser digits than the other parts.
b) Add the individual parts. That is obtain the sum of part 1 + part 2 + ... + part n. The hash value produced by ignoring last carry if any.

4) Digit Extraction 
- A predefined digit of the given number which is considered as the hash address.
Example : M = 12345
- If we extract the 1st , 4th and 5th digit, we will get hash code of : 145.

5) Rotating Hash
Steps to be taken :
a) Use any hash method ( such as division or mid-square method)
b) After getting the hash code/address from the hash method, do rotation.
c) Rotation is performed by shifting the digits to get a new hash address.
Example : M is a hash address which contain the value 20101
The rotation result will be 10102 ( fold the digits ).

Collision

- A collision or clash is a situation that occurs when two distinct pieces of data have the same hash value, checksum, fingerprint or cryptographic digest.
- In general there are 2 ways to handle collisions :'

a) Linear Probing

- The idea of this concept is to process one by one until what we are searching is found, it is similar to linear search.


Example of  Linear Probing algorithm :
void linear_probing(item, h[]) {
  hash_key = hash(item);
  i = has_key;
  while ( strlen(h[i] != 0 ) {
  if ( strcmp(h[i], item) == 0 ) {
  // DUPLICATE ENTRY
  }
  i = (i + 1) % TABLE_SIZE;
  if ( i == hash_key ) {
  // TABLE IS FULL
  }
  }
  h[i] = item;
}


b) Chaining 

-In chaining, we store each string in a chain (linked list). So if there is collision, we only
need to iterate on that chain.

Example of Chaining algorithm :
void chaining(item, h[]) {
  hash_key = hash(item);
  trail = NULL, lead = h[hash_key];
  while ( lead ) {
  if ( strcmp(lead->item, item) == 0 ) { // DUPLICATE ENTRY }
  trail = lead; lead = lead->next;
  }
  p = malloc new node;
  p->item = item;
  p->next = NULL;
  if ( trail == 0 ) h[hash_key] = p; else trail->next = p;
}










Monday, March 2, 2020

Monday, 2 March 2020



1) STACK CONCEPT

- A stack is a collection data structure which has two fundamental operation: push and pop.

Data are stored in Last In First Out (LIFO) way. In the pushdown stacks only two operations are allowed: Push the item into the stack, and Pop the item out of the stack. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. Push adds an item to the top of the stack, Pop removes the item from the top.

- Top means the most recent data.








2) QUEUE CONCEPT


- Queue is a collection data structure which has two fundamental operation: push and pop (similar to stack). The data are stored in First In First Out (FIFO) way. This linear data structure can only be inserted from the bottom and used from the top.




3) INFIX, PREFIX, POSTFIX

- Infix = Operators are written in between their operands. (Operands are in the middle)

Example --> A + B

- Prefix = Operators are written after their operands. ( Operands are on the right )

Example --> A B +

- Postfix = Operators are written before their operands. (Operands are on the left)

Example --> + A B





4) Depth-First Search (DFS)

-Depth-First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores as far as possible along each branch before backtracking.

DFS has many uses such as :

- Finding articulation point and bridge in a graph.

- Topological sorting.

- Finding connected component.

- Finding the bridges of a graph.

- Planarity Testing.

- etc.

5) Breadth First Search (BFS)

-Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root, and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

- It uses the opposite strategy as Depth-first search (DFS), which instead explores the node branch as far as possible before being forced to backtrack and expand other nodes.

BFS has many uses such as :

- Finding the shortest path between two nodes u and v, with path length measured by number of edges.

- Ford-Fulkerson method for computing the maximum flow in a flow network.

- Serialization/Deserialization of a binary tree vs serialization in sorted order, allows the tree to be re-constructed in an efficient manner.

-Testing bipartiteness of a graph.

































Tuesday, February 25, 2020

Tuesday, 25 Febuary 2020

1) Pointer is a data type whose value refers to another value stored
elsewhere in computer memory using its address.

Two most important operators used with pointer type:
a) & the address operator
b) * the dereferencing operator

Example in declaring pointer :
data type *ptr name;
eg: int *pointer;
    char *pointer;

2) Array is a collection of similar data elements, these data
have the same data type (homogenous).

Array's element are stored in consecutive memory locations
and are referenced by an index.

Array index starts from zero.

3) Structure is a user-defined data type that can store related information
(even of different data types) together, while an array can store only
entities of same data types.

It is a collection of variables under a single name.

The variables within a structure are of different data types and each has a name that
is used to select it from the structure.

4) A data structure is an arrangement of data either in the computer's memory or on the
disk storage.

Some common examples of data structures are :
- Arrays
- Linked Lists
- Queues
- Stacks
- Binary Trees
- Hash Tables

5) Linked list are collection of nodes which doesnt store its node in a consecutive
memory locations and can be accessed only in an sequential manner.

Single linked list are a type of linked list that goes in a direction (only one direction) so it will only move forward and cant go back to the previous data. Only can refer to next data.

Singly-linked-list.svg





  • Example of Single linked list ^^ 


Double linked list are type of linked list that can goes forward and backward so it can goes to two direction , next and previous data.

Doubly-linked-list.svg



  • Example of Double linked list ^^


Circular linked list is when the head can refer to the tail and the tail can also refer to the head, this creates a loop. In this linked list. A Circular linked list does not contain NULL because the head and the tail can refer to each other.

Circularly-linked-list.svg






Tuesday, 10 March 2020

Hashing -Hashing is a method / technique used for storing and retrieving keys in a rapid manner. -Hashing is also used to index and ret...