SlideShare a Scribd company logo
TanmaySinha_Student Seminar Series_VIT
University
AGENDA
Dictionaries,
             Symbol table and their
 implementation




                                  Series_VIT University
                                  TanmaySinha_Student Seminar
What is Hashing…..Why
 Hashing????
Components
Comparison of techniques
Time Complexity
Examples
DICTIONARIES
 Real time examples of dictionaries
 Spelling Checker




                                              Series_VIT University
                                              TanmaySinha_Student Seminar
 Symbol tables generated by assemblers and
  compilers
 Routing tables used in networking
  components(for DNS lookup)
SYMBOL TABLEA MODIFIED DICTIONARY
 Data   structure that associates a value with
  key
 Basic operations allowed




                                                  Series_VIT University
                                                  TanmaySinha_Student Seminar
 Implemented using

1)Arrays(Unordered/Ordered)-O(n), O(n) /O(lg n)
2)Linked List(Ordered/Unordered)-O(n)
3)Binary Search Trees-O(lg n)
4)HASHING….!!!!
 THE “DREADED” TAG of TIME
  COMPLEXITY of an algorithm..!!!!!
UNDERSTANDING HASHING
 ArraysHash Table
 Example Design an algorithm for printing the




                                                       Series_VIT University
                                                       TanmaySinha_Student Seminar
  1st repeated character, if there are duplicate
  elements in it……!!!!!!
 Possible Solutions From Brute Force Approach to a
  better solution
 IF ARRAYS ARE THERE……WHY
  HASHING…?????
 Map Keys to locations…!!!
COMPONENTS IN HASHING
 Hash Table
1)Generalization of an array




                                                      Series_VIT University
                                                      TanmaySinha_Student Seminar
2)Direct addressing
3)ProblemsLess Locations and more possible
keysanalogous to VIRTUAL MEMORY concept
 Basically , a hash table is a data structure that
  stores the keys and their associated values!!!
COMPONENTS IN HASHING…CONTD
 Hash Function
1)Transform the key to index, ‘k’ to ‘h(k)’….thereby
reducing range of array indices!!




                                                       Series_VIT University
                                                       TanmaySinha_Student Seminar
2)Characteristics of Good Hash fn
 Minimize collision

 Be quick and easy to compare

 Distribute key values evenly in the hash table

 Use all the information provided in the key

 Have a high load factor for a given set of keys
COMPONENTS IN HASHING…CONTD
  DEFINING TERMS
1.  Load Factor No. of elements in hash




                                                 Series_VIT University
                                                 TanmaySinha_Student Seminar
    table/hash table size=n/m
2.  Collisions2 records stored in same memory
    location
 What if the keys are non-integers…???

 Choice of x=33,37,39,41 gives atmost 6
   collisions on a vocabulary of 50000 elglish
   words!!!!!!!!
COLLISION RESOLUTION
TECHNIQUES
   Process of finding an alternate location
   Direct Chaining- array of linked lists –




                                                  Series_VIT University
                                                  TanmaySinha_Student Seminar
    Separate chaining
   Open Addressing – array based – Linear
    Probing, Quadratic probing , Double Hashing
CHAINING
 Slot ‘x’ contains a pointer(reference) to head
  of the list of all the stored elements that hash to
  ‘x’
 Analogous to adjacency matrix




                                                        Series_VIT University
                                                        TanmaySinha_Student Seminar
  representation of graphs
 Doubly Linked list preferable Given the
  node’s address, it helps to delete quickly(takes an
  i/p element ‘x’ and not it’s key ‘k’)
 Worst case behaviour is terribleall ‘n’ keys
  hash to the same slot,creating a list of length ‘n’
 Avg. Case behaviour can be improved , if we
  assume that any given element in equally likely
  to hash into any of the table slotsSIMPLE
  UNIFORM HASHING!!!!
LINEAR PROBING
 Search Sequentially If location occupied, check
  next location
 Restrictionno. of elements inserted into the table <




                                                          Series_VIT University
                                                          TanmaySinha_Student Seminar
  table size
 Fn. For rehashing

H(Key)= (n+1) % tablesize
 Problems – Clustering!!!

 Importance of Tablesizeshould be prime,should
  not be a power of 2
 PROBLEM IN DELETION->use of tombstones!!!!
EXAMPLE
 0          H(key)= (key )% 13
 1          18 % 13=5
 2    41    41 % 13=2




                                Series_VIT University
                                TanmaySinha_Student Seminar
 3          22% 13=9
 4
            44%13=55+1=6
 5    18
            59%13=7
 6    44
            32%13=66+16+1+1=8
 7    59
 8    32    31%13=5+1+1+1+1+1=10

 9    22    73%13=8+1+1+1=11
 10   31
 11   73
 12
QUADRATIC PROBING
 Our main requirement now is to eliminate
  CLUSTERING problem




                                                Series_VIT University
                                                TanmaySinha_Student Seminar
 Instead of step size 1 , if the location is
  occupied check at locations i+12 , i+22 ……
 Fn. For rehashing

H(Key)= (n+k2 ) % tablesize
EXAMPLE
 0          H(key)= (key+k2 )% 11
 1          31 % 11=9

 2    2     19 % 11=8




                                   Series_VIT University
                                   TanmaySinha_Student Seminar
 3    13    2 % 11=2

            13%11=214%11=3
 4    25
            25%11=326%11=4
 5    5
            24%11=225%11=328%11
 6    24
             =6
 7    9
            21%11=10
 8    19    9%11=99+12 , 9+22 , 9+32
 9    31     % 11=7
 10   21
DOUBLE HASHING
 Reduces Clustering in a better way.
 Use of a 2nd hash function h2(offset), such that h2!=0




                                                           Series_VIT University
                                                           TanmaySinha_Student Seminar
  and h2!=h1
 Concept

 First probe at location h1

 If it’s occupied, probe at location
  (probe+k*offset)(h1+h2) , (h1+2*h2)…….
 Specialized case is Linear Probing offset is 1

 If Size of table is prime, then the technique
  ensures we look at all table locations.
EXAMPLE
0
           H1(key)= key% 11
1          H2(key)=7-(key%7)
2          58 % 11=3




                                     Series_VIT University
                                     TanmaySinha_Student Seminar
3    58    14 % 11=33+7=10

4          91% 11=33+73+2*7
            %11= 6
5
           25%11=33+33+2*3=9
6    91

7
             (key%7) lies between 0
8             and 6, so that h2 always
9    25       lies between 1 and 7
10   14
COMPARISON
Linear Probing            Quadratic probing            Double Hashing



Fastest amongst three     Easier to implement and      Makes more efficient use
                          deploy                       of memory




                                                                             Series_VIT University
                                                                             TanmaySinha_Student Seminar
Uses few probes           Uses extra memory for        Uses few probes but
                          links + does not probe all   takes more time
                          table locations

Problem of Primary        Problem of Secondary         More complicated to
Clustering                Clustering                   implement


Interval between probes   Interval between probes      Interval between probes
is fixed – often at 1     increases proportional to    is computed by another
                          hash value                   hash function
HOW DOES HASHING GET O(1)
COMPLEXITY???
   Each block(may be a linked list) on the avg. stores max. no.
    of elements less than the “Load Factor(lf)”
    Generally “Load Factor” is constant So,searching time




                                                              Series_VIT University
                                                              TanmaySinha_Student Seminar

    becomes constant
   Rehashing the elements with bigger hash table size , if
    avg. no. of elements in block is > Load Factor
   Access time of table depends on Load factor, which in-turn
    depends on Hash Function
   Unsuccessful/Successful Search For chaining.Total
    time = O(1+lf), including time req. to compute h(k)
   Unsuccessful/Successful Search For Probing.Total
    time = O(1/(1+lf)), including time req. to compute h(k)
EXTRA POINTS
 Static Hashing data is staticset of keys fixed
 ExampleSet of reserved words in a programming




                                                     Series_VIT University
                                                     TanmaySinha_Student Seminar
  language, set of file names on CD-ROM
 Dynamic Hashingkeys can change dynamically.

 Example Cache design, Hash functions in
  Cryptography
A ONE-WAY HASH FUNCTION TAKES VARIABLE-LENGTH INPUT—IN THIS
CASE, A MESSAGE OF ANY LENGTH, EVEN THOUSANDS OR MILLIONS OF
BITS—AND PRODUCES A FIXED-LENGTH OUTPUT; SAY, 160-
BITS(MESSAGE DIGEST)




                        hash function




                                                               Series_VIT University
                                                               TanmaySinha_Student Seminar
    plaintext
                                 digest signed
                                 with private
                                 key


                message digest                         plaintext
                                                       +
                                                       signature


                      private key
                      use for
                      signing
PROBLEM 1
Can you Give an algorithm for finding the 1st non
repeated character in the string????? For e.g, the
1st non repeated character in the string “abzddab”
is ‘z’




                                                                Series_VIT University
                                                                TanmaySinha_Student Seminar
   Brute Force approach           Improvement using
    For each character in the        hash tables
    string, scan the remaining      Create a hash table by
    string….If that character        reading all characters in i/p
    doesn’t appear, we’re done       string and keep their
    with the solution, else we       count.
    move to the next character      After creating hash table,
   O(n2 )                           just read the hash table
                                     entries to find out, which
                                     element has count = 1
                                    O(n)
PROBLEM 2
      Given an array of ‘n’ elements. Find 2
      elements in the array whose sum is equal to
      given element ‘K’
                                       Alternative Approach
    Brute  ForceO(n2      )




                                                                   Series_VIT University
                                                                   TanmaySinha_Student Seminar

                                       ObejctiveA[x]+A[y]=K
   Improving Time
    ComplexityO(nlgn)                 Insert A[x] into hash table.
                                       Before moving to next
   Maintain 2 indices ‘low=0’
                                        element,check whether K-
    and ‘high=n-1’.
                                        A[x] also exists in hash
   Compute A[low]+A[high]              table.
   If sum is < K, decrement           Existence of such a no.
    ‘high’ , else increment ‘low’       means that we are able to
   If sum = K, that’s the              find the indices.
    solution…BINGO!!!                  Else,proceed to next i/p
                                        element.
                                       O(n)
TanmaySinha_Student Seminar
Series_VIT University
                              THANK YOU FOR PATIENT
                              LISTENING!!!

More Related Content

What's hot

Hashing
HashingHashing
Hashing
debolina13
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
Krish_ver2
 
Hashing data
Hashing dataHashing data
Hashing data
umair khan
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
Andres Mendez-Vazquez
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
Hayi Nukman
 
Hashing
HashingHashing
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
Prof Ansari
 
Hashing
HashingHashing
Hashing
kurubameena1
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
Shyam Khant
 
Hashing
HashingHashing
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
sumitbardhan
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
Jawad Khan
 
linear probing
linear probinglinear probing
linear probing
rajshreemuthiah
 
Hashing
HashingHashing
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
Hanif Durad
 

What's hot (20)

Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing data
Hashing dataHashing data
Hashing data
 
Hashing
HashingHashing
Hashing
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Hashing
HashingHashing
Hashing
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
 
Hashing
HashingHashing
Hashing
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
 
Hashing
HashingHashing
Hashing
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
linear probing
linear probinglinear probing
linear probing
 
Hashing
HashingHashing
Hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 

Similar to Application of hashing in better alg design tanmay

presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
jainaaru59
 
Language Technology Enhanced Learning
Language Technology Enhanced LearningLanguage Technology Enhanced Learning
Language Technology Enhanced Learning
telss09
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
Sander Timmer
 
Claire98
Claire98Claire98
Claire98
Yves Caseau
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
Sri Ambati
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and Tensorflow
Oswald Campesato
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
H2 o berkeleydltf
H2 o berkeleydltfH2 o berkeleydltf
H2 o berkeleydltf
Oswald Campesato
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
R Basics
R BasicsR Basics
Presentation on Text Classification
Presentation on Text ClassificationPresentation on Text Classification
Presentation on Text Classification
Sai Srinivas Kotni
 
Machine Learning meets DevOps
Machine Learning meets DevOpsMachine Learning meets DevOps
Machine Learning meets DevOps
Pooyan Jamshidi
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingFilip De Sutter
 
Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGL
Oswald Campesato
 
deepnet-lourentzou.ppt
deepnet-lourentzou.pptdeepnet-lourentzou.ppt
deepnet-lourentzou.ppt
yang947066
 
Finding similar items in high dimensional spaces locality sensitive hashing
Finding similar items in high dimensional spaces  locality sensitive hashingFinding similar items in high dimensional spaces  locality sensitive hashing
Finding similar items in high dimensional spaces locality sensitive hashing
Dmitriy Selivanov
 
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Mail.ru Group
 
Can recurrent neural networks warp time
Can recurrent neural networks warp timeCan recurrent neural networks warp time
Can recurrent neural networks warp time
Danbi Cho
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Raffi Khatchadourian
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
Oswald Campesato
 

Similar to Application of hashing in better alg design tanmay (20)

presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 
Language Technology Enhanced Learning
Language Technology Enhanced LearningLanguage Technology Enhanced Learning
Language Technology Enhanced Learning
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
 
Claire98
Claire98Claire98
Claire98
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and Tensorflow
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
 
H2 o berkeleydltf
H2 o berkeleydltfH2 o berkeleydltf
H2 o berkeleydltf
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
R Basics
R BasicsR Basics
R Basics
 
Presentation on Text Classification
Presentation on Text ClassificationPresentation on Text Classification
Presentation on Text Classification
 
Machine Learning meets DevOps
Machine Learning meets DevOpsMachine Learning meets DevOps
Machine Learning meets DevOps
 
Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Deep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGLDeep Learning in your Browser: powered by WebGL
Deep Learning in your Browser: powered by WebGL
 
deepnet-lourentzou.ppt
deepnet-lourentzou.pptdeepnet-lourentzou.ppt
deepnet-lourentzou.ppt
 
Finding similar items in high dimensional spaces locality sensitive hashing
Finding similar items in high dimensional spaces  locality sensitive hashingFinding similar items in high dimensional spaces  locality sensitive hashing
Finding similar items in high dimensional spaces locality sensitive hashing
 
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
 
Can recurrent neural networks warp time
Can recurrent neural networks warp timeCan recurrent neural networks warp time
Can recurrent neural networks warp time
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
 

Recently uploaded

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

Application of hashing in better alg design tanmay

  • 2. AGENDA Dictionaries, Symbol table and their implementation Series_VIT University TanmaySinha_Student Seminar What is Hashing…..Why Hashing???? Components Comparison of techniques Time Complexity Examples
  • 3. DICTIONARIES  Real time examples of dictionaries  Spelling Checker Series_VIT University TanmaySinha_Student Seminar  Symbol tables generated by assemblers and compilers  Routing tables used in networking components(for DNS lookup)
  • 4. SYMBOL TABLEA MODIFIED DICTIONARY  Data structure that associates a value with key  Basic operations allowed Series_VIT University TanmaySinha_Student Seminar  Implemented using 1)Arrays(Unordered/Ordered)-O(n), O(n) /O(lg n) 2)Linked List(Ordered/Unordered)-O(n) 3)Binary Search Trees-O(lg n) 4)HASHING….!!!!  THE “DREADED” TAG of TIME COMPLEXITY of an algorithm..!!!!!
  • 5. UNDERSTANDING HASHING  ArraysHash Table  Example Design an algorithm for printing the Series_VIT University TanmaySinha_Student Seminar 1st repeated character, if there are duplicate elements in it……!!!!!!  Possible Solutions From Brute Force Approach to a better solution  IF ARRAYS ARE THERE……WHY HASHING…?????  Map Keys to locations…!!!
  • 6. COMPONENTS IN HASHING  Hash Table 1)Generalization of an array Series_VIT University TanmaySinha_Student Seminar 2)Direct addressing 3)ProblemsLess Locations and more possible keysanalogous to VIRTUAL MEMORY concept  Basically , a hash table is a data structure that stores the keys and their associated values!!!
  • 7. COMPONENTS IN HASHING…CONTD  Hash Function 1)Transform the key to index, ‘k’ to ‘h(k)’….thereby reducing range of array indices!! Series_VIT University TanmaySinha_Student Seminar 2)Characteristics of Good Hash fn  Minimize collision  Be quick and easy to compare  Distribute key values evenly in the hash table  Use all the information provided in the key  Have a high load factor for a given set of keys
  • 8. COMPONENTS IN HASHING…CONTD  DEFINING TERMS 1. Load Factor No. of elements in hash Series_VIT University TanmaySinha_Student Seminar table/hash table size=n/m 2. Collisions2 records stored in same memory location  What if the keys are non-integers…???  Choice of x=33,37,39,41 gives atmost 6 collisions on a vocabulary of 50000 elglish words!!!!!!!!
  • 9. COLLISION RESOLUTION TECHNIQUES  Process of finding an alternate location  Direct Chaining- array of linked lists – Series_VIT University TanmaySinha_Student Seminar Separate chaining  Open Addressing – array based – Linear Probing, Quadratic probing , Double Hashing
  • 10. CHAINING  Slot ‘x’ contains a pointer(reference) to head of the list of all the stored elements that hash to ‘x’  Analogous to adjacency matrix Series_VIT University TanmaySinha_Student Seminar representation of graphs  Doubly Linked list preferable Given the node’s address, it helps to delete quickly(takes an i/p element ‘x’ and not it’s key ‘k’)  Worst case behaviour is terribleall ‘n’ keys hash to the same slot,creating a list of length ‘n’  Avg. Case behaviour can be improved , if we assume that any given element in equally likely to hash into any of the table slotsSIMPLE UNIFORM HASHING!!!!
  • 11. LINEAR PROBING  Search Sequentially If location occupied, check next location  Restrictionno. of elements inserted into the table < Series_VIT University TanmaySinha_Student Seminar table size  Fn. For rehashing H(Key)= (n+1) % tablesize  Problems – Clustering!!!  Importance of Tablesizeshould be prime,should not be a power of 2  PROBLEM IN DELETION->use of tombstones!!!!
  • 12. EXAMPLE 0  H(key)= (key )% 13 1  18 % 13=5 2 41  41 % 13=2 Series_VIT University TanmaySinha_Student Seminar 3  22% 13=9 4  44%13=55+1=6 5 18  59%13=7 6 44  32%13=66+16+1+1=8 7 59 8 32  31%13=5+1+1+1+1+1=10 9 22  73%13=8+1+1+1=11 10 31 11 73 12
  • 13. QUADRATIC PROBING  Our main requirement now is to eliminate CLUSTERING problem Series_VIT University TanmaySinha_Student Seminar  Instead of step size 1 , if the location is occupied check at locations i+12 , i+22 ……  Fn. For rehashing H(Key)= (n+k2 ) % tablesize
  • 14. EXAMPLE 0  H(key)= (key+k2 )% 11 1  31 % 11=9 2 2  19 % 11=8 Series_VIT University TanmaySinha_Student Seminar 3 13  2 % 11=2  13%11=214%11=3 4 25  25%11=326%11=4 5 5  24%11=225%11=328%11 6 24 =6 7 9  21%11=10 8 19  9%11=99+12 , 9+22 , 9+32 9 31 % 11=7 10 21
  • 15. DOUBLE HASHING  Reduces Clustering in a better way.  Use of a 2nd hash function h2(offset), such that h2!=0 Series_VIT University TanmaySinha_Student Seminar and h2!=h1  Concept  First probe at location h1  If it’s occupied, probe at location (probe+k*offset)(h1+h2) , (h1+2*h2)…….  Specialized case is Linear Probing offset is 1  If Size of table is prime, then the technique ensures we look at all table locations.
  • 16. EXAMPLE 0  H1(key)= key% 11 1  H2(key)=7-(key%7) 2  58 % 11=3 Series_VIT University TanmaySinha_Student Seminar 3 58  14 % 11=33+7=10 4  91% 11=33+73+2*7 %11= 6 5  25%11=33+33+2*3=9 6 91 7  (key%7) lies between 0 8 and 6, so that h2 always 9 25 lies between 1 and 7 10 14
  • 17. COMPARISON Linear Probing Quadratic probing Double Hashing Fastest amongst three Easier to implement and Makes more efficient use deploy of memory Series_VIT University TanmaySinha_Student Seminar Uses few probes Uses extra memory for Uses few probes but links + does not probe all takes more time table locations Problem of Primary Problem of Secondary More complicated to Clustering Clustering implement Interval between probes Interval between probes Interval between probes is fixed – often at 1 increases proportional to is computed by another hash value hash function
  • 18. HOW DOES HASHING GET O(1) COMPLEXITY???  Each block(may be a linked list) on the avg. stores max. no. of elements less than the “Load Factor(lf)” Generally “Load Factor” is constant So,searching time Series_VIT University TanmaySinha_Student Seminar  becomes constant  Rehashing the elements with bigger hash table size , if avg. no. of elements in block is > Load Factor  Access time of table depends on Load factor, which in-turn depends on Hash Function  Unsuccessful/Successful Search For chaining.Total time = O(1+lf), including time req. to compute h(k)  Unsuccessful/Successful Search For Probing.Total time = O(1/(1+lf)), including time req. to compute h(k)
  • 19. EXTRA POINTS  Static Hashing data is staticset of keys fixed  ExampleSet of reserved words in a programming Series_VIT University TanmaySinha_Student Seminar language, set of file names on CD-ROM  Dynamic Hashingkeys can change dynamically.  Example Cache design, Hash functions in Cryptography
  • 20. A ONE-WAY HASH FUNCTION TAKES VARIABLE-LENGTH INPUT—IN THIS CASE, A MESSAGE OF ANY LENGTH, EVEN THOUSANDS OR MILLIONS OF BITS—AND PRODUCES A FIXED-LENGTH OUTPUT; SAY, 160- BITS(MESSAGE DIGEST) hash function Series_VIT University TanmaySinha_Student Seminar plaintext digest signed with private key message digest plaintext + signature private key use for signing
  • 21. PROBLEM 1 Can you Give an algorithm for finding the 1st non repeated character in the string????? For e.g, the 1st non repeated character in the string “abzddab” is ‘z’ Series_VIT University TanmaySinha_Student Seminar  Brute Force approach  Improvement using For each character in the hash tables string, scan the remaining  Create a hash table by string….If that character reading all characters in i/p doesn’t appear, we’re done string and keep their with the solution, else we count. move to the next character  After creating hash table,  O(n2 ) just read the hash table entries to find out, which element has count = 1  O(n)
  • 22. PROBLEM 2 Given an array of ‘n’ elements. Find 2 elements in the array whose sum is equal to given element ‘K’  Alternative Approach Brute ForceO(n2 ) Series_VIT University TanmaySinha_Student Seminar   ObejctiveA[x]+A[y]=K  Improving Time ComplexityO(nlgn)  Insert A[x] into hash table.  Before moving to next  Maintain 2 indices ‘low=0’ element,check whether K- and ‘high=n-1’. A[x] also exists in hash  Compute A[low]+A[high] table.  If sum is < K, decrement  Existence of such a no. ‘high’ , else increment ‘low’ means that we are able to  If sum = K, that’s the find the indices. solution…BINGO!!!  Else,proceed to next i/p element.  O(n)
  • 23. TanmaySinha_Student Seminar Series_VIT University THANK YOU FOR PATIENT LISTENING!!!