lec 1 and 2
lec 1 and 2
AND ALGORITHMS
Electrical Engineering
قسم الهندسة الكهربية
وحدة ضمان الجودة Department
2- Course Aims
The aims of this course are:
1. Familiarizing the students with algorithm definition and evaluation methods.
2. Mastering the common data structures, common sort and search algorithms.
3. Solving problems using data structures such as arrays, lists, stacks, queues, binary trees, binary
search trees, and graphs and writing programs for these solutions.
1
3. Implement data structures for different applications. (PLO 2)
4. Analyze, develop, implement, evaluate, and test basic algorithms for sorting and searching. (PLO 2)
5. Choose the appropriate data structure and algorithm design method for a specific
(PLO 15)
application.
6. Design and apply algorithms using data structures for various real-life software problems. (PLO 17)
5- Course Contents
Data types and methods of representation - structures files - the representation of data structures
in the storage media and memory - lists - stacks - queues - linear and sequential memory - trees -
algorithms to search and order - and methods of modification and analysis - project .
Experiments (Computer Lab):
Lists implementation- Queues implementation- Trees implementation- Search algorithms for
Lists, Queues and Trees- Sorting algorithms for Lists, Queues and Trees.
Practical
Tutorial
Course Content
1 2 3 4 5 6
Overview of Data Structures
1 3 2 0
Introduction to algorithm definition and complexity
2 3 2 0
Arrays and Lists.
3 3 1 1
Lab: Array implementation.
Arrays and Lists.
4 3 1 1
Lab: Array implementation.
Stacks and Queues
5 3 1 1
Lab: Stack implementation.
Stacks and Queues
6 3 1 1
Lab: Stack implementation.
Sort Algorithms
7 3 1 1
Lab: Sort algorithms
Mid-Term Exam 8
Sort Algorithms
9 3 1 1
Lab: Sort algorithms
Search Algorithms
10 3 1 1
Lab: Serach algorithm
2
Trees
11 3 1 1
Lab: Tree implementation
Trees
12 3 1 1
Lab: Tree implementation
Graph
13 3 1 1
Lab: Graph implementation
Graph
14 3 1 1
Lab: Graph implementation
(week) 1 2 3 4 5 6
Written Exam (Final Term) 3hr 16 100 66.7%
Written Exam (Mid-Term) 90 m 8 15 10.0%
Oral / Practical Exam 30 m 15 25 16.7%
In class evaluation (quizzes) 5 3.3%
Self-Learning (Mini Projects) 5 3.3%
During the Term
0.0%
0.0%
Total Marks 150 100% 100%
9- List of References
- Course Notes
o Power Point of Instructor
3
o Lecture Notes
Essential Books:
1- Marcin Jamro,"C# Data Structures and Algorithms", Packt Publishing Ltd, 2018.
Recommended Books:
1. Michael McMillan, "Data Structures and Algorithms using C#", Cambridge University Press 2012.
Periodical Research:
https://www.ekb.eg/ar/home
4
Chapter 1
Introduction to Data Structures
DATA STRUCTURE
• A data structure is a way for organizing and storing data in memory so that it can be used efficiently
and it will allow the most efficient algorithm to be used.
• A well-designed data structure allows a variety of critical operations to be performed, using as few
resources, both execution time and memory space, as possible.
• To develop a program of an algorithm, we should select an appropriate data structure for that algorithm.
• Algorithm + Data structure = Program
• A data structure as a logical concept that must address two fundamental concerns:
• 1. First, how the data will be stored.
• 2. Second, what operations will be performed on it.
TYPES OF DATA STRUCTURES
• Algorithm is a method of representing the step by step procedure for solving a problem.
• It is a method of finding the right answer to a problem or to a different problem by breaking
the problem into simple cases.
• Algorithm is represented by pseudocode or flowchart
• Every algorithm must satisfy the following criteria:
❖ Input, Output, Definiteness, Finiteness and Effectiveness
PROBLEM SOLVING STEPS
PRACTICAL ALGORITHM DESIGN ISSUES
• The three basic design goals that one should strive for in a program are:
1. Try to save Time : (Time complexity).
2. Try to save Space: (Space complexity)
3. Try to save Face: by preventing the program from locking up or generating reams
of garbled data.
PERFORMANCE OF A PROGRAM
• The performance of a program is the amount of computer memory and time needed to run a program:
1.Time Complexity: is the amount of computer time it needs to run to completion.
2. Space Complexity is the amount of memory it needs to run to completion.
• The space need by a program has the following components:
a. Instruction space: Instruction space is the space needed to store the compiled version of the
program instructions.
b. Data space: Data space is the space needed to store all constant and variable values.
c. Environment stack space: The environment stack is used to save information needed to resume
execution of partially completed functions.
• Instruction Space: The amount of instructions space that is needed depends on factors such
as:
• The compiler used to complete the program into machine code.
• The compiler options in effect at the time of compilation
• The target computer.
• Data space has two components:
• Space needed by constants and simple variables in program.
• Space needed by dynamically allocated objects such as arrays and class instances.
ALGORITHM COMPLEXITY
• Asymptotic notations are the mathematical notations used to describe the running time of
an algorithm when the input tends towards a particular value or a limiting value.
• There are mainly three asymptotic notations: Theta notation, Omega notation and Big-O
notation.
• Theta Notation (Θ-notation) - average case
❖Theta notation encloses the function from above and below. Since it represents the upper and
the lower bound of the running time of an algorithm, it is used for analyzing the average case
complexity of an algorithm.
• Omega Notation (Ω-notation) - best case
❖Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best
case complexity of an algorithm. For any value of n, the minimum time required by the algorithm is given by
Omega Ω(f(n)).
• Big-O Notation (O-notation) - worst case
❖Big-O notation represents the upper bound of the running time of an algorithm. Thus, it gives the worst case
complexity of an algorithm. It is widely used to analyze an algorithm as we are always interested in the worst
case scenario. For any value of n, the running time of an algorithm does not cross time provided by O(f(n)).
• Developers typically solve for the worst case scenario, Big-O, because you’re not expecting your algorithm
to run in the best or even average cases when calculating time complexity of an algorithm.
ORDER-OF-MAGNITUDE ANALYSIS AND BIG O
NOTATION
• Ignoring constant factors: O(c f(n)) = O(f(n)), where c is a constant; e.g. O(20 n3) = O(n2)
• Ignoring smaller terms: If a<b then O(a+b) = O(b), for example O(n2 +n)= O(n2)
• n and log n are "bigger" than any constant, from an asymptotic view (that means for large
enough n). So if k is a constant, an O(n + k) algorithm is also O(n), by ignoring smaller terms.
Similarly, an O(log n + k) algorithm is also O(log n).
• Another consequence of the last item is that an O(n log n + n) algorithm, which is O(n(log n +
1)), can be simplified to O(n log n).
CALCULATING THE RUNNING TIME OF A
PROGRAM
• Example 1:
• Let’s consider a short piece of source code:
• If y, z are scalars, this piece of code takes a constant amount of time, which we write as O(1).
• O(1) means some constant, it might be 5, or 1 or 1000.
• Auxiliary Space: O(1)
• Example2:
• This loop will run exactly n times, and because the inside of the loop takes constant time, the total running time is
proportional to n.
• Time Complexity: O(n).
• the code is said to have linear running time.
• Auxiliary Space: O(1)
• Example 4:
• Analysis for nested for loop
• The outer for loop executes n times, while the inner loop executes n times for every execution of the
outer loop.That is, the inner loop executes nxn = n2 times.
• so the running time of the code is,Time Complexity, : O(n2) steps.
• This piece of code is said to have quadratic running time.
• Auxiliary Space: O(1)
• Example 5: