data structure introduction computer program? computer program input (ds) (algorithm) + programming...

Post on 13-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Data Structure

Introduction

Computer Program?

Computer Program

Input(DS)

(Algorithm)+

Programming language

Output(DS)

Data Structures+ Algorithms + language = Program

Problem solution

Data structures?

Array

Linked List

Tree Queue

Stack

Graph

in

in

Represent the data in a particular way, so that it can be used efficiently

Data may be organized in different ways

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

6

1,2,6: Once3,4,5 : Once per each iteration of for loop, N iteration

The complexity function of the algorithm is : f(N) = 3N +3

2 3 1 2 1

1 2 1 1 2

2 1 1 3 1

1 2 1 2 0

9

7

8

6

rows

matrix

GrandTotal

K

j

Both Example1 and example2 (in the next slide) produce the same results

More Examples:

Given the following input, find the grand total = ΣΣ matrix (k,j)

Example - 1:1. GrandTotal = 0;2. for (int k = 0 ; k < n-1 ; ++k )3. {4. rows[ k ] = 0;5. for ( int j = 0 ; j < n-1 ; ++j )6. {7. rows[ k ] = rows[ k ] + matrix[ k ][ j ];8. GrandTotal = GrandTotal + matrix[ k ]

[ j ];9. }10. } Example-1 requires 2N2

additions. Example - 2:1. GrandTotal = 0;2. for (int k = 0 ; k < n-1 ; ++k )3. {4. rows[ k ] = 0;

5. for ( int j = 0 ; j < n-1 ; ++j )6. rows[ k ] = rows[ k ] + matrix[ k ]

[ j ];7.8. GrandTotal = GrandTotal + rows[ k ];9. }

Example-2 requires N2+N additions.

O-notationLet g(n) : N ↦ N be a function. Then we haveO(g(n)) = { f(n) : there exist positive constants c and n0 such that

0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }

nn0

cg(n)

f(n)

0

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-notationIntuition: concentrate on the leading term, ignore

constants

19 n3 + 17 n2 - 3n becomes O(n3)2 n lg n + 5 n1.1 - 5 becomes n1.1

Complexity TermO(1) constantO(log n) logarithmicO(n) linearO(n lg n) n log n “linear-logarithmic”O(nb) polynomial(n2 :square, n3 :cube)O(bn) b > 1 exponentialO(n!) factorial

Complexity categories

growth rates of some common complexity functions.

Example1use big-O notation to analyze the time efficiency of the following

fragment of C++ codes.

1. for ( k=1 ; k <= n/2 ; ++k )2. {3. . . .4. for ( j=1 ; j <= n*n ; ++j )5. {6. . . .7. }8. }

n2 * n/2 = n3/2 O(n3), with c = ½

Example21. for ( k=1 ; k <= n/2 ; ++k )2. {3. . . .4. }5. for ( j=1 ; j <= n*n ; ++j )6. {7. . . .8. }

n/2 + n2 O(n2)

Example31. while ( k > 1 )2. {3. . . .4. k = k/2 ;5. }

• 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

“Sequential search” or Linear

• 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

Linear-Search[A, n, x]1 for i ← 1 to n2 if A[i] = x3 return i4 else i ← i + 15 return 0

LINEARSEARCH algorithm is in the class O(n)

Binary-Search

Binary-Search[A, n, x]1 low ← 12 high ← n3 while low ≤ high4 mid ← (low + high)/25 if A[mid] = x6 return mid7 elseif A[mid] < x8 low ← mid + 19 else high ← mid − 110 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.

A[8..14]= 12 15 22 23 27 32 35

8 9 10 11 12 13 14

A[1..14]= 1 4 5 7 8 9 10 12 15 22 23 27 32 35

1 2 3 4 5 6 7 8 9 10 11 12 13 14

A[8..10]= 12 15 22

8 9 10

Example: Searching for x = 35 or any value greater than 35. The array is sorted in nondecreasing order. A[1..14] =

14 5 7 8 9 10 12 15 22 23 27 32 35

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1215 22 23 27 32 35

8 9 10 11 12 13 14

27 32 35

12 13 14

35

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.

Selection SortIn 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

2 7 8 5 4

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

Selection Sort AlgorithmInput: An array A[1..n] of n elements.Output: A[1..n] sorted in nondecreasing order.

1. for i 1 to n - 12. k i3. for j i + 1 to n {Find the i th smallest element.}

4. if A[j] < A[k] then k j5. end for6. 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 Sortpublic 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] }}

top related