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

CSC2203 Algorithms Data Structure Lecture Notes 1

The document discusses fundamental data structures, categorizing them into linear and non-linear types, and explaining their characteristics and applications. It also introduces the concept of abstract data types (ADTs), emphasizing their role in encapsulating implementation details and providing a uniform interface for operations. Additionally, it highlights the importance of selecting appropriate data structures for efficient data management in programming.

Uploaded by

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

CSC2203 Algorithms Data Structure Lecture Notes 1

The document discusses fundamental data structures, categorizing them into linear and non-linear types, and explaining their characteristics and applications. It also introduces the concept of abstract data types (ADTs), emphasizing their role in encapsulating implementation details and providing a uniform interface for operations. Additionally, it highlights the importance of selecting appropriate data structures for efficient data management in programming.

Uploaded by

choemmanuel01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Fundamental data structures


A data structure is a named location that can be used to store and organize data.
Data structure is a storage that is used to store and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated efficiently.
Depending on your requirement and project, it is important to choose the right data structure for
your project. For example, if you want to store data sequentially in the memory, then you can go for
the Array data structure.

Note: Data structure and data types are slightly different. Data structure is the collection of data types
arranged in a specific order.
1.1 Types of Data Structure
Basically, data structures are divided into two categories:
• Linear data structure
• Non-linear data structure
1.1.1 Linear data structures
In linear data structures, the elements are arranged in sequence one after the other. Since elements are
arranged in particular order, they are easy to implement.
However, when the complexity of the program increases, the linear data structures might not be the best
choice because of operational complexities.
Popular linear data structures are:
a) Array Data Structure
In an array, elements in memory are arranged in continuous memory. All the elements of an array are of the
same type. And, the type of elements that can be stored in the form of arrays is determined by the
programming language.
b) Stack Data Structure
In stack data structure, elements are stored in the LIFO principle. That is, the last element stored in a stack
will be removed first.

1
It works just like a pile of plates where the last plate kept on the pile will be removed first.

c) Queue Data Structure


Unlike stack, the queue data structure works in the FIFO principle where first element stored in the queue
will be removed first.
It works just like a queue of people in the ticket counter where first person on the queue will get the ticket
first.

d) Linked List Data Structure


In linked list data structure, data elements are connected through a series of nodes. And, each node contains
the data items and address to the next node.

2
1.1.2 Non linear data structures
Unlike linear data structures, elements in non-linear data structures are not in any sequence. Instead, they
are arranged in a hierarchical manner where one element will be connected to one or more elements.
Non-linear data structures are further divided into graph and tree based data structures.
Linear Vs Non-linear Data Structures
Now that we know about linear and non-linear data structures, let's see the major differences between
them.

Linear Data Structures Non Linear Data Structures

The data items are arranged in sequential order, The data items are arranged in non-sequential
one after the other. order (hierarchical manner).

All the items are present on the single layer. The data items are present at different layers.

It can be traversed on a single run. That is, if we It requires multiple runs. That is, if we start from
start from the first element, we can traverse all the the first element it might not be possible to
elements sequentially in a single pass. traverse all the elements in a single pass.

Different structures utilize memory in different


The memory utilization is not efficient.
efficient ways depending on the need.

The time complexity increases with the data size. Time complexity remains the same.

Example: Arrays, Stack, Queue Example: Tree, Graph, Map

1.1.3 Why Data Structure?


Knowledge about data structures help you understand the working of each data structure. And, based on
that you can select the right data structures for your project.
This helps you write memory and time efficient code.
2. Concept of an abstract data type) and the tradeoffs between different implementations of
ADTs.
An abstract data type (ADT) is the specification of a data type within some language, independent of an
implementation. The interface for the ADT is defined in terms of a type and a set of operations on that
type. The behavior of each operation is determined by its inputs and outputs. An ADT does not
specify how the data type is implemented. These implementation details are hidden from the user of the
ADT and protected from outside access, a concept referred to as encapsulation.
3
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values and
a set of operations. The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented. It does not specify how data will be organized in memory
and what algorithms will be used for implementing the operations. It is called “abstract” because it gives
an implementation-independent view.
The process of providing only the essentials and hiding the details is known as abstraction.

The user of data type does not need to know how that data type is implemented, for example, we
have been using Primitive values like int, float, char data types only with the knowledge that these
data type can operate and be performed on without any idea of how they are implemented.
So, a user only needs to know what a data type can do, but not how it will be implemented. Think of
ADT as a black box which hides the inner structure and design of the data type. Now we’ll define
three ADTs namely List ADT, Stack ADT, Queue ADT.
A. List ADT
• The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.
• The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.
• The List ADT Functions is given below:
• get() – Return an element from the list at any given position.
• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty list.
• removeAt() – Remove the element at a specified location from a non-empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.
• isFull() – Return true if the list is full, otherwise return false.
B. Stack ADT
• In Stack ADT Implementation instead of data being stored in each node, the pointer to data is
stored.
• The program allocates memory for the data and address is passed to the stack ADT.
• The head node and the data nodes are encapsulated in the ADT. The calling function can only
see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is not empty.

4
• peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
C. Queue ADT
• The queue abstract data type (ADT) follows the basic design of the stack abstract data type.
• Each node contains a void pointer to the data and the link pointer to the next element in the
queue. The program’s responsibility is to allocate memory for storing the data.
• enqueue() – Insert an element at the end of the queue.
• dequeue() – Remove and return the first element of the queue, if the queue is not empty.
• peek() – Return the element of the queue without removing it, if the queue is not empty.
• size() – Return the number of elements in the queue.
• isEmpty() – Return true if the queue is empty, otherwise return false.
• isFull() – Return true if the queue is full, otherwise return false.
Features of ADT:
• Abstraction: The user does not need to know the implementation of the data structure.
• Better Conceptualization: ADT gives us a better conceptualization of the real world.
• Robust: The program is robust and has the ability to catch errors.
From these definitions, we can clearly see that the definitions do not specify how these ADTs will be
represented and how the operations will be carried out. There can be different ways to implement an
ADT, for example, the List ADT can be implemented using arrays, or singly linked list or doubly
linked list. Similarly, stack ADT and Queue ADT can be implemented using arrays or linked lists.

Example 2.1
The mathematical concept of an integer, along with operations that manipulate integers, form a data
type. The int variable type is a physical representation of the abstract integer. The int variable type,
along with the operations that act on an int variable, form an ADT. Unfortunately,
the int implementation is not completely true to the abstract integer, as there are limitations on the
range of values an int variable can store. If these limitations prove unacceptable, then some other
representation for the ADT “integer” must be devised, and a new implementation must be used for
the associated operations.
Example 2.2
An ADT for a list of integers might specify the following operations:
1. Insert a new integer at a particular position in the list.
2. Return True if the list is empty.
3. Reinitialize the list.
4. Return the number of integers currently in the list.
5. Retrieve the integer at a particular position in the list.
6. Delete the integer at a particular position in the list.
From this description, the input and output of each operation should be clear, but the
implementation for lists has not been specified.
One application that makes use of some ADT might use particular member functions of that ADT more than
a second application, or the two applications might have different time requirements for the various
operations. These differences in the requirements of applications are the reason why a given ADT might be
supported by more than one implementation.

5
Example 2.3
Two popular implementations for large disk-based database applications are hashing and the B-tree. Both
support efficient insertion and deletion of records, and both support exact-match queries. However, hashing
is more efficient than the B-tree for exact-match queries. On the other hand, the B-tree can perform range
queries efficiently, while hashing is hopelessly inefficient for range queries. Thus, if the database application
limits searches to exact-match queries, hashing is preferred. On the other hand, if the application requires
support for range queries, the B-tree is preferred. Despite these performance issues, both implementations
solve versions of the same problem: updating and searching a large collection of records.
The concept of an ADT can help us to focus on key issues even in non-computing applications.
Example 2.4
When operating a car, the primary activities are steering, accelerating, and braking. On nearly all passenger
cars, you steer by turning the steering wheel, accelerate by pushing the gas pedal, and brake by pushing the
brake pedal. This design for cars can be viewed as an ADT with operations “steer”, “accelerate”, and “brake”.
Two cars might implement these operations in radically different ways, say with different types of engine,
or front- versus rear-wheel drive. Yet, most drivers can operate many different cars because the ADT
presents a uniform method of operation that does not require the driver to understand the specifics of any
particular engine or drive design. These differences are deliberately hidden.
The concept of an ADT is one instance of an important principle that must be understood by any successful
computer scientist: managing complexity through abstraction. A central theme of computer science is
complexity and techniques for handling it. Humans deal with complexity by assigning a label to an assembly
of objects or concepts and then manipulating the label in place of the assembly. Cognitive psychologists call
such a label a metaphor. A particular label might be related to other pieces of information or other labels.
This collection can in turn be given a label, forming a hierarchy of concepts and labels. This hierarchy of
labels allows us to focus on important issues while ignoring unnecessary details.
Example 2.5
We apply the label “hard drive” to a collection of hardware that manipulates data on a particular type of
storage device, and we apply the label “CPU” to the hardware that controls execution of computer
instructions. These and other labels are gathered together under the label “computer”. Because even the
smallest home computers today have millions of components, some form of abstraction is necessary to
comprehend how a computer operates.
Consider how you might go about the process of designing a complex computer program that implements
and manipulates an ADT. The ADT is implemented in one part of the program by a particular data structure.
While designing those parts of the program that use the ADT, you can think in terms of operations on the
data type without concern for the data structure’s implementation. Without this ability to simplify your
thinking about a complex program, you would have no hope of understanding or implementing it.
Example 2.6
Consider the design for a relatively simple database system stored on disk. Typically, records on disk in
such a program are accessed through a buffer pool rather than directly. Variable length records might use
a memory manager to find an appropriate location within the disk file to place the record. Multiple index
structures will typically be used to support access to a collection of records using multiple search keys.
Thus, we have a chain of classes, each with its own responsibilities and access privileges. A database query

6
from a user is implemented by searching an index structure. This index requests access to the record by
means of a request to the buffer pool. If a record is being inserted or deleted, such a request goes through
the memory manager, which in turn interacts with the buffer pool to gain access to the disk file. A program
such as this is far too complex for nearly any human programmer to keep all of the details in their head at
once. The only way to design and implement such a program is through proper use of abstraction and
metaphors. In object-oriented programming, such abstraction is handled using classes.
Data types have both a logical form and a physical form. The definition of the data type in terms of an ADT is
its logical form. The implementation of the data type as a data structure is its physical form. Sometimes you
might see the term concrete implementation, but the word concrete is redundant. The figure below
illustrates this relationship between logical and physical forms for data types. When you implement an ADT,
you are dealing with the physical form of the associated data type. When you use an ADT elsewhere in your
program, you are concerned with the associated data type’s logical form. Some sections of this book focus
on physical implementations for a given data structure. Other sections use the logical ADT for the data
structure in the context of a higher-level task.

The ADT defines the logical form of the data type. The data structure implements the physical form of the
data type. Users of an ADT are typically programmers working in the same language as the implementer of
the ADT. Typically, these programmers want to use the ADT as a component in another application. The
interface to an ADT is also commonly referred to as the Application Programmer Interface, or API, for the
ADT. The interface becomes a form of communication between the two programmers.
Example 2.7
A particular programming environment might provide a library that includes a list class. The logical form of
the list is defined by the public functions, their inputs, and their outputs that define the class. This might be
all that you know about the list class implementation, and this should be all you need to know. Within the
class, a variety of physical implementations for lists is possible.
3. Data structures for various scenarios and problems, proficiency with analyzing the running
time of various algorithms associated with data structures.
3.1 Real-time application of Data Structures
A data structure is a particular way of organizing data in a computer so that it can be used effectively.
In this article, the real-time applications of all the data structures are discussed.
Application of Arrays:
Arrays are the simplest data structures that store items of the same data type. A basic application of
Arrays can be storing data in tabular format. For example, if we wish to store the contacts on our
phone, then the software will simply place all our contacts in an array.

7
Some other applications of the arrays are:
1. Arrangement of the leader-board of a game can be done simply through arrays to store the score and
arrange them in descending order to clearly make out the rank of each player in the game.
2. A simple question Paper is an array of numbered questions with each of them assigned some marks.
3. 2D arrays, commonly known as, matrices, are used in image processing.
4. It is also used in speech processing, in which each speech signal is an array.
5. Your viewing screen is also a multidimensional array of pixels.
6. Book titles in a Library Management Systems.
7. Online ticket booking.
8. Contacts on a cell phone.
9. For CPU scheduling in computer.
10. To store the possible moves of chess on a chessboard.
11. To store images of a specific size on an android or laptop.

Application of Strings:
1. Spam email detection.
2. Plagiarism detection.
3. Search engine.
4. Digital forensic and information retrieval system
5. Spell checkers.
6. In the database to check valid information of the user

Application of Matrix:
Matrix is an ordered collection of columns and rows of elements. It is necessary to enclose the elements of a
matrix within the brackets.

8
Some applications of a matrix are:
1. In geology, matrices are used for making seismic surveys.
2. Used for plotting graphs, and statistics and also to do scientific studies and research in almost
different fields.
3. Matrices are also used in representing real-world data like the population of people, infant mortality
rate, etc.
4. They are the best representation methods for plotting surveys.
5. For refraction and reflection in science optics.
6. Electronic circuit and quantum physics.
7. Media player.
8. Mailing list.
9. Symbol table creation.
Application of Linked Lists:
A linked list is a sequence data structure, which connects elements, called nodes, through links.
Some other applications of the linked list are:
1. Images are linked with each other. So, an image viewer software uses a linked list to view the
previous and the next images using the previous and next buttons.
2. Web pages can be accessed using the previous and the next URL links which are linked using a linked
list.
3. The music players also use the same technique to switch between music.
4. To keep the track of turns in a multi-player game, a circular linked list is used.
5. MS-Paint drawings and shapes are connected via a linked list on canvas.
6. Escalators — Circular linked List.
7. Each of the lines of code in an IDE internally is a record on a doubly-linked list.
8. Left/Right swipe on Tinder uses a doubly-linked list.
9. Social media content “feeds”.
10. Used for symbol table management in a designing compiler
11. Used in switching between applications and programs (Alt + Tab) in the Operating system
(implemented using Circular Linked List)
12. Train coaches are connected to one another in a doubly-linked list fashion.
13. It can be used to implement Stacks, Queues, Graphs, and Trees.
14. To perform undo operation.

9
15. Back button.[LIFO]
16. Syntax in the coding editor.
17. History of visited pages.
Application of Stack:
A stack is a data structure that uses LIFO order.
Some Applications of a stack are:
1. Converting infix to postfix expressions.
2. Undo/Redo button/operation in word processors.
3. Syntaxes in languages are parsed using stacks.
4. It is used in many virtual machines like JVM.
5. Forward-backward surfing in the browser.
6. History of visited websites.
7. Message logs and all messages you get are arranged in a stack.
8. Call logs, E-mails, Google photos’ any gallery, YouTube downloads, Notifications (latest appears first).
9. Scratch cards earned after Google pay transaction.
10. Wearing/Removing Bangles, Pile of Dinner Plates, Stacked chairs.
11. Changing wearables on a cold evening, first in, comes out at last.
12. Last Hired, First Fired - which is typically utilized when a company reduces its workforce in an
economic recession.
13. Loading bullets into the magazine of a gun. The last one to go in is fired first. Bam!
14. Java Virtual Machine.
15. Recursion.
16. Used in IDEs to check for proper parentheses matching
17. Media playlist. To play previous and next song
Application of Queue:
A queue is a data structure that uses FIFO order.
Some applications of a queue are:
1. Operating System uses queues for job scheduling.
2. To handle congestion in the networking queue can be used.
3. Data packets in communication are arranged in queue format.
4. Sending an e-mail, it will be queued.

10
5. Server while responding to request
6. Uploading and downloading photos, first kept for uploading/downloading will be completed first
(Not if there is threading)
7. Most internet requests and processes use queue.
8. While switching multiple applications, windows use circular queue.
9. In Escalators, Printer spooler, Car washes queue.
10. A circular queue is used to maintain the playing sequence of multiple players in a game.
11. A queue can be implemented in - Linked List-based Queue, Array-based Queue, Stack-based Queue.
12. Uploading and downloading photos, first kept for uploading/downloading will be completed first
(Not if there is threading).
13. Handle website traffic
14. CPU scheduling

Priority Queue:
1. Process scheduling in the kernel.
2. Priority queues are used in file downloading operations in a browser
3. Vehicle at the toll center.

Application of Sorting Algorithms


1. Order things by their value.
2. Backend Databases (Merge Sort).
3. Playing Cards with your friends (Insertion Sort).
4. sort () - uses IntroSort (a hybrid of Quicksort, Heapsort, and Insertion Sort), Faster than qsort()
5. Contact list on the phone
6. Online shopping. To sort prize in different range. example: flipkart and amazon.
Application of Graph:
Graph is a data structure where data is stored in a collection of interconnected vertices (nodes) and edges
(paths).
Some applications of a graph are:
1. Facebook’s Graph API uses the structure of Graphs.
2. Google’s Knowledge Graph also has to do something with Graph.
3. Dijkstra algorithm or the shortest path first algorithm also uses graph structure to find the smallest
path between the nodes of the graph.

11
4. The GPS navigation system also uses shortest path APIs.
5. Networking components have a huge application for graph
6. Facebook, Instagram, and all social media networking sites every user is Node
7. Data organization
8. React’s virtual DOM uses graph data structures.
9. MS Excel uses DAG (Directed Acyclic Graphs).
10. Path Optimization Algorithms, BFS, DFS.
11. Recommendation Engines.
12. Scientific Computations, Flight Networks, Page ranking.
13. Google map to find nearest location.
14. Facebook to suggest mutual friends

Application of Tree:
Trees are hierarchical structures having a single root node.
Some applications of the trees are:
1. XML Parser uses tree algorithms.
2. The decision-based algorithm is used in machine learning which works upon the algorithm of the
tree.
3. Databases also use tree data structures for indexing.
4. Domain Name Server(DNS) also uses tree structures.
5. File explorer/my computer of mobile/any computer
6. BST used in computer Graphics
7. Posting questions on websites like Quora, the comments are a child of questions.
8. Parsers(XML parser).
9. Code Compression(zip).
10. DOM in Html.
11. Evaluate an expression (i.e., parse).
12. Integral to compilers/automata theory.
13. To store the possible moves in a chess game.
14. To store the genealogy information of biological species.
15. Used by JVM (Java Virtual Machine) to store Java objects.

12
Application of Binary Search Tree:
1. D Game Engine.
2. Computer Graphics Rendering.
3. Routing table.
RED-BLACK TREE
1. Used when there is frequent Insertion/Deletion and few searches.
2. K -mean Clustering using a red-black tree, Databases, Simple-minded database, searching words
inside dictionaries, searching on the web.
3. Process Scheduling in Linux.
AVL TREE
1. More Search and less Insertion/Deletion.
2. Data Analysis and Data Mining and the applications which involve more searches.
SUFFIX TREE
1. Fast full-text search, used in most word processors.
TRIE
1. Dictionary application.
2. Autocomplete feature in searching.
3. Auto-completing the text and spells checking.
Application of Hash Tables:
Hash Tables are store data in key-value pairs. It only stores data that has a key associated with it. Inserting
and Searching operations are easily manageable while using Hash Tables.

Some applications of a hashtable are:

13
1. Data stored in databases is generally of the key-value format which is done through hash tables.
2. Every time we type something to be searched in google chrome or other browsers, it generates the
desired output based on the principle of hashing.
3. Message Digest, a function of cryptography also uses hashing for creating output in such a manner
that reaching the original input from that generated output is almost next to impossible.
4. In our computers we have various files stored in it, each file has two very crucial pieces of
information that is, the filename and file path, in order to make a connection between the filename to
its corresponding file path hash tables are used.
5. Social network “feeds”.
6. Password hashing.
7. Used for fast data lookup - symbol table for compilers, database indexing, caches, Unique data
representation.
8. To store a set of fixed keywords that are referenced very frequently.
Application of Heap:
A Heap is a special case of a binary tree where the parent nodes are compared to their children with their
values and are arranged accordingly.

Some applications of heaps are:


1. In heapsort Algorithm, is an algorithm for sorting elements in either min heap(the key of the parent
is less than or equal to those of its children) or max heap(the key of the parent is greater than or
equal to those of its children), sorting is done with the creation of heaps.
2. Heaps are used to implementing a priority queue where priority is based on the order of heap
created.
3. Systems concerned with security and embedded system such as Linux Kernel uses Heap Sort because
of the O( n log(n) ).

14
4. If we are stuck in finding the Kthsmallest (or largest) value of a number then heaps can solve the
problem in an easy and fast manner.
5. Used by JVM (Java Virtual Machine) to store Java objects.
APPLICATION OF GREEDY ALGORITHM:
1. Dijkstra algorithm.
2. Shopping on a tight budget but want to buy gifts for all family members.
3. Prim’s and Kruskal’s algorithms are used for finding the minimum spanning trees.
DIJKSTRA ALGORITHM
1. Used in applications like Google Maps to find the shortest path in a graph.
PRIM’S and KRUSKAL’S
1. Used for finding the minimum spanning trees.
APPLICATION OF DYNAMIC PROGRAMMING:
A. Real-life examples
1. In Google Maps to find the shortest path between the source and the series of destinations (one by
one) out of the various available paths.
2. In networking to transfer data from a sender to various receivers in a sequential manner.
B. Applications in Computer science
1. Multi-stage graph
2. Traveling salesman problem
3. Largest common subsequence – to identify similar videos used by youtube
4. Optimal search binary tree- to get optimized search results.
5. Single source shortest path- Bellman-Ford Algorithm.
6. Document Distance Algorithms- to identify the extent of similarity between two text documents used
by Search engines like Google, Wikipedia, Quora, and other websites.
7. To schedule jobs on a processor.
8. To find potential partners.
9. In theory, graphics, AI, system.
APPLICATION OF BACKTRACKING:
1. Suppose we are coding a chess-playing algorithm and at a certain point, the algorithm finds that a set
of steps fails to win. In this situation, the algorithm will reverse back to the safe state and try another
possible set of steps.
2. Sudoku solver

15
3. 2048 game
4. Computer networking.
5. To solve problem of the N Queen.
6. To solve the problem of the Maze.
7. To find the Hamiltonian Path present in a graph.
8. To the love problem of Knight’s Tour Problem.

MISC:
1. Binary search can be used in negotiations.
If the seller has a price in mind but does not reveal it, you can name a low bid, to which the seller
will respond with a high ask price. You can then raise your bid, and the seller can lower her asking
price. Since each of you has an ideal price in mind, the named bids and asks can be roughly halfway
toward the price desirable by each side. This is not exactly the binary search algorithm, but very
much in the spirit of binary search.
2. Hashmap has its internal implementation in the AVL tree.
3.2 Analyzing the running time of various algorithms associated with data structures.
Analysing Time Complexity in Data Structures and Algorithms

16

You might also like