Data Structures 1st Week

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

CS1010 Programming Methodology
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Chapter 1 – Basic Concepts
Recursion. Binary search example postponed to end of lecture.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Recursion.
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
What is Program  A Set of Instructions  Data Structures + Algorithms  Data Structure = A Container stores Data  Algoirthm = Logic + Control.
Data Structures 1- Course Syllabus. 2- Introduction about Data Structures.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Prof. Hsin-Mu (Michael) Tsai ( 蔡欣穆 ) Department of Computer Science and Information Engineering National Taiwan University BASICS.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
Data Structures in C Networking Laboratory Chap. 1-1 Copyright(c) 2000, Sungkyunkwan University Chapter #1: BASIC CONCEPTS Fundamentals of Data Structures.
CS Data Structures Chapter 1 Basic Concepts.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 8 Algorithms.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1.
RECURSIONL27-L28. To learn and the following concepts To design a recursive algorithm To solve problems using recursion To understand the relationship.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Introduction toData structures and Algorithms
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Introduction to Algorithms
CS1010 Programming Methodology
5.13 Recursion Recursive functions Functions that call themselves
Chapter 17 Recursion.
Decrease-and-Conquer Approach
COP 3503 FALL 2012 Shayan Javed Lecture 15
Recursion Chapter 12.
Towers of Hanoi Move n (4) disks from pole A to pole C
Data Structure 김용성 Data Structure in C.
Functions Dr. Sajib Datta
Intro to Recursion.
CSCI2100 Data Structures Tutorial 7
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CSC215 Lecture Algorithms.
CNG 140 C Programming (Lecture set 8)
Programming application CC213
A finite sequence of operations that solves a given task
Programming We have seen various examples of programming languages
ITEC 2620M Introduction to Data Structures
Basics of Recursion Programming with Recursion
Introduction to Algorithms
Basic Concepts of Algorithm
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
ICS103 Programming in C Lecture 11: Recursive Functions
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Intro to Arrays Storing List of Data.
Data structures & Algorithm Strategies
Presentation transcript:

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

Chapter 1 Basic Concepts

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

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

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 >

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

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

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

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

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

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

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

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]);

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

(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; }

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

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”); }

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;

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)

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

Recursive algorithms(3/3)

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

(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

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

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

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

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

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)