Chapter 2 Introduction DSA
Chapter 2 Introduction DSA
AND
ALGORITHM
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
1
NO TOPIC CLO
01 A General Overview CLO1
02 Introduction to Data Structures and Algorithm CLO1
03 String Processing CLO1
04 Abstract Data Types CLO1
05 Linked list CLO1
06 Stack and Queue CLO1
07 Recursion CLO1
08 Complexity Analysis CLO2
09 Sorting and Searching techniques CLO2
10 Trees CLO2
11 Graph CLO3
12 P & NP CLO3
2
• Data are simply values or sets of values
• Data item refers to a single unit of values
• Data items that are divided into sub-items are called group
items (e.g., employee name: first name, middle
name and last name)
• Collections of data are frequently organized into a hierarchy of
fields, records, and files.
•An entity is something that has certain attributes or properties
which may be assigned values. (e.g., possible
attributes : name, age, social security number etc)
•Entities with similar attributes form entity set.
•Field is a single elementary unit of information representing
an attribute of an entity
•Record is the collection of field values
•File is the collection of records of the entities
3
Why we need data structure?
• Data structures help us to organize the data in the
computer, resulting in more efficient programs.
• An efficient program executes faster and helps minimize
the usage of resources like memory and disk.
4
What does organizing the data mean?
The data should be arranged in a way that it is easily
Accessible.
Data organization can also be for the collection of records
that can be searched, processed in any order, or modified.
Sometimes you may realize that the application is too slow
and taking more time.
There are chances that it may be due to the data structure
used, not due to the CPU speed and memory.
The choice of data structure and algorithm can make the
difference between a program running in a few seconds or
many days.
5
6
A data structure is an organized collection of data
with specific aggregate properties
Data may be organized in different ways: the
logical or mathematical model for a particular
organization of data is called the data structure.
OR
A data structure is a particular way of organizing data
in a computer so that it can used efficiently.
7
Logical or mathematical description of the structure
8
9
• A state of difficulty that needs to be resolved
• Problems exist where goals need to be attained and there is
uncertainty about solution
• A Problem is an opportunity for improvement
•A problem is the difference between your current state and
your goal state
10
• People make decisions everyday
• Examples:
Should I wear casual or formal today?
Should I watch TV or go out to cinema?
what career?
what course?
What shoes?
• Everything needs a DECISION AS A SOLUTION TO THE
PROBLEM
11
•WASTAGE OF TIME AND RESOURCES
12
1. Identify the problem
2. Understand the problem
3. Identify alternative ways to solve the problem
4. Select the best way to solve the problem from the list of
alternative solutions
5. List instructions that enable you to solve the problem
using selected solution
6. Evaluate the solution
13
• Algorithmic
Solutions that can be solved with a series of
known actions are called Algorithmic Solutions
• Heuristic
Employing a self-learning approach to the
solution of a problems is known as Heuristic
Solutions
14
• Algorithmic
Solutions that can be solved with a series of
known actions are called Algorithmic Solutions
Examples: To make a cup of coffee, To find largest
of three numbers
• Heuristic
Employing a self-learning approach to the
solution of a problems is known as Heuristic
Solutions
Examples: how to buy the best stock? , How to
play chess?
15
• Computers use algorithmic solutions
• Program – set of instructions that make up solution to a
problem
• Results – outcome of running the program
• Testing – Are the outcomes what you expected and
correct
• Documentation – two types
manual documentation
instructions telling users how to use the
program
16
• Clearly define the problem
• Analyse the problem and formulate a method to solve it
(see also .validation.).
• Describe the solution in the form of an algorithm.
• Draw a flowchart of the algorithm.
• Write the computer program.
• Compile and run the program (debugging).
• Test the program (debugging) (see also verification.).
17
• Constants
• Variables
• Operators
• Hierarchy of operations
• Data types
• Equations
• Functions
• Expressions
18
• Certain organizational tools can help us to solve
problems
• Problem Analysis Charts – a beginning analysis of the
problem
• Structure/Interactivity Charts – shows the overall
layout or structure of the solution
• IPO Chart – shows the input, the processing and the
output
• Algorithm – show the sequence of instructions
comprising the solution
• Flowcharts – graphic representations of the algorithms
19
• Problem Analysis Chart (PAC) separates the problem in 4
parts
1. Given Data: Data given in the problem or
provided by user-Data, constants, variables
2. Required Results: Requirements to produce the
output-information and format required
3. Processing required: List of processing required
– equations, or searching or sorting techniques
4. Solution alternatives: List of ideas for the
solution.
20
• Example: Create a Problem Analysis chart for calculating
the Gross pay , given the formula GrossPay = Hours *
PayRate
• Given Data
Hours
Pay Rate
• Required Results
Gross Pay
• Processing required
GrossPay = Hours * PayRate
• Solution alternatives
Define the hours worked And pay rate as
constants
Define the hours worked and pay rate as
input values
21
• This breakdowns a complex problem into simpler tasks.
• This divides your solution in modules – subtasks.
• Structure charts connect modules together to show the
interaction of processing between the modules
• Each module should contain the tasks to
finish/accomplish one function - e.g calculating results,
reading input
22
• This extends and organizes the information in the
problem analysis chart.
• It shows in more detail what data items are input,
• what processing takes place on the data and what
information will be the end result, the output.
• In the IPO chart the output is the first to be completed
and then the input and then the processing
23
24
25
• After using the structure chart and the IPO chart the next
step in organizing the solution is to for the programmer to
develop a set of instructions for the computer – called
algorithms or Psuedocode.
26
27
An algorithm is a sequence of program instructions designed to
compute a particular result
28
An algorithm is a sequence of program instructions designed
to compute a particular result.
Once an algorithm is given for a problem and decided
(somehow) to be correct, an important step is to determine
how much in the way of resources, such as time or space,
the algorithm will require.
An algorithm that solves a problem but requires a year is
hardly of any use. Likewise, an algorithm that requires
thousands of gigabytes of main memory is not (currently)
useful on most machines.
29
Pseudo Code
Basic elements of Pseudo code
Basic operations of Pseudo code
Flow Chart
Symbols used in flow charts
Examples
30
There are two commonly used tools to help
to document program logic (the algorithm).
These are
Flowcharts
Pseudocode.
Generally,
flowcharts work well for small
problems but Pseudocode is used for larger
problems.
31
Pseudo-Code is simply a numbered list of
instructions to perform some task.
32
Number each instruction
This is to enforce the notion of an ordered sequence of
operations
Furthermore we introduce a dot notation (e.g. 3.1 come
after 3 but before 4) to number subordinate operations
for conditional and iterative operations
Each instruction should be unambiguous and effective.
Completeness. Nothing is left out.
33
A Variable
Having name and value
There are two operations performed on a variable
Assignment Operation is the one in which we
associate a value to a variable.
The other operation is the one in which at any given
34
Assignment Operation
This operation associates a value to a variable.
While writing Pseudo-code you may follow your own
syntax.
Some of the possible syntaxes are:
Assign 3 to x
Set x equal to 3
x=3
35
Read Operation
In this operation we intend to retrieve the value
previously assigned to that variable. For example Set
Value of x equal to y
Read the input from user
This operation causes the algorithm to get the value of a
variable from the user.
Get x Get a, b, c
36
Some of the common symbols
used in flowcharts are shown.
With flowcharting, essential
steps of an algorithm are
shown using the shapes
above.
The flow of data between
steps is indicated by arrows,
or flowlines.
37
38
Write an algorithm and draw flowchart to calculate
equivalent resistance of circuit given below:
39
Write an algorithm and draw flowchart to calculate equivalent
resistance of circuit given below:
Pseudocode
Step 1: Input R1,R2,R3
Step 2: Re = R1+R2+R3
Step 3: Print Re
40
Write an algorithm and draw flowchart to calculate equivalent
resistance of circuit given below:
Pseudocode
Step 1: Input R1,R2,R3
Step 2: Re = R1+R2+R3
Step 3: Print Re
Draw Flowchart
41
Write an algorithm that will read the two sides of a rectangle and
calculate its area.
42
Write an algorithm that will read the two sides of a rectangle and
calculate its area.
Pseudocode
Step 1: Input W, L
Step 2: Area = W * L
Step 3: Print Area
43
Write an algorithm that will read the two sides of a rectangle
and calculate its area.
Pseudocode
Step 1: Input W, L
Step 2: Area = W * L
Step 3: Print Area
Draw Flowchart
44
Write an algorithm and draw a flowchart to convert the length in
feet to centimeter.
45
Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Pseudocode
Draw Flowchart
46
Write an algorithm and draw a flowchart that reads two
values, determines the largest value and prints the largest
value with an identifying message.
47
Write an algorithm and draw a flowchart that reads two
values, determines the largest value and prints the largest
value with an identifying message.
Pseudocode
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX = VALUE1
else
MAX = VALUE2
endif
Step 3: Print “The largest value is”, MAX
Draw Flowchart
48
Wish
You
Good Luck
49