Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester, 2010-2011.

Similar presentations


Presentation on theme: "Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester, 2010-2011."— Presentation transcript:

1 Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester, 2010-2011

2 Learning Objectives ► Define introductory terms, such as algorithm and data structure ► Write pseudo-code according to conventions ► Review mathematical concepts such as summation, logarithms, and induction

3 Introduction Beware of bugs in the above code; I have only proven it correct, not tried it. --Donald Knuth

4 Definitions ► Algorithm – a step-by-step procedure to perform a task ► Real world:Balancing a checkbook ► CS:Adding a list of numbers ► Data Structure – a systematic way of organizing and accessing data ► Real world:Filing Cabinet ► CS:Hierarchical file system

5 Why Study Algorithms? ► The obvious solution to a problem is not always the most efficient. ► Example: Adding integers from 1 to n ► Obvious method: int sum = 0; for (int i = 1; i <= n; i ++) sum = sum + i; ► Is there a better way?

6 Why Study Data Structures? ► Algorithms and data structures are usually developed hand-in-hand ► Example: Pushing and popping from a stack ► The behavior of an algorithm depends on how the data is structured. ► Example: Searching a disc vs. searching a tape ► Tape: fast-forward, rewind ► Disc:select a track

7 Design Goals ► Correctness ► Should correctly solve the task it is designed for ► For all possible inputs! ► Always depends on the specific task. ► Efficiency ► Should not use any more of the computer’s resources than necessary ► Processing time ► Memory

8 Implementation Goals ► Robustness ► Gracefully handle incorrect input ► Example: Therac-25 ► Adaptability ► Evolve in response to change ► Example: Y2K bug ► Reusability ► Allow use in many applications ► Example: Java class libraries

9 How will we study it? ► Write algorithms in Java or another programming language? ► Pitfalls: ► Solutions become tied to a particular language or paradigm ► How do we test the correctness and efficiency of an algorithm before its written?

10 Pseudo-Code ► Combine high-level descriptions with familiar programming language structures ► Written for humans, not computers Algorithm addFromOneToN(n) Input: An integer n Output: The sum of all integers from 1 to n sum ← 0 for i ← 1 to n do sum ← sum + i return sum

11 Pseudo-code Conventions ► Expressions ► Algorithm Structures ► Control Structures

12 Pseudo-code Conventions: Expressions ► Standard Math Symbols ► + - * / ( ) ► Relational Operators ► ≤ ≥ = ≠ ► Boolean Operators ► and or not ► Assignment Operator: ← ► Array Indexing: A[i]

13 Pseudo-code Conventions: Algorithm Structure ► Algorithm heading Algorithm name(param1, param2,...): Input: input elements Output: output elements ► Statements callobject.method(arguments) return statementreturn value control structures

14 Pseudo-code Conventions: Control Structure ► Decision Structures ► If... then... [else] ► Loops ► While... do ► Repeat... until ► For... do

15 General Rules ► Communicate high level ideas and not implementation details (programming language specifics) ► Clear and informative

16 Pseudo-Code ► Given an array A with size n, write pseudocode to find the maximum element in A Algorithm arrayMax(A,n): Input: An array A storing n integers. Output: The maximum element in A. currentMax ← A[0] for i ← 1 to (n - 1) do if currentMax < A[i] then currentMax ← A[i] return currentMax

17 Review: Summation

18

19 Review: Logarithms ► log b m = x  b x = m ► log b (mn) = log b m + log b n ► log b (m/n) = log b m - log b n ► log b (m n ) = n log b m ► log b b = 1 ► log b m = (log a m) / (log a b) ► 0 < a < b  log a < log b

20 Logarithm Conventions ► In… ► Calculus: log is implied to be base e ► Physics: log is implied to be base 10 ► CS: log is implied to be base 2 ► For clarification, we may use lg or lb to denote log base 2 ► i.e. lg a = lb a = log 2 a

21 Review: Mathematical Induction ► Step 1 – Check the base case ► Is the statement true for n = 0 or 1? ► Step 2 – State the induction assumption ► “The statement is true for all n ≤ k” ► Step 3 – Prove the next case in the sequence ► Is the statement true for n = k + 1? ► This will (normally) use the Step 2 assumption in its proof

22 Mathematical Induction ► Step 1: Base Case ► Step 2: Inductive Step assume for all n ≤ k

23 Mathematical Induction ► Step 3: Proof of next case – Show that


Download ppt "Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester, 2010-2011."

Similar presentations


Ads by Google