new mexico computer science for all introduction to algorithms maureen psaila-dombrowski
TRANSCRIPT
New Mexico Computer Science For All
Introduction to Algorithms
Maureen Psaila-Dombrowski
What do these things have in common?
•Doing the laundry•Going to the grocery store•Making chocolate chip cookies
Each is an repeated actionEach follows the same steps each time you do it
Each can be thought of as an algorithm
Algorithm
•A set of instructions that can be used repeatedly to solve a problem or complete a task.
Why Use Algorithms?
•Don’t have think about the details of the problem – we already know how to solve it. ▫Once you know how to go to the grocery store,
you do not need to look at a map to decide where to make the next turn.
•All the information is in the algorithm – just have to follow it.
•Same algorithm can be used ▫to sort a list whether it has 5 elements or 5000
•Anyone can follow it
Computers and Algorithms•Computers
▫Speed ▫Accuracy▫Well-suited for solving tedious problems
large telephone directory adding a long column of numbers
•Computers solving problems▫A technique for solving the problem. ▫Algorithms▫No algorithm no solution▫A general case solution (in some cases)
Characteristics of an Algorithm
•Receives input or initial conditions
•Produces a result (output)
•Contains steps ▫Well-ordered▫Clearly defined▫Do-able
•Clear stopping point or conditions
•General
Already Use Algorithms
•Wiggle
Types of Algorithms
•Similar types can be grouped together
▫Highlight how a problem can be attacked or solution used
▫Different ways to group
Implementation: Iteration, deterministic, etc…
Methodology: Brute Force, Divide and Conquer,…
Application: Sorting, Searching,…
Example: Types of Algorithms•Counting – counting large groups of items
•Sorting - puts items in a certain order in a list
•Searching - finding an item in a list
•Mapping/Graphing - solving problems related to graph theory (shortest distance, cheapest distance,…)
•Encryption – Encodes or decodes information
•Packing – how to fit the most items in a given space
•Maze – Creating or solving mazes
Developing an Algorithm• What’s the problem?
• Start with input and output
• Abstraction and Decomposition▫Break the problem into parts▫Detail the steps required for each step
• Write the pseudo-code
• Refine the solution, if necessary
• Write the code
• Debug, test the code with different cases
• Your done!
Example Algorithm: Take the Average
•Input and output
▫Input – list of numbers ( 5, 10, 12, 14, 16)▫Output – average of the list
•Abstraction and decomposition – what are the steps
▫Add up the numbers in the list (total)▫Count the numbers in the list (count)▫Average = total/count
Example Algorithm: Take the Average
•Write the Pseudo-Code
Initialize total and count to 0
For each number in the listadd that number to the totaladd 1 to count
Divide total by count
•Refine if necessary – might find improvements
•Write the code – in the language you are using
•Debug – always!
Analyzing Algorithms•There are many ways to solve the same
problem
•Analyze algorithms▫Evaluate its suitability (speed, memory
needed)▫Compare it with other possible algorithms ▫Find improvements
•Algorithms tend to become better during analysis▫shorter▫simpler▫more elegant
Big O•Used in computer science
▫When analyzing algorithms Measure of how an algorithm runs as you
increase the input size dramatically.▫Processing time▫Memory used
▫Another way to classify algorithms
•Common Big O values▫O(n)▫O(n log(n))▫O(n2)
Summary• Algorithm - A set of instructions to solve a problem
• Use algorithms because Don’t have think about the details of the problem All the information is in the algorithm Anyone can follow it
• Characteristics Receives input and Produces a result (output) Contains steps
▫Well-ordered▫Clearly defined▫Doable
Stops by itself General
• Algorithm Analysis is important Choose the best one One analysis used is Big O notation