4.2 Fundamentals of data structures Flashcards

(29 cards)

1
Q

What is an array

A

an indexed set of related elements, they are finite and can only contain one data type

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

what is a one-dimensional and two-dimensional array useful for

A

1D is useful to store vectors
2D is useful to store a matrix

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

what are the python commands to read/write to files

A

open: file_object = open(“filename”, “a”)
where a is append mode, w is write mode and r is read more
read: file_object.read()
write: file_object.write(“”)
close: file_object.close()

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

What are static structures

A

they have a fixed size so they are declared in memory as a series of sequential contiguous memory locations - if a data item exceeds the number in the static structure an overflow error will occur

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

What are dynamic structures

A

they change in size to store content so they are stored in memory with references to where the next item is stored - new data items can be added but required more work for computer to set up and use

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

what are the uses of a queue

A

the breadth first search algorithm uses a queue to track which nodes have been visited
also queues are used by keyboard buffers so letters are typed in the order clicked

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

compare advantages and disadvantages of static and dynamic structures

A

Static structures are simple to implement and provides fast access to elements, but they can be inefficient as memory may be wasted if the structure is too large, or it may be unable to store all required data if too small
Dynamic structures are more flexible and efficient in terms of memory usage, but they are more complex to manage, can be slower to access due to the use of pointers and may lead to fragmentation or memory leaks if not handled correctly.

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

what structure is a queue

A

a first in first out structure so it has two pointers, one at the front and one at the rear - the rear pointer is used to identify where to place new items and the front pointer is used to identify the items to return first

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

why using a circular queue is more efficient than a linear queue

A

a circular queue is more efficient because it reuses empty spaces left by dequeued elements. In a linear queue, once the rear reaches the end, no more elements can be added even if there is space at the front. A circular queue solves this by wrapping around, making better use of memory and avoiding the need to shift elements.

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

what is a graph

A

graphs are data structures used to represent more complex relationships - they consist of nodes which are joined by edges

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

what are typical uses for graphs

A

Social networks – representing users as nodes and friendships as edges
Routing and navigation – cities or locations as nodes, roads as edges
Network topology – devices as nodes, connections as edges

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

what is a weighted graph

A

A weighted graph is a graph where each edge has a numerical value representing things like cost, distance, or time

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

what are nodes and edges

A

nodes represent individual entities whereas edges are the connections between nodes, showing relationships

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

what is the difference between a directed and undirected graph

A

In a directed graph, edges have a direction so the relationship is one-way whereas in an undirected graph, edges have no direction representing a two-way relationship.

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

how is an adjacency matrix used to represent graph

A

uses a 2d array, where each row and column corresponds to a node, if there is an edge between two nodes, the matrix cell at that row and column is set to 1 (or the weight if it’s a weighted graph) otherwise, it is 0. In a directed graph, the direction is shown by which side of the matrix the value is on.

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

how is an adjacency list used to represent graph

A

An adjacency list represents a graph by storing a list of nodes, where each node has its own list of adjacent nodes.

17
Q

when to use adjacency list or adjacency matrix

A

Use an adjacency list when the graph is sparse (few edges) - it’s more space-efficient and faster to iterate over neighbors.
Use an adjacency matrix when the graph is dense (many edges) or when you need to check if an edge exists quickly

18
Q

what is a tree

A

a connected, undirected graph with no cycles

19
Q

what is a rooted tree

A

a type of tree data structure where one node is designated as the root, and all other nodes are connected by a single path from the root. It has a hierarchical structure, with parent-child relationships between nodes

20
Q

what is a binary tree and a common application

A

a rooted tree in which each node has at most two children - common application is a binary search tree

21
Q

what is a hash table

A

a data structure that stores key-value pairs, it uses a hash function to compute an index (hash code) in an array, where the corresponding value is stored

22
Q

what are the uses of hash tables

A

Hash tables are used for fast data lookup, such as in dictionaries, caches, database indexing, symbol tables, and for tasks like counting frequencies or ensuring unique items in sets

23
Q

what is a hashing algorithm

A

takes an input and outputs a hash, a hash is unique to the input and cannot be reversed

24
Q

what is meant by a collision in a hash table

A

A collision in a hash table occurs when two different keys produce the same hash index, meaning they try to store data in the same location

25
how are collisions handled using rehashing
Rehashing handles collisions by applying a second hash function or probing strategy to find a new empty slot in the hash table. Common methods include linear probing which is checking the next slot in the table until an empty slot is found
26
what is a dictionary
an abstract data type that stores key-value pairs, allowing you to quickly retrieve a value using its key
27
what are applications of dictionaries
used for tasks like storing user data, counting word frequencies, mapping names to phone numbers, tracking inventory, and creating lookup tables for quick access to related values
28
what is the convex combination of two vectors
A convex combination of two vectors u and v is a linear combination of the form: A = 𝜆u + (1-𝜆)v where 0≤λ≤1 It means the result A lies on the line segment between u and v, and is a weighted average of the two vectors.
29
what are the applications of dot product in vectors
finding the angle between two vectors