Graphs: Definitions and Representations (Chapter 9)

Similar documents
Graphs: Definitions and Representations (Chapter 9)

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

CSE 332 Data Abstractions: Graphs and Graph Traversals

Chapter 9: Elementary Graph Algorithms Basic Graph Concepts

Luke. Leia 2. Han Leia

Graphs: Topological Sort / Graph Traversals (Chapter 9)

Lesson 22: Basic Graph Concepts

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

Note. Out of town Thursday afternoon. Willing to meet before 1pm, me if you want to meet then so I know to be in my office

Introduction to Graphs. common/notes/ppt/

Today s Outline CSE 221: Algorithms and Data Structures Graphs (with no Axes to Grind)

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

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

Analysis of Algorithms Prof. Karen Daniels

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

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

ECE 242. Data Structures

L10 Graphs. Alice E. Fischer. April Alice E. Fischer L10 Graphs... 1/37 April / 37

Graphs & Digraphs Tuesday, November 06, 2007

GRAPH THEORY. What are graphs? Why graphs? Graphs are usually used to represent different elements that are somehow related to each other.

Reading. Graph Terminology. Graphs. What are graphs? Varieties. Motivation for Graphs. Reading. CSE 373 Data Structures. Graphs are composed of.

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

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

CSE 373: Data Structures and Algorithms

Minimum Spanning Trees and Shortest Paths

Shortest Paths. CSE 373 Data Structures Lecture 21

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

Advanced Data Structures and Algorithms

What are graphs? (Take 1)

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

CSE 100: GRAPH ALGORITHMS

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

An Early Problem in Graph Theory. Clicker Question 1. Konigsberg and the River Pregel

Minimum Spanning Trees and Shortest Paths

Introduction to Graph Theory

Chapter 9 Graph Algorithms

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

Graphs Introduction and Depth first algorithm

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

COT 6405 Introduction to Theory of Algorithms

Reading. Graph Introduction. What are graphs? Graphs. Motivation for Graphs. Varieties. Reading. CSE 373 Data Structures Lecture 18

Chapter 9 Graph Algorithms

Algorithm Design and Analysis

CMSC 380. Graph Terminology and Representation

Lesson 2 7 Graph Partitioning

CS 310 Advanced Data Structures and Algorithms

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

TCOM 501: Networking Theory & Fundamentals. Lecture 11 April 16, 2003 Prof. Yannis A. Korilis

Data Organization: Creating Order Out Of Chaos

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

Lecture 5: Graphs & their Representation

WEEK 1 CLASS NOTES AARON G. CASS

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

3.1 Basic Definitions and Applications

Applications of BFS and DFS

Programming Abstractions

An Early Problem in Graph Theory

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Algorithm Design and Analysis

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

SELF-BALANCING SEARCH TREES. Chapter 11

(Refer Slide Time: 05:25)

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

LECTURE 26 PRIM S ALGORITHM

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

DIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

Algorithm Design and Analysis

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

Graphs. The ultimate data structure. graphs 1

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

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

Shortest Paths and Minimum Spanning Trees

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

Graph Algorithms Using Depth First Search

Graphs. Ric Glassey

Lecture 23 Representing Graphs

Data Structures Lecture 14

Module 5 Graph Algorithms

Graphs and their representations. EECS 214, Fall 2018

1. Graph and Representation

Graph. Vertex. edge. Directed Graph. Undirected Graph

Announcements Problem Set 4 is out!

Chapter 9 Graph Algorithms

Single Source, Shortest Path Problem

Design and Analysis of Algorithms

ECE 242 Data Structures and Algorithms. Graphs II. Lecture 27. Prof.

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

Lecture 1: Introduction and Motivation Markus Kr otzsch Knowledge-Based Systems

KNOWLEDGE GRAPHS. Lecture 1: Introduction and Motivation. TU Dresden, 16th Oct Markus Krötzsch Knowledge-Based Systems

Definition of Graphs and Trees. Representation of Trees.

Last week: Breadth-First Search

Figure 1: A directed graph.

Graphs Data Structures

Lecture 26: Graphs: Traversal (Part 1)

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

CSE 373 Data Structures & Algorithms

Lecture 9 Graph Traversal

Understand graph terminology Implement graphs using

Graphs. The ultimate data structure. graphs 1

Elements of Graph Theory

Transcription:

oday s Outline Graphs: efinitions and Representations (hapter 9) SE 373 ata Structures and lgorithms dmin: Homework #4 - due hurs, Nov 8 th at 11pm Midterm 2, ri Nov 16 Memory hierarchy Graphs Representations 11/02/2012 1 11/02/2012 2 Graphs graph is a formalism for representing relationships among items Very general definition because very general concept graph is a pair G = (V,E) set of vertices, also known as nodes V = {v 1,v 2,,v n } set of edges E = {e 1,e 2,,e m } Each edge e i is a pair of vertices (v j,v k ) n edge connects the vertices Graphs can be directed or undirected Han Leia 11/02/2012 3 Luke V = {Han,Leia,Luke} E = {(Luke,Leia), (Han,Leia), (Leia,Han)} n? an think of graphs as an with operations like isedge((v j,v k )) ut what the standard operations are is unclear Instead we tend to develop algorithms over graphs and then use data structures that are efficient for those algorithms Many important problems can be solved by: 1. ormulating them in terms of graphs 2. pplying a standard graph algorithm o make the formulation easy and standard, we have a lot of standard terminology about graphs 11/02/2012 4 1

Some graphs Undirected Graphs or each, what are the vertices and what are the edges? Web pages with links acebook friends Input data for the Kevin acon game Methods in a program that call each other Road maps (e.g., Google maps) irline routes amily trees ourse pre-requisites 11/02/2012 5 In undirected graphs, edges have no specific direction Edges are always two-way hus, (u,v) E implies (v,u) E. Only one of these edges needs to be in the set; the other is implicit egree of a vertex: number of edges containing that vertex Put another way: the number of adjacent vertices 11/02/2012 6 irected graphs Self-edges, connectedness, etc. In directed graphs (sometimes called digraphs), edges have a specific direction or 2 edges here hus, (u,v) E does not imply (v,u) E. Let (u,v) E mean u v and call u the source and v the destination In-egree of a vertex: number of in-bound edges, i.e., edges where the vertex is the destination Out-egree of a vertex: number of out-bound edges, i.e., edges where the vertex is the source 11/02/2012 7 self-edge a.k.a. a loop is an edge of the form (u,u) epending on the use/algorithm, a graph may have: No self edges Some self edges ll self edges (in which case often implicit, but we will be explicit) node can have a degree / in-degree / out-degree of zero graph does not have to be connected (In an undirected graph, this means we can follow edges from any node to every other node), even if every node has non-zero degree 11/02/2012 8 2

More notation More notation or a graph G = (V,E): V is the number of vertices E is the number of edges Minimum? Maximum for undirected? Maximum for directed? V = {,,, } E = {(, ), (, ), (, ) (, )} or a graph G = (V,E): V is the number of vertices E is the number of edges Minimum? 0 Maximum for undirected? V V+1 /2 O( V 2 ) Maximum for directed? V 2 O( V 2 ) (assuming self-edges allowed, else subtract V ) If (u,v) E hen v is a neighbor of u, i.e., v is adjacent to u Order matters for directed edges If (u,v) E hen v is a neighbor of u, i.e., v is adjacent to u Order matters for directed edges: In this example v is adjacent to u, but u is not adjacent to v (unless (v,u) E) 11/02/2012 9 11/02/2012 10 Examples again Which would use directed edges? Which would have self-edges? Which could have 0-degree nodes? Web pages with links acebook friends Input data for the Kevin acon game Methods in a program that call each other Road maps (e.g., Google maps) irline routes amily trees ourse pre-requisites Weighted graphs In a weighed graph, each edge has a weight a.k.a. cost ypically numeric (most examples will use ints) Orthogonal to whether graph is directed Some graphs allow negative weights; many don t ainbridge linton Kingston 60 20 30 35 Mukilteo Edmonds Seattle remerton 11/02/2012 11 11/02/2012 12 3

Examples Paths and ycles What, if anything, might weights represent for each of these? o negative weights make sense? Web pages with links acebook friends Input data for the Kevin acon game Methods in a program that call each other Road maps (e.g., Google maps) irline routes amily trees ourse pre-requisites 11/02/2012 13 path is a list of vertices [v 0,v 1,,v n ] such that (v i,v i+1 ) E for all 0 i < n. Say a path from v 0 to v n cycle is a path that begins and ends at the same node (v 0 ==v n ) Seattle Salt Lake ity hicago San rancisco allas Example path (that also happens to be a cycle): [Seattle, Salt Lake ity, hicago, allas, San rancisco, Seattle] 11/02/2012 14 Path Length and ost Path length: Number of edges in a path (also called unweighted cost ) Path cost: sum of the weights of each edge Example where: P= [Seattle, Salt Lake ity, hicago, allas, San rancisco] Seattle 2 San rancisco 3.5 2 2 2.5 3 Salt Lake ity 2.5 hicago allas 11/02/2012 15 2.5 length(p) = 4 cost(p) = 9.5 Simple paths and cycles simple path repeats no vertices, (except the first might be the last): [Seattle, Salt Lake ity, San rancisco, allas] [Seattle, Salt Lake ity, San rancisco, allas, Seattle] Recall, a cycle is a path that ends where it begins: [Seattle, Salt Lake ity, San rancisco, allas, Seattle] [Seattle, Salt Lake ity, Seattle, allas, Seattle] simple cycle is a cycle and a simple path: [Seattle, Salt Lake ity, San rancisco, allas, Seattle] 11/02/2012 16 4

Paths/cycles in directed graphs Paths/cycles in directed graphs Example: Example: Is there a path from to? Is there a path from to? No oes the graph contain any cycles? oes the graph contain any cycles? No 11/02/2012 17 11/02/2012 18 Undirected graph connectivity irected graph connectivity n undirected graph is connected if for all pairs of vertices u,v, there exists a path from u to v directed graph is strongly connected if there is a path from every vertex to every other vertex onnected graph isconnected graph directed graph is weakly connected if there is a path from every vertex to every other vertex ignoring direction of edges n undirected graph is complete, a.k.a. fully connected if for all pairs of vertices u,v, there exists an edge from u to v complete a.k.a. fully connected directed graph has an edge from every vertex to every other vertex 11/02/2012 19 11/02/2012 20 5

Examples rees as graphs or undirected graphs: connected? or directed graphs: strongly connected? weakly connected? Web pages with links acebook friends Input data for the Kevin acon game Methods in a program that call each other Road maps (e.g., Google maps) irline routes amily trees ourse pre-requisites When talking about graphs, we say a tree is a graph that is: undirected acyclic connected So all trees are graphs, but not all graphs are trees How does this relate to the trees we know and love?... Example: G E H 11/02/2012 21 11/02/2012 22 Rooted rees We are more accustomed to rooted trees where: We identify a unique ( special ) root We think of edges as directed: parent to children Given a tree, once you pick a root, you have a unique rooted tree (just drawn differently and with undirected edges) E G H redrawn 11/02/2012 23 E G H Rooted rees (nother example) We are more accustomed to rooted trees where: We identify a unique ( special ) root We think of edges as directed: parent to children Given a tree, once you pick a root, you have a unique rooted tree (just drawn differently and with undirected edges) E redrawn G H E G H 11/02/2012 24 6

irected acyclic graphs (Gs) G is a directed graph with no (directed) cycles Every rooted directed tree is a G ut not every G is a rooted directed tree: Every G is a directed graph ut not every directed graph is a G: Examples Which of our directed-graph examples do you expect to be a G? Web pages with links Input data for the Kevin acon game Methods in a program that call each other irline routes amily trees ourse pre-requisites 11/02/2012 25 11/02/2012 26 ensity / sparsity Recall: In an undirected graph, 0 E < V 2 Recall: In a directed graph: 0 E V 2 So for any graph, E is O( V 2 ) One more fact: If an undirected graph is connected, then E V -1 ecause E is often much smaller than its maximum size, we do not always approximate as E as O( V 2 ) his is a correct bound, it just is often not tight If it is tight, i.e., E is Θ( V 2 ) we say the graph is dense More sloppily, dense means lots of edges If E is O( V ) we say the graph is sparse More sloppily, sparse means most (possible) edges missing 11/02/2012 27 What s the data structure? hings we might want to do: iterate over vertices iterate over edges iterate over vertices adj. to a vertex check whether an edge exists find the lowest-cost path from x to y Which data structure is best can depend on: properties of the graph (e.g., dense versus sparse) the common queries (e.g., is (u,v) an edge? versus what are the neighbors of node u? ) We need a data structure that represents graphs: List of vertices + list of edges (rarely good enough) djacency Matrix djacency List 11/02/2012 28 7

djacency matrix ssign each node a number from 0 to V -1 V x V matrix (i.e., 2- array) of booleans (or 1 vs. 0) If M is the matrix, then M[u][v] == true means there is an edge from u to v djacency matrix properties Running time to: Get a vertex s out-edges: Get a vertex s in-edges: ecide if some edge exists: Insert an edge: elete an edge: Space requirements: est for sparse or dense graphs? 11/02/2012 29 11/02/2012 30 djacency matrix properties Running time to: Get a vertex s out-edges: O( V ) Get a vertex s in-edges: O( V ) ecide if some edge exists: O(1) Insert an edge: O(1) elete an edge: O(1) Space requirements: V 2 bits est for dense graphs 11/02/2012 31 djacency matrix properties (cont.) How will the adjacency matrix vary for an undirected graph? Undirected: Will be symmetric about diagonal axis How can we adapt the representation for weighted graphs? Instead of a boolean, store an int/double in each cell Need some value to represent not an edge Say -1 or 0 11/02/2012 32 8

djacency List djacency List Properties / / ssign each node a number from 0 to V -1 n array of length V in which each entry stores a list (e.g., linked list) of all adjacent vertices Running time to: Get all of a vertex s out-edges: Get all of a vertex s in-edges: / / / / / / ecide if some edge exists: Insert an edge: elete an edge: Space requirements: est for dense or sparse graphs? 11/02/2012 33 11/02/2012 34 djacency List Properties / / Undirected graphs Running time to: Get all of a vertex s out-edges: / O(d) where d is out-degree of vertex Get all of a vertex s in-edges: / O( E ) (but could keep a second adjacency list for this!) ecide if some edge exists: O(d) where d is out-degree of source Insert an edge: O(1) elete an edge: O(d) where d is out-degree of source Space requirements: O( V + E ) est for sparse graphs: so usually just stick with linked lists 11/02/2012 35 djacency matrices & adjacency lists both do fine for undirected graphs Matrix: ould save space; only ~1/2 the array is used Lists: Each edge in two lists to support efficient get all neighbors Example: 11/02/2012 36 / / / / 9

Next Okay, we can represent graphs Now let s implement some useful and non-trivial algorithms opological sort: Given a G, order all the vertices so that every vertex comes before all of its neighbors Shortest paths: ind the shortest or lowest-cost path from x to y Related: etermine if there even is such a path 11/02/2012 37 10