Sunteți pe pagina 1din 68

CSALGO

ALGORITHM
Introduction to Programming
PROFESSOR
• Mr. Aldrich Alexis G. Olivar
• Full
• CS Department
• BSCS, MSCS
• Schedule
• 9:00 – 11:00 Monday, Tuesday, Thursday, Friday
GRADING SYSTEM
MIDTERM GRADE
CLASS STANDING (60%)
Long Quizzes (Average of 3 Quizzes) 35%
Machine Problem 30%
Short Quizzes 15%
Seatwork, Assignments, Recitations 10%
(Class Participation)
Attendance 5%
Teachers Evaluation 5%
MIDTERM EXAMINATION 40%
GRADING SYSTEM
FINAL GRADE
CLASS STANDING (60%)
Long Quizzes (Average of 3 Quizzes) 35%
Machine Problem 30%
Short Quizzes 15%
Seatwork, Assignments, Recitations (Class Participation) 10%
Attendance 5%
Teachers Evaluation 5%
MIDTERM EXAMINATION 15%

FINAL EXAMINATION 25%

PASSING GRADE 70%


CLASS POLICIES
• Attendance
• Absentees
• Latecomers
• Mobile phones
• Classroom behavior
LESSON #1

Introduction to
Algorithm
WHAT IS AN ALGORITHM?
WHAT IS AN ALGORITHM?
• An algorithm is a sequence of unambiguous
instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in a finite
amount of time.
problem

algorithm

input “computer” output


What is Algorithm Analysis?

• How to estimate the time required for an algorithm


• Techniques that drastically reduce the running time of an algorithm
• A mathematical framework that more rigorously describes the
running time of an algorithm
• Analysis is performed with respect to a computational model
Importance of Algorithm Analysis
• Need to recognize limitations of various algorithms for solving a
problem
• Need to understand relationship between problem size and running
time
• When is a running program not good enough?

• Need to learn how to analyze an algorithm's running time without


coding it
• Need to learn techniques for writing more efficient code
• Need to recognize bottlenecks in code as well as which parts of code
are easiest to optimize
• General approaches to algorithm design
✓Divide and conquer
✓Greedy method
✓Dynamic Programming
✓Basic Search and Traversal Technique
✓Graph Theory
✓Linear Programming
✓Approximation Algorithm

• Methods of analyzing algorithm correctness and efficiency


– Recursion equations
– Lower bound techniques
– O,Omega and Theta notations for best/worst/average case analysis
Some Application of Algorithm Analysis and Design

• sorting
• data retrieval
• network routing
• Games
What do we analyze about algorithms ?
• Correctness
• Does the input/output relation match algorithm requirement?
• Amount of work done (aka complexity)
• Basic operations to do task
• Amount of space used
• Memory used
• Simplicity, clarity
• Verification and implementation.
• Optimality
• Is it impossible to do better?
Why study algorithms and performance?

• Algorithms help us to understand scalability.


• Performance often draws the line between what is
feasible and what is impossible.
• Algorithmic mathematics provides a language for
talking about program behavior.
• Performance is the currency of computing.
• The lessons of program performance generalize to
other computing resources.
• Speed is fun!
Examples Of Algorithms
The Selection Problem
Problem: given a group of n numbers, determine the kth largest
• Algorithm 1
• Store numbers in an array
• Sort the array in descending order
• Return the number in position k

• Algorithm 2
– Store first k numbers in an array
– Sort the array in descending order
– For each remaining number, if the number is larger than the kth
number, insert the number in the correct position of the array
– Return the number in position k
The Lowest Number Problem
Problem: Input is a sequence of integers stored in an array.
Output the minimum.

INPUT Algorithm OUTPUT


m:= a[1];
25, 90, 53, 23, 11, 34 for I:=2 to size of input 11
if m > a[I] then
m:=a[I];
return s

m
Data-Structure
EUCLID’S ALGORITHM
 Euclid’s algorithm for finding the greatest common divisor
Problem: Find gcd(m,n), the greatest common divisor of
two nonnegative, not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60
Euclid’s algorithm is based on repeated application of
equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the
problem trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
Which algorithm is better?

The algorithms are correct, but which is the best?


• Measure the running time (number of operations
needed).
• Measure the amount of memory used.
• Note that the running time of the algorithms
increase as the size of the input increases.
What do we need?

Correctness: Whether the algorithm computes


the correct solution for all instances

Efficiency: Resources needed by the algorithm

1. Time: Number of steps.


2. Space: amount of memory used.

Measurement “model”: Worst case, Average case


and Best case.
EUCLID’S ALGORITHM
Euclidean Algorithm
- An algorithm to find the greatest common divisor (GCD) of two positive integers.
- It is also known as the greatest common factor (GCF) or highest common factor (HCF)

Formal Description of the Euclidean Algorithm

▪ Input → Two positive integers, a and b.


▪ Output → The GCD, g, of a and b.
▪ Internal Computation
1. If a < b, exchange a and b
2. Divide a by b and get the remainder, r. If r = 0, report b as the GCD of a and b.
3. Replace a by b and replace b by r. Return to step 2.
EUCLID’S ALGORITHM
STEP 1 If n = 0, return m and stop; otherwise go to Step 2
STEP 2 Divide m by n and assign the value of the remainder to r
STEP 3 Assign the value of n to m and the value of r to n. Go to
Step 1.
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Examples:

1. What is the GCD of 1071 and 462?

Solution: a b r
1071 462 147
462 147 21
147 21 0
GDC(1071,462) = 21

2. What is the GCD of 654 and 2322?

Solution: Since a < b, swap integers such that a = 2322 and b = 654
a b r
2322 654 360
654 360 294
360 294 66
294 66 30
GDC(2322,654) = 6
66 30 6
30 6 0
IMPORTANT PROBLEM TYPES
• sorting
• searching
• string processing
• graph problems
• combinatorial problems
• geometric problems
• numerical problems
SORTING
SORTING
SEARCHING
STRING PROCESSING
A string is a sequence of characters from an alphabet.
Text strings: letters, numbers, and special characters.
String matching: searching for a given word/pattern in a text.

Examples:
(i) searching for a word or phrase on WWW or in a Word document
(ii) searching for a short read in the reference genomic sequence
GRAPH PROBLEMS
A graph is a collection of points called vertices, some of which are connected
by line segments called edges.
Modeling real-life problems
Modeling WWW
Communication networks
Project scheduling …
Examples of graph algorithms
Graph traversal algorithms
Shortest-path algorithms
Topological sorting
FUNDAMENTAL DATA STRUCTURES
LINEAR DATA STRUCTURES
LINEAR DATA STRUCTURES
ARRAY VS LINKED LIST
STACKED AND QUEUES
STACKED AND QUEUES
PRIORITY QUEUE AND HEAP
GRAPHS
GRAPHS
GRAPH REPRESENTATION
GRAPH PROPERTIES
Paths
A path from vertex u to v of a graph G is defined as
a sequence of adjacent (connected by an edge)
vertices that starts with u and ends with v.
Simple paths: All edges of a path are distinct.
Path lengths: the number of edges, or the number
of vertices – 1.
Connected graphs
A graph is said to be connected if for every pair of
its vertices u and v there is a path from u to v.
Connected component
The maximum connected subgraph of a given
graph.
GRAPHS
TREES
TREES
ROOTED TREES - LEVELS
ROOTED TREES
ORDERED TREES
ORDERED TREES
ORDERED TREES
ASK ANY QUESTION RELATED TO
OUR TOPIC FOR TODAY.

S-ar putea să vă placă și