1 Course Introduction
1 Course Introduction
and Algorithms
COURSE OVERVIEW
CONTENTS DISCUSSION
INTRODUCTION TO DATA STRUCTURES &
ALGORITHMS
TERMINOLOGY, EXAMPLES & APPLICATION
OOP REVIEW
21
Tentative Course Outline
Type Weightage
Quiz 10%
Assignments 20%
Lab 25%
Project 15%
Final Exam 20%
Class Participation 10%
21
Pre-requisites
21
Course Contents
Revision of OOP
Data Structures
Linear Data Structures
Non-linear Data Structures
Algorithms
Sorting and searching
Shortest Path etc.
21
Books & Tutorials
21
Course Introduction
21
Data Structures and Algorithms?
21
Data Structures and
Algorithms?
Data Structures are the programmatic way of storing data so
that data can be used efficiently.
Almost every enterprise application uses various types of
data structures in one or the other way
21
Why to Learn Data Structure
and Algorithms?
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.
21
Why to Learn Data Structure and
Algorithms? (continued..)
Multiple requests − As thousands of users can search data
simultaneously on a web server, even the fast server fails
while searching the data.
To solve the above-mentioned problems, data structures
come to rescue. Data can be organized in a data structure in
such a way that all items may not be required to be
searched, and the required data can be searched almost
instantly
21
Characteristics of a Data
Structure
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.
21
Examples of Some Data Structures
Linked lists
Stacks
Binary Trees
Red Black Trees
Graphs
Hash Tables
Heaps, etc.
Different
algorithms are needed to build, search, and
manipulate data stored in these various structures
21
Algorithms
Algorithm is a step-by-step procedure, which defines a set of
instructions to be executed in a certain order to get the desired
output.
Algorithms are generally created independent of underlying
languages, i.e. an algorithm can be implemented in more than one
programming language.
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.
21
Applications of Algorithms
21
Plain Facts
The plain fact is that many items in nature have a ‘natural arrangement’ with
their predecessor or successor element.
Think: records in a sequential file
Think: a family ‘tree’
21
Examples of ‘Plain Facts’
21
Data Structures are NOT Created
Equal
All have advantages and disadvantages.
Worst ‘algorithm’ that processes data in one data structure
may be the best in other case.
Never use a data structure or a seemingly inefficient
algorithm.
21
Execution Time Cases
There are three cases which are usually used to compare
various data structure's execution time in a relative manner.
Worst Case − This is the scenario where a particular data
structure operation takes maximum time it can take. If an
operation's worst case time is ƒ(n) then this operation will not
take more than ƒ(n) time where ƒ(n) represents function of n.
Average Case − This is the scenario depicting the average
execution time of an operation of a data structure. If an
operation takes ƒ(n) time in execution, then m operations will
take mƒ(n) time.
Best Case − This is the scenario depicting the least possible
execution time of an operation of a data structure.
21
Terminology
Data − Data are values or set of values.
Data Item − Data item refers to single unit of values.
Group Items − Data items that are divided into sub items are
called as Group Items.
Elementary Items − Data items that cannot be divided are called
as Elementary Items.
Attribute and Entity − An entity is that which contains certain
attributes or properties, which may be assigned values.
Field − Field is a single elementary unit of information
representing an attribute of an entity.
Record − Record is a collection of field values of a given entity.
File − File is a collection of records of the entities in a given entity
set.
21
Data Type
21
Built-in Data Type
21
Derived Data Type
21
Basic Operations
21
Example – Array Data Structure
Element
Index
21
Basic Operations on Arrays
21
Array Insertion Operation
Insert operation is to insert one or more data elements into an
array. Based on the requirement, a new element can be added at the
beginning, end, or any given index of array.
21
Insertion Operation - Algorithm
21
Example
LA = array of 10 elements
LA = {1,3,5,7,0,.....}
Insert 10 at 3rd position
Result: LA = {1,3,10,5,7, ....}
21
Array – Deletion Operation
21
Deletion Operation-
Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the algorithm
to delete an element available at the Kth position of LA
21
Example
LA = array of 10 elements
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Delete 5 at 3rd position
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
21
Array - Search
21
Array Search-Algorithm
21
Example
21
Update
21
Update-Algorithm
21
Example
21
Object Oriented Programming
- Revision
21
Object Oriented Programming
Addressed two problems of procedural languages:
Lack of correspondence between the program and the real world, (physical
description vs program realization) and
The internal organization of the program.
21
Examples:
Programs were (largely) ‘procedural.’
Divided into functions, sections, paragraphs, ‘routines.’
Data:
either ‘local’ to one construct or
‘global’ to many.
21
Objects
21
Classes
A class is ageneralization of many objects;
A class is atemplate; an abstraction.
Objects are merely ‘instances’ of classes.
Access these objects by referencing them. (References
are ‘addresses’ of the objects)
We often create references to objects first and then
create the objects themselves.
e.g.
thermostat therm1, therm2; // references
therm1 = new thermostat(); // creates the object
therm2 = new thermostat(); // w therm1 and 2 pointing
// bank.java // // demonstrates basic OOP syntax file name or application name or package
// name…(more ahead)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class BankAccount
{
private double balance; // account balance most object data is private
public void withdraw(double amount) // makes withdrawal // most methods (services) are
{ // public. When might they be private?
balance = balance - amount; // What is the signature of an object?
}
public void display() // displays balance
{
System.out.println("balance=" + balance);
}
} // end class BankAccount // I require scope terminators 21on methods and classes.
Note: object ‘encapsulates’ everything about bank account objects; ‘ information hiding’ too.
…and the second class
class BankApp
{
public static void main(String[] args)
{
BankAccount ba1 = new BankAccount(100.00); // create acct
// what does this statement do?
System.out.print("Before transactions, "); // Explain what this REALLY is!
Outputs:
Before transactions, balance=100
21
After transactions, balance=154.35
Inheritance and Polymorphism
Had
1. encapsulation
2. information hiding
21
Inheritance and Polymorphism
Inheritance
Creation of a class (subclass, derived class, child class…) from a base class
(super class, parent, …)
Add features in subclasses (derived classes); override features in base class,
etc.
Inheritance is all about reuse!!
Polymorphism
Are different derived classes from a base class.
Methods are said to be ‘polymorphic.’
In practice, polymorphism usually refers to referencing a method (a
‘polymorphic’ method) that will execute a different method for different
objects of different derived classes all of which come from the base class.
Method name is same;
Pointer to object is different.
Significant facility to support extension, maintainability, and a number of
other design features.
Polymorphism is all about design, extension, and reuse.
21
You don’t need to worry about the Java – C++ similarities
and features.
21
Java Library Data Structures
Super data structures, etc. in the Java API in general.
Data Structures found in java.util.* package.
We will NOT use these.
Do NOT use these for this course.
21