A finite sequence of operations that solves a given task

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Instructor Information: Dr. Radwa El Shawi Room: Week # 1: Overview & Review.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
DATA STRUCTURES (CS212D) Week # 1: Overview & Review.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Data Structure Introduction.
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.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
ALGORITHMS.
Data Structures & Algorithms CHAPTER 1 Introduction Ms. Manal Al-Asmari.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Lecture 2. Algorithms and Algorithm Convention 1.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Introduction toData structures and Algorithms
CSC 421: Algorithm Design & Analysis
CSE15 Discrete Mathematics 03/06/17
Problem Solving & Computer Programming
ICS 3UI - Introduction to Computer Science
CS1010 Programming Methodology
Data Structures 1st Week
Algorithms and Problem Solving
Applied Discrete Mathematics Week 2: Functions and Sequences
DDC 2423 DATA STRUCTURE Main text:
Unit 3: ALGORITHMS AND FLOWCHARTS
The Design and Analysis of Algorithms
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
ALGORITHMS AND FLOWCHARTS
Algorithms An algorithm is a sequence of steps written in the form of English phrases that specific the tasks that are performed while solving the problem.It.
Lecture 2 of Computer Science II
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
Algorithm Analysis CSE 2011 Winter September 2018.
CSC 421: Algorithm Design & Analysis
Enough Mathematical Appetizers!
Priority Queues Linked-list Insert Æ Æ head head
Computation.
Data Structures (CS212D) Overview & Review.
CSE 143 Lecture 5 Binary search; complexity reading:
Problem Solving Techniques
Algorithms Chapter 3 With Question/Answer Animations
Software John Sum Institute of Technology Management
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
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.
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Lecture 5: complexity reading:
Applied Discrete Mathematics Week 6: Computation
Introduction to Algorithms and Programming
Week # 1: Overview & Review
Advanced Algorithms Analysis and Design
Data Structures (CS212D) Overview & Review.
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
CSE 2010: Algorithms and Data Structures Algorithms
Algorithms and Problem Solving
Building Java Programs
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Introduction to Data Structure
Discrete Mathematics and its Applications
Building Java Programs
Discrete Mathematics CS 2610
Advanced Analysis of Algorithms
Presentation transcript:

1 Data Structures and Algorithm Analysis (based on slides of Harry Zhou) A finite sequence of operations that solves a given task Good characteristics: correctness efficiency robustness Data Structures: A way to organize data so that common operations (e.g. insert a new item, search, etc.) are performed efficiently Examples: linked lists, queues, stacks, trees, graphs

2 The maximum contiguous subsequence sum problem Given integers A1 A2 .. An. Find the sequence that produces the max value j k=i Ak The sum is a zero if all integers < 0 Example: -2 11 -4 13 -5 2 The sum of the sequence from the 2nd to the 4th number is 20, which is the largest possible value 1 -3 4 -2 -1 6 The sum of the sequence from the 3rd number to the 6th number (4 -2 -1 6) is the largest possible value: 7

3 Algorithm 1: conduct an exhaustive search(a brute force algorithm) It calculates all subsequences, such as the sequence of 1 number, the sequence of 2 numbers, and so on, starting at the first position, then at the 2nd position, until the last position Trace: (1) i=0 j=0 sum = -2 j=1 sum = -2+11 j=2 sum = -2+11-4 j=3 sum = -2+11-4+13 j=4 sum = -2+11-4+13-5 max = 18 i=0 j=3 max = 0 for(i=0; i<length; i++) for(j=i; j<length; j++) sum = 0 for(k=i; k<=j; k++) sum += a[k] if(sum > max) max = sum start = i end = j Time complexity: O(n3) Example: The following numbers are in the array a: -2 11 -4 13 -5

4 Cont. trace (2) i=1 j=1 sum = 11 j=2 sum = 11-4 j=3 sum = 11-4+13 max = 20 i=1 j=3 (3) i=2 j=2 sum = -4 j=3 sum = -4+13 j=4 sum = -4+13-5 max, i and j remain unchanged (4) i =3 j=3 sum = 13 j=4 sum = 13-5 max, i and j remain unchanged (5) i=4 j=4 sum = -5 max, i an j remain unchanged

5 Algorithm 2 Observation: j k=i Ak = Aj + j-1 k=i Ak Sequence: -2 11 -4 13 -5 Example: once we calculate -2+11-4+13=18, we need to perform only one addition to calculate the entire sequence that is: 18-5 = 13 The algorithm max=0 for(i=0; i<length; i++) sum =0 for(j=i; j<length; j++) sum += a[j] if sum > max max = sum;start=i;end=j; Time complexity: O(n2) The statement sum +=a[j] adds one additional number to the sum reducing redundant work

6 Algorithm 3 Let Ai,j be the sequence from ij and Si,j be its sum. Observation: if Si,j <0 and q>j, then Ai,q is not the max Example: 2 -1 -3 6 Ai,j=A0,2 S0,2 = -2 then S0,3 is not max Improvement: When a negative number is detected, we advance the index i. Using the above example, the value of i would be changed to 3

7 Cont. algorithm 3 sequence: -2 11 -4 13 -5 (1) j=0 a[j] = -2 i=0 sum -2 max =0 i=1 (reset) (2) j=1 a[j] = 11 i=1 sum=11 max=11 start=1 end =1 (3) j=2 a[j]= -4 sum = 7 max =11 (4) j=3 a[j] =13 sum =20 max = 20 start =1 end = 3 (5) j=4 a[j]=-5 sum = 15 max =20 start =1 end =3 i=0; max =0; sum = 0; for(j=0; j<length; j++) sum +=a[j] if sum > max max = sum start=i; end = j; else if(sum <0) i=j+1 sum=0 Time complexity: O(n) Basic idea: as long as the sum >0, add another number to it. If not, start over again

8 Summary: Algorithm 1 is O(n3) Algorithm 2 is O(n2) and all of them are correct algorithms One importance issue in the design of algorithm is efficiency

9 Steps in designing software Specification input, output, expected performance, features and sample of execution System Analysis chose top-down design or OOD (sort of bottom-up) Design (OUR FOCUS IN THIS COURSE) * choose data representation: create ADTs * algorithm design Refinement and Coding implementation Verification correctness analysis and testing Documentation manual and program comments

10 Algorithm Definition Definition: A specification of (1) the sequence of steps required to perform a task, and (2) the data objects used in performing each step Properties: (1) finite number of steps (2) must terminate (3) each step should be precise in meaning (4) has generality Representations: (1) flow chart (2) pseudocode: between English and a programming language