BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide and Conquer Strategy
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 MT258 Computer Programming and Problem Solving Unit 9.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Getting Started Introduction to Algorithms Jeff Chastine.
Data Structure Introduction.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
1 M I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
Algorithms Sorting – Part 3.
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Top 50 Data Structures Interview Questions
Chapter 15 Lists Objectives
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Chapter 4 Divide-and-Conquer
Growth Functions Algorithms Lecture 8
Chapter 4: Divide and Conquer
Divide and Conquer (Merge Sort)
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Divide & Conquer Algorithms
Algorithms Sorting.
Presentation transcript:

BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3

BITS Pilani, Pilani Campus Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] Selection Sort

BITS Pilani, Pilani Campus  n 2 /2 comparisons Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] Analysis of Selection Sort cost times c 1 1 c 2 n c 3 n-1 c 4 c 5 c 6 c 7 n-1  n exchanges

BITS Pilani, Pilani Campus Divide the problem into a number of sub-problems –Similar sub-problems of smaller size Conquer the sub-problems –Solve the sub-problems recursively –Sub-problem size small enough  solve the problems in straightforward manner Combine the solutions to the sub-problems –Obtain the solution for the original problem Divide-and-Conquer

BITS Pilani, Pilani Campus To sort an array A[p.. r]: Divide –Divide the n-element sequence to be sorted into two subsequences of n/2 elements each Conquer –Sort the subsequences recursively using merge sort –When the size of the sequences is 1 there is nothing more to do Combine –Merge the two sorted subsequences Merge Sort Approach

BITS Pilani, Pilani Campus Alg.: MERGE-SORT (A, p, r) if p < r Check for base case then q ←  (p + r)/2  Divide MERGE-SORT (A, p, q) Conquer MERGE-SORT (A, q + 1, r) Conquer MERGE (A, p, q, r) Combine Initial call: MERGE-SORT (A, 1, n) Merge Sort p r q

BITS Pilani, Pilani Campus Example – n Power of q = Example

BITS Pilani, Pilani Campus Example – n Power of

BITS Pilani, Pilani Campus Example – n Not a Power of q = q = 9 q =

BITS Pilani, Pilani Campus Example – n Not a Power of

BITS Pilani, Pilani Campus Input: Array A and indices p, q, r such that p ≤ q < r –Subarrays A[p.. q] and A[q r] are sorted Output: One single sorted subarray A[p.. r] Merging p r q

BITS Pilani, Pilani Campus Idea for merging: –Two piles of sorted cards Choose the smaller of the two top cards Remove it and place it in the output pile –Repeat the process until one pile is empty –Take the remaining input pile and place it face-down onto the output pile Merging

BITS Pilani, Pilani Campus MERGE(A, 9, 12, 16) prq

BITS Pilani, Pilani Campus Example: MERGE(A, 9, 12, 16)

BITS Pilani, Pilani Campus Example (cont.)

BITS Pilani, Pilani Campus Example (cont.)

BITS Pilani, Pilani Campus Example (cont.) Done!

BITS Pilani, Pilani Campus Alg.: MERGE(A, p, q, r) 1.Compute n 1 and n 2 2.Copy the first n 1 elements into L[1.. n 1 + 1] and the next n 2 elements into R[1.. n 2 + 1] 3.L[n 1 + 1] ←  ; R[n 2 + 1] ←  4. i ← 1; j ← 1 5. for k ← p to r 6. do if L[ i ] ≤ R[ j ] 7. then A[k] ← L[ i ] 8. i ← i else A[k] ← R[ j ] 10. j ← j + 1 Merge - Pseudocode pq rq + 1 L R   p r q n1n1 n2n2

BITS Pilani, Pilani Campus Initialization (copying into temporary arrays): –  (n 1 + n 2 ) =  (n) Adding the elements to the final array (the last for loop): –n iterations, each taking constant time   (n) Total time for Merge: –  (n) Running Time of Merge

BITS Pilani, Pilani Campus The recurrence is based on the three steps of the paradigm: –T(n) – running time on a problem of size n –Divide the problem into a subproblems, each of size n/b: takes D(n) –Conquer (solve) the subproblems aT(n/b) –Combine the solutions C(n)  (1) if n ≤ c T(n) = aT(n/b) + D(n) + C(n) otherwise Analyzing Divide-and Conquer Algorithms

BITS Pilani, Pilani Campus Divide: –compute q as the average of p and r: D(n) =  (1) Conquer: –recursively solve 2 subproblems, each of size n/2  2T (n/2) Combine: –MERGE on an n -element subarray takes  (n) time  C(n) =  (n)  (1) if n =1 T(n) = 2T(n/2) +  (n) if n > 1 MERGE-SORT Running Time

BITS Pilani, Pilani Campus T(n) = cif n = 1 2T(n/2) + cnif n > 1 Use Master’s Theorem: Compare n with f(n) = cn Case 2: T(n) = Θ(nlgn) Solve the Recurrence

BITS Pilani, Pilani Campus A data type can be considered abstract when it is defined in terms of operations on it, and its implementation is hidden. Abstract Data Types (ADT) Examples of ADT List Stack Queue

BITS Pilani, Pilani Campus An array is a sequenced collection of elements, normally of the same data type. – Single-dimensional – Multi-dimensional Array Data Structure

BITS Pilani, Pilani Campus Memory layout The index in an one-dimensional array directly define the relative positions of the element in actual memory. two-dimensional array is stored in memory using row-major or column-major storage

BITS Pilani, Pilani Campus We have stored the two-dimensional array students in memory. The array is 100 × 4 (100 rows and 4 columns). Show the address of the element students[5][3] assuming that the element student[1][1] is stored in the memory location with address 1000 and each element occupies only one memory location. The computer uses row-major storage.

BITS Pilani, Pilani Campus The common operations on arrays as structures are searching, insertion, deletion and traversal. An array is more suitable when the number of deletions and insertions is small, but a lot of searching and retrieval activities are expected. Operations on array

BITS Pilani, Pilani Campus Example We have stored the two-dimensional array students in memory. The array is 100 × 4 (100 rows and 4 columns). Show the address of the element students[5][3] assuming that the element student[1][1] is stored in the memory location with address 1000 and each element occupies only one memory location. The computer uses row-major storage. Solution We can use the following formula to find the location of an element, assuming each element occupies one memory location. If the first element occupies the location 1000, the target element occupies the location 1018.

BITS Pilani, Pilani Campus A linked list is a collection of data in which each element contains the location of the next element. A linked list is a collection of data in which each element contains the location of the next element. Each element contains two parts: data and link. The name of the list is the same as the name of this pointer variable. Each element contains two parts: data and link. The name of the list is the same as the name of this pointer variable. Linked Lists

BITS Pilani, Pilani Campus Search Insertion Deletion Traversal Operations on linked lists

BITS Pilani, Pilani Campus Search operation Moving of pre and cur pointers in searching a linked list

BITS Pilani, Pilani Campus Values of pre and cur pointers in different cases Search operation

BITS Pilani, Pilani Campus Insertion Four cases can arise: Inserting into an empty list. Insertion at the beginning of the list. Insertion at the end of the list. Insertion in the middle of the list.

BITS Pilani, Pilani Campus Inserting a node at the beginning of a linked list Insertion

BITS Pilani, Pilani Campus Inserting a node at the end of the linked list Insertion

BITS Pilani, Pilani Campus Insertion Inserting a node in the middle of the linked list

BITS Pilani, Pilani Campus Deletion Two cases are: deleting the first node deleting any other node.

BITS Pilani, Pilani Campus Deletion Deleting the first node of a linked list

BITS Pilani, Pilani Campus Deletion Deleting a node at the middle or end of a linked list

BITS Pilani, Pilani Campus Traversal

BITS Pilani, Pilani Campus It is a dynamic data structure in which the list can start with no nodes and then grow as new nodes are needed It is a suitable structure if a large number of insertions and deletions are needed, but searching a linked list is slower that searching an array. It is a very efficient data structure for sorted list that will go through many insertions and deletions Linked List - Applications

BITS Pilani, Pilani Campus Linked List - Operations

BITS Pilani, Pilani Campus Linked List - Operations

BITS Pilani, Pilani Campus Linked List - Operations

BITS Pilani, Pilani Campus Singly linked list: It has only head part and corresponding references to the next nodes. Doubly linked list: A linked list which has both head and tail parts, thus allowing the traversal in bi-directional fashion. Except the first node, the head node refers to the previous node. Circular linked list: A linked list whose last node has reference to the first node. Variations of the Linked List

BITS Pilani, Pilani Campus The Circular Linked List The Doubly-Linked List Variations of the Linked List

BITS Pilani, Pilani Campus Linear Lists Operations on Linear Lists List -- Insert --- Delete --- Traverse --- Empty ----

BITS Pilani, Pilani Campus Linear Lists (Insert)

BITS Pilani, Pilani Campus General linear list ADT We define a general linear list as an ADT as shown below:

BITS Pilani, Pilani Campus A general list ADT can be implemented using either an array or a linked list. General linear list implementation

BITS Pilani, Pilani Campus A stack is a restricted linear list in which all additions and deletions are made at one end, the top. (LIFO) A stack is a restricted linear list in which all additions and deletions are made at one end, the top. (LIFO) Stacks

BITS Pilani, Pilani Campus Stack --- Push --- Pop ---- Empty--- Operations on stacks

BITS Pilani, Pilani Campus Operations on stacks

BITS Pilani, Pilani Campus Stack ADT

BITS Pilani, Pilani Campus Stack ADTs can be implemented using either an array or a linked list. Stack implementation

BITS Pilani, Pilani Campus A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front.(FIFO). A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front.(FIFO). Queues

BITS Pilani, Pilani Campus Queue Enqueue Dequeue Empty Operations on queues

BITS Pilani, Pilani Campus Operations on queues

BITS Pilani, Pilani Campus Queue ADT

BITS Pilani, Pilani Campus A queue ADT can be implemented using either an array or a linked list Queue implementation