Data Structures Page 1 of 24
A.1. Arrays (Vectors) n-element vector start address + ielementsize 0 +1 +2 +3 +4... +n-1 start address continuous memory block static, if size is known at compile time dynamic, if size is determined at run time random access of O(1) to each element optimal cache prefetch, pipelining for sequential processing of data Page 2 of 24
n 2 1 3 Index calculation 2-dimensional array (matrix) startaddress n 2 0 +1 +2 +3 +4... +n2-1 (0,0) (0,1) +n 2 +n +1... 2 +2n2-1 (1,0) (1,1)... +(n1-1)n2 +nn-1 1 2 n 1 startaddress + (i n +i ) elementsize 1 2 2 0-based indices (0 i d n d 1 d dim): ( address = startaddress + elementsize (for 1-based indices replace i d with (i d 1)) startaddress i dim + dim 1 d=1 n 3 ( )) dim i d p=d+1 n p Page 3 of 24 n 1 +(n1-1)n2n 3 +(n n 2-1)n +n1n2n 3-1
insertion/deletion of vector elements needs copy operations insert an element into a vector vector more data copy data new allocated vector if enough extra memory is reserved (at the end), there s no need to allocate a new vector delete an element into a vector copy elements memory leak Page 4 of 24 allocation of new vector to avoid memory leaks arrays are unflexible with respect of insertion/deletion
A.2. Lists (double) linked list intrusive non-intrusive data data there s a pointer to the next element double linked lists have also pointer to previous element intrusive lists store data elements in links (elements belong to no more than one list) for cyclic lists the first and last element are connected (there s no first or last element...) (bi)directional sequential access (iterator) O(n) access to elements Page 5 of 24
insertion/deletion of list elements needs changing pointers insert an element into a list delete an element from a list Page 6 of 24 lists are flexible to insert/remove sublists
A.3. Hash tables hash table a hash function will map a search key to a table entry the hash function is not surjective: a table entry might be empty not injective: > 1 search keys might be mapped to a table entry collision handling necessary common method: a collision list for each table entry instead of one large list, there will be several smaller ones combining array and list... Page 7 of 24
A.4. Trees binary tree root layer 0 layer 1 leave a tree is a graph, which consists of nodes and edges one node is called root node each node, except the root node, has one parent and is a child of that node a leaf node has no children there s exactly one path from one node to another nodes with the same depth l (distance to the root) form the layer l a tree with l layers has height h = l, which is the maximum depth the structure is recursive: a node with all its children and their children forms a subtree the nodes of a binary tree have at most 2 children: a left and a right one the lth layer of a binary tree can at most contains 2 l, the full (complete) tree n = 2 h+1 1 nodes. The height of such a tree is h = log 2 (n + 1) 1 Page 8 of 24
Syntax/Expression trees * - 7 4 5 inorder traversal: ( (7-4) * 5) preorder traversal: (* (- 7 4) 5) postorder traversal: ( (7 4 -) 5 *) the inner nodes of this binary tree contain binary (or unary) operators with the children as operands each leaf node contains a constant (or variable) postorder traversal (left child,right child,root node) leads to postfix notation, known as RPN (Reverse Polish Notation) (stack) widespread infix notation needs brackets (postfix doesn t) Page 9 of 24
Point-kD-tree w 010 01 11 0 1 100 10 w 0 1 00 000 each node represents a point P R d 000 each node recursivly subdivides the (sub)domain (implicitly) in 2 subdomains by a hyperplane containing that point, and which is parallel to an alternating coordinate axis 00 010 01 100 10 11 Page 10 of 24
Point-Quadtree w 02 0 03 1 13 112 11 w 01 010 each node represents a point P R 2 (R d ) 010 each node recursivly subdivides the (sub)domain (implicitly) in 4 (2 d ) subdomains by 2 straight lines (d hyperplanes) containing that point and which are parallel to the coordinate axis 01 0 02 03 11 112 1 13 Page 11 of 24
Region-Quadtree each node represents a two dimensional region, the root represents the whole domain the nodes of each layer correspond to equal sized regions the side lengths are halved for the next layer leaf nodes are marked inside/outside Page 12 of 24
A.5. Abstract Data Structures stack (LIFO) queue (FIFO) Stack: LIFO principle: Last In First Out operations: * push: put a new element on the top of the stack * pop: get an element from the top of the stack Queue: FIFO principle: First In First Out operations: * push: put a new element to the end of the queue * pull: get the element at the beginning of the queue Stacks and queues can be implemented with an list (or an vector). Page 13 of 24
Priority Queue: there s a priority value for each element the element with the highest priority is always pulled setting increasing/decreasing priority values will emulate a stack/queue implementation with a (sorted) list, a sorted/partially ordered binary tree or heap (partially ordered tree mapped onto an array) Partially Ordered binary Tree (POT): elements of a lower layer are greater: childs > parent (analogous: smaller childs < parent) add element 4 9 7 5 14 7 4 7 5 9 14 7 1 4 7 1 9 14 7 5 1 7 4 9 14 7 5 1 7 4 9 14 7 5 initial config. 1 insert element bubbleup 1 bubbleup 2 final config. 1 remove element 1 5 4 4 7 4 7 4 7 5 7 5 9 14 7 5 9 14 7 9 14 7 9 14 7 initial config. 2 delete&replace element bubbledown (smaller child) final config. 2 heap of initial config. 1 POT: 4 7 5 9 14 7 layer-wise with 1-based index: idx child1 = idx parent 2, idx child2 = idx child1 + 1, idx parent = idx child 2 Page 14 of 24
Graphs A graph is a pair G = (V, E) with nodes/vertices V (G) = {v i i = 1,..., n} and edges E(G) = {e ij =< v i, v j > v i, v j V (G)} (a graph contains topological, but no geometrical information) for a directed graph/digraph these edges are ordered pairs of vertices (arrows), whereas for an undirected graph they re unordered (lines). A symmetric digraph (also including inverted edges) is equivalent to an undirected graph The degree of a vertex is the number of edges incident to the vertex A weight w ij := w (e ij ) R is assigned to each edge e ij E(G), if G is a weighted graph. (vertices weights are also possible) V 1 V 4 V 7 V 2 40 5 V 3 12 3 6 10 12 V 5 20 V 6 8.5 7 25 3 14 13 V 9 5 V 10 8 Page 15 of 24
A.6. Data structures for graphs a graph can be stored as array/matrix: each element stores the weight v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 v 1 40 3 v 2 12 v 3 5 v 4 7 14 v 5 6 8.5 20 v 6 10 12 v 7 5 v 8 25 13 10 v 9 3 edge-list: each element stores the vertices ids and the weigth e 1 v 1 v 2 40 e 2 v 1 v 4 3 e 3 v 2 v 4 12 e 4 v 3 v 2 5 e 5 v 4 v 7 7 e 6 v 4 v 8 14 e 7 v 5 v 2 6 e 8 v 5 v 4 8.5 e 9 v 5 v 6 20 e 10 v 6 v 2 10 e 11 v 6 v 3 12 e 12 v 7 v 8 5 e 13 v 8 v 5 25 e 14 v 8 v 6 13 e 15 v 8 v 9 10 e 16 v 9 v 6 3 Page 16 of 24
adjacency lists: each vertex object stores the adjacent vertices v 1 v 2 40 v 4 3 v 2 v 4 12 v 3 v 2 5 v 4 v 7 7 v 8 14 v 5 v 2 6 v 4 8.5 v 6 20 v 6 v 2 10 v 3 12 v 7 v 8 5 v 8 v 5 25 v 6 13 v 9 10 v 9 v 6 3 or edges v 1 e 1 e 2 v 2 e 3 v 3 e 4 v 4 e 5 e 6 v 5 e 7 e 8 e 9 v 6 e 10 e 11 v 7 e 12 v 8 e 13 e 14 e 15 v 9 e 16 (cmp. hash table) Page 17 of 24
Traversal of a graph from a starting vertex breadth-first traversal Algorithm: 1. add starting vertex to a queue 2. as long as queue is not empty (a) pull vertex out of queue and mark it (b) add adjacent unmarked vertices to queue depth-first traversal analogous to breadth-first traversal, but use stack instead of queue leads to graph search algorithms breadth-first search (BFS) and depth-first search (DFS) also check pulled vertex and stop, if it fulfills the search condition Page 18 of 24
Matrix bandwidth optimization find node numbering in an undirected (unweighted) graph, such as the maximum absolute node number difference between adjacent nodes is minimized bandwidth optimization within FEM codes based on direct solvers problem is NP-complete heuristic approaches Cuthill-McKee algorithm: node with minimal degree is typically chosen as starting node breadth-first traversal with adjacent vertices added in increasing order of degree and pulled vertices numbered in increasing order Reverse Cuthill-McKee algorithm: resulting index numbers are reversed: i N i + 1 i (which is equivalent to numbering in decreasing order) Page 19 of 24
Shortest paths find the shortest path in an directed weighted graph from v start to v target (or to all other vertices) each vertex on the shortest path is visited only once (cmp. Hamiltonian path) to find the shortest path to a vertex, the shortest path of the predecessor of that vertex has to be found. (recursive formulation) each vertex just has to store its predecessor shortest path tree Dijkstra algorithm: like BFS/DFS, but with Priority queue based on min. distances to the vertex and new shortest distances instead of marks. V 1 V 4 V 7 3 7 5 V 2 V 3 10 12 V 5 V 6 25 13 V 9 V 10 8 40 0 38 3 40 28 10 15 25 Page 20 of 24
Dijkstra algorithm: 1 v start, v target V given start-, target-node 2 w i,j R + edge lengths, 3 v.adj V given successor 4 v.dist R + distances, 5 v.pre V predecessors to be determined 6 v, v K V 7 v.dist, d R + declaration 8 B V 9 foreach v V do initialization 10 v.pre NULL shortest path predecessor unknown 11 end foreach 12 v start.pre v start initialize start node 13 v start.dist 0 14 B {v start} initialize front 15 while B do 16 v K choose v B v.dist = min choose node from front 17 B B \ {v K } and remove it 18 if v K = v target do end reached target? 19 foreach v v K.adj do check successor 20 d v K.dist + w vk,v calculate new distance berechnen 21 if v.pre = NULL do successor new? 22 v.pre v K initialize successor 23 v.dist d 24 B B {v} push to front 25 else do successor already in front 26 if d < v.dist do new path shorter? 27 B B \ {v} 28 v.pre v K reinitialize successor 29 v.dist d 30 B B {v} and change priority in Front 31 end if 32 end if 33 end foreach 34 end while Page 21 of 24
Dijkstra algorithm example V 1 V 4 V 7 V 2 40 5 V 3 12 3 6 10 12 V 5 20 V 6 8.5 7 25 3 14 13 V 9 5 V 10 8 0 40 3 2 7 14 10 17 0 2 40 40 3 3 0 40 3 10 2 5 17 15 Page 22 of 24
0 40 3 40 28 25 13 10 2 25 10 15 40 38 0 40 10 12 40 3 228 10 25 15 0 40 3 40 28 3 10 15 225 0 38 2 40 40 3 28 10 25 15 Page 23 of 24
step v K v K.adj B 1 < v 1 > 2 v 1 < v 4, v 2 > < v 4, v 2 > 3 v 4 < v 7, v 8 > < v 7, v 8, v 2 > 4 v 7 < v 8 > < v 8, v 2 > 5 v 8 < v 9, v 6, v 5 > < v 9, v 6, v 2, v 5 > 6 v 9 < v 6 > < v 6, v 2, v 5 > 7 v 6 < v 2, v 3 > < v 2, v 3, v 5 > 8 v 2 < v 4 > < v 3, v 5 > step dist 1 2 3 4 5 6 7 8 9 1 0 2 0 40 3 3 0 40 3 10 17 4 0 40 3 10 15 5 0 40 3 40 28 10 15 25 6 0 40 3 40 28 10 15 25 7 0 38 40 3 40 28 10 15 25 8 0 38 40 3 40 28 10 15 25 step pre 1 2 3 4 5 6 7 8 9 1 v 1 v 1 NULL NULL NULL NULL NULL NULL NULL 2 v 1 v 1 NULL v 1 NULL NULL NULL NULL NULL 3 v 1 v 1 NULL v 1 NULL NULL v 4 v 4 NULL 4 v 1 v 1 NULL v 1 NULL NULL v 4 v 7 NULL 5 v 1 v 1 NULL v 1 v 8 v 8 v 4 v 7 v 8 6 v 1 v 1 NULL v 1 v 8 v 8 v 4 v 7 v 8 7 v 1 v 6 v 6 v 1 v 8 v 8 v 4 v 7 v 8 8 v 1 v 6 v 6 v 1 v 8 v 8 v 4 v 7 v 8 Page 24 of 24