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

DSA Chapter 0

This document provides information about a course on data structures and algorithms. It outlines course details like credit hours, instructor information, class schedule, learning outcomes, course contents covering topics like sorting, searching, linked lists, and trees. It also describes teaching strategies, assessment criteria, required software and hardware, references, prerequisites, and details about laboratory classes including pre-lab, in-lab and post-lab exercises.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

DSA Chapter 0

This document provides information about a course on data structures and algorithms. It outlines course details like credit hours, instructor information, class schedule, learning outcomes, course contents covering topics like sorting, searching, linked lists, and trees. It also describes teaching strategies, assessment criteria, required software and hardware, references, prerequisites, and details about laboratory classes including pre-lab, in-lab and post-lab exercises.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

1

DATA STRUCTURE AND


ALGORITHMS
COURSE CODE: INSY2041
ECTS: 5
LECTURE : 48 HRS(3HRS/WEEK)
LAB : 32 HRS (2HRS/WEEK)
TUT : 16 HRS(1HR/WEEK)
INSTRUCTOR INFORMATION
NAME: ANDARGACHEW A.
OFFICE LOCATION : ESHETU CHOLE BUILDING #319
EMAIL: [email protected]

3/18/2024 Outline
Course Information
2

 Academic Year: 2013E.C.


 Semester: I
 Class Schedule:
Lec Lab
Section 1 Section 2
W1,2,Th1
M5,6 M7,8

 Class Room: -
Lecture Room Lab Room
311 510

3/18/2024
Course Description
3

 This course aims to introduce you some basic data


structures and algorithms which are to be used as tools
in designing solutions to problems.
 It will make students familiar with the specification,
usage, implementation and analysis of these data
structures and algorithms.
 Introduction of Data Structures, Measuring Complexity,
Complexity of Algorithm Big-O Notation. Simple and
advanced Sorting and Searching Algorithms.
 Abstract Data Types, Structures, Pointers, Arrays, Linked Lists,
Stacks, Queues, Trees.
 Laboratory exercises are dedicated to practice the basics
on concepts on data structures.
3/18/2024
Learning Outcome
4

 Explain the basic techniques for the design and


analysis of efficient Algorithm;
 Determine when and how to use the various data
structures including Linked lists, Queues, Stacks,
Binary trees and Search trees.
 Design algorithms to solve real-life problems using
the tools introduced Analyze your solution, and
efficiently implement your solution.
 Apply data structures and algorithms that are
frequently used in information processing
3/18/2024
Course Contents
5

1. Review of C++ concepts


2. Complexity analysis
3. Sorting and Searching algorithm
4. Linked Lists
5. Stack and Queues
6. Recursion
7. Binary Trees
8. Hashing

3/18/2024
Teaching Strategy
6

 The course will be delivered in the form of


 Lectures

 Demonstration

 Student presentations
 Group discussions, and

 Individual and group project works.

3/18/2024
Assessment Criteria
7 Assessment Forms % of credit Given Submission
allotted week week

Lecture 100%

• Participation and Attendance


• Quizzes (All the quizzes will be unannounced
and there will be no makeup quizzes)
• Assignments
• Test
• Final examination
Practice 100%

• Participation and Attendance


• Lab assignment
• Lab Exam
• Projects 3/18/2024
Required SW/HW
8

 Hardware
 Desktop Computer or Laptop
 Software
 Code Blocks C++ compiler or other C++ compilers

3/18/2024
References
9

 Textbook
 “Data Structures and Algorithms in C++” by A. Drozdek
(Brooks/Cole, 2013)
 Other
 “Data Structures and Algorithms with Object-Oriented
Design Patterns in C++” by B. R. Preiss.
 Any related Data Structures and Algorithms books.

3/18/2024
Prerequisite skill and knowledge
10

 Understanding a given problem


1. Obtain a description of the problem.
2. Analyze the problem.
3. Develop a high-level algorithm.
1. Refine the algorithm by adding more detail.
2. Review the algorithm.
 Programming skill
 C++ (Course programming language)
 Java
 Any programming language
 Learn from your experience
 EXPERIMENT. FAIL. LEARN. REPEAT

3/18/2024
Laboratory Class
11

 Each laboratory includes three parts:


1. Pre-lab exercises
2. In-lab exercises and
3. Post-lab exercises.

3/18/2024
Pre-lab
12

 The Pre-lab exercise is a homework assignment that


links the lecture with the laboratory period.
 In the Pre-lab, students explore and create on their own and
at their own pace.
 A Pre-lab assignment–including a review of the
relevant lecture and textbook materials–typically
takes an evening to complete (that is, two to three
hours).
❖ You should complete the Pre-lab exercise before coming
to the lab.
 Your work need not be perfect, but your effort must be
real (roughly 80% correct).

3/18/2024
In-lab
13

 Each In-lab consists of one, two or three exercises, and


each exercise has a distinct role.
 We use the first hour of the laboratory period to
resolve any problems the students might have
experienced in completing the Pre-lab exercises.
 The intention is to give constructive feedback so that students
leave the lab with working Pre-lab software - a significant
accomplishment on their part.
 During the second hour, the students complete one/two
of the In-lab exercises to reinforce the concepts learned
in the Pre-lab.

3/18/2024
Post-lab
14

 The last phase of each laboratory is a homework


assignment to be done following the laboratory
period.
 In the Post-lab, students analyze the efficiency or utility of
a given data structure or algorithm.
 Each Post-lab exercise should take roughly thirty minutes to
complete.
❖ After the lab, the students complete one of the Post-lab
exercises and turn it in during their next lab period.
 Late submission is not acceptable

3/18/2024
15

BASIC AND OVERVIEW

DATA STRUCTURE &


ALGORITHM
3/18/2024 DSA- CH-0
DATA STRUCTURE
16

 A systematic way to organize data in


order to use it EFFICIENTLY.
 Data structure terms
 Interface
◼ Represents the set of operations that a data structure
supports. (traversing, searching, insertion, deletion, sorting,
merging)
 Implementation
◼ The internal representation of a data structure.

3/18/2024
17 3/18/2024
18 3/18/2024
Characteristics of a Data Structure
19

 Correctness − Data structure implementation should


implement its interface correctly.
 Time Complexity − Running time or the execution
time of operations of data structure must be as
small as possible.
 Space Complexity − Memory usage of a data
structure operation should be as little as possible.

3/18/2024
Why Data Structure?
20

 As applications are getting complex and data rich,


there are three common problems that applications face
now-a-days.
 Data Search − Consider an inventory of 1 million(106) items
of a store. If the application is to search an item, it has to
search an item in 1 million(106) items every time slowing
down the search. As data grows, search will become slower.
 Processor speed − Processor speed although being very
high, falls limited if the data grows to billion records.
 Multiple requests − As thousands of users can search data
simultaneously on a web server, even the fast server fails
while searching the data.

3/18/2024
ALGORITHM
21

 It is a step-by-step procedure, which defines a set


of instructions to be executed in a certain order to
get the desired output.
 Algorithmsare generally created independent of
underlying languages, i.e. an algorithm can be
implemented in more than one programming language.

3/18/2024
22

 From the data structure point of view, following are


some important categories of algorithms −
 Search − Algorithm to search an item in a data
structure.
 Sort − Algorithm to sort items in a certain order.
 Insert − Algorithm to insert item in a data structure.
 Update − Algorithm to update an existing item in a
data structure.
 Delete − Algorithm to delete an existing item from a
data structure.

3/18/2024
How to Write an Algorithm?
23

 There are no well-defined standards for writing algorithms.


Rather, it is problem and resource dependent.
 Algorithms are never written to support a particular
programming code.
◼ As we know that all programming languages share basic code
constructs like loops (do, for, while), flow-control (if-else), etc. These
common constructs can be used to write an algorithm.
 We write algorithms in a step-by-step manner, but it is not
always the case.
 Algorithm writing is a process and is executed after the
problem domain is well-defined.
 That is, we should know the problem domain, for which we are
designing a solution.

3/18/2024
Characteristics of an Algorithm
24

 Not all procedures can be called an algorithm. An


algorithm should have the following characteristics −
 Unambiguous − Algorithm should be clear and unambiguous.
◼ Each of its steps (or phases), and their inputs/outputs should be clear
and must lead to only one meaning.
 Input − An algorithm should have 0 or more well-defined inputs.
 Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
 Finiteness − Algorithms must terminate after a finite number of
steps.
 Feasibility − Should be feasible with the available resources.
 Independent − An algorithm should have step-by-step directions,
which should be independent of any programming code.
3/18/2024
Algorithm Examples
25

 Algorithm to add two numbers


 Algorithm to find the largest among three numbers
 Algorithm to find all the roots of quadratic equation
 Algorithm to find the factorial
 Algorithm to check prime number
 Algorithm of Fibonacci series

3/18/2024
Example
26

 Problem − Design an algorithm to add two numbers


and display the result.
 step 1 − START
 step 2 − declare three integers a, b & c

 step 3 − define values of a & b

 step 4 − add values of a & b

 step 5 − store output of step 4 to c

 step 6 − print c

 step 7 − STOP

3/18/2024
27

 Algorithms tell the programmers how to


code the program. Alternatively, the
algorithm can be written as −
 step 1 − START ADD
 step 2 − get values of a & b

 step 3 − c ← a + b

 step 4 − display c

 step 5 − STOP

3/18/2024
28

 We design an algorithm to get a solution of a given


problem. A problem can be solved in more than one
ways.

3/18/2024
Algorithm Analysis
29

 Efficiency of an algorithm can be analyzed at two


different stages, before implementation and after
implementation. They are the following −
 A Priori Analysis − This is a theoretical analysis of an
algorithm.
◼ Efficiency of an algorithm is measured by assuming that all other
factors, for example, processor speed, are constant and have no
effect on the implementation.
 A Posterior Analysis − This is an empirical analysis of an
algorithm.
◼ The selected algorithm is implemented using programming
language. This is then executed on target computer machine.
◼ In this analysis, actual statistics like running time and space
required, are collected.

3/18/2024
Algorithm Complexity
30

 Suppose X is an algorithm and n is the size of input


data, the time and space used by the algorithm X are
the two main factors, which decide the efficiency of X.
 Time Factor − Time is measured by counting the number of
key operations such as comparisons in the sorting algorithm.
 Space Factor − Space is measured by counting the
maximum memory space required by the algorithm.
◼ The complexity of an algorithm f(n) gives the running time and/or
the storage space required by the algorithm in terms of n as the
size of input data.

3/18/2024
Introduction to flowchart and pseudo-code
31

 In the problem-solving phase of computer


programming, you will be designing algorithms.
 This means that you will have to be conscious of the
strategies you use to solve problems in order to apply
them to programming problems.
 These algorithms can be designed though the use
of flowcharts or pseudo-code.

3/18/2024
Flowcharts
32

 Flowcharting is a tool developed in the computer


industry, for showing the steps involved in a
process.
 A flowchart is a diagram made up of boxes,
diamonds and other shapes, connected by arrows
- each shape represents a step in the process, and
the arrows show the order in which they occur.
 Flowcharting combines symbols and flow-lines, to
show figuratively the operation of an algorithm

3/18/2024
33

 Flowcharting Symbols
 There are 6 basic symbols commonly used in
flowcharting of programming language:
◼ Terminal,Process, input/output, Decision, Connector and
Predefined Process.
 This is not a complete list of all the possible
flowcharting symbols, it is the ones used most often in
the structure of programming language.

3/18/2024
34 3/18/2024
35

 General Rules for flowcharting


1. All boxes of the flowchart are connected with Arrows.
(Not lines)
2. Flowchart symbols have an entry point on the top of the
symbol with no other entry points. The exit point for all
flowchart symbols is on the bottom except for the Decision
symbol.
◼ The Decision symbol has two exit points; these can be on the
sides or the bottom and one side.
3. Generally a flowchart will flow from top to bottom.
However, an upward flow can be shown as long as it does
not exceed 3 symbols.

3/18/2024
36

4. Connectors are used to connect breaks in the flowchart.


Examples are:
◼ From one page to another page.
◼ From the bottom of the page to the top of the same page.
◼ An upward flow of more than 3 symbols
5. Subroutines and Interrupt programs have their own and
independent flowcharts.
6. All flow charts start with a Terminal or Predefined Process
(for interrupt programs or subroutines) symbol.
7. All flowcharts end with a terminal or a contentious loop.

3/18/2024
Exercise 1:
37

 Print the following text


 Hello World
 Hello Ethiopia

 Hello Addis Ababa

3/18/2024
38 3/18/2024
Exercise:2
39

 Add 10 and 20
 To solve this problem we will take a variable sum and
set it to zero.
 Then we will take the two numbers 10 and 20 as
input.
 Next we will add both the numbers and save the
result in the variable sum i.e., sum = 10 + 20.
 Finally, we will print the value stored in the variable
sum.

3/18/2024
40 3/18/2024
Exercise 3:
41

 Print Hello World 10 times


 Thisproblem is also solved using the loop concept. We
take a variable count and set it to zero. Then we print
"Hello World" and increment count by 1.

3/18/2024
42 3/18/2024
PSEUDO-CODE
43

 Pseudo-code is a generic way of describing an


algorithm without use of any specific programming
language syntax.
 It
models and resembles real programming code, and is
written at roughly the same level of detail.
 Pseudo-code, by nature, exists in various forms,
although most borrow syntax from popular
programming languages (like C, Lisp, or FORTRAN).
 Natural language is used whenever details are
unimportant or distracting.

3/18/2024
44

 In the algorithm design, the steps of the algorithm are


written in free English text and, although brevity is
desired, they may be as long as needed to describe the
particular operation.
 The steps of an algorithm are said to be written in
pseudo-code.
 The following programming structure will be covered
1. Sequence structures
2. Selection and
3. Iteration or Repetition.
3/18/2024
45

 The sequence structure


 This structure is the most elementary structure.
 The sequence structure is a case where the steps in an
algorithm are constructed in such a way that, no
condition step is required.
 The sequence structure is the logical equivalent of a
straight line.

3/18/2024
46

 Example:1
 Suppose you are required to design an algorithm for
finding the average of six numbers, and the sum of the
numbers is given.
 The pseudo-code will be as follows
◼ Start
◼ Get the sum
◼ Average = sum / 6
◼ Output the average
◼ Stop

3/18/2024
47

 Decision Structure or Selection Structure


 Itis a case where in the algorithm, one has to make a
choice of two alternatives by making decision
depending on a given condition.
 Selection structures are also called case selection
structures when there are two or more alternatives to
choose from.

3/18/2024
48

 The selection requires the following


 Choose alternative actions as a result of testing a
logical condition
 Produce code to test a sequence of logical tests

 All computer languages provide a means of


selection.
 Usually it is in the form of If statement and our pseudo-
code is no exception to this.
 We will use the if statement together with logical
operators to test for true or false as shown below.

3/18/2024
49

 Example 2:
 The following shows how the selection control structure is
used in a program where a user chooses the options for
multiplying the numbers or adding them or subtracting.
◼ Use variables: choice, of the type character; ans, number1,
number2, of type integer
◼ display “choose one of the following”
◼ display “m for multiply”
◼ display “a for add”
◼ display “s for subtract”
◼ accept choice
◼ display “input two numbers you want to use”
◼ accept number1, number2

3/18/2024
50

◼ if choice = m then ans = number1 * number2


◼ if choice = a then ans = number1 + number2
◼ if choice = s then ans = number1 - number2
◼ display ans

3/18/2024
51

 Repetition or Iteration Structure


 Thisstructure causes the certain steps to be repeated.
 The Repetition structure can be implemented using
◼ Repeat Until Loop
◼ The While Loop
◼ The For Loop

3/18/2024
52

 The Repeat Until loop


 The syntax is
REPEAT
A statement or block of statements
UNTIL a true condition

3/18/2024
53

 The WHILE loop


 The second type of iteration we will look at is the while
iteration.
 This type of conditional loop tests for terminating
condition at the beginning of the loop.
 In this case no action is performed at all if the first test
causes the terminating condition to evaluate as false.
 The syntax is
WHILE (a condition is true)
A statement or block of statements
ENDWHILE

3/18/2024
54

 The FOR Loop


 The third type of iteration, which we shall use when the
number of iterations is known in advance, is a for loop.
 This, in its simplest form, uses an initialization of the variable
as a starting point, a stop condition depending on the value
of the variable.
 The variable is incremented on each iteration until it reaches
the required value. The pseudo-code syntax will be:
FOR (starting state, stopping condition, increment)
Statements
ENDFOR

3/18/2024
55

 Example 3:
A program segment to print out each character typed
at a keyboard until the character ‘q’ is entered.
◼ WHILE letter <> ‘q’
◼ ACCEPT letter
◼ DISPLAY “The character you typed is”, letter
◼ ENDWHILE

3/18/2024
Exercise: Flowchart & Pseudo-code
56

1. Design an algorithm and the corresponding flowchart


for finding the sum of the numbers 2, 4, 6, 8, …, n
2. Using flowcharts, write an algorithm to read 100
numbers and then display the sum.
3. Write an algorithm to read two numbers then display
the largest.
4. Write an algorithm to read two numbers then display
the smallest
5. Write an algorithm to read three numbers then
display the largest.
6. Write an algorithm to read 100 numbers then display
the largest.
3/18/2024
57

Questions?
3/18/2024
58

Thank You
3/18/2024

You might also like