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

1 Course Introduction

This document provides an overview of a course on data structures and algorithms. It outlines the course contents which include a review of object-oriented programming and data structures like linear and non-linear data structures. It also covers algorithms for sorting, searching, and shortest paths. The document discusses prerequisites, textbooks, and introduces some basic concepts around data structures and algorithms.

Uploaded by

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

1 Course Introduction

This document provides an overview of a course on data structures and algorithms. It outlines the course contents which include a review of object-oriented programming and data structures like linear and non-linear data structures. It also covers algorithms for sorting, searching, and shortest paths. The document discusses prerequisites, textbooks, and introduces some basic concepts around data structures and algorithms.

Uploaded by

Namra Khatoon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Data Structures

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

 Programming Fundamentals &Objected Oriented


Programming (passed with a good grade)
 Before starting this course:
 You should have a basic understanding of C++ and Java
Programming Languages

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

 Data Structures and Algorithms in Java, 4th edition, Michael


T. Goodrich
 Data Structures and Algorithm Analysis in C++, 4th edition,
Mark Allen Weiss
 Data Structures and Algorithms: Annotated Reference with
Examples, 1st Edition, Granville Barnett, and Luca Del
Tongo 2008.
 Tutorials point:
https://www.tutorialspoint.com/data_structures_algorithms/index.htm

21
Course Introduction

21
Data Structures and Algorithms?

 Data Structures are merely different arrangements of data


in primary memory or on disk, and
 Algorithms are sequences of instructions that manipulate
the data in these data structures in a variety of ways.

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

 Many data structures:


 Arrays

 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

 The following computer problems can be solved using Data


Structures −
 Fibonacci number series
 Knapsack problem
 Tower of Hanoi
 All pair shortest path by Floyd-Warshall
 Shortest path by Dijkstra
 Project scheduling
 This is just the beginning....

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’

 If we model this natural occurring arrangement in the computer using


some kind of data structure that reflects the real world realities of the data
and their implicit relationships, then we need specialized algorithms for
building, retrieving, and otherwise accessing the data itself.

21
Examples of ‘Plain Facts’

 Consider a sequence of numbers – may be best represented in an array. (a


physical data structure)
 Assume we need to ‘search’ this sequence for a number…
 An Algorithm processes this arrangement of data.
 If ordered and sufficiently large: binary search
 If unordered: sequential search
 Performance issues can be huge: either way.

 We may best ‘represent’ (model) a family and children by a tree structure.


(a logical data structure)
 But how might we best add members to the tree? Search for members?
Delete members? What are the Algorithms to process the Structure?

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

 Data type is a way to classify various types of data such as


integer, string, etc. which determines the values that can be
used with the corresponding type of data, the type of
operations that can be performed on the corresponding type
of data. There are two data types −
 Built-in Data Type
 Derived Data Type

21
Built-in Data Type

 Those data types for which a language has built-in support


are known as Built-in Data types. For example, most of the
languages provide the following built-in data types.
 Integers
 Boolean (true, false)
 Floating (Decimal numbers)
 Character and Strings

21
Derived Data Type

 Those data types which are implementation independent as


they can be implemented in one or the other way are known
as derived data types. These data types are normally built by
the combination of primary or built-in data types and
associated operations on them. For example −
 List
 Array
 Stack
 Queue

21
Basic Operations

 The data in the data structures are processed by certain


operations. The particular data structure chosen largely
depends on the frequency of the operation that needs to be
performed on the data structure.
 Traversing
 Searching
 Insertion
 Deletion
 Sorting
 Merging

21
Example – Array Data Structure

 Element
 Index

21
Basic Operations on Arrays

 Following are the basic operations supported by an array.


 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the
value.
 Update − Updates an element at the given index.
 CRUD operations (create, read, update, and delete)

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

 Let LA be a Linear Array (unordered) with N elements


and K is a positive integer such that K<=N. Following is the
algorithm where ITEM is inserted into the Kth position of
LA

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

 Deletion refers to removing an existing element from the


array and re-organizing all elements of an array.

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

 You can perform a search for an array element based on its


value or its index.

21
Array Search-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 find an element with a value of ITEM using sequential
search.

21
Example

 The original array elements are :


 LA[0] = 1
 LA[1] = 3
 LA[2] = 5
 LA[3] = 7
 LA[4] = 8
 Search at which position is element 5
 Output: Found element 5 at position 3

21
Update

 Update operation refers to updating an existing element


from the array at a given index.

21
Update-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 update an element available at the Kth position of LA.

21
Example

 The original array elements are : LA[0] = 1 LA[1] = 3


LA[2] = 5 LA[3] = 7 LA[4] = 8
 Update Element 5 to 10.
 The array elements after updation:
 LA[0] = 1
 LA[1] = 3
 LA[2] = 10
 LA[3] = 7
 LA[4] = 8

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.

 Most real world objects have properties.


 They provide certain actions or services on data.
 They can carry out a task; they provide “services” for clients.

21
Examples:
 Programs were (largely) ‘procedural.’
 Divided into functions, sections, paragraphs, ‘routines.’
 Data:
 either ‘local’ to one construct or
 ‘global’ to many.

 Global Data, No practical way to specify what


 methods could access a variable and others couldn’t.
 Result: for global data, all data – even data not needed
by particular procedures – was available to all (or most)
procedures.
 Recall: Call by Reference; Call by Value??
 Frequently a source of inadvertent errors!!!

21
Objects

 Encapsulate data (attributes, fields, properties) with the


operations (methods, member functions) that operate on
them.
 Only methods within an object are allowed access to data in
the object. (in Java).
 An object is a real world representation of a real ‘thing.’
 An object is an instantiation of a class.

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

// to their respective objects.


21
 // bank.java // // demonstrates basic OOP syntax
 class BankAccount
 {
 private double balance; // account balance
 public BankAccount(double openingBalance) // constructor
 {
 balance = openingBalance;
 }
 public void deposit(double amount) // makes deposit
 {
 balance = balance + amount;
 }
 public void withdraw(double amount) // makes withdrawal
 {
 balance = balance - amount;
 }
 public void display() // displays balance
 {
 System.out.println ("balance=" + balance);
 }
 } // end class BankAccount
KNOW:
 class BankApp Instance variables; local variables; static (class) variables)
 {
 public static void main(String[] args)
 {
 BankAccount ba1 = new BankAccount(100.00); // reference and acct object
 System.out.print ("Before transactions, ");
 ba1.display(); // display balance
 ba1.deposit(74.35); // make deposit
 ba1.withdraw(20.00); // make withdrawal

 System.out.print ("After transactions, ");


 ba1.display(); // display balance
 } // end main()
21
 } // end class BankApp
Let’s look at the two Classes:

// 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 BankAccount(double openingBalance) // constructor Constructor never has a ‘return type’


{
balance = openingBalance;
}
public void deposit(double amount) // makes deposit: four methods (“services” provided
// to clients of this object.
{
balance = balance + amount;
}

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!

ba1.display(); // display balance // Explain generically what this really is!


ba1.deposit(74.35); // make deposit // Explain generically what this really is!

ba1.withdraw(20.00); // make withdrawal

System.out.print("After transactions, "); // What is this statement? What does it do?

ba1.display(); // display balance


} // end main() 
} // end class BankApp  Note the scope terminators

Outputs:
Before transactions, balance=100
21
After transactions, balance=154.35
Inheritance and Polymorphism
 Had
 1. encapsulation
 2. information hiding

 What is encapsulation? Examples?

 What is information hiding? Examples?


 Who cares??

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

You might also like