Download Data Structures 1st Week

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

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)
3. Design
(Abstract data type,
algorithm)
4. Coding
Problem
Solution
(Program code)
5. Verification
3/23
Requirements
 Define purpose/goal of system
– 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
input
System
4/23
output
Analysis
 Methodologies
– Simple problem: just do it
– Complex problem: break-down
 Top-down approach
– Break down problem into manageable piece
Problem
SubSubproblem problem
Subproblem
Subproblem Sub-
problem
…
SubSubproblem problem
Subproblem Sub-
problem
< Broken into manageable pieces >
5/23
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
input
Data
operation
6/23
output
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
7/23
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
8/23
Verification
 Correctness proofs
– 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
9/23
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
10/23
(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];
}
11/23
(ex) Selection sort
 Problem definition: sort n integers
list[0]
list[1]
list[2]
list[3]
list[4]
6
5
3
4
2
step0
2
2
5
3
3
5
4
4
6
6
step1 step2 step3
12/23
2
3
4
5
6
step4
2
3
4
5
6
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]);
}
13/23
Selection sort source (cont’d)
sort(list, n);
printf(“\n Sorted array:\n”);
for(i = 0; i < n; i++)
printf(“%d”, list[i]);
printf(“\n”);
/* print out sorted numbers */
}
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);
}
}
14/23
(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;
}
15/23
(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;
}
16/23
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”);
}
17/23
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;
}
18/23
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)
19/23
Recursive algorithms(2/3)
 Recursive algorithm
long fib (long num)
{
// Base Case
if (num == 0 || num == 1)
return num;
// General Case
return (fib (num - 1) + fib (num - 2));
} // fib
20/23
Recursive algorithms(3/3)
21/23
Recursive algorithms(3/3)
 # of function calls to calculate Fibonacci numbers
22/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
23/23
(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;
}
24/23
Recursion Properties
 Recursion is effective for
– 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
25/23
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;
}
26/23
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.”
27/23
Abstract data type Natural_Number
 Objects
– An ordered subrange of the integers (0 ... INT_MAX)
 Functions
–
–
–
–
–
–
Nat_No
Boolean
Nat_No
Boolean
Nat_No
Nat_No
Zero( )
Is_Zero(x)
Add(x, y)
Equal(x, y)
Successor(x)
Subtract(x, y)
28/23