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

lec 1-ds algo

The document outlines a course on Data Structures and Algorithms (CS-102) taught by Syeda Nazia Ashraf, covering topics such as arrays, linked lists, stacks, queues, trees, and algorithm complexity. It includes a detailed course outline, grading criteria, textbook references, and the goals of the course, emphasizing the importance of understanding data structures and algorithms in computer science. The document also discusses algorithm design, performance analysis, and the significance of selecting appropriate data structures for efficient programming.

Uploaded by

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

lec 1-ds algo

The document outlines a course on Data Structures and Algorithms (CS-102) taught by Syeda Nazia Ashraf, covering topics such as arrays, linked lists, stacks, queues, trees, and algorithm complexity. It includes a detailed course outline, grading criteria, textbook references, and the goals of the course, emphasizing the importance of understanding data structures and algorithms in computer science. The document also discusses algorithm design, performance analysis, and the significance of selecting appropriate data structures for efficient programming.

Uploaded by

hamza ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures & Algorithm

CS-102

Lecture 1
Introduction to Data Structures
and Algorithms

Lecturer: Syeda Nazia Ashraf 1


Instructor: Syeda Nazia Ashraf
• Lecturer, Computer Science in CS Department,
SMIU, Karachi
• M.Engg. in Computer Systems from NED
University, Karachi
• BS Computer Engg. from SSUET, Karachi
• More than 2 years Teaching Experience in
FUUAST, Karachi
• One year Teaching Experience in SMIU, Karachi

2
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
Course Outline (Cont.)
• Balanced Binary Tree, Expression Tree, Complete Binary Tree
• Heap
• Hashing
• Sorting Algorithms (Selection, Insertion, Bubble)
• NlogN Algorithms (Divide and Conquer, Merge Sort, Quick
Sort)
• AVL Trees

• AVL Rotations
• AVL Rotations Insert Operation
• AVL Rotations Delete Operation
• Algorithm Complexity 4
Grading
• Quiz & Assignment = 20
• Lab = 20
• Midterm = 20
• Final Exam = 40
• Course Web Page:
http:// lms.smiu.edu.pk
• Email : [email protected]

5
Text Book:
Programme Design with Pseudo-code, Bailey and
Lundgaard, Brooks/Cole Publishing, 1988

Reference Books:
• Simple Programme Design: A step-by-step
approach, 4/e, Lesley Anne Robertson, ISBN: 0-
619-16046-2 © 2004.
• Theory and Problems of Data Structures, Schaum’s
Outline Series
• Data Structures and their Implementations, Robert
J. Baron, Linda G. Shapiro
6
Goals of this Course

• Learn the commonly used data structures.


• How to design good algorithms.
• Cover well-known data structures such as
dynamic arrays, linked lists, stacks, queues,
tree and graphs.
• Implement data structures and algorithms in
C/C++ code.

7
Algorithm
•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.
8
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.
9
Algorithm Design
• Algorithmic is a branch of computer science that
consists of designing and analyzing computer
algorithms
• The “design” pertain to
– The description of algorithm at an abstract level by
means of a pseudo language, and
– Proof of correctness that is, the algorithm solves the
given problem in all cases.
• The “analysis” deals with performance evaluation
(complexity analysis).

10
Algorithm Performance Analysis
• Does the program efficiently use primary and
secondary storage?
• Is the program running time acceptable for the task?
• Space Complexity
– The space complexity of a program is the measure of the
amount of memory that it needs to run to completion
• Time complexity
– The Time complexity of a program is the measure of the
amount of computer time it needs to run to completion

11
Performance Estimation
• How to determine which algorithm is better?
• We need some mechanism to predict the
performance with out actually executing the
program
• Mechanism should be independent of the
compiler and underlying hardware

12
Algorithm Analysis
• The complexity of an algorithm is a function g(n) that gives bound of the
number of operation (or running time) performed by an algorithm when
the input size is n

• There are two interpretations of bound.


• Worst-case Complexity

– The running time for any given size input will be lower than the upper bound
except possibly for some values of the input where the maximum is reached.
Big O is used to calculate the worst case complexity for an algorithm
• Average-case Complexity
– The running time for any given size input will be the average number of
operations over all problem instances for a given size
• Best-case complexity
– The best case of the algorithm is the function is defined by the minimum
number of steps taken on any instance of size n.

13
Big-Oh(O)
• Determining the exact step count of the
program can be very difficult task
• Because of the exactness of the definition of
the step, exact step count is not very useful for
comparative purposes
• E.g. which one is better
– 45n+3 or 100n+10
• We use some asymptotic notation for measure
of growth

14
Some useful Big-O estimates
• O(1): constant
• O(log(n)): Logarithm
• O(n): Linear
• O(nlog(n)): Log Linear
• O(n2): Quadratic
• O(n3):cubic
• O(2n):exponential

15
Pseudo code
• High-level description of Example: find
an algorithm max element of
• More structured than an array
English prose Algorithm arrayMax(A, n)
• Less detailed than a Input array A of n integers
program Output maximum element
• Preferred notation for of A
describing algorithms
• Hides program design currentMax := A[0]
issues for i :=1 to n  1 do
if A[i] 
currentMax then
currentMax
:= A[i] 16
Pseudocode Details
• Contr ol flow • Method call
– if … then … [else …] var.method (arg [, arg…])
– while … do … • Return value
– repeat … until … return expression
– for … do … • Expressions
– Indentation replaces braces ¬ Assignment
(like  in Java)
• Method declaration = Equality testing
Algorithm method (arg [, arg…]) (like  in Java)
Input … n2 Superscripts and other
Output … mathematical formatting
allowed

17
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

18
• 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

19
Data structures
•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

20
Need for Data Structures
 Data structures organize data  more
efficient programs.
 More powerful computers  more complex
applications.
 More complex applications demand more
calculations.

21
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.

22
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.

23
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must
meet.
2. Determine the basic operations that must
be supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.

24
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.

25
26
Arrays
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive
integers.
 To access a cell, use the array name and an
index:
x[0], x[1], x[2], x[3], x[4], x[5]

27
Array Layout (Linear-array)
x[0]
Array cells are x[1]
contiguous in
computer memory x[2]

The memory can be x[3]


thought of as an
x[4]
array
x[5]

28
Linear(1-Dimensional Array)

A linear array A[8] consisting of numbers is


pictured in following figure.

29
2-DIMENSIONAL ARRAY

SALES is the name given to the Array

30
2-Dimensional Array

31
Link List

32
Tree

33
34
Data Structure Operations

35
Data Abstraction and Abstract Data Types
(ADT)
• A data type is a template for objects and a set of operations
that define the behavior of the objects ( or Instances) of that
type.
• An abstract Data Type (ADT) is a data type in which the
implementation details are hidden and the user is concerned
with the properties ( or behavior) of that type.
• An ADT has two components:
- Interface (the behavior)
- Implementation
• Example:
- int, float

36
Abstract Data Type
• The data structures used to
implement the data type can Interface 1 Interface 2
only be accessed through the
interface.
Implementation
Implementation
• Any change in the
implementation does not
change the user programs as Interface 3 Interface 4
long as the interface remains
the same
• This is also known as the data
encapsulation or data
abstraction.

37
38

You might also like