introduction to data structures and algorithms

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

Upload: mackenzie-valdez

Post on 01-Jan-2016

67 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms

CS 110: Data Structures and Algorithms

First Semester, 2010-2011

Page 2: Introduction to Data Structures and Algorithms

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

Page 3: Introduction to Data Structures and Algorithms

Introduction

Beware of bugs in the above code; I have only proven it correct, not tried it.

--Donald Knuth

Page 4: Introduction to Data Structures and Algorithms

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

Page 5: Introduction to Data Structures and Algorithms

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?

Page 6: Introduction to Data Structures and Algorithms

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

Page 7: Introduction to Data Structures and Algorithms

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

Page 8: Introduction to Data Structures and Algorithms

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

Page 9: Introduction to Data Structures and Algorithms

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?

Page 10: Introduction to Data Structures and Algorithms

Pseudo-Code

►Combine high-level descriptions with familiar programming language structures

►Written for humans, not computersAlgorithm addFromOneToN(n)Input: An integer nOutput: The sum of all integers from 1 to n

sum ← 0

for i ← 1 to n do

sum ← sum + i

return sum

Page 11: Introduction to Data Structures and Algorithms

Pseudo-code Conventions

►Expressions►Algorithm Structures►Control Structures

Page 12: Introduction to Data Structures and Algorithms

Pseudo-code Conventions:Expressions

►Standard Math Symbols► + - * / ( )

►Relational Operators► < > ≤ ≥ = ≠

►Boolean Operators► and or not

►Assignment Operator: ←►Array Indexing: A[i]

Page 13: Introduction to Data Structures and Algorithms

Pseudo-code Conventions:Algorithm Structure

►Algorithm headingAlgorithm name(param1, param2,...):

Input: input elements

Output: output elements

►Statementscall

object.method(arguments)

return statement return value

control structures

Page 14: Introduction to Data Structures and Algorithms

Pseudo-code Conventions:Control Structure

►Decision Structures► If ... then ... [else]

►Loops►While ... do►Repeat ... until►For ... do

Page 15: Introduction to Data Structures and Algorithms

General Rules

►Communicate high level ideas and not implementation details (programming language specifics)

►Clear and informative

Page 16: Introduction to Data Structures and Algorithms

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

Page 17: Introduction to Data Structures and Algorithms

Review: Summation

Page 18: Introduction to Data Structures and Algorithms

Review: Summation

Page 19: Introduction to Data Structures and Algorithms

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

Page 20: Introduction to Data Structures and Algorithms

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

Page 21: Introduction to Data Structures and Algorithms

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

Page 22: Introduction to Data Structures and Algorithms

Mathematical Induction

►Step 1: Base Case

►Step 2: Inductive Step assume for all n ≤ k

Page 23: Introduction to Data Structures and Algorithms

Mathematical Induction

►Step 3: Proof of next case – Show that