Data Structure Introduction.

Slides:



Advertisements
Similar presentations
Chapter 1: INTRODUCTION TO DATA STRUCTURE
Advertisements

Introduction to Algorithms Quicksort
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
Analysis of Algorithms
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Simple Sorting Algorithms
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Algorithm Efficiency and Sorting
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
1 ©2008 DEEDS Group Introduction to Computer Science 2 - SS 08 Asymptotic Complexity Introduction in Computer Science 2 Asymptotic Complexity DEEDS Group.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Mathematics Review and Asymptotic Notation
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 211 Data Structures Lecture 13
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Analysis of Algorithms CS342 S2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
Data Structure and Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Growth of Functions & Algorithms
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Simple Sorting Algorithms
DATA STRUCTURES Introduction: Basic Concepts and Notations
Teach A level Computing: Algorithms and Data Structures
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
Selection sort Given an array of length n,
Simple Sorting Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Algorithms.
Presentation transcript:

Data Structure Introduction

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

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

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

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

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

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.

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

O-notation Intuition: 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 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

Complexity categories growth 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 n/2 + n2  O(n2) for ( k=1 ; k <= n/2 ; ++k ) { . . . } for ( j=1 ; j <= n*n ; ++j ) n/2 + n2  O(n2)

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)

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

Simple Search Algorithms CS250-Data structure

“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)

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 + 1 9 else high ← mid − 1 10 return 0

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

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

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

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 - 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] } }