SlideShare a Scribd company logo
1 of 35
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
dwharder@gmail.com
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Data Structures
and Algorithms
2
Data Structures and Algorithms
Outline
This topic will describe:
– The concrete data structures that can be used to store information
– The basic forms of memory allocation
• Contiguous
• Linked
• Indexed
– The prototypical examples of these: arrays and linked lists
– Other data structures:
• Trees
• Hybrids
• Higher-dimensional arrays
– Finally, we will discuss the run-time of queries and operations on arrays
and linked lists
2.2
3
Data Structures and Algorithms
Memory Allocation
Memory allocation can be classified as either
– Contiguous
– Linked
– Indexed
Prototypical examples:
– Contiguous allocation: arrays
– Linked allocation: linked lists
2.2.1
4
Data Structures and Algorithms
Memory Allocation
Contiguous, adj.
Touching or connected throughout in an unbroken sequence.
Meriam Webster
Touching, in actual contact, next in space; meeting at a common
boundary, bordering, adjoining.
www.oed.com
2.2.1.1
5
Data Structures and Algorithms
Contiguous Allocation
An array stores n objects in a single contiguous space of memory
Unfortunately, if more memory is required, a request for new
memory usually requires copying all information into the new
memory
– In general, you cannot request for the operating
system to allocate to you the next n memory
locations
2.2.1
2.2.1.1
6
Data Structures and Algorithms
Linked Allocation
Linked storage such as a linked list associates two pieces of data
with each item being stored:
– The object itself, and
– A reference to the next item
• In C++ that reference is the address of the next node
2.2.1.2
7
Data Structures and Algorithms
Linked Allocation
This is a class describing such a node
template <typename Type>
class Node {
private:
Type node_value;
Node *next_node;
public:
// ...
};
2.2.1.2
8
Data Structures and Algorithms
Linked Allocation
The operations on this node must include:
– Constructing a new node
– Accessing (retrieving) the value
– Accessing the next pointer
Node( const Type& = Type(), Node* = nullptr );
Type value() const;
Node *next() const;
2.2.1.2
Pointing to nothing has been represented as:
C NULL
Java/C# null
C++ (old) 0
C++ (new) nullptr
Symbolically Ø
9
Data Structures and Algorithms
Linked Allocation
For a linked list, however, we also require an object which links to
the first object
The actual linked list class must store two pointers
– A head and tail:
Node *head;
Node *tail;
Optionally, we can also keep a count
int count;
The next_node of the last node is assigned nullptr
2.2.1.2
10
Data Structures and Algorithms
Linked Allocation
The class structure would be:
template <typename Type>
class List {
private:
Node<Type> *head;
Node<Type> *tail;
int count;
public:
// constructor(s)...
// accessor(s)...
// mutator(s)...
};
2.2.1.2
11
Data Structures and Algorithms
Indexed Allocation
With indexed allocation, an array of pointers
(possibly NULL) link to a sequence of allocated
memory locations
Used in the C++ standard template library
Computer engineering students will see indexed
allocation in their operating systems course
2.2.1.3
12
Data Structures and Algorithms
Indexed Allocation
Matrices can be implemented using indexed allocation:
2.2.1.3
1 2 3
4 5 6
 
 
 
13
Data Structures and Algorithms
Indexed Allocation
Matrices can be implemented using indexed allocation
– Most implementations of matrices (or higher-dimensional arrays) use
indices pointing into a single contiguous block of memory
2.2.1.3
Row-major order Column-major order
C, Python Matlab, Fortran
1 2 3
4 5 6
 
 
 
14
Data Structures and Algorithms
Other Allocation Formats
We will look at some variations or hybrids of these memory
allocations including:
– Trees
– Graphs
– Deques (linked arrays)
– inodes
2.2.2
15
Data Structures and Algorithms
Trees
The linked list can be used to store linearly ordered data
– What if we have multiple next pointers?
A rooted tree (weeks 4-6) is similar
to a linked list but with multiple next
pointers
2.2.2.2
16
Data Structures and Algorithms
Trees
A tree is a variation on a linked list:
– Each node points to an arbitrary number of subsequent nodes
– Useful for storing hierarchical data
– We will see that it is also useful for storing sorted data
– Usually we will restrict ourselves to trees where each node points to at
most two other nodes
2.2.2.2
17
Data Structures and Algorithms
Graphs
Suppose we allow arbitrary relations between any two objects in a
container
– Given n objects, there are n2 – n possible relations
• If we allow symmetry, this reduces to
– For example, consider the network
2
2
n n

2.2.2.2
18
Data Structures and Algorithms
Arrays
Suppose we allow arbitrary relations between any two objects in a
container
– We could represent this using a two-dimensional array
– In this case, the matrix is
symmetric
A B C D E F G H I J K L
A × × ×
B × × × × ×
C × × × × × ×
D × × ×
E × ×
F × ×
G × × ×
H × × ×
I × ×
J × × ×
K × × ×
L × × ×
2.2.2.2
19
Data Structures and Algorithms
Array of Linked Lists
Suppose we allow arbitrary relations between any two objects in a
container
– Alternatively, we could use a hybrid: an array of linked lists
A
B
C
D
E
F
G
H
I
J
K
L
2.2.2.2
20
Data Structures and Algorithms
Linked Arrays
Other hybrids are linked lists of arrays
– Something like this is used for the C++ STL deque container
For example, the alphabet could be stored either as:
– An array of 26 entries, or
– A linked list of arrays of 8 entries
2.2.2.3
21
Data Structures and Algorithms
Hybrid data structures
The Unix inode was used to store information about large files
– The first twelve entries can reference the first twelve blocks (48 KiB)
2.2.2.4
22
Data Structures and Algorithms
Hybrid data structures
The Unix inode was used to store information about large files
– The next entry is a pointer to an array that stores the next 1024 blocks
This stores files up to 4 MiB on a 32-bit computer
2.2.2.4
23
Data Structures and Algorithms
Hybrid data structures
The Unix inode was used to store information about large files
– The next entry has two levels of indirection for files up to 4 GiB
2.2.2.4
24
Data Structures and Algorithms
Hybrid data structures
The Unix inode was used to store information about large files
– The last entry has three levels of indirection for files up to 4 TiB
2.2.2.4
25
Data Structures and Algorithms
Algorithm run times
Once we have chosen a data structure to store both the objects and
the relationships, we must implement the queries or operations as
algorithms
– The Abstract Data Type will be implemented as a class
– The data structure will be defined by the member variables
– The member functions will implement the algorithms
The question is, how do we determine the efficiency of the
algorithms?
2.2.3
26
Data Structures and Algorithms
Operations
We will us the following matrix to describe operations at the
locations within the structure
Front/1st Arbitrary
Location
Back/nth
Find ? ? ?
Insert ? ? ?
Erase ? ? ?
2.2.3
27
Data Structures and Algorithms
Operations on Sorted Lists
Given an sorted array, we have the following run times:
Front/1st Arbitrary
Location
Back/nth
Find Good Okay Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good
* only if the array is not full
2.2.3.1
28
Data Structures and Algorithms
Operations on Lists
If the array is not sorted, only one operations changes:
Front/1st Arbitrary
Location
Back/nth
Find Good Bad Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good
* only if the array is not full
2.2.3.2
29
Data Structures and Algorithms
Operations on Lists
However, for a singly linked list where we a head and tail pointer, we
have:
Front/1st Arbitrary
Location
Back/nth
Find Good Bad Good
Insert Good Bad Good
Erase Good Bad Bad
2.2.3.3
30
Data Structures and Algorithms
Operations on Lists
If we have a pointer to the kth entry, we can insert or erase at that
location quite easily
– Note, this requires a little bit of trickery: we must modify the value
stored in the kth node
– This is a common co-op interview question!
Front/1st Arbitrary
Location
Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Bad
2.2.3.3
31
Data Structures and Algorithms
Operations on Lists
For a doubly linked list, one operation becomes more efficient:
Front/1st Arbitrary
Location
Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Good
2.2.3.4
32
Data Structures and Algorithms
Following Classes
The next topic, asymptotic analysis, will provide the mathematics
that will allow us to measure the efficiency of algorithms
It will also allow us the measure the memory requirements of both
the data structure and any additional memory required by the
algorithms
33
Data Structures and Algorithms
Following Classes
Following our discussion on asymptotic and algorithm analysis, we
will spend
– 13 lectures looking at data structures for storing linearly ordered data
– One week looking at data structures for relation-free data
– Four lectures on sorting
– One week on partial orderings and adjacency relations, and
– Two weeks on algorithm design techniques
34
Data Structures and Algorithms
Summary
In this topic, we have introduced the concept of data structures
– We discussed contiguous, linked, and indexed allocation
– We looked at arrays and linked lists
– We considered
• Trees
• Two-dimensional arrays
• Hybrid data structures
– We considered the run time of the algorithms required to perform
various queries and operations on specific data structures:
• Arrays and linked lists
35
Data Structures and Algorithms
References
Wikipedia, https://en.wikipedia.org/wiki/Data_structure
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.

More Related Content

Similar to 2.02.Data_structures_and_algorithms (1).pptx

Summer Training Project On Data Structure & Algorithms
Summer Training Project On Data Structure & AlgorithmsSummer Training Project On Data Structure & Algorithms
Summer Training Project On Data Structure & AlgorithmsKAUSHAL KUMAR JHA
 
Summer training project on Data structures and algorithms.pptx
Summer training project on Data structures and algorithms.pptxSummer training project on Data structures and algorithms.pptx
Summer training project on Data structures and algorithms.pptxlavkumar420720
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxSaralaT3
 
2.01.Containers_relations_ADTs.pptx
2.01.Containers_relations_ADTs.pptx2.01.Containers_relations_ADTs.pptx
2.01.Containers_relations_ADTs.pptxBinish Raza
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptNORSHADILAAHMADBADEL
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfAxmedcarb
 
DATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESDATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESsuthi
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfraji175286
 
Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)MUHAMMAD AAMIR
 
Iare ds lecture_notes_2
Iare ds lecture_notes_2Iare ds lecture_notes_2
Iare ds lecture_notes_2RajSingh734307
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfMaryJacob24
 
Data structure (basics)
Data structure (basics)Data structure (basics)
Data structure (basics)ShrushtiGole
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxsabithabanu83
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptxSajalFayyaz
 
Chapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdfChapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdfTamiratDejene1
 
Data Structure the Basic Structure for Programming
Data Structure the Basic Structure for ProgrammingData Structure the Basic Structure for Programming
Data Structure the Basic Structure for Programmingpaperpublications3
 

Similar to 2.02.Data_structures_and_algorithms (1).pptx (20)

Summer Training Project On Data Structure & Algorithms
Summer Training Project On Data Structure & AlgorithmsSummer Training Project On Data Structure & Algorithms
Summer Training Project On Data Structure & Algorithms
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Summer training project on Data structures and algorithms.pptx
Summer training project on Data structures and algorithms.pptxSummer training project on Data structures and algorithms.pptx
Summer training project on Data structures and algorithms.pptx
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Ch1
Ch1Ch1
Ch1
 
Lect 1-2 Zaheer Abbas
Lect 1-2 Zaheer AbbasLect 1-2 Zaheer Abbas
Lect 1-2 Zaheer Abbas
 
2.01.Containers_relations_ADTs.pptx
2.01.Containers_relations_ADTs.pptx2.01.Containers_relations_ADTs.pptx
2.01.Containers_relations_ADTs.pptx
 
Lect 1-2
Lect 1-2Lect 1-2
Lect 1-2
 
Chapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.pptChapter 1 - Introduction to Data Structure.ppt
Chapter 1 - Introduction to Data Structure.ppt
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdf
 
DATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESDATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTES
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
 
Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)
 
Iare ds lecture_notes_2
Iare ds lecture_notes_2Iare ds lecture_notes_2
Iare ds lecture_notes_2
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
Data structure (basics)
Data structure (basics)Data structure (basics)
Data structure (basics)
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Chapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdfChapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdf
 
Data Structure the Basic Structure for Programming
Data Structure the Basic Structure for ProgrammingData Structure the Basic Structure for Programming
Data Structure the Basic Structure for Programming
 

More from DrBashirMSaad

DS basics functions.ppt
DS basics functions.pptDS basics functions.ppt
DS basics functions.pptDrBashirMSaad
 
Algorithm analysis.pptx
Algorithm analysis.pptxAlgorithm analysis.pptx
Algorithm analysis.pptxDrBashirMSaad
 
Searching Informed Search.pdf
Searching Informed Search.pdfSearching Informed Search.pdf
Searching Informed Search.pdfDrBashirMSaad
 
procress and threads.ppt
procress and threads.pptprocress and threads.ppt
procress and threads.pptDrBashirMSaad
 
1b-150720094704-lva1-app6892.pdf
1b-150720094704-lva1-app6892.pdf1b-150720094704-lva1-app6892.pdf
1b-150720094704-lva1-app6892.pdfDrBashirMSaad
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptDrBashirMSaad
 
lec06-programming.ppt
lec06-programming.pptlec06-programming.ppt
lec06-programming.pptDrBashirMSaad
 
Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptDrBashirMSaad
 

More from DrBashirMSaad (11)

DS basics functions.ppt
DS basics functions.pptDS basics functions.ppt
DS basics functions.ppt
 
Algorithm analysis.pptx
Algorithm analysis.pptxAlgorithm analysis.pptx
Algorithm analysis.pptx
 
Searching Informed Search.pdf
Searching Informed Search.pdfSearching Informed Search.pdf
Searching Informed Search.pdf
 
procress and threads.ppt
procress and threads.pptprocress and threads.ppt
procress and threads.ppt
 
1b-150720094704-lva1-app6892.pdf
1b-150720094704-lva1-app6892.pdf1b-150720094704-lva1-app6892.pdf
1b-150720094704-lva1-app6892.pdf
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
lec06-programming.ppt
lec06-programming.pptlec06-programming.ppt
lec06-programming.ppt
 
CS351-L1.ppt
CS351-L1.pptCS351-L1.ppt
CS351-L1.ppt
 
Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
 
002 AWSSlides.pdf
002 AWSSlides.pdf002 AWSSlides.pdf
002 AWSSlides.pdf
 

Recently uploaded

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 

Recently uploaded (20)

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 

2.02.Data_structures_and_algorithms (1).pptx

  • 1. ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@gmail.com © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Data Structures and Algorithms
  • 2. 2 Data Structures and Algorithms Outline This topic will describe: – The concrete data structures that can be used to store information – The basic forms of memory allocation • Contiguous • Linked • Indexed – The prototypical examples of these: arrays and linked lists – Other data structures: • Trees • Hybrids • Higher-dimensional arrays – Finally, we will discuss the run-time of queries and operations on arrays and linked lists 2.2
  • 3. 3 Data Structures and Algorithms Memory Allocation Memory allocation can be classified as either – Contiguous – Linked – Indexed Prototypical examples: – Contiguous allocation: arrays – Linked allocation: linked lists 2.2.1
  • 4. 4 Data Structures and Algorithms Memory Allocation Contiguous, adj. Touching or connected throughout in an unbroken sequence. Meriam Webster Touching, in actual contact, next in space; meeting at a common boundary, bordering, adjoining. www.oed.com 2.2.1.1
  • 5. 5 Data Structures and Algorithms Contiguous Allocation An array stores n objects in a single contiguous space of memory Unfortunately, if more memory is required, a request for new memory usually requires copying all information into the new memory – In general, you cannot request for the operating system to allocate to you the next n memory locations 2.2.1 2.2.1.1
  • 6. 6 Data Structures and Algorithms Linked Allocation Linked storage such as a linked list associates two pieces of data with each item being stored: – The object itself, and – A reference to the next item • In C++ that reference is the address of the next node 2.2.1.2
  • 7. 7 Data Structures and Algorithms Linked Allocation This is a class describing such a node template <typename Type> class Node { private: Type node_value; Node *next_node; public: // ... }; 2.2.1.2
  • 8. 8 Data Structures and Algorithms Linked Allocation The operations on this node must include: – Constructing a new node – Accessing (retrieving) the value – Accessing the next pointer Node( const Type& = Type(), Node* = nullptr ); Type value() const; Node *next() const; 2.2.1.2 Pointing to nothing has been represented as: C NULL Java/C# null C++ (old) 0 C++ (new) nullptr Symbolically Ø
  • 9. 9 Data Structures and Algorithms Linked Allocation For a linked list, however, we also require an object which links to the first object The actual linked list class must store two pointers – A head and tail: Node *head; Node *tail; Optionally, we can also keep a count int count; The next_node of the last node is assigned nullptr 2.2.1.2
  • 10. 10 Data Structures and Algorithms Linked Allocation The class structure would be: template <typename Type> class List { private: Node<Type> *head; Node<Type> *tail; int count; public: // constructor(s)... // accessor(s)... // mutator(s)... }; 2.2.1.2
  • 11. 11 Data Structures and Algorithms Indexed Allocation With indexed allocation, an array of pointers (possibly NULL) link to a sequence of allocated memory locations Used in the C++ standard template library Computer engineering students will see indexed allocation in their operating systems course 2.2.1.3
  • 12. 12 Data Structures and Algorithms Indexed Allocation Matrices can be implemented using indexed allocation: 2.2.1.3 1 2 3 4 5 6      
  • 13. 13 Data Structures and Algorithms Indexed Allocation Matrices can be implemented using indexed allocation – Most implementations of matrices (or higher-dimensional arrays) use indices pointing into a single contiguous block of memory 2.2.1.3 Row-major order Column-major order C, Python Matlab, Fortran 1 2 3 4 5 6      
  • 14. 14 Data Structures and Algorithms Other Allocation Formats We will look at some variations or hybrids of these memory allocations including: – Trees – Graphs – Deques (linked arrays) – inodes 2.2.2
  • 15. 15 Data Structures and Algorithms Trees The linked list can be used to store linearly ordered data – What if we have multiple next pointers? A rooted tree (weeks 4-6) is similar to a linked list but with multiple next pointers 2.2.2.2
  • 16. 16 Data Structures and Algorithms Trees A tree is a variation on a linked list: – Each node points to an arbitrary number of subsequent nodes – Useful for storing hierarchical data – We will see that it is also useful for storing sorted data – Usually we will restrict ourselves to trees where each node points to at most two other nodes 2.2.2.2
  • 17. 17 Data Structures and Algorithms Graphs Suppose we allow arbitrary relations between any two objects in a container – Given n objects, there are n2 – n possible relations • If we allow symmetry, this reduces to – For example, consider the network 2 2 n n  2.2.2.2
  • 18. 18 Data Structures and Algorithms Arrays Suppose we allow arbitrary relations between any two objects in a container – We could represent this using a two-dimensional array – In this case, the matrix is symmetric A B C D E F G H I J K L A × × × B × × × × × C × × × × × × D × × × E × × F × × G × × × H × × × I × × J × × × K × × × L × × × 2.2.2.2
  • 19. 19 Data Structures and Algorithms Array of Linked Lists Suppose we allow arbitrary relations between any two objects in a container – Alternatively, we could use a hybrid: an array of linked lists A B C D E F G H I J K L 2.2.2.2
  • 20. 20 Data Structures and Algorithms Linked Arrays Other hybrids are linked lists of arrays – Something like this is used for the C++ STL deque container For example, the alphabet could be stored either as: – An array of 26 entries, or – A linked list of arrays of 8 entries 2.2.2.3
  • 21. 21 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files – The first twelve entries can reference the first twelve blocks (48 KiB) 2.2.2.4
  • 22. 22 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files – The next entry is a pointer to an array that stores the next 1024 blocks This stores files up to 4 MiB on a 32-bit computer 2.2.2.4
  • 23. 23 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files – The next entry has two levels of indirection for files up to 4 GiB 2.2.2.4
  • 24. 24 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files – The last entry has three levels of indirection for files up to 4 TiB 2.2.2.4
  • 25. 25 Data Structures and Algorithms Algorithm run times Once we have chosen a data structure to store both the objects and the relationships, we must implement the queries or operations as algorithms – The Abstract Data Type will be implemented as a class – The data structure will be defined by the member variables – The member functions will implement the algorithms The question is, how do we determine the efficiency of the algorithms? 2.2.3
  • 26. 26 Data Structures and Algorithms Operations We will us the following matrix to describe operations at the locations within the structure Front/1st Arbitrary Location Back/nth Find ? ? ? Insert ? ? ? Erase ? ? ? 2.2.3
  • 27. 27 Data Structures and Algorithms Operations on Sorted Lists Given an sorted array, we have the following run times: Front/1st Arbitrary Location Back/nth Find Good Okay Good Insert Bad Bad Good* Bad Erase Bad Bad Good * only if the array is not full 2.2.3.1
  • 28. 28 Data Structures and Algorithms Operations on Lists If the array is not sorted, only one operations changes: Front/1st Arbitrary Location Back/nth Find Good Bad Good Insert Bad Bad Good* Bad Erase Bad Bad Good * only if the array is not full 2.2.3.2
  • 29. 29 Data Structures and Algorithms Operations on Lists However, for a singly linked list where we a head and tail pointer, we have: Front/1st Arbitrary Location Back/nth Find Good Bad Good Insert Good Bad Good Erase Good Bad Bad 2.2.3.3
  • 30. 30 Data Structures and Algorithms Operations on Lists If we have a pointer to the kth entry, we can insert or erase at that location quite easily – Note, this requires a little bit of trickery: we must modify the value stored in the kth node – This is a common co-op interview question! Front/1st Arbitrary Location Back/nth Find Good Bad Good Insert Good Good Good Erase Good Good Bad 2.2.3.3
  • 31. 31 Data Structures and Algorithms Operations on Lists For a doubly linked list, one operation becomes more efficient: Front/1st Arbitrary Location Back/nth Find Good Bad Good Insert Good Good Good Erase Good Good Good 2.2.3.4
  • 32. 32 Data Structures and Algorithms Following Classes The next topic, asymptotic analysis, will provide the mathematics that will allow us to measure the efficiency of algorithms It will also allow us the measure the memory requirements of both the data structure and any additional memory required by the algorithms
  • 33. 33 Data Structures and Algorithms Following Classes Following our discussion on asymptotic and algorithm analysis, we will spend – 13 lectures looking at data structures for storing linearly ordered data – One week looking at data structures for relation-free data – Four lectures on sorting – One week on partial orderings and adjacency relations, and – Two weeks on algorithm design techniques
  • 34. 34 Data Structures and Algorithms Summary In this topic, we have introduced the concept of data structures – We discussed contiguous, linked, and indexed allocation – We looked at arrays and linked lists – We considered • Trees • Two-dimensional arrays • Hybrid data structures – We considered the run time of the algorithms required to perform various queries and operations on specific data structures: • Arrays and linked lists
  • 35. 35 Data Structures and Algorithms References Wikipedia, https://en.wikipedia.org/wiki/Data_structure These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.