1 / 23

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms. CS 110: Data Structures and Algorithms First Semester, 2010-2011. Learning Objectives. Define introductory terms, such as algorithm and data structure Write pseudo-code according to conventions

Download Presentation

Introduction to Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: intsum = 0; for (inti = 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 onhow the data is structured. • Example: Searching a disc vs. searching a tape • Tape: fast-forward, rewind • Disc: select a track

  7. 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 Design Goals

  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 call object.method(arguments) return statement return 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. Review: Summation

  19. Review: Logarithms • logb m = x  bx = m • logb (mn) = logb m + logb n • logb (m/n) = logb m - logb n • logb (mn) = n logb m • logb b = 1 • logb m = (loga m) / (loga 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 = log2 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

More Related