Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure Introduction.

Similar presentations


Presentation on theme: "Data Structure Introduction."— Presentation transcript:

1 Data Structure Introduction

2 Computer Program? Computer Program Problem solution
(Algorithm) + Programming language Input (DS) Output (DS) Problem solution Data Structures+ Algorithms + language = Program

3 Data structures? Data may be organized in different ways in in Array
Represent the data in a particular way, so that it can be used efficiently Array Linked List in Stack Tree Graph in Queue

4 Data Structures: Definition
Data structure is the logical or mathematical model of a particular organization of data The model must be : Simple: Rich: mirror the actual relationships of the data The Goal: to organize data Efficiency Criteria storage of data retrieval of data manipulation of data

5 Data Structure Operations
Traversing Accessing each record exactly once so that certain items in the record may be processed Searching Finding the location of the record with the given key value or finding the location of all records which satisfy one or more conditions Insertion Adding a new record to the structure Deletion Removing a record from the structure Sorting Arrange the records in a logical order Merging Combining records from two or more files or data structures into one

6 Data Type Data Type A data type is a collection of objects and a set of operations that act on those objects. Abstract Data Type An abstract data type(ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations.

7 What is an Algorithm? An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm is a well defined computational procedure that takes some value(s) as input, and produces some value(s) as output Algorithm is finite number of computational statements that transform input into the output Algorithm Definition : A finite set of statements that guarantees an optimal solution in finite interval of time

8 Complexity Analysis of Algorithms
Analyze the running time as a function of n (# of input elements). Efficient Algorithms Consumes lesser amount of resources while solving a problem of size n Memory Time

9 Simple Example 1 2 3 4 5 6 1,2,6: Once 3,4,5 : Once per each iteration
// Input: int A[N], array of N integers // Output: Sum of all numbers in array A int Sum(int A[], int N){ int s=0; for (int i=0; i< N; i++) s = s + A[i]; return s; } 1 2 3 4 5 1,2,6: Once 3,4,5 : Once per each iteration of for loop, N iteration The complexity function of the algorithm is : f(N) = 3N +3 6 9

10 More Examples: Given the following input, find the grand total = ΣΣ matrix (k,j) matrix rows j 1 2 3 9 7 8 6 K GrandTotal Both Example1 and example2 (in the next slide) produce the same results

11 Example - 1: Example - 2: Example-1 requires 2N2 additions.
GrandTotal = 0; for (int k = 0 ; k < n-1 ; ++k ) { rows[ k ] = 0; for ( int j = 0 ; j < n-1 ; ++j ) rows[ k ] = rows[ k ] + matrix[ k ][ j ]; GrandTotal = GrandTotal + matrix[ k ][ j ]; } Example-1 requires 2N2 additions. Example - 2: GrandTotal = 0; for (int k = 0 ; k < n-1 ; ++k ) { rows[ k ] = 0; for ( int j = 0 ; j < n-1 ; ++j ) rows[ k ] = rows[ k ] + matrix[ k ][ j ]; GrandTotal = GrandTotal + rows[ k ]; } Example-2 requires N2+N additions.

12 O-notation Let g(n) : N ↦ N be a function. Then we have
O(g(n)) = { f(n) : there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 } n n0 cg(n) f(n) Notation: f(n) = O(g(n)) meaning: f(n) in O(g(n)) Think of the equality as meaning in the set of functions 12

13 O-notation Intuition: concentrate on the leading term, ignore constants 19 n n2 - 3n becomes O(n3) 2 n lg n n becomes n1.1 Complexity Term O(1) constant O(log n) logarithmic O(n) linear O(n lg n) n log n “linear-logarithmic” O(nb) polynomial(n2 :square, n3 :cube) O(bn) b > 1 exponential O(n!) factorial

14 Complexity categories growth rates of some common complexity functions.

15 Example1 use big-O notation to analyze the time efficiency of the following fragment of C++ codes. for ( k=1 ; k <= n/2 ; ++k ) { . . . for ( j=1 ; j <= n*n ; ++j ) } n2 * n/2 = n3/2  O(n3), with c = ½

16 Example2 n/2 + n2  O(n2) for ( k=1 ; k <= n/2 ; ++k ) { . . . }
for ( j=1 ; j <= n*n ; ++j ) n/2 + n2  O(n2)

17 Example3  O(log2n) while ( k > 1 ) { . . . k = k/2 ; }
Because the loop control variable is cut in half each time through the loop, the number of times that statements inside the loop will be executed is log2n.  O(log2n)

18 Next: Search Algorithms Simple Sorting Algorithms Linear search
Binary search Simple Sorting Algorithms Bubble sort Insertion sort Selection sort

19 Simple Search Algorithms
CS250-Data structure

20 “Sequential search” or Linear
Linear-Search[A, n, x] 1 for i ← 1 to n 2 if A[i] = x 3 return i 4 else i ← i + 1 5 return 0 scan the entries in A and compare each entry with x. If after j comparisons, 1 ≤ j ≤ n, the search is successful, i.e., x = A[j], j is returned; otherwise a value of 0 is returned indicating an unsuccessful search. Let x=55  unsuccessful search x=54  successful search i=5 LINEARSEARCH algorithm is in the class O(n)

21 Binary-Search begin the search in the middle of the list & compare the data of that middle to the target. If A[mid] = target  successful search If A[mid] < target  search again in the upper part of the list If A[mid] > target  search again in the lower part of the list Each comparison or iteration reduces the search space to half N/2 Untill item is found or space is out of range. Complexity O(logn) Binary-Search[A, n, x] 1 low ← 1 2 high ← n 3 while low ≤ high 4 mid ← (low + high)/2 5 if A[mid] = x 6 return mid 7 elseif A[mid] < x 8 low ← mid else high ← mid − 1 10 return 0

22 In this instance, we want to search for element x = 22.
1 4 5 7 8 9 10 12 15 22 23 27 32 35 2 3 6 11 13 14 In this instance, we want to search for element x = 22. First, we compare x with the middle element A[└(1 + 14)/2┘] = A[7] = 10. Since 22 > A[7], and since it is known that A[i] <= A[i + 1], 1 <= i < 14, x cannot be in A[1..7], and therefore this portion of the array can be discarded. So, we are left with the subarray Repeating this procedure Finally, we find that x = A[10], and the search is successfully completed. A[8..14] = 12 15 22 23 27 32 35 8 9 10 11 13 14 A[8..10] = 12 15 22 8 9 10

23 Example: Searching for x = 35 or any value greater than 35
Example: Searching for x = 35 or any value greater than 35. The array is sorted in nondecreasing order. A[1..14] = 1 4 5 7 8 9 10 12 15 22 23 27 32 35 2 3 6 11 13 14 12 15 22 23 27 32 35 8 9 10 11 13 14 27 32 35 12 13 14 35 14

24 Simple Sorting Algorithms
CS250-Data structure

25 The Sorting Problem Input: a sequence of n numbers A=‹a1, a2, …, an›
Re-arrange an array A of n numbers to be in non-escending order. simple sorting techniques: Bubble Sort. Selection Sort. Insertion Sort.

26 Selection Sort In the selection sort, we find the smallest value in the array and move it to the first index, then we find the next-smallest value and move it to the second index, and so on. 7 2 8 5 4

27 The outer loop iterates n-1 times. The inner loop iterates n-i times
Selection Sort Algorithm Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1. for i  1 to n k  i 3. for j  i + 1 to n {Find the i th smallest element.} 4. if A[j] < A[k] then k  j 5. end for 6. if k  i then swap( A[i] , A[k]) 7. end for The outer loop iterates n-1 times. The inner loop iterates n-i times There is a comparison in each iteration. The sort method executes swap() once on each iteration of its outer loop The total number of swap is O(n) The total number of comparisons = (n-1)+(n-2)+…+2+1 = n(n-1)/2 = O(n2) The total cost of the selection sort is : O(n2)

28 Code for Selection Sort
public static void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } // Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i] } // a[min] is least among a[outer]..a[a.length - 1] int temp = a[outer]; a[outer] = a[min]; a[min] = temp; // Invariant: for all i <= outer, if i < j then a[i] <= a[j] } }


Download ppt "Data Structure Introduction."

Similar presentations


Ads by Google