7 | Data structures / graphs Flashcards
Definition: Data Structure
Data structure is a particular way of organizing and storing data in a computer so it can be accessed and modified efficiently.
Data structure
- collection of data values
- relationship among the values
- functions / operations which can be applied to the data
Definition: Array
Array – data structure consisting of a fixed number of items of the same types (items = elements) usually stored in contiguous storage cells.
Access to every element is obtained by specifying a single subscript or index.
Name an advantage of arrays
We can calculate the address of any given element in a
constant time
It takes time in the order of Ο(1) to
* read a value of a single element
* change a value of a single element
Name a disadvantage of arrays
no dynamic value modification (fixed number of items, of same type)
What is the algorithm for initialising an array? what is the time complexity?
for i in 1 to n do:
tab(i) <- 0
time: cn, is in Theta(n)
What is the algorithm for finding the minimum of an array? what is the time complexity?
min = T(1)
for i in 1 to n do:
if min > T(i)
min = T(i)
time:
c_i + ∑(2<=i<=n) of c + c = 2c + (n-1)c ∈ Θ(n)
What is the algorithm for moving elements (when inserting a new element into an array)? what is the time complexity?
algorithm:
for i in n+1 to k+1
T(i) <- T(i-1)
T(k) <- a
time:
[∑(k+1<= i<= n+1) of c ] + c
= [ (n+1) - (k+1) + 1)c + c
= (n+1)c + c
∈ Θ(n)
Stack
Definition?
Stack – data structure in which items are added to and
subsequently removed from the structure on a last-in-first-out
(LIFO) basis
Stack
Operations on a stack?
push, pop
Stack
Push?
Add an item x to the stack (push)
counter ← counter + 1
stack[counter] ← x
Stack
Pop?
Remove an item from the stack (pop)
𝑥 ← stack[counter]
counter ← counter - 1
Stack: 10, 3, 5
what happens when you pop, pop, push, pop, pop ?
–> you have an empty stack
Stack
implementation?
Implemented by an array whose index runs from 1 to the
maximum number of elements allowed along with a counter
Queue
Definition?
Queue – data structure in which items are added and removed
on a first-in-first-out (FIFO) basis
Queue
Operations?
Add an item to the queue (enqueue)
Remove an item from the queue (dequeue)
Stacks and Queues
Disadvantages?
- Maximum elements allowed should be known a priori
- Too little, will affect the performance
- Too many, will waste resources
Record
Definition?
Record – data structure comprised a fixed number of items,
called fields, that are possibly of different types
Two-dimensional array
Reading and modifying a value in ____ time
Reading and modifying a value in constant time
since the address of any given element can be
determined in constant time
Record
Access to the fields?
Access to the fields (dot notation) for Jeanne
Jeanne.name, Jeanne.age
Record
pointers
Record are commonly used in conjuction with pointers
type student = ↑ person
says that student is a pointer to a record of type person
such a record can be created dynamically
student ← new person
student ↑ means the record that student points to
student ↑.name, student ↑.age are the fields
If a pointer has a value of nil, it does not point to any record
List
Definition?
List is a collection of items arranged in a certain order.
Unlike arrays and records, the number of items in a list is
usually not fixed and is not bounded a priori
List
An item of a list is called a ____.
An item of a list is called a node.
List
Two types of implementation?
List can be implemented
as an array
or as with use of pointers and records (for the nodes)
List
implemented as an array?
List can be implemented as an array (of integers)
type list = record
counter: 0 . . 𝑚𝑎𝑥𝑙𝑒𝑛𝑔𝑡ℎ
value: array [1. . maxlength] of integers
List
implemented with use of pointers and records?
List can be implemented with use of pointers and records (for the nodes)
type list = ↑ node
type node = record
value:integer
next: ↑ node
Each node, except the last, points to its successor
The last has a value nil
List
different implementations - determining the kth item?
type list = record
counter: 0 . . 𝑚𝑎𝑥𝑙𝑒𝑛𝑔𝑡ℎ
value: array [1. . maxlength] of integers
–> in constant time
type list = ↑ node
type node = record
value:integer
next: ↑ node
In time 𝑂(𝑘) , due to the following of pointers
List
different implementations - reserve storage a priori?
type list = record
counter: 0 . . 𝑚𝑎𝑥𝑙𝑒𝑛𝑔𝑡ℎ
value: array [1. . maxlength] of integers
–> yes
type list = ↑ node
type node = record
value:integer
next: ↑ node
–> No, it can grow and shrink dynamically
List
different implementations - inserting an item after the kth item?
Inserting an item after the k-th item
type list = record
counter: 0 . . 𝑚𝑎𝑥𝑙𝑒𝑛𝑔𝑡ℎ
value: array [1. . maxlength] of integers
–> in time Θ 𝑛
type list = ↑ node
type node = record
value:integer
next: ↑ node
–> In time 𝑂(1) , by copying and changing few fields
List
Implementation with array + counter vs with pointers and nodes
what do we gain / lose?
With nodes, pointers:
we gained dynamic memory allocation
Accessing: but we lost efficiency of accessing an element of a list: we need to traverse list to find the kth element
Insertion: much easier to insert a new element after kth element - only two operations - constant time operation
Graphs
Informal definition?
Informal: a graph (𝐺) is an abstract representation of a set of components (nodes/vertices) where some pairs of the components are connected by links (edges).
Graphs
Formal definition?
A graph is a pair 𝐺 = (𝑉, 𝐸) , such that 𝐸 ⊆ {{𝑢, 𝑣}|𝑢, 𝑣 ∈ 𝑉}.
We refer to 𝑉 as a set of nodes (or vertices) and 𝐸 as a set of edges.