Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures 1st Week

Similar presentations


Presentation on theme: "Data Structures 1st Week"— Presentation transcript:

1 Data Structures 1st Week
Chapter 1 Basic Concepts 1.1 Overview: System Life Cycle 1.2 Algorithm Specification 1.3 Data Abstraction

2 Chapter 1 Basic Concepts

3 Overview: System Life Cycle
Problem solving 1. Requirement (Input, output) 2. Analysis (Break down) Solution (Program code) Problem 3. Design (Abstract data type, algorithm) 4. Coding 5. Verification

4 Requirements Define purpose/goal of system Input Output System input
Define input, output of system Covers all cases Definite/detailed description Input The information that we are given Output The results that we must produce System input output

5 Analysis Methodologies Top-down approach … Simple problem: just do it
Complex problem: break-down Top-down approach Break down problem into manageable piece Problem Sub- problem Sub- problem Sub- problem Sub- problem Sub- problem Sub- problem Sub- problem Sub- problem Sub- problem < Broken into manageable pieces >

6 Design(1/2) Find solution from perspective of data objects and operations on them Data objects: abstract data type (ADT) Operations: specification of algorithm -> If the problem is broken-down into manageable pieces, design is easy. Note: Language dependent, implementation decisions are postponed !! Program operation Data input operation output operation

7 Design(2/2) (ex) Design a scheduling system for a university
Data objects: students, courses, professors Operations Add a course to the list of university courses Search for the courses taught by some professors

8 Refinement and coding Choose representations for the data objects and write algorithms for each operation The order is crucial A data object’s representation can determine the efficiency of the algorithms related to it

9 Verification Correctness proofs Testing Error removal
Selecting algorithms that have been proven correct can reduce the number of errors Testing Error-free program Requires working code and sets of test data Error removal The ease of error removal depends on the design and coding decisions Well-documented and modularized programming

10 Algorithm specification
Definition: a finite set of instruction that accomplishes a particular task Input: zero or more quantities Output: at least one quantity Definiteness: clear and unambiguous instructions Finiteness: for all cases, algorithm terminates after finite step Difference from program Effectiveness: basic and feasible instructions Description of an algorithm Natural language, flowchart, C-style code

11 (ex) Selection sort Sorts a set of n≥1 integers
From those integers that are currently unsorted, find the smallest and place it next the sorted list Not tell us where and how the integers are initially sorted, or where we should place the result for(i=0; i<n-1; i++) { Examine list[i] to list[n-1] and suppose that the smallest integer is at list[min]; Interchange list[i] and list[min]; }

12 (ex) Selection sort Problem definition: sort n integers
list[0] list[1] list[2] list[3] list[4] step0 step1 step2 step3 step4 sorted unsorted

13 Selection sort source #include <stdio.h> #include <math.h>
#define MAX_SIZE 101 #define SWAP(x, y, t) ((t)=(x), (x)=(y), (y)=(t)) void sort(int[], int); /* selection sort */ void main(void) { int i, n; int list[MAX_SIZE]; printf(“Enter the number of numbers to generate: ”); scanf(“%d”, &n); if(n < 1 || n > MAX_SIZE) { fprintf(stderr, “Improper value of n\n”); exit(1); } for(i = 0; i < n; i++) { /* randomly generate numbers */ list[i] = rand() % 1000; printf(“%d”, list[i]);

14 Selection sort source (cont’d)
sort(list, n); printf(“\n Sorted array:\n”); for(i = 0; i < n; i++) /* print out sorted numbers */ printf(“%d”, list[i]); printf(“\n”); } void sort(int list[], int n) { int i, j, min, temp; for(i = 0; i < n-1; i++) { min = i; for(j = i+1; j < n; j++) if(list[j] < list[min]) min = j; SWAP(list[i], list[min], temp);

15 (ex) Binary search Assume that we have n≥1 distinct integers that are already sorted and sorted in the array list. We must figure out if an integer searchnum is in this list. If it is we should return an index, i, such that list[i] = searchnum. If searchnum is not present, we should return -1. while(there are more integers to check) { middle = (left + right) / 2; if(searchnum < list[middle]) right = middle -1; else if(searchnum == list[middle]) return middle; else left = middle + 1; }

16 (ex) Searching an ordered list
int binsearch(int list[], int searchnum, int left, int right) { /* search list[0] <= list[1] <= ••• <= list[n-1] for searchnum. Return its position if found. Otherwise return -1 */ int middle; if (left <= right) { middle = (left + right) / 2; switch(COMPARE(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0: return middle; case 1: right = middle – 1; } return -1;

17 Binary search source #include <stdio.h>
#define COMPARE(x, y) (((x) < (y))? -1 : ((x) == (y))? 0 : 1) #define NUM_EL 10 int binsearch(int list[], int searchnum, int left, int right); void main(void) { int nums[NUM_EL] = {5, 10, 22, 32, 45, 67, 73, 98, 99, 101}; int i, item, location; int left = 0; int right = NUM_EL – 1; for(i = 0; i < 10; ++i) printf(“%d”, nums[i]); printf(“\nEnter the item you are searching for: ”); scanf(“%d”, &item); location = binsearch(nums, item, left, right); if(location > -1) printf(“The item was found at index location %dth\n”, location + 1); else printf(“The item was not found in the array\n”); }

18 Binary search source (cont’d)
int binsearch(int list[], int searchnum, int left, int right) { int middle; while(left <= right) { middle = (left + right) / 2; switch(COMPARE(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0: return middle; case 1: right = middle - 1; } return -1;

19 Recursive algorithms(1/3)
Recursion: Functions call themselves Express a complex process in very clear terms Any function can be written recursively Good when the problem is defined recursively (ex) Fibonacci numbers: each number is the sum of previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … Recursive design Fibonacci(n) = 0 if n = 0 = 1 if n = 1 = Fibonacci(n-1) + Fibonacci(n-2)

20 Recursive algorithms(2/3)
long fib (long num) { // Base Case if (num == 0 || num == 1) return num; // General Case return (fib (num - 1) + fib (num - 2)); } // fib

21 Recursive algorithms(3/3)

22 Recursive algorithms(3/3)
# of function calls to calculate Fibonacci numbers

23 (ex) Transformation of iterative program into recursive version
Establish boundary conditions that terminate the recursive calls Implement the recursive calls so that each call brings us one step closer to a solution (ex) Recursive implementation of binary search

24 (ex) Recursive implementation of binary search
int binsearch(int list[], int searchnum, int left, int right) { /* search list[0] <= list[1] <= ••• <= list[n-1] for searchnum. Return its position if found. Otherwise return -1 */ int middle; if(left <= right) { middle = (left + right) / 2; switch(COMPARE(list[middle], searchnum)) { case -1: return binsearch(list, searchnum, middle + 1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle – 1); } return -1;

25 Recursion Properties Recursion is effective for Problems of recursion
Problems that are naturally recursive Binary search Algorithms that use a data structure naturally recursive Tree Problems of recursion Function call overhead Time Stack memory Stability

26 Data abstraction The real world abstractions must be represented in terms of data types Basic data types integer, real, character, etc. Array collections of elements of the same basic data type e.g. , int list[5] Structure collections of elements whose data types need not be the same e.g. , struct student { char last_name; int student_id; char grade; }

27 Data abstraction (cont’d)
Data type A collection of objects and a set of operations that act on those objects Abstract data type: data type organized by Specifications of objects Requirements/properties of objects Specifications of operations on the objects Description of what the function does. Names, arguments, result of each functions  “What a data type can do.” Abstract data type does not include Representation of objects Implementation of operations  “How it is don is hidden.”

28 Abstract data type Natural_Number
Objects An ordered subrange of the integers (0 ... INT_MAX) Functions Nat_No Zero( ) Boolean Is_Zero(x) Nat_No Add(x, y) Boolean Equal(x, y) Nat_No Successor(x) Nat_No Subtract(x, y)


Download ppt "Data Structures 1st Week"

Similar presentations


Ads by Google