WITH C++ Prof. Amr Goneid AUC Part 13. Abstract Data Types (ADTs) - - PowerPoint PPT Presentation

with c
SMART_READER_LITE
LIVE PREVIEW

WITH C++ Prof. Amr Goneid AUC Part 13. Abstract Data Types (ADTs) - - PowerPoint PPT Presentation

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 13. Abstract Data Types (ADTs) Prof. amr Goneid, AUC 1 Data Modeling and ADTs Prof. Amr Goneid, AUC 2 Data Modeling and ADTs Data Modeling Abstract Data types


slide-1
SLIDE 1
  • Prof. amr Goneid, AUC

1

CSCE 110 PROGRAMMING FUNDAMENTALS

WITH C++

  • Prof. Amr Goneid

AUC

Part 13. Abstract Data Types (ADT’s)

slide-2
SLIDE 2
  • Prof. Amr Goneid, AUC

2

Data Modeling and ADTs

slide-3
SLIDE 3
  • Prof. Amr Goneid, AUC

3

Data Modeling and ADTs

 Data Modeling  Abstract Data types (ADTs)  A Classification of Abstract Structures  Another Classification  Special Data Structures  Examples on Modeling  Example ADT’s

slide-4
SLIDE 4
  • Prof. Amr Goneid, AUC

4

  • 1. Data Modeling

 Real-world applications need to be reduced to

a small number of existing problems (top- down design)

 Real-world data need to be described in an

abstract way in terms

  • f

fundamental structures

slide-5
SLIDE 5
  • Prof. Amr Goneid, AUC

5

Data Modeling

 The collection of data in some organization is

called a “Data Structure”

 The sequences of operations to be done on

the data are called “Algorithms”

slide-6
SLIDE 6
  • Prof. Amr Goneid, AUC

6

Data Modeling

 The word Algorithm comes from the name of Abu

Ja’afar Mohamed ibn Musa Al Khowarizmi (c. 825 A.D.)

 An Algorithm is a procedure to do a certain task  An Algorithm is supposed to solve a general, well-

specified problem

slide-7
SLIDE 7
  • Prof. Amr Goneid, AUC

7

Data Modeling

 A real-world application is basically

Data Structures + Algorithms

slide-8
SLIDE 8
  • Prof. Amr Goneid, AUC

8

Data Modeling

 Data and the Operations on that data are

parts of an object that cannot be separated.

 These two faces of an object are linked.

Neither can be carried out independently of the other.

slide-9
SLIDE 9
  • Prof. Amr Goneid, AUC

9

The Data Cone

Real-world Data ADTs Data Structures

Fundamental Data Types

slide-10
SLIDE 10
  • Prof. Amr Goneid, AUC

10

  • 2. Abstract Data Types (ADTs)

 The most important attribute of data is its type.  Type implies certain operation. It also prohibits other

  • perations.

 For example, + - * / are allowed for types int and

double, but the modulus (%) is allowed for int and prohibited for double.

 When a certain data organization + its operations are

not available in the language, we build it as a new data type. To be useful to many applications, we build it as an Abstract Data Type.

slide-11
SLIDE 11
  • Prof. Amr Goneid, AUC

11

Abstract Data Types (ADTs)

An ADT represents the logical or conceptual level

  • f the data.

It consists of:

1.

A collection of data items in some Data Structure

2.

Operations (algorithms) on the data items

For example, a Stack supports retrieval in LIFO (Last In First Out) order. Basic operations are push and pop. It can be implemented using arrays (static

  • r dynamic) or linked lists
slide-12
SLIDE 12
  • Prof. amr Goneid, AUC

12

Abstract Data Types (ADTs)

 ADT (Abstract Data Type) is a description of

a new data type together with operations on the data.

 An ADT can be used in one or more

applications.

 The definition of the data type is separated

from its implementation (Data Abstraction).

 (e.g. ADT Table can be implemented using a

static array, a dynamic array or a linked list.

slide-13
SLIDE 13
  • Prof. amr Goneid, AUC

13

Using ADT’s

ADT ADT ADT ADT ADT

Program Program Program

Standard Types/Libraries User Built ADT’s

slide-14
SLIDE 14
  • Prof. Amr Goneid, AUC

14

ADT Definition

 The first step in creating an ADT is the process of

Data Abstraction

 Data Abstraction provides a complete

description of the following items independent

  • f the way it will be implemented:

A definition of the ADT.

Elements or members of that ADT.

Relationship between the members.

The fundamental operations on the members.

slide-15
SLIDE 15
  • Prof. Amr Goneid, AUC

15

ADT Implementation

Usually, an ADT can be implemented in different

  • ways. To the applications, such implementation

should be completely hidden. The Implementation part will describe:

 how the ADT will be implemented using native

Data Structures or other pre-defined ADT’s in C++.

 how the relationships and fundamental

  • perations on the members will be

implemented as C++ functions.

 In Object Oriented Programming, ADTs are

created as Classes

slide-16
SLIDE 16
  • Prof. amr Goneid, AUC

16

  • 3. A Classification of Abstract

Structures

According to the relationship between members Data Structures Set Linear Tree Graph

slide-17
SLIDE 17
  • Prof. amr Goneid, AUC

17

Abstract Structures

 Sets: No relationship. Only that elements are

members of the same set.

 Linear: Sequential, one-to-one relationship

e.g Arrays, Strings and Streams

 Trees: Non-Linear, hierarchical one-to-many.  Graph: Non-Linear, many-to-many.

Arrays, Structs, pointers and standard Classes are used to model different ADT’s.

slide-18
SLIDE 18
  • Prof. Amr Goneid, AUC

18

Sets

 Order of elements does not matter. Only that

they are members of the same set ({1,3,4} is identical to {1,4,3}).

 Can be implemented using arrays or linked

lists.

 Used in problems seeking:

 groups  collection  selection  packaging

slide-19
SLIDE 19
  • Prof. Amr Goneid, AUC

19

Linear Structures

 Sequential, one-to-one relationship.

Examples: Tables, Stacks, Queues, Strings and Permutations.

 Can be implemented using arrays and linked lists

(structs and pointers).

 Used in problems dealing with:

 Searching, Sorting, stacking, waiting lines.  Text processing, character sequences, patterns  Arrangements, ordering, tours, sequences.

slide-20
SLIDE 20
  • Prof. Amr Goneid, AUC

20

Trees

 Non-Linear, hierarchical one-to-many.

Examples: Binary Trees, Binary Search Trees (BST)

 Can be implemented using arrays, structs

and pointers

 Used in problems dealing with:

 Searching  Hierarchy  Ancestor/descendant relationship  Classification

slide-21
SLIDE 21
  • Prof. Amr Goneid, AUC

21

Graphs

 Non-Linear, many-to-many.  Can be implemented using arrays or linked

lists

 Used to model a variety of problems dealing

with:

 Networks  Circuits  Web  Relationship  Paths

slide-22
SLIDE 22
  • Prof. Amr Goneid, AUC

22

  • 4. Another Classification of

Abstract Structures

According to their functions Special Abstract Structures Containers Dictionaries Priority Queues Disjoint Sets Graphs Strings Geometric DS

slide-23
SLIDE 23
  • Prof. Amr Goneid, AUC

23

Containers

 Permit storage and retrieval of data items

independent of content (access by location

  • nly).

 Support two basic operations:

 Put (x,C): Insert item x in container C  Get (C): Retrieve next item from C.

slide-24
SLIDE 24
  • Prof. Amr Goneid, AUC

24

Containers

 Examples:

 Stacks: Last-In-First-Out (LIFO) structures  Queues: First-In-First-Out (FIFO) structures  Tables: Retrieval by position.

slide-25
SLIDE 25
  • Prof. Amr Goneid, AUC

25

Dictionaries

 A form of container that permits access by content.  Support the following main operations:

 Insert (D,x): Insert item x in dictionary D  Delete (D,x): Delete item x from D  Search (D,k): search for key k in D

slide-26
SLIDE 26
  • Prof. Amr Goneid, AUC

26

Dictionaries

 Examples:

 Unsorted arrays and Linked Lists: permit linear search  Sorted arrays: permit Binary search  Ordered Lists: permit linear search  Binary Search Trees (BST): fast support of all dictionary

  • perations.

 Hash Tables: Fast retrieval by hashing key to a position.

slide-27
SLIDE 27
  • Prof. Amr Goneid, AUC

27

Priority Queues

 Allow processing items according to a certain

  • rder (Priority)

 Support the following main operations:

 Insert (Q,x): Insert item x in priority queue

Q

 Remove (Q): Return and remove item with

Highest/Lowest Priority

slide-28
SLIDE 28
  • Prof. Amr Goneid, AUC

28

Priority Queues

 Examples:

 Heaps and Partially Ordered Trees (POT)  Major DS in HeapSort

slide-29
SLIDE 29
  • Prof. Amr Goneid, AUC

29

Disjoint Sets

  • Disjoint sets are collections of elements with no common

elements between the sets.

  • A set can be identified by a parent node and children

nodes.

slide-30
SLIDE 30
  • Prof. Amr Goneid, AUC

30

Disjoint Sets

 Support the following main operations:

 Find (i): Find Parent (set) containing node (i)  Union (i,j): make set (i) the child of set (j)

 Examples:

 Representation of disjoint collections of data  Representation of Trees, Forests and Graphs

slide-31
SLIDE 31
  • Prof. Amr Goneid, AUC

31

Graphs

 Can be used to represent any relationship and a wide

variety of structures.

slide-32
SLIDE 32
  • Prof. Amr Goneid, AUC

32

Graphs

 Can be used to represent any relationship and a wide

variety of structures.

 Well-known graph algorithms are the basis for many

  • applications. Examples of such algorithms are:

 Minimum Spanning Trees  Graph traversal (Depth-First and Breadth-First)  Shortest Path Algorithms

slide-33
SLIDE 33
  • Prof. Amr Goneid, AUC

33

  • 5. Special Data Structures

 Strings:

Typically represented as arrays of characters. Various operations support pattern matching and string editing

slide-34
SLIDE 34
  • Prof. Amr Goneid, AUC

34

Special Data Structures

 Geometric Data Structures:

Represent collections of data points and

  • regions. Data points can represent segments.

Segments can represent polygons that can represent regions.

slide-35
SLIDE 35
  • Prof. Amr Goneid, AUC

35

  • 6. Examples on Modeling

 Problem:

In Encryption problems, we need to do arithmetic on very large integers (e.g. 300 digits or more)

 ADTs:

List

 Data Structures:

1-D array or Linked List

slide-36
SLIDE 36
  • Prof. Amr Goneid, AUC

36

Examples on Modeling

 Problem: (Knapsack Problem)

We have (n) objects each with a weight and a price and a container with maximum capacity (m). Select whole or fractions of the objects such that the total weight does not exceed (m) and the total price is maximum

 ADTs:

List

 Data Structures:

1-D array or Linked List

slide-37
SLIDE 37
  • Prof. Amr Goneid, AUC

37

Examples on Modeling

 Problem: (Chess Games)

8-Queens problem, Knight’s Tour problem, etc

 ADTs:

Board ADT

 Data Structures:

2-D array

slide-38
SLIDE 38
  • Prof. Amr Goneid, AUC

38

Examples on Modeling

 Problem: (Dictionary)

We would like to build and use a small dictionary of words that translates from language (A) to language (B)

 ADTs:

Key Table or List

 Data Structures:

1-D array or linked list

slide-39
SLIDE 39
  • Prof. Amr Goneid, AUC

39

Examples on Modeling

 Problem: (Small and fast Directory)

We would like to build and use a small and fast directory to check username and pass word logins

 ADTs:

Hash Table

 Data Structures:

1-D array

slide-40
SLIDE 40
  • Prof. Amr Goneid, AUC

40

Examples on Modeling

 Problem: (Large and fast Directory)

We would like to build and use a large and fast telephone directory.

 ADTs:

Binary Search Tree

 Data Structures:

Linked Structure (Nodes)

slide-41
SLIDE 41
  • Prof. Amr Goneid, AUC

41

Examples on Modeling

 Problems:

 Evaluation of arithmetic expressions  The Hanoi Towers game

 ADTs:

Stack

 Data Structures:

1-D array or Linked List

slide-42
SLIDE 42
  • Prof. Amr Goneid, AUC

42

Examples on Modeling

 Problem:

We would like to simulate the waiting process for airplanes to land in an airport.

 ADTs:

Queue

 Data Structures:

1-D array or Linked List

slide-43
SLIDE 43
  • Prof. Amr Goneid, AUC

43

Examples on Modeling

 Problem:

 Sorting a set of elements  Selection of the kth smallest (largest) element

 ADTs:

Priority Queue

 Data Structures:

1-D array

slide-44
SLIDE 44
  • Prof. Amr Goneid, AUC

44

Examples on Modeling

 Problem:

 Find the shortest path between a source and a

destination

 Find the exit in a Maze

 ADTs:

Graph

 Data Structures:

2-D array or Linked list

slide-45
SLIDE 45
  • Prof. Amr Goneid, AUC

45

Examples on Modeling

 Problem:

Find a wiring scheme for electrical power with minimum cost of wiring

 ADTs:

Graph Priority Queue Disjoint Sets

 Data Structures:

1-D arrays, 2-D array, Linked list

slide-46
SLIDE 46
  • Prof. amr Goneid, AUC

46

  • 7. Examples ADT’s:

The Array as an ADT

 Abstraction:

A homogenous sequence of elements with a fixed size that allows direct access to its elements.

 Elements (members):

Any type, but all elements must be of the same type

 Relationship:

Linear (One-To-one). Ordered storage with direct access.

 Fundamental Operations:

 create array  store an element in the array at a given position (direct

access)

 retrieve an element from a given position (direct access)

slide-47
SLIDE 47
  • Prof. amr Goneid, AUC

47

Examples on ADT’s: ADT rational

Abstraction: A rational number (fraction) is a rational representation of two integers (x,y). Elements or Members: A numerator (x) and a denominator (y), both are

  • integers. (y) cannot be zero

Relationship: The representation is equivalent to x / y

slide-48
SLIDE 48
  • Prof. amr Goneid, AUC

48

ADT rational (continued)

Fundamental Operations:

 Read a fraction from keyboard  Display a fraction on the screen  Add Fractions

f = f1 + f2 (e.g. ½ + ¼ = ¾)

 Subtract Fractions f = f1 – f2 (e.g. ½ - 1/3 = 1/6)  Multiply Fractions

f = f1 * f2 (e.g. ½ * ¾ = 3/8)

 Divide Fractions

f = f1 / f2 (e.g. 1/5 / ¼ = 4/5)

 Reduce Fractions

(e.g. 2/6 = 1/3)