CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

PSU CS Algorithms Analysis and Design Graphs.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Dan Grossman Spring 2010.
Graph & BFS.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
CSE332: Data Structures & Algorithms Lecture 13: Introduction to Graphs Dan Grossman Fall 2013.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
CSE 332 Data Abstractions: Graphs and Graph Traversals Kate Deibel Summer 2012 July 23, 2012CSE 332 Data Abstractions, Summer
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Nicki Dell Spring 2014.
Data Structures & Algorithms Graphs
COSC 2007 Data Structures II Chapter 14 Graphs I.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Winter 2015.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Copyright © Curt Hill Graphs Definitions and Implementations.
CSE332: Data Structures & Algorithms Lecture 14: Introduction to Graphs Aaron Bauer Winter 2014.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Graphs and Shortest Paths Using ADTs and generic programming.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CS 201: Design and Analysis of Algorithms
CSE 373 Topological Sort Graph Traversals
CSE332: Data Structures & Algorithms Introduction to Graphs
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CS 3343: Analysis of Algorithms
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Paul Beame in lieu of Richard Anderson
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Directed Graph Algorithms
Directed Graph Algorithms
Based on slides of Dan Suciu
Presentation transcript:

CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1

Graphs 2  A graph is a formalism for representing relationships among items  Very general definition because very general concept  A graph is a pair of sets G = (V,E)  A set of vertices, also known as nodes V = {v 1,v 2,…,v n }  A set of edges E = {e 1,e 2,…,e m }  Each edge e i is a pair of vertices  An edge “connects” the vertices  Graphs can be directed or undirected Han Leia Luke V = {Han,Leia,Luke} E = {(Luke,Leia), (Han,Leia), (Leia,Han)}

Some graphs 3 For each, what are the vertices and what are the edges?  Web pages with links  Facebook friends  “Input data” for the Kevin Bacon game  Methods in a program that call each other  Road maps (e.g., Google maps)  Airline routes  Family trees  Course pre-requisites …… Quite versatile & useful

Undirected Graphs 4  In undirected graphs, edges have no specific direction  Edges are always “two-way” Thus, (u,v)  E implies (v,u)  E. – Only one of these edges needs to be in the set; the other is implicit Degree of a vertex: number of edges containing that vertex – Put another way: the number of adjacent vertices A B C D

Directed graphs 5  In directed graphs (sometimes called digraphs), edges have a specific direction Thus, (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-Degree of a vertex: number of in-bound edges, i.e., edges where the vertex is the destination Out-Degree of a vertex: number of out-bound edges, i.e., edges where the vertex is the source or 2 edges here A B C D A B C

Self-edges, connectedness, etc. 6  A self-edge a.k.a. a loop is an edge of the form (u,u)  Depending on the use/algorithm, a graph may have:  No self edges  Some self edges  All self edges (in which case often implicit, but we will be explicit)  A node can have a degree / in-degree / out-degree of zero  (Undirected) Connected: We can follow edges from any node to get to any other node  Not necessarily connected, even if every node has non-zero degree

More notation 7 For a graph G=(V,E) :  |V| is the number of vertices  |E| is the number of edges  Minimum edges? 00  Maximum edges for undirected?  |V||V+1|/2  O(|V| 2 )  Maximum edges for directed?  |V| 2  O(|V| 2 ) (assuming self-edges allowed, else subtract |V| )  If (u,v)  E  Then v is a neighbor of u, i.e., v is adjacent to u  Order matters for directed edges A B C D

Examples again 8 Which would use directed edges? Which would have self- edges? Which could have 0-degree nodes?  Web pages with links  Facebook friends  “Input data” for the Kevin Bacon game  Methods in a program that call each other  Road maps (e.g., Google maps)  Airline routes  Family trees  Course pre-requisites ……

Weighted graphs 9  In a weighed graph, each edge has a weight a.k.a. cost  Typically numeric (most examples will use ints)  Orthogonal to whether graph is directed  Some graphs allow negative weights; many don’t Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton

Examples 10 What, if anything, might weights represent for each of these? Do negative weights make sense?  Web pages with links  Facebook friends  “Input data” for the Kevin Bacon game  Methods in a program that call each other  Road maps (e.g., Google maps)  Airline routes  Family trees  Course pre-requisites ……

Paths and Cycles 11  A 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 ”  A cycle is a path that begins and ends at the same node ( v 0 == v n ) Seattle San Francisco Dallas Chicago Salt Lake City Example cycle: [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]

Path Length and Cost 12  Path length: Number of edges in a path  Path cost: sum of the weights of each edge Example where P= [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle] Seattle San Francisco Dallas Chicago Salt Lake City length(P) = 5 cost(P) = 11.5

Simple paths and cycles 13  A simple path repeats no vertices, except the first might be the last [Seattle, Salt Lake City, San Francisco, Dallas] [Seattle, Salt Lake City, San Francisco, Dallas, Seattle]  Recall, a cycle is a path that ends where it begins [Seattle, Salt Lake City, San Francisco, Dallas, Seattle] [Seattle, Salt Lake City, Seattle, Dallas, Seattle]  A simple cycle is a cycle and a simple path [Seattle, Salt Lake City, San Francisco, Dallas, Seattle]

Paths/cycles in directed graphs 14 Example: Is there a path from A to D? Does the graph contain any cycles? A B C D

Undirected graph connectivity 15  An undirected graph is connected if for all pairs of vertices u,v, there exists a path from u to v  An 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 Connected graphDisconnected graph

Directed graph connectivity 16  A directed graph is strongly connected if there is a path from every vertex to every other vertex  A directed graph is weakly connected if there is a path from every vertex to every other vertex ignoring direction of edges  A complete a.k.a. fully connected directed graph has an edge from every vertex to every other vertex

Examples 17 For undirected graphs: connected? For directed graphs: strongly connected? weakly connected?  Web pages with links  Facebook friends  “Input data” for the Kevin Bacon game  Methods in a program that call each other  Road maps (e.g., Google maps)  Airline routes  Family trees  Course pre-requisites  …

Trees as graphs 18 When talking about graphs, we say a tree is a graph that is:  acyclic  connected  undirected So all trees are graphs, but not all graphs are trees A B DE C F HG Example:

Rooted Trees 19  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) A B DE C F HG redrawn A B DE C F HG

Rooted Trees 20  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) A B DE C F HG redrawn F GHC A B DE

Directed acyclic graphs (DAGs) 21  A DAG is a directed graph with no (directed) cycles  Every rooted directed tree is a DAG  But not every DAG is a rooted directed tree  Every DAG is a directed graph  But not every directed graph is a DAG

Problem Representation 22  Decision Tree as rooted, directed tree  Start at root; follow outcome of comparisons a < b < c, b < c < a, a < c < b, c < a < b, b < a < c, c < b < a a < b < c a < c < b c < a < b b < a < c b < c < a c < b < a a < b a > b a ? b a < b < c a < c < b c < a < b a < b < c a < c < b b < a < c b < c < a c < b < a b < c < a b < a < c a > ca < c b < c b > c b < c b > c c < a c > a

Problem Representation 23  Quick/MergeSort as a graph  Nodes as conceptual states of data base cases divide combine results

Density / sparseness 24  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 |V|-1 ≤ |E|  Because |E| is often much smaller than its maximum size, we do not always approximate as |E| as O (|V| 2 )  This 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”

Now the data structure 25  Okay, so graphs are really useful for lots of data and questions we might ask like “what’s the lowest-cost path from x to y”  But we need a data structure that represents graphs  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 )  So we’ll discuss the two standard graph representations…  Different trade-offs, particularly time versus space

Adjacency matrix 26  Assign each node a number from 0 to |V|-1  A |V| x |V| matrix (i.e., 2-D 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 ABC A B C D D A B C D T T TT FFF FFF F F FFFF

Adjacency matrix properties 27  Running time to:  Get a vertex’s out-edges: O(|V|)  Get a vertex’s in-edges: O(|V|)  Decide if some edge exists: O(1)  Insert an edge: O(1)  Delete an edge: O(1)  Space requirements:  |V| 2 bits  Best for dense graphs ABC A B C D D T T TT FFF FFF F F FFFF A B C D

Adjacency matrix properties 28  How will the adjacency matrix vary if (un)directed?  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 ABC A B C D D T T TT FFF FFF F F FFFF

Adjacency List 29  Assign each node a number from 0 to |V|-1  An array of length |V| in which each entry stores a list (e.g., linked list) of all adjacent vertices A B C D A B C D B/ A/ DB/ /

Adjacency List Properties 30  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!)  Decide if some edge exists: O(d) where d is out-degree of source  Insert an edge: O(1)  Delete an edge: O(d) where d is out-degree of source  Space requirements:  O(|V|+|E|)  Best for sparse graphs: so usually just stick with linked lists A B C D B/ A/ DB/ /

Undirected graphs 31 Adjacency matrices & adjacency lists both do fine for undirected graphs  Matrix: Could save space; only ~1/2 the array is used  Lists: Each edge in two lists to support efficient “get all neighbors” A B C D ABC A B C D D T T TT FFF FTF F F FFTF F T T A B C D B/ A DB/ C/ C/

Next… 32 Okay, we can represent graphs Now let’s implement some useful and non-trivial algorithms  Topological sort: Given a DAG, order all the vertices so that every vertex comes before all of its neighbors