7 | Data structures / graphs Flashcards

1
Q

Definition: Data Structure

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Definition: Array

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name an advantage of arrays

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Name a disadvantage of arrays

A

no dynamic value modification (fixed number of items, of same type)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the algorithm for initialising an array? what is the time complexity?

A

for i in 1 to n do:
tab(i) <- 0

time: cn, is in Theta(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the algorithm for finding the minimum of an array? what is the time complexity?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the algorithm for moving elements (when inserting a new element into an array)? what is the time complexity?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stack

Definition?

A

Stack – data structure in which items are added to and
subsequently removed from the structure on a last-in-first-out
(LIFO) basis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Stack

Operations on a stack?

A

push, pop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Stack

Push?

A

Add an item x to the stack (push)

counter ← counter + 1
stack[counter] ← x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Stack

Pop?

A

Remove an item from the stack (pop)

π‘₯ ← stack[counter]
counter ← counter - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Stack: 10, 3, 5

what happens when you pop, pop, push, pop, pop ?

A

–> you have an empty stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Stack

implementation?

A

Implemented by an array whose index runs from 1 to the
maximum number of elements allowed along with a counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Queue

Definition?

A

Queue – data structure in which items are added and removed
on a first-in-first-out (FIFO) basis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Queue

Operations?

A

Add an item to the queue (enqueue)
Remove an item from the queue (dequeue)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Stacks and Queues

Disadvantages?

A
  • Maximum elements allowed should be known a priori
  • Too little, will affect the performance
  • Too many, will waste resources
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Record

Definition?

A

Record – data structure comprised a fixed number of items,
called fields, that are possibly of different types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Two-dimensional array

Reading and modifying a value in ____ time

A

Reading and modifying a value in constant time

since the address of any given element can be
determined in constant time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Record

Access to the fields?

A

Access to the fields (dot notation) for Jeanne
Jeanne.name, Jeanne.age

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Record

pointers

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

List

Definition?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

List

An item of a list is called a ____.

A

An item of a list is called a node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

List

Two types of implementation?

A

List can be implemented

as an array

or as with use of pointers and records (for the nodes)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

List

implemented as an array?

A

List can be implemented as an array (of integers)

type list = record
counter: 0 . . π‘šπ‘Žπ‘₯π‘™π‘’π‘›π‘”π‘‘β„Ž
value: array [1. . maxlength] of integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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
26
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
27
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
28
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
29
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
30
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).
31
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.
32
Graphs If 𝑒 = {𝑒, 𝑣} is an edge, then: 𝑒 and 𝑣 ________ 𝑒
are incident with
33
Graphs If 𝑒 = {𝑒, 𝑣} is an edge, then: 𝑒 ______ 𝑒 and 𝑣.
is incident with connects (only different types can be incident!)
34
Graphs If 𝑒 = {𝑒, 𝑣} is an edge, then: 𝑒 and 𝑣 are ______
adjacent neighbours (only same type can be adjacent!)
35
Graphs The number of nodes will be denoted as
𝑛 = |𝑉(𝐺)|
36
Graphs The number of edges will be denoted as π‘š = |𝐸(𝐺)|
π‘š = |𝐸(𝐺)|
37
Graphs simple graph?
A graph is called simple, if it does not contain multi-edges or loops. Otherwise it is called a multi-graph
38
Graphs parallel edges?
Two edges 𝑒 and 𝑓 are called parallel, if they connect the same vertices. Parallel edges form a multi-edge.
39
Graphs loop?
If an edge connects a vertex to itself (i.e. 𝑒 = {𝑣, 𝑣}) it is called a loop.
40
Graphs Directed graph?
(A directed graph is a pair 𝐺 = 𝑉, 𝐸 , such that 𝐸 βŠ† {(𝑒, 𝑣)|𝑒, 𝑣 ∈ 𝑉}. We refer to 𝑉 as a set of nodes (or vertices) and 𝐸 as a set of directed edges.) __For the edge 𝑒 = (𝑒, 𝑣), 𝑒 is the head and 𝑣 is the tail__
41
Graphs Weighted directed graph?
A weighted directed graph is a pair 𝐺 = 𝑉, 𝐸 , such that 𝐸 βŠ† {(𝑒, 𝑣)|𝑒, 𝑣 ∈ 𝑉}, with a weight function 𝑀: 𝐸 β†’ ℝ. weight = distance, resource availability / capacity
42
Graphs How to represent a graph on a computer?
Three ways: Adjacency matrix incidence matrix or adjacency list
43
Graphs Adjacency matrix - undirected graphs? symmetrical?
𝐴[𝑒, 𝑣] = 1 𝑖𝑓 (𝑒, 𝑣) ∈ 𝐸(𝐺) 𝐴[𝑒, 𝑣] = 0 𝑖𝑓 (𝑒, 𝑣) βˆ‰ 𝐸(𝐺) Constant time to check for presence of an edge between two nodes Edge weights can be considered (instead of 1). Adjacency matrix of an undirected graph is symmetric
44
Graphs Adjacency matrix - directed graphs? symmetrical?
𝐴[𝑒, 𝑣] = 1 𝑖𝑓 (𝑒, 𝑣) ∈ 𝐸(𝐺) 𝐴[𝑒, 𝑣] = 0 𝑖𝑓 (𝑒, 𝑣) βˆ‰ 𝐸(𝐺) Adjacency matrix of an undirected graph may not be symmetrical
45
Graphs Incidence matrix - undirected graphs?
𝐴[𝑒,𝑒] = 1 𝑖𝑓 𝑒 ∈ 𝑒 𝐴[𝑒,𝑒] = 0 𝑖𝑓 𝑒 βˆ‰ 𝑒 Edge weights can be considered (instead of 1).
46
Graphs Incidence matrix - directed graphs
𝐴[𝑒,𝑒] = βˆ’1 𝑖𝑓𝑒 = (𝑒, 𝑣) 𝐴[𝑒,𝑒] = 1 𝑖𝑓 𝑒 = (𝑣, 𝑒) 𝐴[𝑒,𝑒] = 0, 𝑒 βˆ‰ 𝑒
47
Graphs incidence relation ?
between different types node - edge (incidence matrix allows you to represent more relationships eg stoichiometric matrices in CMCN)
48
Graphs adjacence relation
between same type eg node - node edge - edge
49
Graphs Advantage/disadvantage of matrix representation?
advantages: - constant time to determine whether there is an edge between two nodes - can directly see what neighbours are disadvantages - not dynamically changeable
50
Graphs Advantage/disadvantage of list representation?
- you have to traverse through list in order to find a node in the list - dynamic changes allowed - Space required is quadratic in the number of nodes - For sparse graphs (number of edges which grows linearly with the number of edges), list representation is space efficient - Look-up of neighbours in constant time, but checking all neighbours requires investigation of an entire row - All neighbours can be investigated in time smaller than 𝑛, but looking up neighbours is not in constant time
51
Graphs What is a weighted directed hypergraph
a pair 𝐺 = (𝑉, 𝐸), such that 𝐸 βŠ† (𝑆,𝑄) , 𝑆,𝑄 ∈ 𝒫(𝑉), with a weight function 𝑀𝑆: 𝑆 β†’ ℝ weight = resource demand / production eg 𝑉 = {𝑣1, 𝑣2, 𝑣3, 𝑣4, 𝑣5} 𝑀({𝑣1, 𝑣6}) = {βˆ’1, βˆ’2} 𝑀({𝑣4, 𝑣7}) = {1,1}
52
Graphs How can you represent a hypergraph on a computer?
incidence matrix (eg stoichiometric matrix in CMCN)
53
Graphs Walk - formal definition? Closed if?
Let 𝐺 = (𝑉, 𝐸) be a simple graph. A sequence π‘Š = (𝑣1, 𝑒1, 𝑣2, … π‘’π‘˜βˆ’1, π‘£π‘˜) with 𝑣𝑖 ∈ 𝑉(𝐺) , 1 ≀ 𝑖 ≀ π‘˜ and 𝑒𝑖 = 𝑣𝑖, 𝑣𝑖+1 ∈ 𝐸(𝐺) , 1 ≀ 𝑖 ≀ π‘˜ βˆ’ 1 is called a walk. A walk is closed if 𝑣1 = π‘£π‘˜
54
Graphs Walk: can nodes and edges be repeated?
Yes (in a path they cannot)
55
Graphs Path - formal definition?
Let 𝐺 = (𝑉, 𝐸) be a simple graph. A walk π‘Š = (𝑣1, 𝑒1, 𝑣2, … π‘’π‘˜βˆ’1, π‘£π‘˜) is a path if 𝑣𝑖 β‰  𝑣j, 1 ≀ 𝑖, j ≀ π‘˜, i β‰  j A path is a cycle if 𝑣1 = π‘£π‘˜
56
Graphs Path: can nodes and edges be repeated?
No (except in a cycle - then first/last is the same) If repetition --> a path but not a walk
57
Graphs minimal vs. minimum?
minimal refers to subgraphs (or subsets) minimum refers to cardinality (number of nodes/edges) A subgraph 𝐻 of 𝐺 is minimal with respect to a property 𝑃 if 𝐻 satisfies 𝑃 and there exists no strict subgraph 𝐻′ of 𝐻 that also satisfies 𝑃. (Analogously: maximal vs. maximum)
58
Graphs How do you know if a graph is minimal?
check if removal of any object from the structure breaks the property in question --> if so then it is minimal
59
Graphs 1. For some property, if you have the minimal, can you find the minimum? 2. If you have the minimum, can you find the minimal?
Yes No
60
Graph Connectedness?
Let 𝐺 = (𝑉, 𝐸) be a graph. Let 𝐺 is connected if for every two nodes there exists a path between any two nodes 𝑒 and 𝑣 in 𝐺 Connected component is a maximal connected subgraph
61
Graphs what is a connected component? relevant for what?
re connectedness: connected component is a maximal connected subgraph. (relevant for minimum spanning trees)
62
Graphs Tree definition
A graph 𝐺 is a tree if it is connected and has no cycles. Hence, a connected forest is a tree.
63
Graphs Forest
A graph 𝐺 is a forest if it has no cycles. Hence, a connected forest is a tree.
64
Graphs rooted tree
A tree is called a rooted tree if one vertex has been designated the root.
65
Graphs rooted tree: - parent/child of a vertex (uniqueness?)
66
Graphs rooted tree: - parent/child of a vertex (uniqueness?)
- the parent of a vertex is the vertex connected to it on the path to the root, - every vertex except the root has a unique parent, - a child of a vertex v is a vertex of which v is the parent
67
Graphs rooted tree: - leaf - node - ancestor
- a leaf is a vertex without children - a node that is not a leaf is call internal - Node u is an ancestor of v if it lies on the path from the root to v
68
Graphs Ways to represent a rooted tree on a computer What are the two ways?
Two ways (↑ = a pointer) ``` type treenode1 = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› eldestchild: ↑ treenode 1 next-sibling: ↑ treenode 1 ``` ``` type treenode2 = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› parent: ↑ treenode 2 ```
69
Graphs Two qays to represent a rooted tree on a computer ``` type treenode1 = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› eldestchild, next-sibling: ↑ treenode 1 ``` ``` type treenode2 = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› parent: ↑ treenode 2 ``` Advantages/disadvantages of 1/2 ?
1: All nodes can be represented with the same record Many operations are inefficient (e.g. finding a parent of a node) 2: Very economical representation Many operations are inefficient, unless they are applied to a node always going up the rooted tree
70
Graphs Representation of binary and k-ary trees
``` type binary-node = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› left-child: ↑ binary-node right-child: ↑ binary-node ``` ``` type k-ary-node = record value: π‘–π‘›π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘‘π‘–π‘œπ‘› child: array [1. . π‘˜] π‘œπ‘“ ↑ π‘˜-π‘Žπ‘Ÿπ‘¦-node ```
71
Graphs Search tree definition
A binary tree where value contained in every internal node is both: - greater than or equal to value in left child or any of childβ€˜s descendants - smaller than or equal to the value in the right child or any of the childβ€˜s descendants
72
Graphs Search based on a search tree - algo?
``` function search(x,r) if r = nil then return nil #{x is not in the tree} else if x = r ↑.value then return r else if x < r ↑.value then return search(x, r ↑.left-child) else return search(x, r ↑.right-child) ```
73
Graphs Height of a tree?
Height of it's root
74
Graphs Nodes: height, depth, level?
- Height of a node: number of edges in the longest path from the node to a leaf - Depth of a node: number of edges in the path from the root to the node - Level of a node: height of the root – depth of the nodes
75
Graphs special node in a binary tree?
Zoran: parent of only one child (online: Only child of its parent)
76
Graphs essentially complete binary tree? (Heap)
Binary tree is essentialy complete if: - each internal node has exactly two children (possible exception of one special node) - If there is a special node: situated on level 1, has a left but no right child - Either all leaves are on level 0, or else they are on levels 0 and 1, and no leaf on level 1 is to the left of an internal node at the same level
77
Graphs Heap definition?
Heap - special type of a rooted tree - essentially complete binary tree such that value of any internal node is greater than or equal to the values of its children - can be implemented efficiently with an array, without any explicit pointers
78
Graphs essentially complete binary tree of height k: - how many nodes on level i? - how many nodes at most on level 0? (Heap)
- 2π‘˜βˆ’π‘– nodes on level 𝑖, 1 ≀ 𝑖 ≀ π‘˜ - at most 2π‘˜ nodes on level 0
79
Graphs essentially complete binary tree - relationship between k (height) and n (nodes)? (Heap)
2π‘˜ ≀ 𝑛 < 2π‘˜+1 Therefore, π‘˜ ≀ log2𝑛 < π‘˜ + 1, i.e. π‘˜ = ⌊log2π‘›βŒ‹