data structures 2 Flashcards
graph
set of vertices or nodes connected by weighted or unweighted edges or arcs which are one or two way
undirected graph
set of vertices or nodes connected by weighted/unweighted edges or arcs which are bidirectional
directed/ digraph
set of vertices or nodes connected by weighted/unweighted edges or arcs which are all one way
weighted edges
there is a cost to go from one to another node eg distance
computer needs to represent connections and weighting in a ___ representation
structured and numerical
ways of implementing a graph
adjacency matrix and adjacency list
adjacency matrix
uses a 2D array, each row/column represents a node. A value in the intersection of (i,j) indicates edge connecting node i and node j.
Value stored in the cell is the weighting. If graph is unweighted then a 1 is stored in the cell.
Undirected graphs are symmetric and v.v
adv/disadv adjacency matrix
ADV: convenient, easy to add an edge
DISADV: if many nodes but sparse graph with few edges means most cells are empty: larger the graph the more memory wasted
if using a static 2D array, it is harder to add or delete nodes
adjacency list
a list of all the nodes is created, with all the nodes pointing to a list of all adjacent nodes (adjacency lists) to which it is directly linked. If weighted, adjacency list can be implemented as a dictionary: key= node being linked to, value= edge weight. If unweighted, dictionary data structure not necessary
adv adjacency list
uses much less memory to represent a sparsely connected graph: space efficient
depth first traversal
go as far down one route as possible before backtracking and taking next route
breadth first search/traversal
visit all the neighbours of a node then all the neighbours of the nodes just visited, before moving further away from the start node
applications of graphs
- computer networks: node representing computers and weighted edges representing bandwidth between them
- web pages and links eg Google PageRank
- roads eg nodes: towns edges: distance, fare, time
- states in a finite state machine
- tasks in a project where some have to be completed before others
tree
non linear data structure for storing hierarchical data, made of a number of nodes and outgoing edges
uses of trees
storing hierarchical data eg game moves/folder structure
to make info easy to search
manipulating sorted lists of data
rooted tree
a tree with a root
tree node
contains tree data
tree edge
an edge connects two nodes; every node except root node has an incoming edge from exactly one other node
root of tree
only node with no incoming edges
tree children
set of nodes that have incoming edges from the same node
tree parent
a node is a parent of all the nodes it connects to with outgoing edges
subtree
the set of nodes compromised of a parent and all descendants of the parent. it can be a leaf
leaf node
node with no children
rooted tree in terms of graphs
its a connected graph. A node can only be connected to one parent node and to its children. It has no CYCLES as no edges between children or branches.
binary tree
a rooted tree in which each node has a maximum of two children
binary search tree
binary tree which holds items in a way they can easily and quickly be searched and items added, and the whole tree can be printed out in sequence
ways of traversing a tree. The names refer to…
pre order
in order
post order
The names refer to whether the root of each subtree is visited before, between or after both branches have been traversed
pre order
root, left, right. Used in prefix notation eg in functional programming notation ie x = sum a b (sum being root), used in some compilers and calculators
in order traversal
left root right. used in infix notation eg a + b, outputs in alphabetical order
post order traveral
left right root. used in post-fix notation, reverse polish notation- used by compilers to evaluate expressions
you implement a binary search tree using..
tree[0] = 1, 17, 4 means..
pointer of ‘-1’ means..
array of records, list of tuples, 3 separate lists or arrays (for each pointer and the data items).
tree[0] = 1, 17, 4 means the first node holds data ‘17’ and has tree[1] on left and tree[4] on right.
Pointer of -1 is a rogue value ie no child on relevant side
in order traversal pseudocode
procedure inorderTraverse(p) if tree[p].left != -1 inorderTraverse(tree[p].left) endif print(tree[p].data); if tree[p].right != -1 inorderTraverse(tree[p].right) endif endprocedure
post order and pre order traversal pseudocode
same as in order traversal but for
post order: tree[p].data is printed after left and right
pre order: tree[p].data is printed before left and right