Documente Academic
Documente Profesional
Documente Cultură
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%
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
• 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?
• 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.
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?
Solution: a b r
1071 462 147
462 147 21
147 21 0
GDC(1071,462) = 21
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.