Data structures allow for efficient representation of data and solutions to real-world problems like insertion, deletion, search, and sort. Common data structures include arrays, linked lists, stacks, queues, trees, and hashes. Arrays use contiguous memory allocation while linked lists connect elements using pointers. Trees and hashes are useful for modeling hierarchical and associative data respectively. Recursion and traversal algorithms like breadth-first and depth-first are used to process tree and graph structures.
2. Why we need Data Structures?
•Efficient and Intuitive representation of data
•Tree using arrays vs tree using pointers
•To solve real life problems efficiently
•Insertion
•Deletion
•Search
•Sort
•Applications
•Social networks
•Employee hierarchy
•Recommended items
6. •Contiguous and fixed memory allocation (independent of
language)
•Random access and modification
•List of (index, value); index is non-negative integer; all values in
a given array are of the same data type
•To hold various types of values or have non-numerical indices,
use associative arrays/dictionaries – The Dictionary Problem?
7. •Arrays may also be:
•2-D : array of 1-D arrays (a 1-D array is a data type in itself)
•3-D : array of 2-D arrays (a 2-D array is a data type in itself)
•Memory placement of multi-dimensional arrays
1.row-major
2.column-major
•Useful Operation
a.Modify
b.Access
c.Swap
d.In-place reverse
8. Structure of an Array
template<class T> class Array{
int size;
T *arr;
void put();
void get();
…….
};
Useful Libraries
#include <vector>
10. Special (Arrays ??)
•Diagonal matrix, upper/lower triangular matrix, trigonal matrix,
symmetric/asymmetric matrices
•Generally deal with 2-D matrices, but 3-D or higher cases are
also possible. Generally deal with square matrix, but rectangular
(non-square) are also possible
•More like functions
11. Special (Arrays ??)
int spec_matrix(int i, int j){
return no_cols*i + j + 1;
}
•Performance ??
17. Why?
•To store heterogeneous data
•To store sparse data
•Flexibility of increase/decrease in size; easy insertion and
deletion of elements
•Useful Operations
•insertion
•deletion
18.
19. The Structure
template <class T> class node{
T data;
node<T> *next; // Extra (4?) bytes; size of a pointer
};
template <class T> LinkedList{
node<T> *head;
int size; // …..etc etc etc
};
Useful Libraries
#include <list>
22. Tweak some more !
•Doubly Linked Lists
•Extra (4?) bytes space vs better accessibility
•Insertion/deletion ?
•Circular Linked Lists
•How to find the end?
•Tail pointer
•Null ‘next’ pointer from last node
•Last node points to first (circular)
25. •To solve a task using that task itself
•; a task should have recursive nature
•; generally can be transformed by tweaking some parts of the
task
•Example: task of piling up n coins vs picking up a
suitcase.
•Let the task be a C function. What are the parts of the
task:
1.Input it takes
2.What it does
3.Output it gives
26. •A task is performed recursively when generally a large input
can’t be handled directly.
•So, recursion is all about simplifying the input at every step till it
becomes trivial (base case)
27. Implementation – run time stack
•Activation Records (AR)
•Store the state of a method
1.input parameters
2.return values
3.local variables
4.return addresses
34. Why?
•Want to store dictionaries?, associative arrays?
•arrays with non-numerical indices
•String operations made easy
•Ex: Finding anagrams
•Ex: Counting frequency of words in a string
35. Associative Arrays
•(key, value) pairs where key is not necessarily a non-negative
integer; can be string etc.
•Ex: no. of students in each department
•“cse” => 68
•“eee” => 120
•“mech” => 70
•“biotech” => 30
•Do not allow duplicate keys
•Dict (“cse”) = “data structures”
•Dict (“cse”) = “algorithms”
Dict(“cse”) = {“data structures”, “algorithms”}
36. Hash Functions
1.HashTable : an array of fixed size
•TableSize - preferably prime and large
2.Hash function (map to an index of the HashTable)
Techniques
•use all characters
•use aggregate properties - length, frequencies of
characters
•first 3 characters, odd characters
Evaluation
•Uniform distribution; load factor λ?
•Utilize table space
•Quickly computable
37. 3. Collision resolution
1.separate chaining
•Linked list at each index
•Insertion (at head?)
•Desired length of a chain : close to λ
•Avg. time for Successful search = 1 + 1 + λ/2
•Disadvantages
•slow?
•different data structures - array/linked lists?
38. 1.open addressing
•Single table
•Desired λ ~ 0.5
•Apply h0(x), h1(x), h2(x) …
• hi(x) = h(x) + f(i); f(0) = 0
3 ways to do it
1.linear probing : f(i) is linear in i
•f(i) = i (quickly computable vs primary clustering?)
2.quadratic probing : f(i) is quadratic in i
3.double hashing
•H(x) = h(x) + f(i).h2(x)
Rehashing
•What if the table gets full (70%, …. , 100%)
•Create a new HashTable double? the size
44. Breadth First Traversal (BFT)
•Traverse the nodes depth-wise; nodes at depth 0 before nodes
at depth 1 before nodes at depth 2 ....
•Done using a queue
•Ex: 1,2,3,4,5,7,8,6
45. Depth First Traversal (DFT)
•Move to next child only after all nodes in the current child are
marked
•Done using a stack
•Ex: a, b, c, d, e, h, f, g
47. Retrieval
•Stores the prefixes of a set of strings in an efficient manner
•Used to store associative arrays/dictionaries
48. How to create a Trie
•Ex: tin, ten, ted, tea, to, i, in, inn
49. Pairs of anagrams
•Sort all the strings
•acute -> acetu
•obtuse -> beostu … etc
•Insert them into the trie
•Keep storing collisions i.e. multiple values for each key
•Each set of values gives groups of anagrams
50. Suffix Tree/Patricia/Radix Tree
•Stores the suffixes of a string
•O(n) space and time to build
•Does not exist for all strings; add special symbol $ at the end
51. Advantages of Suffix Trees
•Store n suffixes in O(n) space.
•Improved string operations. Eg. substring lookup, Longest
common substring operation (generalized suffix trees?)
Generalized Suffix Trees
•Each string terminated by a different special symbol
•More space efficient
•Have different set of algorithms
52. Longest Common Substring
Longest Common Substring
1.Make a “generalized suffix tree” for the (2?) strings
2.Traverse the tree to mark all internal nodes as 1, 2 or (1,2)
depending on whether it is parent to a leaf node terminating
with the special symbol of string 1 and string 2.
3.Find the deepest internal node marked 1,2
Pattern Matching ?