Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CMSC 420: Lecture 3 - Multidimensional Arrays, Graphs, and Constant-Time Initialization, Study notes of Data Structures and Algorithms

A lecture note from cmsc 420 covering multidimensional arrays, graphs, and constant-time initialization. Topics include abstract data types, implementations, techniques, row-major vs. Column-major order, sparse matrices, threading, and graph terminology. The lecture also discusses various graph representations and algorithms such as breadth-first search and depth-first search.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-ftn-1
koofers-user-ftn-1 🇺🇸

10 documents

1 / 37

Toggle sidebar

Related documents


Partial preview of the text

Download CMSC 420: Lecture 3 - Multidimensional Arrays, Graphs, and Constant-Time Initialization and more Study notes Data Structures and Algorithms in PDF only on Docsity! Multidimensional Arrays & Graphs CMSC 420: Lecture 3 Mini-Review • Abstract Data Types: • List • Stack • Queue • Deque • Dictionary • Set • Implementations: • Linked Lists • Circularly linked lists • Doubly linked lists • XOR Doubly linked lists • Ring buffers • Double stacks • Bit vectors Techniques: Sentinels, Zig-zag scan, link inversion, bit twiddling, self- organizing lists, constant-time initialization Multidimensional Arrays • Often it’s more natural to index data items by keys that have several dimensions. E.g.: • (longitude, latitude) • (row, column) of a matrix • (x,y,z) point in 3d space • Aside: why is a plane “2-dimensional”? Row-major vs. Column-major order • 2-dimensional arrays can be mapped to linear memory in two ways: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 Row-major order Column-major order 1 2 3 4 1 2 3 4 5 1 2 3 4 1 2 3 4 5 Addr(i,j) = Base + 5(i-1) + (j-1) Addr(i,j) = Base + (i-1) + 4(j-1) Row-major vs. Column-major order • Generalizes to more than 2 dimensions • Think of indices <i1, i2, i3, i4, i5,...,id> as an odometer. - Row-major order: last index varies fastest - Column-major order: first index varies fastest Linked 2-d Array Allocation A 2 1 C 3 1 M 3 4 F 1 4 A 4 2 Q 1 2 P 2 5 Z 2 3 E 4 5 Threading A 2 1 C 3 1 M 3 4 F 1 4 A 4 2 Q 1 2 P 2 5 Z 2 3 E 4 5 • Column pointers allow iteration through items with same column index. • Example of threading: adding additional pointers to make iteration faster. • Threading useful when the definition of “next” depends on context. • We’ll see additional examples of threading with trees. Hierarchical Tables • Combination of sequential and linked allocation. • Particularly useful when filled elements cluster together, or when all entries in one dimension are always known. • Natural to implement by combining Perl arrays, C++ vectors, etc. 4 x 5 x 5 x 3 array Image Graphs • Black & white image, 0/1 pixels (crossword puzzle, e.g.) • G = (V,E), a set of vertices V and edges E - V = {set of pixels} - {u,v} in E if pixels u and v are next to each other. • Separate connected parts of the graph = disjoint regions of the image (space fill, e.g.) • Graph defined this way is planar (can be drawn without edge crossings). Graphs – Terminology • Graph G = (E, V) - V = set of vertices - E = set of pairs of vertices, represents edges • Degree of vertex = # of edges adjacent to it • If there is an edge {u,v} then u is adjacent to v. • Edge is incident to its endpoints. • Directed graph = edges are arrows • out-degree, in-degree • The set of vertices adjacent to a node u is called its neighbors. vertex or node edge Graphs – Example • V = {u,v,w,x,y,z} • E = { {u,v}, {v,w}, {u,x}, {w,x}, {z,y}, {x,y}} u v w x y z Graphs – Basic properties • Undirected graphs: • What’s the maximum number of edges? (A graph that contains all possible edges is called complete) • What’s the sum of the all the degrees? • Directed graphs: • What’s the maximum number of edges? • What’s the sum of all the degrees? Graphs – Isomorphism • Two graphs G1 = (V1, E1) and G2 = (V2, E2) are isomorphic if there’s a 1-to-1 and onto mapping f(v) between V1 and V2 such that: {u,v} in E1 iff {f(u), f(v)} in E2. • In other words, G1 and G2 represent the same topology. Does checking whether two graphs are isomorphic seem like an easy problem or a hard problem? Graphs – ADT • S = vertices() • S = edges() • neighbors(G, v) • insert_edge(G, u,v) • insert_vertex(G, u) • remove_edge(G, u,v) • remove_vertex(G, u) Return sets - set ADT we talked about last time may be useful Time to perform these tasks will depend on implementation. What are ways to implement graphs? Adjacency Matrix 1 1 1 1 1 1 1 1 2 3 4 5 6 7 1 2 3 4 5 6 72-dimensional matrix: 1 in entry (u,v) if edge (u,v) is present; 0 otherwise What’s special about the adjacency matrix for an undirected graph? What kind of adjacency matrix makes sense for undirected graphs? Undirected Adjacency Matrix • Undirected graph = symmetric adjacency matrix because edge {u,v} is the same as edge {v, u}. • Can use upper triangular matrix we discussed above. • Weights on the edges can be represented by numbers in the matrix (as long as there is some “out of band” number to mean “no edge present”) • What if most edges are absent? Say |E| = O(|V|). Graph is sparse. Adjacency Lists A B A C B B DC D A rr ay o f l en gt h |V | A B C D In an undirected graph, each edge is stored twice (each edge is adjacent to two vertices) Breadth-First Search Initially, every vertex is “unvisited” Q maintains a queue of vertices that we’ve seen but not yet processed. While there are vertices that we’ve seen but not processed... Process one of them and add its unseen neighbors to the queue and mark them seen. Why a queue? BFS(G, u): mark each vertex unvisited Q = new Queue enqueue(Q, u) while not empty(Q): w = dequeue(Q) if w is unvisited: VISIT(w) mark w as visited for v in Neighbors(G, w): enqueue(Q, v) Breadth-First Search – Running time If G is represented by adjacency LIST, then BFS takes time O(|V| + |E|): |V| because you need to visit each node at least once to mark them unseen |E| because each edge is considered at most twice. What if G is represented by adjacency MATRIX? BFS(G, u): mark each vertex unvisited Q = new Queue enqueue(Q, u) while not empty(Q): w = dequeue(Q) if w is unvisited: VISIT(w) mark w as visited for v in Neighbors(G, w): enqueue(Q, v) Depth-First Search • Visit the nodes of a graph, starting at a given node v. • Immediately after visiting a node u, visit its neighbors. • I.e. we walk as far as we can, and only then “backtrack” • If G is connected, we’ll eventually visit all nodes. 0 1 3 4 5 2 7 6 Numbers indicate a possible sequence of visits. Recursive DFS Recursive_DFS(G, u): ProcessOnEnter(u) mark u visited for w in Neighbors(u): if w is unvisited: DFS(G, w) ProcessOnExit(u) What if G is not connected? Traverse(G): mark all vertices as unvisited for u in Vertices(G): if u is unvisited: DFS(G, u) Can use BFS search as well Connected Components Connected_Components(G): mark all vertices as unvisited cc = 0 for u in Vertices(G): if u is unvisited: DFS(G, u, ++cc) Connected components: path between every pair of nodes within a component; no path between components. DFS (or BFS) will explore all vertices of a component
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved