Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3."— Presentation transcript:

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

2 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 1329648

3 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

4 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

5 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

6 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 12345678 623174 2 5 p r q

7 BITS Pilani, Pilani Campus Example – n Power of 2 12345678 q = 4 6231742 5 1234 742 5 5678 623 1 12 2 5 34 74 56 3 1 78 62 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 Example

8 BITS Pilani, Pilani Campus Example – n Power of 2 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 12345678 7654322 1 1234 754 2 5678 632 1 12 5 2 34 74 56 3 1 78 62

9 BITS Pilani, Pilani Campus Example – n Not a Power of 2 62537416274 1234567891011 q = 6 416274 123456 62537 7891011 q = 9 q = 3 274 123 416 456 537 789 62 1011 74 12 2 3 16 45 4 6 37 78 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8

10 BITS Pilani, Pilani Campus Example – n Not a Power of 2 77665443221 1234567891011 764421 123456 76532 7891011 742 123 641 456 753 789 62 1011 2 3 4 6 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8 74 12 61 45 73 78

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

12 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

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

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

15 BITS Pilani, Pilani Campus Example (cont.)

16 BITS Pilani, Pilani Campus Example (cont.)

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

18 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 + 1 9. else A[k] ← R[ j ] 10. j ← j + 1 Merge - Pseudocode pq 7542 6321 rq + 1 L R   12345678 6321754 2 p r q n1n1 n2n2

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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.

27 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

28 BITS Pilani, Pilani Campus Example 11.28 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.

29 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

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

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

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

33 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.

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

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

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

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

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

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

40 BITS Pilani, Pilani Campus Traversal

41 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

42 BITS Pilani, Pilani Campus Linked List - Operations

43 BITS Pilani, Pilani Campus Linked List - Operations

44 BITS Pilani, Pilani Campus Linked List - Operations

45 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

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

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

48 BITS Pilani, Pilani Campus Linear Lists (Insert)

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

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

51 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

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

53 BITS Pilani, Pilani Campus Operations on stacks

54 BITS Pilani, Pilani Campus Stack ADT

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

56 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

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

58 BITS Pilani, Pilani Campus Operations on queues

59 BITS Pilani, Pilani Campus Queue ADT

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


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

Similar presentations


Ads by Google