Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Similar documents
CSE 332: Data Structures & Parallelism Lecture 19: Introduction to Graphs. Ruth Anderson Autumn 2018

Chapter 9 Graph Algorithms

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Chapter 9 Graph Algorithms

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Chapter 9 Graph Algorithms

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

Algorithms. Graphs. Algorithms

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

CS61BL. Lecture 5: Graphs Sorting

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

Algorithm Design (8) Graph Algorithms 1/2

Lecture 5: Graphs & their Representation

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

Graphs & Digraphs Tuesday, November 06, 2007

More Graph Algorithms

ECE 242. Data Structures

Introduction to Graph Theory

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

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

Graphs: Definitions and Representations (Chapter 9)

Graphs. Carlos Moreno uwaterloo.ca EIT

Graph and Digraph Glossary

Graphs: Definitions and Representations (Chapter 9)

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

CMSC 380. Graph Terminology and Representation

Outline. Graphs. Divide and Conquer.

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

Some Graph Theory for Network Analysis. CS 249B: Science of Networks Week 01: Thursday, 01/31/08 Daniel Bilar Wellesley College Spring 2008

Lecture 10: Graph Data Structures Steven Skiena

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Data Structures Brett Bernstein

Graphs Definitions. Gunnar Gotshalks. GraphDefinitions 1

CSE 373: Data Structures and Algorithms. Hash Tables. Autumn Shrirang (Shri) Mare

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

Minimum Spanning Tree

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Basic Graph Definitions

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Final Examination CSE 100 UCSD (Practice)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Elementary Graph Algorithms CSE 6331

CSE 373: Data Structures and Algorithms. Final Review. Autumn Shrirang (Shri) Mare

Design and Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CS 561, Lecture 9. Jared Saia University of New Mexico

Graph Representations and Traversal

Paths. Path is a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along edges of the graph.

Strongly Connected Components

Konigsberg Bridge Problem

11/26/17. directed graphs. CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10. undirected graphs

GRAPHS Lecture 19 CS2110 Spring 2013

Graphs and Graph Algorithms. Slides by Larry Ruzzo

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

Homework 4 Solutions CSE 101 Summer 2017

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Lecture 9 Graph Traversal

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

CS521 \ Notes for the Final Exam

Hash Tables: Handling Collisions

Priority Queues and Heaps

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.

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graphs. Edges may be directed (from u to v) or undirected. Undirected edge eqvt to pair of directed edges

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Graphs. The ultimate data structure. graphs 1

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Chapter 2 Graphs. 2.1 Definition of Graphs

L20-21: Graph Data Structure

Lecture 3: Recap. Administrivia. Graph theory: Historical Motivation. COMP9020 Lecture 4 Session 2, 2017 Graphs and Trees

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Pred 8 1. Dist. Pred

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Graphs. 04/01/03 Lecture 20 1

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Trees and Intro to Counting

CSE 373: Data Structures and Algorithms. Graph Traversals. Autumn Shrirang (Shri) Mare

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

7.3 Spanning trees Spanning trees [ ] 61

Goals! CSE 417: Algorithms and Computational Complexity!

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

Introduction to Graphs

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

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

Graph Theory S 1 I 2 I 1 S 2 I 1 I 2

CSE 417 Branch & Bound (pt 4) Branch & Bound

Representations of Graphs

Lecture Summary CSC 263H. August 5, 2016

Graphs Introduction and Depth first algorithm

Spanning and weighted spanning trees. A different kind of optimization. (graph theory is cool.)

Transcription:

Graphs Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Warmup Discuss with your neighbors: Come up with as many kinds of relational data as you can (data that can be represented with a graph). CSE 373 SU 18 BEN JONES 2

Announcements Project 2 Due on Friday Next individual HW assignment assigned tonight, due next Wednesday - Two problems merge sort and graph practice Sign up for Project 3 partners by Thursday night! I am gone tonight through Tuesday. Robbie will be lecturing again. No instructor office hours tomorrow or Tuesday next week. CSE 373 SU 18 BEN JONES 3

Graph: Formal Definition A graph is defined by a pair of sets G = (V, E) where - V is a set of vertices - A vertex or node is a data entity V = { A, B, C, D, E, F, G, H } G H - E is a set of edges - An edge is a connection between two vertices F E = { (A, B), (A, C), (A, D), (A, H), (C, B), (B, D), (D, E), (D, F), (F, G), (G, H)} D A C E B CSE 373 SP 18 - KASEY CHAMPION 4

Graph Vocabulary Graph Direction - Undirected graph edges have no direction and are two-way V = { A, B, C } E = { (A, B), (B, C) } inferred (B, A) and (C,B) - Directed graphs edges have direction and are thus one-way V = { A, B, C } E = { (A, B), (B, C), (C, B) } A Undirected Graph: A B Undirected Graph: C B Degree of a Vertex - Degree the number of edges containing that vertex A : 1, B : 1, C : 1 - In-degree the number of directed edges that point to a vertex A : 0, B : 2, C : 1 - Out-degree the number of directed edges that start at a vertex A : 1, B : 1, C : 1 C CSE 373 SP 18 - KASEY CHAMPION 5

Food for thought Is a graph valid if there exists a vertex with a degree of 0? Yes A B A B A B C C C A has an in degree of 0 B has an out degree of 0 Are these valid? Is this a valid graph? Yup A B A C has both an in degree and an out degree of 0 A B C Yes! D C Sure CSE 373 SP 18 - KASEY CHAMPION 6

Graph Vocabulary Self loop an edge that starts and ends at the same vertex A Parallel edges two edges with the same start and end vertices A B Simple graph a graph with no self-loops and no parallel edges Complete graph a graph with edges between every pair of vertices CSE 373 SP 18 - KASEY CHAMPION 7

For simple, undirected graphs What is the fewest number of edges a graph with n vertices can have? 0 What is the sum of the degrees of all vertices in a graph? (in terms of V and E ) 2 E What is the maximum number of edges a graph with n vertices can have? n(n-1)/2 = O n 2 - (the complete graph on n vertices K n ) CSE 373 SU 18 BEN JONES 8

Representing Graphs Discuss with your neighbor: How would you implement a non-weighted, undirected graph? Assume there are n vertices, and those vertices are numbered 0, 1, 2,, n-1. As an example: 0 1 4 5 G = (V, E) 6 2 3 V = {0,1,2,3,4,5,6}, E = { (0,1), (0,2), (1,3), (3,5), (4,5) } CSE 373 SU 18 BEN JONES 9

Representing Graphs 6 0 1 2 3 4 5 CSE 373 SU 18 BEN JONES 10

Adjacency Matrix A matrix is a table of numbers, a[u][v]. 6 0 1 2 3 4 5 In an adjacency matrix a[u][v] is 1 if there is an edge (u,v), and 0 otherwise. Can represent both undirected and directed graphs Can represent self-loops and parallel edges (interpret as the # of edges between two vertices Time Complexity ( V = n, E = m): Add Edge: O(1) Remove Edge: O(1) Check edge exists from (u,v): O(1) Get neighbors of u (out): O(n) Get neighbors of u (in): O(n) 0 1 2 3 4 5 6 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 2 1 0 0 1 0 0 0 3 0 1 1 0 0 1 0 4 0 0 0 0 0 1 0 5 0 0 0 1 1 0 0 6 0 0 0 0 0 0 0 Space Complexity: O(n 2 ) CSE 373 SU 18 BEN JONES 11

Sparsity A Dense Graph is a graph with many edges E V 2 A Sparse Graph is a graph with few edges E V Adjacency Matrices are very wasteful of space for spare graphs they are almost all 0s! How could we save space? CSE 373 SU 18 BEN JONES 12

Adjacency List An array where the u th element contains a list of neighbors of u. 6 0 1 2 3 4 5 Can represent both undirected and directed graphs In the directed case, put the out neighbors (a[u] has v for all (u,v) in E) Can represent self-loops and parallel edges (repeat neighbor) Time Complexity ( V = n, E = m): Add Edge: O(1) Remove Edge: O( min(n, m) ) Check edge exists from (u,v): O( min(n, m) ) Get neighbors of u (out): O(n) Get neighbors of u (in): O(n + m) 0 1 2 3 4 5 6 1 2 0 3 0 3 1 2 5 5 3 4 Space Complexity: O(n + m) CSE 373 SU 18 BEN JONES 13

Graph Vocabulary Weighted Graph a graph with numeric weights associated with each edge A 3 B 0 C D -5 - can be directed or undirected - weights can be negative, positive or 0 - common to specify only non-negative edge weights, or only positive edge weights - denoted e = (u, v, w) or sometimes e = ((u,v), w) - Weights often carry meaning such as distance, (e.g. driving time between cities) CSE 373 SU 18 BEN JONES 14

Representing Weighted Graphs Adjacency Matrix You can make the value of at each element the weight of the edge - In an int array (int[]) you can t distinguish between 0 weight edge and no edge - Solution 1 You know ahead of time that 0 weight (or negative weight) edges do not exist and use that to represent no edge - Solution 2 (Java) Use an Integer array (Integer[]) have null represent no edge Adjacency list Store pairs (neighbor, edge weight) CSE 373 SU 18 BEN JONES 15

Storing Data In Graphs We often have data associated with vertices and edges Vertex Data Examples: - Facebook: Name, Age, Birthday, Hometown, Likes - Google Maps: City name, elevation, hours of operation - Internet: Page title, page contents, date last modified Edge Data Examples: - Facebook: Date friendship was made, friend vs. acquaintance, etc. - Google Maps: Length of road, speed limit CSE 373 SU 18 BEN JONES 16

Storing Data in Graphs We could have a Graph<V, E> where V is a data type for Vertices and E is one for Edges Adjacency Matrix: E[][] now each entry has a pointer to edge data, or null if that edge is not in the graph Adjacency List: E[] the neighbor lists are now a list of edges Both: Maintain a list V[] of vertices Alternative Adjacency List: V[] just a list of vertices, where each vertex contains within it a list of (outgoing) edges. CSE 373 SU 18 BEN JONES 17

Arrays HashTables When we analyze and describe graph algorithms, for simplicity we assume that each vertex has a unique identity 0, 1, 2, n-1. Accesses in Adjacency Matrices and getting an adjacency list are both worst case O(1) for arrays. In reality, we often don t have unique sequential integers for each vertex. In a real graph implementation, we often use HashMaps or HashSets. This would get us average case O(1), but worst case O(n). This is bad in analysis, but fine in practice. An adjacency list that stores references to the actual vertex objects can avoid repeated table lookups. We could always use a hash table to give unique integer IDs to every element, incurring an O(n) worst case, but O(1) average case overhead before each call (which can save our worst case analysis for any O(n) or slower algorithm), but we don t usually bother to. CSE 373 SU 18 BEN JONES 18

Graph Vocabulary Path A sequence of connected vertices A B C D (Directed) Path A path in a directed graph must follow the direction of the edges A B C D Path Length (unweighted) The number of edges in a path - (A,B,C,D) has length 3. Simple Path A path that doesn t repeat vertices (except maybe first=last) Cycle A path that starts and ends at the same vertex (of length at least 1) CSE 373 SU 18 BEN JONES 19

Graph Vocabulary Connected Graph A graph that has a path from every vertex to every other vertex (i.e. every vertex is reachable from every other vertex. Strongly Connected Graph A directed graph that is connected (note the direction of the edges!) Weakly Connected Graph A directed graph that is connected when interpreted as undirected. (Note all strongly connected graphs are also weakly connected) CSE 373 SU 18 BEN JONES 20

Paths and Reachability Very common questions: - Is there a path between two vertices? (Can I drive from Seattle to LA?) - What is the length of the shortest path between two vertices? (How long will it take?) Less common, but still important: What is the longest path in a graph? - In general a hard problem - 7 degrees of Kevin Bacon - Length of this path is called the diameter of a graph CSE 373 SU 18 BEN JONES 21

Trees A tree is a connected, acyclic graph. An a tree there exist exactly one path between every pair of vertices. A graph consisting of several disconnected trees is called a forest. The trees we have seen so far have been rooted trees interpret one vertex as the root, and its neighbors are now children, and the root of their own subtrees. How many edges does a tree with n vertices have? n - 1 CSE 373 SU 18 BEN JONES 22

DAGs DAG stands for Directed, Acyclic, Graph This is the directed graph analog of a forest. The trees we have made so far in this class have been implemented as weakly connected DAGs. Can be used to represent dependencies: i.e. A must be completed before either B or C, and both B and C must be completed before D. Scheduling these tasks is called topological sort. CSE 373 SU 18 BEN JONES 23

Subgraphs Take a graph, and delete vertices and edges so you still have a graph. The remaining red vertices and edges form a subgraph. Formally: G = V, E G = V, E V V and E E and G is a graph CSE 373 SU 18 BEN JONES 24

Interesting Subgraphs Cliques - A clique is a complete subgraph - A maximal clique is a clique that you could not add any more vertices to and still have a clique. This is distinct from the maximum clique, which is the largest clique in a graph. CSE 373 SU 18 BEN JONES 25

Interesting Subgraphs Connected Components - A connected component is a maximal, connected subgraph - In directed graphs, you have two kinds: strongly connected, and weakly connected: CSE 373 SU 18 BEN JONES 26

Interesting Subgraphs A Spanning Tree is a subgraph that is both a tree and includes every vertex (it spans the graph). Every connected graph has at least one spanning tree. They are like skeletons of the graph. An important problem is finding the minimum spanning tree. We will learn 2 algorithms for this. It is useful for optimization tasks (e.g. minimum cost to build a road network). CSE 373 SU 18 BEN JONES 27

Other interesting Graph Problems - Circuits paths or cycles that touch every vertex - Reductions Everything we ve seen so far in this class can be represented as a graph a lot of other problems can too! Graphs can solve many problems. CSE 373 SU 18 BEN JONES 28