0% found this document useful (0 votes)
104 views62 pages

DSA Week 4 Spring 2022

The document discusses different types of collections and data structures used to implement them including arrays and grids. It describes operations on arrays like insertion, removal and resizing. It also covers two-dimensional arrays or grids and how to define a grid class.

Uploaded by

Mehmood Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views62 pages

DSA Week 4 Spring 2022

The document discusses different types of collections and data structures used to implement them including arrays and grids. It describes operations on arrays like insertion, removal and resizing. It also covers two-dimensional arrays or grids and how to define a grid class.

Uploaded by

Mehmood Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

CS211 – DATA STRUCTURES AND ALGORITHMS

LECTURE 7: PYTHON PROGRAMMING ARRAY & STACKS


WEEK -04

Asst. Prof. Syed Faisal Ali


[email protected]
Computer Science
(Software Engineering)
Sec A
SPRING 2022
OVERVIEW OF COLLECTIONS

 Collection: Group of items that we want to treat as conceptual unit


 Examples:
 Lists, strings, stacks, queues, binary search trees, heaps, graphs, dictionaries, sets, and bags
 Can be homogeneous or heterogeneous
 Lists are heterogeneous in Python
 Four main categories:
 Linear, hierarchical, graph, and unordered

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 2


LINEAR COLLECTIONS

 Ordered by position

 Everyday examples:
 Grocery lists
 Stacks of dinner plates
 A line of customers waiting at a bank

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 3


HIERARCHICAL COLLECTIONS

 Structure reminiscent of an upside-down tree

 D3’s parent is D1; its children are D4, D5, and D6


 Examples: a file directory system, a company’s organizational tree, a book’s table of contents

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 4


GRAPH COLLECTIONS
 Graph: Collection in which each data item can have many predecessors and many
successors

 D3’s neighbors are its predecessors and successors


 Examples: Maps of airline routes between cities; electrical wiring diagrams for
buildings
COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 5
UNORDERED COLLECTIONS
 Items are not in any particular order
 One cannot meaningfully speak of an item’s predecessor or successor

 Example: Bag of marbles


COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 6
OPERATIONS ON COLLECTIONS

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 7


Operations on Collections (continued)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 8


ABSTRACTION AND ABSTRACT DATA TYPES

 To a user, a collection is an abstraction


 In CS, collections are abstract data types (ADTs)
 ADT users are concerned with learning its interface
 Developers are concerned with implementing their behavior in the most efficient manner possible
 In Python, methods are the smallest unit of abstraction, classes are the next in size, and modules are the largest
 We will implement ADTs as classes or sets of related classes in modules

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 9


DATA STRUCTURES FOR IMPLEMENTING COLLECTIONS: ARRAYS

 “Data structure” and “concrete data type” refer to the internal representation of an ADT’s data
 The two data structures most often used to implement collections in most programming languages are arrays
and linked structures
 Different approaches to storing and accessing data in the computer’s memory
 Different space/time trade-offs in the algorithms that manipulate the collections

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 10


THE ARRAY DATA STRUCTURE

 Array: Underlying data structure of a Python list


 More restrictive than Python lists
 We’ll define an Array class

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 11


RANDOM ACCESS AND CONTIGUOUS MEMORY
 Array indexing is a random access operation

 Address of an item: base address + offset


 Index operation has two steps:
COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 12
STATIC MEMORY AND DYNAMIC MEMORY

 Arrays in older languages were static


 Modern languages support dynamic arrays
 To readjust length of an array at run time:
 Create an array with a reasonable default size at start-up
 When it cannot hold more data, create a new, larger array and transfer the data items from the old array
 When the array seems to be wasting memory, decrease its length in a similar manner
 These adjustments are automatic with Python lists

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 13


PHYSICAL SIZE AND LOGICAL SIZE
 The physical size of an array is its total number of array cells
 The logical size of an array is the number of items currently in it

 To avoid reading garbage, must track both sizes

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 14


PHYSICAL SIZE AND LOGICAL SIZE (CONTINUED)

 In general, the logical and physical size tell us important things about the state of the array:
 If the logical size is 0, the array is empty
 Otherwise, at any given time, the index of the last item in the array is the logical size minus 1.
 If the logical size equals the physical size, there is no more room for data in the array

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 15


OPERATIONS ON ARRAYS

 We now discuss the implementation of several operations on arrays


 In our examples, we assume the following data settings:

 These operations would be used to define methods for collections that contain arrays

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 16


INCREASING THE SIZE OF AN ARRAY

 The resizing process consists of three steps:


 Create a new, larger array
 Copy the data from the old array to the new array
 Reset the old array variable to the new array object

 To achieve more reasonable time performance, double array size each time you increase its size:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 17


DECREASING THE SIZE OF AN ARRAY

 This operation occurs in Python’s list when a pop results in memory wasted beyond a threshold
 Steps:
 Create a new, smaller array
 Copy the data from the old array to the new array
 Reset the old array variable to the new array object

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 18


INSERTING AN ITEM INTO AN ARRAY THAT GROWS

 Programmer must do four things:


 Check for available space and increase the physical size of the array, if necessary
 Shift items from logical end of array to target index position down by one
 To open hole for new item at target index

 Assign new item to target index position


 Increment logical size by one

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 19


INSERTING AN ITEM INTO AN ARRAY THAT GROWS (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 20


REMOVING AN ITEM FROM AN ARRAY

 Steps:
 Shift items from target index position to logical end of array up by one
 To close hole left by removed item at target index

 Decrement logical size by one


 Check for wasted space and decrease physical size of the array, if necessary
 Time performance for shifting items is linear on average; time performance for removal is linear

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 21


REMOVING AN ITEM FROM AN ARRAY (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 22


COMPLEXITY TRADE-OFF: TIME, SPACE, AND ARRAYS

 Average-case use of memory for is O(1)


 Memory cost of using an array is its load factor
COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 23
TWO-DIMENSIONAL ARRAYS (GRIDS)
 Sometimes, two-dimensional arrays or grids are more useful than one-
dimensional arrays

 Suppose we call this grid table; to access an item:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 24


PROCESSING A GRID

 In addition to the double subscript, a grid must recognize two methods that return the number of rows and the
number of columns
 Examples: getHeight and getWidth

 The techniques for manipulating one-dimensional arrays are easily extended to grids:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 25


CREATING AND INITIALIZING A GRID
 Let’s assume that there exists a Grid class:

height initial fill value


width

 After a grid has been created, we can reset its cells to any values:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 26


DEFINING A GRID CLASS

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 27


DEFINING A GRID CLASS (CONTINUED)

 The method __getitem__ is all that is needed to support the client’s use of the double subscript:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 28


RAGGED GRIDS AND MULTIDIMENSIONAL ARRAYS

 In a ragged grid, there are a fixed number of rows, but the number of columns in
each row can vary
 An array of lists or arrays provides a suitable structure for implementing a ragged
grid
 Dimensions can be added to grid definition if needed; the only limit is computer’s
memory
 A three-dimensional array can be visualized as a box filled with smaller boxes stacked in
rows and columns
 Depth, height, and width; three indexes; three loops

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 29


LINKED STRUCTURES

 After arrays, linked structures are probably the most commonly used data structures in programs
 Like an array, a linked structure is a concrete data type that is used to implement many types of collections,
including lists
 We discuss in detail several characteristics that programmers must keep in mind when using linked structures to
implement any type of collection

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 30


SINGLY LINKED STRUCTURES AND DOUBLY LINKED STRUCTURES

An empty
link

 No random access; must traverse list


 No shifting of items needed for insertion/removal
 Resize at insertion/removal with no memory cost

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 31


NONCONTIGUOUS MEMORY AND NODES

 A linked structure decouples logical sequence of items in the structure from any ordering in memory
 Noncontiguous memory representation scheme
 The basic unit of representation in a linked structure is a node:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 32


NONCONTIGUOUS MEMORY AND NODES (CONTINUED)

 Depending on the language, you can set up nodes to use noncontiguous memory in several ways:
 Using two parallel arrays

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 33


NONCONTIGUOUS MEMORY AND NODES (CONTINUED)

 Ways to set up nodes to use noncontiguous memory (continued):


 Using pointers (a null or nil represents the empty link as a pointer value)
 Memory allocated from the object heap

 Using references to objects (e.g., Python)


 In Python, None can mean an empty link
 Automatic garbage collection frees programmer from managing the object heap

 In the discussion that follows, we use the terms link, pointer, and reference interchangeably

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 34


DEFINING A SINGLY LINKED NODE CLASS

 Node classes are fairly simple


 Flexibility and ease of use are critical
 Node instance variables are usually referenced without method calls, and constructors allow the user to set a node’s link(s)
when the node is created
 A singly linked node contains just a data item and a reference to the next node:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 35


USING THE SINGLY LINKED NODE CLASS
 Node variables are initialized to None or a new Node object

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 36


USING THE SINGLY LINKED NODE CLASS (CONTINUED)

 node1.next = node3 raises AttributeError


 Solution: node1 = Node("C", node3), or
Node1 = Node("C", None)
node1.next = node3
 To guard against exceptions:
if nodeVariable != None:
<access a field in nodeVariable>
 Like arrays, linked structures are processed with loops

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 37


USING THE SINGLY LINKED NODE CLASS (CONTINUED)

 Note that when the data are displayed, they appear in the reverse order of their insertion

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 38


OPERATIONS ON SINGLY LINKED STRUCTURES

 Almost all of the operations on arrays are index based


 Indexes are an integral part of the array structure
 Emulate index-based operations on a linked structure by manipulating links within the structure
 We explore how these manipulations are performed in common operations, such as:
 Traversals
 Insertions
 Removals

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 39


TRAVERSAL

 Traversal:Visit each node without deleting it


 Uses a temporary pointer variable
 Example:
probe = head
while probe != None:
<use or modify probe.data>
probe = probe.next
 None serves as a sentinel that stops the process

 Traversals are linear in time and require no extra memory

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 40


TRAVERSAL (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 41


SEARCHING

 Resembles a traversal, but two possible sentinels:


 Empty link
 Data item that equals the target item
 Example:
probe = head
while probe != None and targetItem != probe.data:
probe = probe.next
if probe != None:
<targetItem has been found>
else:
<targetItem is not in the linked structure>
 On average, it is linear for singly linked structures

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 42


SEARCHING (CONTINUED)
 Unfortunately, accessing the ith item of a linked structure is also a sequential search
operation
 We start at the first node and count the number of links until the ith node is reached

 Linked structures do not support random access


 Can’t use a binary search on a singly linked structure
 Solution: Use other types of linked structures

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 43


REPLACEMENT
 Replacement operations employ traversal pattern

 Replacing the ith item assumes 0 <= i < n

 Both replacement operations are linear on average

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 44


INSERTING AT THE BEGINNING

 Uses constant time and memory


COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 45
INSERTING AT THE END
 Inserting an item at the end of an array (append in a Python list) requires constant time and memory
 Unless the array must be resized
 Inserting at the end of a singly linked structure must consider two cases:

 Linear in time and constant in memory

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 46


INSERTING AT THE END (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 47


REMOVING AT THE BEGINNING

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 48


REMOVING AT THE END

 Removing an item at the end of an array (pop in a Python list) requires constant time and memory
 Unless the array must be resized
 Removing at the end of a singly linked structure must consider two cases:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 49


REMOVING AT THE END (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 50


INSERTING AT ANY POSITION
 Insertion at beginning uses code presented earlier
 In other position i, first find the node at position i - 1 (if i < n) or node at position n - 1 (if i >= n)

 Linear time performance; constant use of memory


COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 51
INSERTING AT ANY POSITION (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 52


REMOVING AT ANY POSITION

 The removal of the ith item from a linked structure has three cases:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 53


COMPLEXITY TRADE-OFF: TIME, SPACE, AND SINGLY LINKED
STRUCTURES

 The main advantage of singly linked structure over array is not time performance but memory performance

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 54


VARIATIONS ON A LINK

 A Circular Linked Structure with a Dummy Header Node


 Doubly Linked Structures

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 55


A CIRCULAR LINKED STRUCTURE WITH A DUMMY HEADER NODE
 A circular linked structure contains a link from the last node back to the first node in the structure
 Dummy header node serves as a marker for the beginning and the end of the linked structure

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 56


A CIRCULAR LINKED STRUCTURE WITH A DUMMY HEADER NODE
(CONTINUED)

 To initialize the empty linked structure:

 To insert at the ith position:

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 57


DOUBLY LINKED STRUCTURES

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 58


DOUBLY LINKED STRUCTURES (CONTINUED)

To insert a new item at the


end of the linked structure

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 59


DOUBLY LINKED STRUCTURES (CONTINUED)

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 60


ASSIGNMENT #3

 Read chapter 3 & 4.


 You need to make notes as previous and do practice for the questions that will be uploaded to you on MS teams.

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 61


END OF LECTURE

COPY RIGHT: ASST. PROF SYED FAISAL ALI 4/12/2022 62

You might also like