0% found this document useful (0 votes)
13 views

ADSA Lecture Unit II Ch1

The document provides information about advanced data structures and algorithms taught as part of a course at the Department of Computer Science and Engineering. It includes 3 units covering topics such as linear and non-linear data structures, searching and sorting algorithms, approximation algorithms, randomized algorithms, and online algorithms. Examples of commonly used sorting algorithms like bubble sort, insertion sort, selection sort, merge sort, and quick sort are described along with pseudocode.

Uploaded by

ranjit.e10947
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ADSA Lecture Unit II Ch1

The document provides information about advanced data structures and algorithms taught as part of a course at the Department of Computer Science and Engineering. It includes 3 units covering topics such as linear and non-linear data structures, searching and sorting algorithms, approximation algorithms, randomized algorithms, and online algorithms. Examples of commonly used sorting algorithms like bubble sort, insertion sort, selection sort, merge sort, and quick sort are described along with pseudocode.

Uploaded by

ranjit.e10947
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Department of Computer Science and Engineering (CSE)

ADVANCED DATA STRUCTURES


&
ALGORITHMS
(23CSH-622)

By : Dr. Ranjit Singh (E10947)

www. cuchd.in University Institute of Engineering


Campus(UIE)
: Gharaun, Mohali
Department of Computer Science and Engineering (CSE)

Contents of the Syllabus

UNIT-I (15h)
Introduction to Basic Data Structures: Importance and need of good data structures and algorithms, Introduction
to linear and non-linear data structure and its importance, Algorithms Complexity and Analysis. [3]
Linear and Non –Linear Data Structures : Arrays , Link Lists, Queues , Trees and related algorithms [6]
Advanced Data Structures: AVL Trees (Insertion , Deletion , Searching), Red-Black Trees, B-trees, B+trees,
Heaps. Data structure for disjoint sets, Augmented data structures. [6]

UNIT-II (15h)
Searching and Sorting : Internal and External Sorting algorithms: Linear Search, Binary Search, Bubble Sort,
Selection Sort, Insertion Sort, Shell Sort, Quick Sort, Heap Sort, Merge Sort, Counting Sort, Radix Sort and
analysis of their complexities and Hashing
[4]
Graphs & Algorithms: Representation, Type of Graphs, Depth- and breadth-first traversals, Planar graphs,
isomorphism, graph coloring, covering and partition, Minimum Spanning Tree: Prim’s and Kruskal’s
algorithms. [6]
String Matching Algorithms: Naïve String Matching, Suffix arrays, Suffix trees, Rabin-Karp, Knuth-Morris-
Pratt, Booyer-Moore algorithm [5]

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Contents of the Syllabus

UNIT-III (15h)
Approximation algorithms: Need of approximation algorithms: Introduction to P, NP, NP-Hard and NP-Complete Greedy
Approach, Dynamic Approach, Knapsack, Huffman Coding, TSP, All pair shortest path, Longest Common Subsequence
Problem, Matrix Chain Multiplication. [7]

Randomized algorithms: Introduction, type of randomized algorithms, Quick sort, min cut
[4]

Online Algorithms: Introduction, Online Paging Problem, k-server Problem. Data compression: Huffman’s coding,
BWT, LZW
[4]

Recommended Books:
1. Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd edition 2012.
2. Horowitz, Sahni and Rajasekaran, “Fundamental of Computer, Algorithms”, University Press (India),
2nd edition.
3. Aho, Haperoft and Ullman, “The Design and analysis of Computer Algorithms”, Pearson Education
India.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Sorting

• Sorting is a process in which records are arranged in ascending or descending order

Types Of Sorting
• Bubble sort
• Insertion sort
• Selection sort
• Merge sort
• Quick sort
• Heap sort
• Shell sort
• Cycle sort
• Count sort
• Radix Sort

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Bubble Sort
• Exchange two adjacent elements if they are out of order. Repeat until array is sorted.
• Example:
Take an array of numbers " 5 1 4 2 8", and sort the array from lowest
number to greatest number using bubble sort. In each step, elements
written in bold are being compared. Three passes will be required;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Step-by-step example

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Bubble Sort
BUBBLE(DATA, N)
Here DATA is an array with N elements. This algorithm sorts the elements in DATA.
1.Repeat steps 2 and 3 for K = 1 to N – 1
2.Set PTR = 1 [Initialize pass pointer PTR]
3.Repeat while PTR ≤ N – K [Executes pass]
a) If DATA[PTR] > DATA[PTR + 1], then
Interchange DATA[PTR] and DATA[PTR + 1]
[End of If structure]
b) Set PTR = PTR + 1
[End of inner loop]
[End of step 1 outer loop]
4.Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Insertion sort

Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Algorithm

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Selection Sort
• Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the
sorted part at the left end and the unsorted part at the right end. Initially, the
sorted part is empty and the unsorted part is the entire list.
• The smallest element is selected from the unsorted array and swapped with the
leftmost element, and that element becomes a part of the sorted array. This
process continues moving unsorted array boundary by one element to the right.
• This algorithm is not suitable for large data sets as its average and worst case
complexities are of Ο(n2), where n is the number of items.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Algorithm

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Heap Sort
• Heap sort is one of the sorting algorithms used to arrange a list of elements in
order.
• Heapsort algorithm uses one of the tree concepts called Heap Tree.
• In this sorting algorithm, we use Max Heap to arrange list of elements in
descending order and Min Heap to arrange list elements in ascending order.
• Step 1 - Construct a Binary Tree with given list of Elements.
• Step 2 - Transform the Binary Tree into Max Heap.
• Step 3 - Delete the root element from Max Heap using Heapify method.
• Step 4 - Put the deleted element into the Sorted list.
• Step 5 - Repeat the same until Min Heap becomes empty.
• Step 6 - Display the sorted list.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Merge Sort
Divide and Conquer
• Divide and Conquer cuts the problem in half each time, but uses the result of both
halves:
• cut the problem in half until the problem is trivial
• solve for both halves
• combine the solutions
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the sub-arrays only contain one element
• Merge the sub-problem solutions together:
• Compare the sub-array’s first elements
• Remove the smallest element and put it into the result array
• Continue the process until all elements have been put into the result array

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Algorithm
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two
halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Quick Sort

Divide: In Divide, first pick a pivot element. After that, partition or


rearrange the array into two sub-arrays such that each element in the left
sub-array is less than or equal to the pivot element and each element in the
right sub-array is larger than the pivot element.

Conquer: Recursively, sort two subarrays with Quicksort.

Combine: Combine the already sorted array.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Quick Sort

Quicksort picks an element as pivot, and then it partitions the given array around the
picked pivot element. In quick sort, a large array is divided into two arrays in which one
holds values that are smaller than the specified value (Pivot), and another array holds the
values that are greater than the pivot.

After that, left and right sub-arrays are also partitioned using the same approach. It will
continue until the single element remains in the sub-array.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Quick Sort
Choosing the pivot

Picking a good pivot is necessary for the fast implementation of quicksort. However, it is
typical to determine a good pivot. Some of the ways of choosing a pivot are as follows –
•Pivot can be random, i.e. select the random pivot from the given array.

•Pivot can either be the rightmost element of the leftmost element of the given array.

•Select median as the pivot element.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Shell Sort
• Shell sort is an algorithm that first sorts the elements far apart from each
other and successively reduces the interval between the elements to be
sorted.
• It is a generalized version of insertion sort.
• In shell sort, elements at a specific interval are sorted. The interval
between the elements is gradually decreased based on the sequence used.
• The performance of the shell sort depends on the type of sequence used
for a given input array.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Contd…

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Contd…

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Count Sort
• In Counting sort, the frequencies of distinct elements of the array to be sorted is
counted and stored in an auxiliary array, by mapping its value as an index of the
auxiliary array.
Algorithm:
• Let's assume that, array A of size N needs to be sorted.
• Initialize the auxiliary array Aux[] as 0.
Note: The size of this array should be ≥max(A[]).
• Traverse array A and store the count of occurrence of each element in the
appropriate index of the Aux array, which means, execute Aux[A[i]]++ for
each i, where i ranges from [0,N−1].
• Initialize the empty array sortedA[]
• Traverse array Aux and copy i into sortedA for Aux[i] number of times
where 0≤i≤max(A[]).

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Count Sort
Example:
Say A={5,2,9,5,2,3,5}.
• Aux will be of the size 9+1 i.e. 10
• Aux={0,0,2,1,0,3,0,0,0,2}.
Notice that Aux[2]=2 which represents the number of occurrences
of 2 in A[]. Similarly Aux[5]=3 which represents the number occurrences
of 5 in A[].
• After applying the counting sort algorithm, sortedA[] will be {2,2,3,5,5,5,9}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

HASHING

Hash Table is a data structure where, data is stored in an array format, where
each data value has its own unique index value. Access of data becomes very
fast if we know the index of the desired data.

Hashing is a technique to convert a range of key values into a range of indexes


of an array.
It is a technique that uniquely identifies a specific item from a collection of similar
items.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Item are in the (key, value) format

1. The hash function converts the item into a small integer or hash value. This integer is used as an index to
store the original data.

2. It stores the data in a hash table. You can use a hash key to locate data quickly.

hash = hashfunc(key)
index = hash % array_size

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Consider an example of hash table of size 20

(1,20)
(2,70)
(42,80) Sr.No. Key Hash Array Index
(4,25) 1 1 1 % 20 = 1 1
(12,44)
2 2 2 % 20 = 2 2
(14,32)
(17,11) 3 42 42 % 20 = 2 2
(13,78) 4 4 4 % 20 = 4 4
(37,98) 5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Good Hash Function

• Efficiently computable.

• Should uniformly distribute the keys (Each table position equally likely for
each key)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Collision Resolution Technique

Hashing in data structure falls into a collision if two keys are


assigned the same index number in the hash table. The collision
creates a problem because each index in a hash table is supposed to
store only one value.
Hashing in data structure uses several collision resolution
techniques to manage the performance of a hash table.
It is a process of finding an alternate location. The collision
resolution techniques can be named as-

 OPEN HASHING (Chaining)


 CLOSE HASHING (open addressing)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Open Hashing

Chaining Method
In chaining, if a hash function produces the same index for multiple
elements, these elements are stored in the same index by using a linked
list.

Now, we can use a key K to search in the linked list by just linearly
traversing. If the intrinsic key for any entry is equal to K then it means
that we have found our entry. If we have reached the end of the linked
list and yet we haven’t found our entry then it means that the entry
does not exist.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example: Let us consider a simple hash function as “key mod 7” and a


sequence of keys as 50, 700, 76, 85, 92, 73, 101

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Close Hashing(Open Addressing)

Linear probing
This hash techniques is known to be the easiest way to resolve any
collisions in hash tables.
In linear probing, the hash table is searched sequentially that starts
from the original location of the hash. If in case the location that we
get is already occupied, then we check for the next location.
The main idea of open addressing is to keep all the data in the
same table to achieve it, we search for alternative slots in the
hash table until it is found.

Let hash(x) be the slot index computed using a hash function


and S be the table size(array_size)
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
…………………………………………..

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Let us consider a simple hash function as “key mod 7” and a sequence of


keys as 50, 700, 76, 85, 92, 73, 101.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Application of Hashing

Message Digest(Cryptographic Hash Function)

Password Verification

File System(to locate files)

Pattern Matching(Detect plagiarism)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Books Recommended

Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw


Hill.
Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C+
+”, Prentice Hall of India.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

References

• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata


McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data
Structures and Algorithms in C++”, Wiley Student Edition.
• “Data Structures and Algorithms for GATE: Solutions to All Previous GATE
Questions Since 1991” by Narasimha Karumanchi
• https://www3.ntu.edu.sg/home/ehchua/programming/cpp/DataStructureAlgorithm.h
tml
• https://www.geeksforgeeks.org/data-structures/
• https://www.prodevelopertutorial.com/searching-algorithm-3-jump-search-explanati
on-and-implementation-in-c-language/
• https://www.prodevelopertutorial.com/ajs-guide-to-data-structures-and-algorithms-t
he-complete-guide-from-beginner-to-expert/

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Thank you

University Institute of Engineering (UIE)

You might also like