1. Data Structures & Algorithm
CSC-102
Lecture 1
Introduction to Data Structures
and Algorithms
Lecturer: Syeda Nazia Ashraf 1
2. Instructor: Syeda Nazia Ashraf
• Lecturer, Computer Science in CS Department,
SMIU, Karachi
• Enrolled in PhD(CS) from Bahria University
Karachi
• M.Engg. in Computer Systems from NED
University, Karachi
• BS Computer Engg. from SSUET, Karachi
• More than 2 years of Experience in FUUAST,
Karachi
• 4 years of Experience in SMIU, Karachi
2
3. Course Outline
• Introduction to Data Structures and Algorithms
• Arrays. Multidimensional Arrays
• Linked Lists
• Doubly Linked List, Circular Link List
• Stack using Linked List
• Infix to Post Fix, Post Fix to Infix
• Queues, Queues using Array
• Priority Queue, Trees
• Application of Binary Tree, Traversing a Binary Tree
• Recursion (Pre order, In order), Level order Traversal
• Degenerate Binary Search Tree
3
7. Goals of this Course
• Learn the commonly used data structures.
• How to design good algorithms.
• Cover well-known data structures such as
arrays, linked lists, stacks, queues, trees and
graphs.
• Implement data structures and algorithms in
C language code.
7
8. •The primary objective of programming is to efficiently
process the input to generate the desired output
•We can achieve this objective in an efficient and neat
style if the input data is organized in a way to help us meet
our goal
• Data structures is nothing but ways and means of
organizing data so that it can be processed easily and
efficiently
•Data structure dictate the manner in which the data can
be processed. In other words the choice of an algorithm
depends upon the underlying data organization
Data structures
8
9. Organizing Data
▪ Any organization for a collection of records
that can be searched, processed in any
order, or modified.
▪ The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.
9
10. Efficiency
▪ A solution is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time
▪ The cost of a solution is the amount of
resources that the solution consumes.
10
11. Data Structure Philosophy
▪ Each data structure has costs and
benefits.
▪ Rarely is one data structure better than
another in all situations.
▪ A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.
11
13. • An algorithm is a set of rules for carrying out calculation either by
hand or on a machine
• An algorithm is a finite step-by-step procedure to achieve a
required result
• An algorithm is a sequence of computational steps that transform
the input into the output.
• An algorithm is a sequence of operations performed on data that
have to be organized in data structures.
• An algorithm is a systematic logical approach used to problems in a
computer
• Step wise algorithm gives us the complete overview of the steps in
detail and it can also be defined as the English language
representation of the steps performed.
Algorithm
13
14. Origin of the Term “Algorithm”
• The name derives from the title of a Latin
book: Algoritmi de numero Indorum
• That book was written by the famous 9th
century Muslim mathematician, Muhammad
ibn Musa al-Khwarizmi
• The study of algorithms began with
mathematicians and was a significant area of
work in the early years
14
15. Why Algorithms are Useful?
• Once we find an algorithm for solving a
problem, we do not need to re-discover it the
next time we are faced with that problem
• Once an algorithm is known, the task of
solving the problem reduces to following
(almost blindly and without thinking) the
instructions precisely
• All the knowledge required for solving the
problem is present in the algorithm
15
17. Why Study Algorithm
• A good understanding of Algorithm Design is a central
element to a good understanding of computer science
and computer programming.
• They apply these to various applications:
- Compilers,
- Operating Systems,
- Data bases,
- Artificial Intelligence,
- Computer Graphics and Vision,
- Networks, etc.
17
18. Properties of Algorithm
18
There are the properties of algorithm that
must have to consider in solving a certain
problem in programming:
▪ Finiteness
▪ Definiteness / Absence of Ambiguity
▪ Sequence Definition
▪ Input and Output Definition
▪ Effectiveness
▪ Scope of Definition
19. Properties of Algorithm
19
• Finiteness
– The algorithm must always terminate after a finite
number of steps.
– The execution of a programmed algorithm must
be complete after a finite number of operations
have been performed. Otherwise, we cannot
claim that the execution produces a solution.
20. Properties of Algorithm
20
• Definiteness / Absence of Ambiguity:
• Each step must be precisely defined; the actions to
be carried out must be rigorously and
unambiguously specified for each case.
• The representation of every step of an algorithm
should have a unique interpretation which also
understand by the human.
– It is convenient to deal with algorithms presented
in notational with scattered detail:
• Example:
–Pseudo code
–Flowcharts
21. Properties of Algorithm
21
• Sequence of Definition
– The sequence in which the steps of the algorithm
are to carried out should be clearly specified.
– In algorithmic specifications, the instructions are
performed from top to button, unless the
instruction themselves otherwise specified.
22. Properties of Algorithm
22
• Input and Output Definition
– Inputs – are the data items that is presented in
the algorithm.
– An algorithm has zero or more inputs, taken from
a specified set of objects
– Outputs – are the data items presented to the
outside world as the result of the execution of a
program based on the algorithm.
– An algorithm has one or more outputs, which
have a specified relation to the inputs.
23. Properties of Algorithm
23
• Effectiveness
– All operations to be performed must be
sufficiently basic that they can be done exactly
and in finite length.
– It consists of basic instructions that are realizable.
This means that the instructions can be performed
by using the given inputs in a finite amount of
time.
– The instructions of an algorithm may order the
computer only to perform tasks that is capable of
carrying out.
24. Properties of Algorithm
24
• Scope Definition
– An algorithm applies to the following:
• Specific problem or class of problem
• The range of inputs has to be predefined
• The range determines the generality of the algorithm.
25. How to express/specify an Algorithm?
25
• Algorithms can be expressed in many kinds of
notation or methods, including:
– Natural language: usually verbose and ambiguous
– Pseudo-Code: also avoids most issues of
ambiguity; vaguely resembles common elements
of programming languages; no particular
agreement on syntax
– Flowcharts: avoid most (if not all) issues of
ambiguity; difficult to modify w/o specialized
tools; largely standardized
– Programming Language: tend to require
expressing low-level details that are not necessary
for a high-level understanding
26. Pseudo code
• High-level description of an algorithm
• More structured than English prose
• Less detailed than a program
• Preferred notation for describing algorithms
• Hides program design issues
• In this type of algorithm, special keywords
(pseudo words) are used to represent the
steps of a given task.
26
27. What is Pseudocode…?
• “Pseudo” means imitation or false and “code”
refers to the instructions written in a
programming language.
• Pseudocode is another programming analysis tool
that is used for planning a program.
• Pseudocode is also called Program Design
Language (PDL).
• Pseudocode is the statement in plain English
which may be translated later into a
programming language (program).
• Pseudo code is an intermediary between an
algorithm and implemented program.
27
28. What is Pseudocode…?
• By wikipedia definition:
–Pseudocode is a compact and informal
high-level description of a computer
programming algorithm that uses the
structural conventions of some
programming language, but is intended for
human reading rather than machine
reading.
28
29. Pseudocode Details
• Control flow
– if … then … [else …]
– while … do …
– repeat … until …
– for … do …
– Indentation replaces braces
• Method declaration
Algorithm method (arg [, arg…])
Input …
Output …
• Method call
var.method (arg [, arg…])
• Return value
return expression
• Expressions
Assignment
(like = in Java)
= Equality testing
(like == in Java)
n2 Superscripts and other
mathematical formatting
allowed
29
34. Example 1
Write an algorithm in pseudo code that finds
the average of two numbers
Solution
Average of Two
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Algorithm : Average of two
34
35. Example 2
Write an algorithm to change a numeric grade to a
pass/no pass grade.
Pass/No Pass Grade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “no pass”
End if
2. Return the grade
End
Algorithm : Pass/no pass Grade
35
36. Example 3 Write an algorithm to change a numeric grade
to a letter grade.
Letter Grade
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Algorithm : Letter grade
Continues on the next slide 36
37. 3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if
Algorithm : Letter grade (continued)
37
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
38. Example 4 Write an algorithm to find the largest of a set
of numbers. You do not know the number of
numbers.
Find Largest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the integer
End if
End while
3. Return Largest
End
Algorithm : Find largest
38
39. Example 5 Write an algorithm to find the
largest of 1000 numbers.
Find Largest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
Algorithm : Find largest of 1000 numbers
39
40. Psuedocode-Another Example
CheckInBook(library, book, fineSlip)
Function: Checks in a book
Input: A Record of library,
A book as String,
Output: fineSlip as String,
updated record of library
precondition: book was checked out of this
library, book is presented at the
check-in-desk
postcondition: fineSlip is issued if the book is overdue,
contents of the library are the original
contents of book
40
41. • Algorithm:
1. Examine due date to see whether the book is late
2. if the book is late
a. calculate fine
b. Issue fineSlip
3. Update library records to show that the book has
been returned
4. check reserve list to see if someone is waiting for
the book
5. If book is on reserve list
a. put the book on the reserve shelf
6. Else
a. Replace the book on the proper shelf,
according to the library shelf arrangement
scheme
41
42. Rules for Pseudocode
– Write only one statement per line.
– Capitalized initial keyword.
– Indent to show hierarchy.
– End multi-line structures.
– Keep statement language independent.
1. Write only one statement per line.
– Each statement in your pseudocode should
express just one action for the computer.
– If the task list is properly drawn, then in most
cases each task will correspond to one line of
pseudocode. 42
44. Rules for Pseudocode
2. Capitalized initial keyword.
– In the example above, READ and WRITE are in
caps.
– There are just a few keywords we will use:
• READ, WRITE, IF, ELSE, ENDIF, WHILE,
ENDWHILE, REPEAT, UNTIL
Rules for Variable Names
• Begin with lowercase letter
• Contain no spaces
• Additional words begin with capital
• Unique names within code
• Consistent use of names
44
45. Rules for Pseudocode
3. Indent to show hierarchy.
– We will use a particular indentation pattern in
each of the design structures:
• SEQUENCE: keep statements that are “stacked” in
sequence all starting in the same column.
• SELECTION: indent the statements that fall inside the
selection structure, but not the keywords that form the
selection
• LOOPING: indent the statements that fall inside the
loop, but not the keywords that form the loop
45
47. Rules for Pseudocode
4. End multi-line structures.
▫ All the initial keyword must always in line with the last or
end of the structure.
Example:
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
5. Keep statement language independent.
▫ Resist the need to write in whatever language you are most
comfortable with. There may be special features available in
the language you plan to eventually write the program in; if
you are SURE it will be written in that language, then you can
use the features. If not, then avoid using the special features.
47
48. Standard for good pseudocode…
• These are follows:
▫ Number each instruction.
This is to enforce the notion, “well-ordered
collection of ... operations.”
▫ Each instruction should be unambiguous.
It means the computing agent, in this case the
reader, should be capable of carrying out the
instructions. And also, each instruction should be
effectively computable (do-able).
▫ Completeness.
Nothing should be left out.
48
49. Common Action Keywords
Several keywords are often used to indicate common
input, output, and processing operations.
Input: READ, OBTAIN, GET
e.g. Read student name (from the student file)
Get system date (from the computer system).
Read student ID number
Get order.
Output: WRITE, PUT, PRINT, DISPLAY, SHOW
e.g. Print "End of the Output"
Write student record to master file
Put out name, address and post code
Output grade; Display "an input error occurred, please re-enter“
Add one: INCREMENT
49
50. Perform Arithmatic: COMPUTE, CALCULATE,
DETERMINE
e.g. Divide total_score by student_count
class_average = total_score / student_count
Compute C = (F – 32) * 5 / 9
Calculate the number of registrations
The + symbol means to add: 2 + 3 = 5.
The – symbol means to subtract: 2 – 3 = -1.
The * symbol means to multiply: 2 * 3 = 6.
The / symbol means to divide: 2 / 3 = .66667
The ^ symbol means to the power of. 2 ^ 3 = 8.
50
51. 51
Assign variable or memory location: SET, INITIALIZE
(To assign a value as a result of some processing, the symbol
"=" is used)
To keep a piece of information for later use, the verbs SAVE
or STORE are used.
e.g. Initialize total_score to 0: total_score = 0
Set student_count to 0: student_count = 0
total_score = total_score + score 1
student_count = student_count + 1
class_average = total_score / student_count
store class_average in class_average_quiz1
52. Compare two variables and select one of the two alternative
choices: IF, THEN, ELSE and ENDIF are the words used to do this.
E.g.
If student is part_time Then
add 1 to part_time_count
Else
add 1 to full_time_count
EndIf
Repeat a set of actions: That is the control structure interation,
more commonly known as a loop. DO WHILE ----END DO , (while---
do) ,for ---- do , repeat----until are the words used in pseudocode.
E.g.
Do While student_total <30
Read student record
Print student name, address
Add 1 to student_total
End Do 52
53. More on Formatting and Conventions in Pseudocoding
•INDENTATION in pseudocode should be identical to its
implementation in a programming language. Try to indent at least
four spaces.
•As noted above, the pseudocode entries are to be cryptic, AND
SHOULD NOT BE PROSE. NO SENTENCES.
•Do not include data declarations in your pseudocode.
•But do cite variables that are initialized as part of their declarations.
E.g. "initialize count to zero" is a good entry.
Function Calls, Function Documentation, and Pseudocode
Calls to Functions should appear as: Call FunctionName (arguments:
field1, field2, etc.)
Returns in functions should appear as: Return (field1)
Function headers should appear as: FunctionName (parameters:
field1, field2, etc. )
53
54. Logical Structure of Pseudocode
• Pseudocode is made up of the following logic
structures that have been proved to be
sufficient for writing any computer program:
–Sequence Logic
–Selection Logic
–Iteration Logic
54
61. Working with Fields
Calculation Symbols
• To symbolize the arithmetic operators we use these
symbols:
– Note: There is a precedence or hierarchy implied in this
symbols.
61
62. Working with Fields
Selection Symbols
• When we have to make a choice between
actions, we almost always base that choice on a
test.
• There is a universally accepted set of symbols
used to represent these phrases:
62
64. What is the difference between Algorithm and
Pseudocode?
• An algorithm is a well defined sequence of steps that provides
a solution for a given problem, while a pseudocode is one of
the methods that can be used to represent an algorithm.
• While algorithms can be written in natural language,
pseudocode is written in a format that is closely related to
high level programming language structures.
• But pseudocode does not use specific programming language
syntax and therefore could be understood by programmers
who are familiar with different programming languages.
• Additionally, transforming an algorithm presented in
pseudocode to programming code could be much easier than
converting an algorithm written in natural language.
64