SlideShare ist ein Scribd-Unternehmen logo
1 von 34
DATA STRUCTURES
AND ALGORITHMS
Lecture 1
Data Structures
■ A data structure is a way to organize data to enable
efficient computation over that data
■ A data structure supports certain operations, each with a:
– Meaning: what does the operation do/return
– Performance: how efficient is the operation
■ Examples:
– List with operations insert and delete
– Stack with operations push and pop
Organizing Data
■ Any organization for a collection of records that can be
easily 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.
Efficiency
■ A solution is said to be efficient if it solves the problem
within its resource constraints.
– Time
– Space
■ The cost of a solution is the amount of resources that the
solution consumes
Need for Data Structure
■ 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.
■ Multiple Requests − As thousands of users can search data
simultaneously on a web server, even the fast server fails
while searching the data.
Data Structures
■ The domain of data structures studies how we can store
and access data.
■ A data structure can be:
– Static: the size of the data structure is fixed. Such data
structures are suitable if it is known that a fixed
number of elements need to be stored.
– Dynamic: the size of the data structure can grow or
shrink as needed by the number of elements.
Classification of Data
Structure
■ Data structure are normally divided into two broad
categories:
– Primitive Data Structure
– Non-Primitive Data Structure
Primitive Data Structure
Primitive Data Structure
Integer Float Character Pointer
Primitive Data Structure
■ Primitive data structures are the fundamental data types
which are supported by a programming language.
■ Some basic data types are integer, real, character, float.
■ The terms ‘data type’, ‘basic data type’, and ‘primitive data
type’ are often used interchangeably
Non-Primitive Data Structure
Non-Primitive Data Structure
Linear Data Structures Non-Linear Data Structures
Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables
Non-Primitive Data Structure
■ Non-primitive data structures are those data structures
which are created using primitive data structures.
■ Examples of such data structures include linked lists,
stacks, trees, and graphs.
■ Non-primitive data structures can further be classified
into two categories: linear and non-linear data structures.
Non-Primitive Data Structure
■ Linear: In Linear data structure, values are arrange in a
linear or sequential order.
– Array: Fixed-size
– Linked-list: Variable-size
– Stack: Add to top and remove from top
– Queue: Add to back and remove from front
– Priority queue: Add anywhere, remove the highest
priority
Non-Primitive Data Structure
■ Non-Linear: The data values in this structure are not
arranged in sequential order.
– Hash tables: Unordered lists which use a ‘hash
function’ to insert and search
– Tree: Data is organized in branches.
– Graph: A more general branching structure, with less
strict connection conditions than for a tree
Data Types
■ A data type is a set of values and a set of operations on
those values. For example:
■ int
– set of values are integer numbers from a given interval
– possible operations: add, subtract, multiply, etc.
■ boolean
– set of values: true, false
– possible operations: negation, and, or, xor, etc.
Data Types
■ String
– ”abc”, ”text”, etc.
– possible operations: get the length, get a character
from a position, get a substring, etc.
■ etc.
Abstract Data Types
■ An Abstract Data Type (ADT) is a data type having the
following two properties:
– the objects from the domain of the ADT are specified
independently of their representation
– the operations of the ADT are specified independently
of their implementation
Abstract Data Types - Domain
■ The domain of an ADT describes what elements belong to
this ADT.
■ If the domain is finite, we can simply enumerate them.
■ If the domain is not finite, we will use a rule that describes
the elements belonging to the ADT.
Abstract Data Types -
Interface
■ After specifying the domain of an ADT, we need to specify
its operations.
■ The set of all operations for an ADT is called its interface.
■ The interface of an ADT contains the signature of the
operations, together with their input data, results,
preconditions and post conditions (but no detail regarding
the implementation of the method).
■ When talking about ADT we focus on the WHAT (it is, it
does), not on the HOW.
ADT - Example
■ Consider the Date ADT (made of three numbers: year,
month and day)
– Elements belonging to this ADT are all the valid dates
(year is positive, maybe less than 3000, month is
between 1 and 12, day is between 1 and maximum
number of possible days for the month)
ADT - Example
■ One possible operation for the Data ADT could be:
difference(Date d1, Date d2)
– Descr: computed the difference in number of days
between two dates
– Pre: d1 and d2 have to be valid Dates, d1 ≤ d2
– Post: difference ← d, d is a natural number and
represents the number of days between d1 and d2.
– Throws: and exception if d1 is greater than d2
ADT - Example
■ This information specifies everything we need to use the
Date ADT, even if we know nothing about how it is
represented or how the operation difference is
implemented.
Why Abstract Data Types?
■ There are several different Abstract Data Types, so
choosing the most suitable one is an important step
during application design.
■ When choosing the suitable ADT we are not interested in
the implementation details of the ADT.
■ Most high-level programming languages usually provide
implementations for different Abstract Data Types.
– In order to be able to use the right ADT for a specific
problem, we need to know their domain and interface.
Why Abstract Data Types?
■ Why do we need to implement our own Abstract Data
Types if they are already implemented in most
programming languages?
– Implementing these ADT will help us understand
better how they work (we cannot use them, if we do
not know what they are doing)
Why Abstract Data Types?
– To learn to create, implement and use ADT for
situations when:
■ we work in a programming language where they are
not readily implemented.
■ we need an ADT which is not part of the standard ones,
but might be similar to them.
Advantages of working with
ADTs
■ Abstraction is defined as the separation between the
specification of an object (its domain and interface) and
its implementation.
■ Encapsulation - abstraction provides a promise that any
implementation of an ADT will belong to its domain and
will respect its interface. And this is all that is needed to
use an ADT.
Advantages of working with
ADTs
■ Localization of change - any code that uses an ADT is still
valid if the ADT changes (because no matter how it
changes, it still has to respect the domain and interface).
■ Flexibility - an ADT can be implemented in different ways,
but all these implementation have the same interface.
Switching from one implementation to another can be
done with minimal changes in the code.
Advantages of working with
ADTs
■ Consider again our example with the Date ADT:
– Assume you used variables of type Date and the
difference operation in your code.
– If the implementation of the Date changes, these
changes will not affect your code, because even if the
representation changes and even if the
implementation of the operations changes, they still
have to respect the interface (the signature and
behavior of the difference operation is still the same).
Advantages of working with
ADTs
– If a new implementation for the Date will appear and
you want to switch to that implementation, it can be
done with minimal modifications in your code
Algorithm
■ Algorithm is a step by step procedure, which defines a set
of instructions to be executed in certain order to get the
desired output.
■ Algorithms are generally created independent of
underlying languages
Algorithm
■ From 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 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
Characteristics of an
Algorithm
■ Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps or phases, and their
input/outputs should be clear and must lead to only one
meaning.
■ Input − An algorithm should have 0 or more well defined
inputs.
■ Output − An algorithm should have 1 or more well
defined outputs, and should match the desired output.
Characteristics of an
Algorithm
■ Finiteness − Algorithms must terminate after a finite
number of steps.
■ Feasibility − Should be feasible with the available
resources.
■ Independent − An algorithm should have step-by-step
directions which should be independent of any
programming code.
How to write an algorithm?
■ There are no well-defined standards for writing
algorithms. Rather, it is problem and resource dependent.
– Algorithms are never written to support a particular
programming code.
– As we know that all programming languages share
basic code constructs like loops (do, for, while), flow-
control (if-else), etc. These common constructs can be
used to write an algorithm.
Algorithm Complexity
■ Time and space used by the Algorithm determines the
efficiency of an algorithm.
■ Time Factor − The time is measured by counting the
number of key operations such as comparisons in sorting
algorithm
■ Space Factor − The space is measured by counting the
maximum memory space required by the algorithm.

Weitere ähnliche Inhalte

Was ist angesagt?

★Mean shift a_robust_approach_to_feature_space_analysis
★Mean shift a_robust_approach_to_feature_space_analysis★Mean shift a_robust_approach_to_feature_space_analysis
★Mean shift a_robust_approach_to_feature_space_analysis
irisshicat
 
Production System in AI
Production System in AIProduction System in AI
Production System in AI
Bharat Bhushan
 

Was ist angesagt? (20)

Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Splay Tree Algorithm
Splay Tree AlgorithmSplay Tree Algorithm
Splay Tree Algorithm
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
★Mean shift a_robust_approach_to_feature_space_analysis
★Mean shift a_robust_approach_to_feature_space_analysis★Mean shift a_robust_approach_to_feature_space_analysis
★Mean shift a_robust_approach_to_feature_space_analysis
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
String in python use of split method
String in python use of split methodString in python use of split method
String in python use of split method
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Hashing
HashingHashing
Hashing
 
Online search skills
Online search skillsOnline search skills
Online search skills
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Production System in AI
Production System in AIProduction System in AI
Production System in AI
 
sort search in C
 sort search in C  sort search in C
sort search in C
 
3.6 constraint based cluster analysis
3.6 constraint based cluster analysis3.6 constraint based cluster analysis
3.6 constraint based cluster analysis
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
03 Single layer Perception Classifier
03 Single layer Perception Classifier03 Single layer Perception Classifier
03 Single layer Perception Classifier
 
Stack data structure in Data Structure using C
Stack data structure in Data Structure using C Stack data structure in Data Structure using C
Stack data structure in Data Structure using C
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 

Ähnlich wie Lecture 01

CHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptxCHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptx
OnkarModhave
 
Lecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptxLecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptx
ArifKamal36
 

Ähnlich wie Lecture 01 (20)

Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
data structures and its importance
 data structures and its importance  data structures and its importance
data structures and its importance
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdf
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - Notes
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
CHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptxCHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptx
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
Basic concepts of data structures and algorithms
Basic concepts of data structures and algorithmsBasic concepts of data structures and algorithms
Basic concepts of data structures and algorithms
 
Basic terminologies
Basic terminologiesBasic terminologies
Basic terminologies
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data types
 
Chapter 1( intro & overview)
Chapter 1( intro & overview)Chapter 1( intro & overview)
Chapter 1( intro & overview)
 
Lecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionLecture 2 Data Structure Introduction
Lecture 2 Data Structure Introduction
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
 
Lecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptxLecture 1. Data Structure & Algorithm.pptx
Lecture 1. Data Structure & Algorithm.pptx
 
Data strucutre basic introduction
Data strucutre basic introductionData strucutre basic introduction
Data strucutre basic introduction
 
UNIT I - Data Structures.pdf
UNIT I - Data Structures.pdfUNIT I - Data Structures.pdf
UNIT I - Data Structures.pdf
 

Kürzlich hochgeladen

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Kürzlich hochgeladen (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Lecture 01

  • 2. Data Structures ■ A data structure is a way to organize data to enable efficient computation over that data ■ A data structure supports certain operations, each with a: – Meaning: what does the operation do/return – Performance: how efficient is the operation ■ Examples: – List with operations insert and delete – Stack with operations push and pop
  • 3. Organizing Data ■ Any organization for a collection of records that can be easily 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.
  • 4. Efficiency ■ A solution is said to be efficient if it solves the problem within its resource constraints. – Time – Space ■ The cost of a solution is the amount of resources that the solution consumes
  • 5. Need for Data Structure ■ 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. ■ Multiple Requests − As thousands of users can search data simultaneously on a web server, even the fast server fails while searching the data.
  • 6. Data Structures ■ The domain of data structures studies how we can store and access data. ■ A data structure can be: – Static: the size of the data structure is fixed. Such data structures are suitable if it is known that a fixed number of elements need to be stored. – Dynamic: the size of the data structure can grow or shrink as needed by the number of elements.
  • 7. Classification of Data Structure ■ Data structure are normally divided into two broad categories: – Primitive Data Structure – Non-Primitive Data Structure
  • 8. Primitive Data Structure Primitive Data Structure Integer Float Character Pointer
  • 9. Primitive Data Structure ■ Primitive data structures are the fundamental data types which are supported by a programming language. ■ Some basic data types are integer, real, character, float. ■ The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ are often used interchangeably
  • 10. Non-Primitive Data Structure Non-Primitive Data Structure Linear Data Structures Non-Linear Data Structures Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables
  • 11. Non-Primitive Data Structure ■ Non-primitive data structures are those data structures which are created using primitive data structures. ■ Examples of such data structures include linked lists, stacks, trees, and graphs. ■ Non-primitive data structures can further be classified into two categories: linear and non-linear data structures.
  • 12. Non-Primitive Data Structure ■ Linear: In Linear data structure, values are arrange in a linear or sequential order. – Array: Fixed-size – Linked-list: Variable-size – Stack: Add to top and remove from top – Queue: Add to back and remove from front – Priority queue: Add anywhere, remove the highest priority
  • 13. Non-Primitive Data Structure ■ Non-Linear: The data values in this structure are not arranged in sequential order. – Hash tables: Unordered lists which use a ‘hash function’ to insert and search – Tree: Data is organized in branches. – Graph: A more general branching structure, with less strict connection conditions than for a tree
  • 14. Data Types ■ A data type is a set of values and a set of operations on those values. For example: ■ int – set of values are integer numbers from a given interval – possible operations: add, subtract, multiply, etc. ■ boolean – set of values: true, false – possible operations: negation, and, or, xor, etc.
  • 15. Data Types ■ String – ”abc”, ”text”, etc. – possible operations: get the length, get a character from a position, get a substring, etc. ■ etc.
  • 16. Abstract Data Types ■ An Abstract Data Type (ADT) is a data type having the following two properties: – the objects from the domain of the ADT are specified independently of their representation – the operations of the ADT are specified independently of their implementation
  • 17. Abstract Data Types - Domain ■ The domain of an ADT describes what elements belong to this ADT. ■ If the domain is finite, we can simply enumerate them. ■ If the domain is not finite, we will use a rule that describes the elements belonging to the ADT.
  • 18. Abstract Data Types - Interface ■ After specifying the domain of an ADT, we need to specify its operations. ■ The set of all operations for an ADT is called its interface. ■ The interface of an ADT contains the signature of the operations, together with their input data, results, preconditions and post conditions (but no detail regarding the implementation of the method). ■ When talking about ADT we focus on the WHAT (it is, it does), not on the HOW.
  • 19. ADT - Example ■ Consider the Date ADT (made of three numbers: year, month and day) – Elements belonging to this ADT are all the valid dates (year is positive, maybe less than 3000, month is between 1 and 12, day is between 1 and maximum number of possible days for the month)
  • 20. ADT - Example ■ One possible operation for the Data ADT could be: difference(Date d1, Date d2) – Descr: computed the difference in number of days between two dates – Pre: d1 and d2 have to be valid Dates, d1 ≤ d2 – Post: difference ← d, d is a natural number and represents the number of days between d1 and d2. – Throws: and exception if d1 is greater than d2
  • 21. ADT - Example ■ This information specifies everything we need to use the Date ADT, even if we know nothing about how it is represented or how the operation difference is implemented.
  • 22. Why Abstract Data Types? ■ There are several different Abstract Data Types, so choosing the most suitable one is an important step during application design. ■ When choosing the suitable ADT we are not interested in the implementation details of the ADT. ■ Most high-level programming languages usually provide implementations for different Abstract Data Types. – In order to be able to use the right ADT for a specific problem, we need to know their domain and interface.
  • 23. Why Abstract Data Types? ■ Why do we need to implement our own Abstract Data Types if they are already implemented in most programming languages? – Implementing these ADT will help us understand better how they work (we cannot use them, if we do not know what they are doing)
  • 24. Why Abstract Data Types? – To learn to create, implement and use ADT for situations when: ■ we work in a programming language where they are not readily implemented. ■ we need an ADT which is not part of the standard ones, but might be similar to them.
  • 25. Advantages of working with ADTs ■ Abstraction is defined as the separation between the specification of an object (its domain and interface) and its implementation. ■ Encapsulation - abstraction provides a promise that any implementation of an ADT will belong to its domain and will respect its interface. And this is all that is needed to use an ADT.
  • 26. Advantages of working with ADTs ■ Localization of change - any code that uses an ADT is still valid if the ADT changes (because no matter how it changes, it still has to respect the domain and interface). ■ Flexibility - an ADT can be implemented in different ways, but all these implementation have the same interface. Switching from one implementation to another can be done with minimal changes in the code.
  • 27. Advantages of working with ADTs ■ Consider again our example with the Date ADT: – Assume you used variables of type Date and the difference operation in your code. – If the implementation of the Date changes, these changes will not affect your code, because even if the representation changes and even if the implementation of the operations changes, they still have to respect the interface (the signature and behavior of the difference operation is still the same).
  • 28. Advantages of working with ADTs – If a new implementation for the Date will appear and you want to switch to that implementation, it can be done with minimal modifications in your code
  • 29. Algorithm ■ Algorithm is a step by step procedure, which defines a set of instructions to be executed in certain order to get the desired output. ■ Algorithms are generally created independent of underlying languages
  • 30. Algorithm ■ From 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 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
  • 31. Characteristics of an Algorithm ■ Unambiguous − Algorithm should be clear and unambiguous. Each of its steps or phases, and their input/outputs should be clear and must lead to only one meaning. ■ Input − An algorithm should have 0 or more well defined inputs. ■ Output − An algorithm should have 1 or more well defined outputs, and should match the desired output.
  • 32. Characteristics of an Algorithm ■ Finiteness − Algorithms must terminate after a finite number of steps. ■ Feasibility − Should be feasible with the available resources. ■ Independent − An algorithm should have step-by-step directions which should be independent of any programming code.
  • 33. How to write an algorithm? ■ There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. – Algorithms are never written to support a particular programming code. – As we know that all programming languages share basic code constructs like loops (do, for, while), flow- control (if-else), etc. These common constructs can be used to write an algorithm.
  • 34. Algorithm Complexity ■ Time and space used by the Algorithm determines the efficiency of an algorithm. ■ Time Factor − The time is measured by counting the number of key operations such as comparisons in sorting algorithm ■ Space Factor − The space is measured by counting the maximum memory space required by the algorithm.