280 likes | 293 Views
Understand the basics of data structures including arrays, linked lists, stack, tree, graph, and queue. Explore operations like traversing, searching, insertion, deletion, and sorting. Learn about data types, abstract data types, and algorithms. Dive into complexity analysis, examples, and O-notation. Get insights on search algorithms and simple sorting techniques. Includes example C++ code fragments for analysis.
E N D
Data Structure Introduction
Computer Program? Computer Program (Algorithm) + Programming language Input (DS) Output (DS) solution Problem Data Structures+ Algorithms + language = Program
Data structures? Data may be organized in different ways Represent the data in a particular way, so that it can be used efficiently Array Linked List in Stack Tree Graph in Queue
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
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
Data Type • Data TypeA data type is a collection of objects and a set of operations that act on those objects. • Abstract Data TypeAn 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.
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
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
Simple Example // 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
More Examples: Given the following input, find the grand total = ΣΣ matrix (k,j) matrix rows j K GrandTotal Both Example1 and example2 (in the next slide) produce the same results
Example - 1: • 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.
O-notation Letg(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} cg(n) f(n) 0 n0 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
O-notation Intuition: concentrate on the leading term, ignore constants 19 n3 + 17 n2 - 3nbecomes O(n3) 2 n lg n + 5 n1.1 - 5becomesn1.1 ComplexityTerm 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
Complexity categoriesgrowth rates of some common complexity functions.
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 = ½
Example2 • for ( k=1 ; k <= n/2 ; ++k ) • { • . . . • } • for ( j=1 ; j <= n*n ; ++j ) • { • . . . • } n/2 + n2 O(n2)
Example3 • 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)
Next: • Search Algorithms • Linear search • Binary search • Simple Sorting Algorithms • Bubble sort • Insertion sort • Selection sort
Simple Search Algorithms CS250-Data structure
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 “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 LINEARSEARCH algorithm is in the class O(n)
Binary-Search 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 + 1 9 else high ← mid − 1 10 return 0 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)
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.
Example: Searching for x = 35 or any value greater than 35. The array is sorted in nondecreasing order. A[1..14] =
Simple Sorting Algorithms CS250-Data structure
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.
7 2 2 2 2 4 2 4 4 7 8 8 5 5 8 5 7 5 5 8 7 8 4 4 7 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.
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 - 1 2. 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)
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] }}