Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Similar documents
Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Graph Algorithms. Definition

Basic Graph Algorithms (CLRS B.4-B.5, )

COT 6405 Introduction to Theory of Algorithms

Graph Representation

22.1 Representations of graphs

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Lecture 9 Graph Traversal

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Inf 2B: Graphs, BFS, DFS

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Announcements. HW3 is graded. Average is 81%

Minimum Spanning Trees Ch 23 Traversing graphs

Basic Graph Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph: representation and traversal

Outline. Graphs. Divide and Conquer.

22.3 Depth First Search

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Graph Representations and Traversal

CSE 100: GRAPH ALGORITHMS

LECTURE 17 GRAPH TRAVERSALS

CS302 - Data Structures using C++

Introduction to Graphs. common/notes/ppt/

CSC263 Week 8. Larry Zhang.

Representations of Graphs

Elementary Graph Algorithms

Algorithm Design and Analysis

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Announcements Problem Set 4 is out!

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CS 310 Advanced Data Structures and Algorithms

CS2 Algorithms and Data Structures Note 9

Graph Search. Adnan Aziz

Graphs & Digraphs Tuesday, November 06, 2007

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Module 11: Additional Topics Graph Theory and Applications

Design and Analysis of Algorithms

W4231: Analysis of Algorithms

Elementary Graph Algorithms

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Basic Graph Definitions

Lecture 3. Brute Force

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Introduction to Computer Science and Programming for Astronomers

Graphs. The ultimate data structure. graphs 1

Unit 2: Algorithmic Graph Theory

TIE Graph algorithms

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

Definition Example Solution

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture 26: Graphs: Traversal (Part 1)

Figure 1: A directed graph.

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Algorithm Design and Analysis

GRAPHS Lecture 17 CS2110 Spring 2014

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CSE 373: Data Structures and Algorithms

CS/COE

Review: Graph Theory and Representation

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CMSC 380. Graph Terminology and Representation

GRAPHS Lecture 19 CS2110 Spring 2013

Graphs. The ultimate data structure. graphs 1

Graphs. Data Structures 1 Graphs

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

(Re)Introduction to Graphs and Some Algorithms

Chapter 22. Elementary Graph Algorithms

Chapter 9 Graph Algorithms

Lesson 2 7 Graph Partitioning

TIE Graph algorithms

Outlines: Graphs Part-2

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Algorithm Design and Analysis

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Graph representation

Elements of Graph Theory

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

CSI 604 Elementary Graph Algorithms

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Graph: representation and traversal

Transcription:

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, selforganizing lists, constant-time initialization

Constant-Time Initialization Design problem: - Suppose you have a long array, most values are 0. - Want constant time access and update - Have as much space as you need. Create a big array: - a = new int[large_n]; - Too slow: for(i=0; i < LARGE_N; i++) a[i] = 0 Want to somehow implicitly initialize all values to 0 in constant time...

Constant-Time Initialization means unchanged Data[] = 1 2 6 12 13 Access(i): if (0 When[i] < count and Where[When[i]] == i) return Where[] = 6 13 12 Count = 3 Count holds # of elements changed Where holds indices of the changed elements. When[] = 1 3 2 Access(i): if 0 When[i] < Count and Where[When[i]] == i: else: return Data[i] return DEFAULT When maps from index i to the time step when item i was first changed.

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 1 2 3 4 5 1 2 3 4 5 1 1 2 3 4 5 1 5 9 13 17 2 6 7 8 9 10 2 2 6 10 14 18 3 11 12 13 14 15 3 3 7 11 15 19 4 16 17 18 19 20 4 4 8 12 16 20 Row-major order Column-major order 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 <i 1, i 2, i 3, i 4, i 5,...,i d > as an odometer. - Row-major order: last index varies fastest - Column-major order: first index varies fastest

Sparse Matrices Sometimes many matrix elements are either uninteresting or all equal to the same value. Would like to implicitly store these items, rather than using memory for them.

Linked 2-d Array Allocation Q 2 F 4 A 1 Z 3 P 5 C 1 M 4 Array with random access to linked list representing rows A 2 E 5 Row represented by linked list; items contain column numbers

Linked 2-d Array Allocation Q 1 2 F 1 4 A 2 1 Z 2 3 P 2 5 C 3 1 M 3 4 A 4 2 E 4 5

Threading 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. Q 1 F 1 2 4 A 2 Z 2 1 3 C 3 M 3 1 4 A 4 2 P 2 5 E 4 5

Hierarchical Tables 4 x 5 x 5 x 3 array 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.

Upper Triangular Matrices Sometimes empty elements are arranged in a pattern. Example: symmetric distance matrix. Want to store in contiguous memory. How do you access item i, j? # elements taken up by the first (i-1) rows: n + (n-1) + (n-2) +... + (n - i + 1) plus j-i come before the j th element in the i th row

Graphs Examples Computer Networks Street map connecting cities Airline routes. Dependencies between jobs (must finish A before starting B) Protein interactions Used to represent relationships between pairs of objects.

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 vertex or node Degree of vertex = # of edges adjacent to it If there is an edge {u,v} then u is adjacent to v. edge 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.

Graphs Example u z v x w y V = {u,v,w,x,y,z} E = { {u,v}, {v,w}, {u,x}, {w,x}, {z,y}, {x,y}}

Graphs More Terminology A path is a sequence of vertices u 1, u 2, u 3,... such that each edge (u i, u i+1 ) is present. A path is simple if each of the u i is distinct. A subgraph of G = (V, E) is a graph H = (V, E ) such that V is a subset of V and an edge (u,v) is in E iff (u,v) is in E and u and v are in V. A graph is connected if there is a path connecting every pair of vertices. A connected component of G is a maximally sized, connected subgraph of G.

Graphs Still More Terminology A cycle is a path u 1, u 2, u 3,..., u k such that u 1 = u k. A graph without any cycles is called acyclic. An undirected acyclic graph is called a free tree (or usually just a tree) A directed acyclic graph is called a DAG (for Directed Acyclic Graph ) Weighted graph means that either vertices or edges (or both) have weights associated with them. Labeled graph = nodes are labeled.

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 G 1 = (V 1, E 1 ) and G 2 = (V 2, E 2 ) are isomorphic if there s a 1-to-1 and onto mapping f(v) between V 1 and V 2 such that: {u,v} in E 1 iff {f(u), f(v)} in E 2. In other words, G 1 and G 2 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() Return sets - set ADT we talked about last time may be useful neighbors(g, v) insert_edge(g, u,v) insert_vertex(g, u) Time to perform these tasks will depend on implementation. remove_edge(g, u,v) remove_vertex(g, u) What are ways to implement graphs?

Graphs Implementations 1. List of edges 2. Adjacency matrix 3. Adjacency list

Edge List Representation Simple: store edges (aka vertex pairs) in a list. Good if: the structure of the graph is not needed, and iterating through all the edges is the common operation. Bad because: testing whether an edge is present may take O( E ). Relationships between edges are not evident from the list (hard to do shortest path, etc.).

Adjacency Matrix 2-dimensional matrix: 1 in entry (u,v) if edge (u,v) is present; 0 otherwise 1 2 1 2 3 4 5 6 7 1 1 1 What s special about the adjacency matrix for an undirected graph? 3 4 1 1 5 What kind of adjacency matrix makes sense for undirected graphs? 6 7 1 1

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 C Array of length V B C A B C A D D B D In an undirected graph, each edge is stored twice (each edge is adjacent to two vertices)

Adjacency MATRIX vs. Adjacency LISTS Matrix: No pointer overhead More space efficient if G is dense Neighbor() operation is slow! O(n) List: More space efficient if G is sparse Neighbor() operation proportional to the degree. Asymptotic running times often faster

Breadth-First Search Visit the nodes of a graph, starting at a given node v. We visit the vertices in increasing order according to their distance from u. I.e. we visit v, then v s neighbors, then their neighbors,... If G is connected, we ll eventually visit all nodes. 2 2 0 1 1 2 Numbers indicate the shortest distance from v (minimum # of edges you must traverse to get from v to the node). 2 2

Breadth-First Search BFS(G, u): mark each vertex unvisited Q = new Queue enqueue(q, u) Initially, every vertex is unvisited Q maintains a queue of vertices that we ve seen but not yet processed. 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) 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?

Breadth-First Search Running time BFS(G, u): mark each vertex unvisited Q = new Queue enqueue(q, u) If G is represented by adjacency LIST, then BFS takes time O( V + E ): 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) 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?

Depth-First Search Visit the nodes of a graph, starting at a given node v. 6 0 4 Immediately after visiting a node u, visit its neighbors. 7 1 3 I.e. we walk as far as we can, and only then backtrack 5 If G is connected, we ll eventually visit all nodes. 2 Numbers indicate a possible sequence of visits.

Depth-First Search DFS(G, u): mark each vertex unvisited S = new Stack push(s, u) while not empty(s): w = pop(s) if w is unvisited: VISIT(w) mark w as visited for v in Neighbors(G, w): push(s, v) Initially, everything vertex is unvisited Q maintains a stack of vertices that we ve seen but not yet processed. Using a stack means that we ll move to one of the neighbors immediately after seeing them.

Depth-First Search vs. Breadth-First Search DFS(G, u): mark each vertex unvisited S = new Stack push(s, u) BFS(G, u): mark each vertex unvisited Q = new Queue enqueue(q, u) while not empty(s): w = pop(s) if w is unvisited: VISIT(w) mark w as visited for v in Neighbors(G, w): push(s, v) 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)

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) DFS (or BFS) will explore all vertices of a component Connected components: path between every pair of nodes within a component; no path between components.