0% found this document useful (0 votes)
2 views

Lecture 1

The document outlines the CS313 Algorithms course taught by Dr. Mohamed Eassa, covering course objectives, concepts of algorithms, and specific algorithms such as finding the maximum element and linear search. Students will learn to analyze and prove the efficiency and correctness of algorithms. The course emphasizes understanding algorithms as sequences of instructions to solve problems efficiently.

Uploaded by

k9731357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1

The document outlines the CS313 Algorithms course taught by Dr. Mohamed Eassa, covering course objectives, concepts of algorithms, and specific algorithms such as finding the maximum element and linear search. Students will learn to analyze and prove the efficiency and correctness of algorithms. The course emphasizes understanding algorithms as sequences of instructions to solve problems efficiently.

Uploaded by

k9731357
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CS313 Algorithms

Dr. Mohamed Eassa


Agenda
• Course Objectives

• Concepts of Algorithms
o Algorithm Definition
o Characteristics of Algorithms
o Algorithm Efficiency
o Analysis and Complexity of Algorithms

2
Course Objectives
• By the end of the course, the successful student will
be able to:

o Understand, explain, model, and analyze a given software


problem as an algorithm.
o Investigate whether the algorithm found is the most
efficient.
o Formulate the time order analysis for an algorithm.
o Formulate the space needs for the implementation of an
algorithm.
o Prove the correctness of an algorithm.

3
Textbook

4
Concepts of Algorithms

• The word “Algorithm” derives from the name of the


mathematician, Mohammed ibn Musa al
Khwarizmi, who was part of the royal court in
Baghdad and who lived from about 780 to 850.

5
Concepts of Algorithms

• What is an algorithm?

o A sequence of instructions describing how to solve problem

o Algorithms must be :

• Correct: For each input produce an appropriate output

• Efficient: run as quickly as possible, and use as little memory as


possible

6
Concepts of Algorithms

• What is an algorithm?

o A program is one type of algorithm


• All programs are algorithms
• Not all algorithms are programs!

o Directions to somebody’s house is an algorithm


o A recipe for cooking a cake is an algorithm
o The steps to compute the cosine of 90° is an algorithm

7
Concepts of Algorithms

• Some algorithms are easy


o Finding the largest (or smallest) value in a list
o Finding a specific value in a list

• Some algorithms are a bit harder


o Sorting a list

• Some algorithms are very hard


o Finding the shortest path between Miami and Seattle

• Some algorithms are essentially impossible

8
Algorithm 1:
Maximum element
• Given a list, how do we find the maximum element
in the list?

• To express the algorithm, we’ll use pseudo-code


o Pseudo-code is an informal high-level description of the operating
principle of a computer program or other algorithm.

o It uses the structural conventions of a programming language but is


intended for human reading rather than machine reading.

9
Algorithm 1:
Maximum element
• Algorithm for finding the maximum element in a list:

procedure max (a1, a2, …, an: integers)


max := a1
for i := 2 to n
if max < ai then max := ai

{max is the largest element}

10
Algorithm 1:
Maximum element
procedure max (a1, a2, …, an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai

max 9
7
4

a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8

i 10
9
8
7
6
5
4
3
2
11
Algorithm 1:
Maximum element

• How long does this take?

• If the list has n elements, it takes n “steps”

12
Algorithm 2: Linear search
• Given a list, find a specific element in the list
o List does NOT have to be sorted!

procedure linear_search (list, x_value)


for ( int i = 1 ; i <= n ; i++ )
if x_value == list (i)
location = i and Enf if
Else location = 0 and Enf if
End for and End procedure

{location is the subscript of the term that equals x,


or it is 0 if x is not found}

13
Algorithm 2: Linear search
procedure linear_search (a_list, x_value)
for ( int i = 1 ; i <= n ; i++ )
if x_value == a_list (i)
location = i and Enf if x 3
Else location = 0 and Enf if
location 8
End for and End procedure
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8

i 1
8
7
6
5
4
3
2
14
Algorithm 2: Linear search
procedure linear_search (a_list, x_value)
for ( int i = 1 ; i <= n ; i++ )
if x_value == a_list (i)
location = i and Enf if x 11
Else location = 0 and Enf if
location 0
End for and End procedure
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8

i 1
10
9
8
7
6
5
4
3
2
11
15
Algorithm 2: Linear search
• How long does this take?

• If the list has n elements, worst case scenario is that


it takes n “steps”

16
Questions & Answers

You might also like