1 CSE 332: Graphs Richard Anderson Spring 2016. 2 Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss 9.1-9.3.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
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.
Connected Components, Directed Graphs, Topological Sort COMP171.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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 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.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
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 Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
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
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 332 Data Abstractions: Graphs and Graph Traversals Kate Deibel Summer 2012 July 23, 2012CSE 332 Data Abstractions, Summer
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.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
COSC 2007 Data Structures II Chapter 14 Graphs I.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Winter 2015.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
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.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE332: Data Structures & Algorithms Lecture 14: Introduction to Graphs Aaron Bauer Winter 2014.
Graphs and Shortest Paths Using ADTs and generic programming.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CSE 373 Topological Sort Graph Traversals
CSE332: Data Structures & Algorithms Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
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.
Graph Algorithms Using Depth First Search
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Paul Beame in lieu of Richard Anderson
Directed Graph Algorithms
Richard Anderson Autumn 2016 Lecture 5
Based on slides of Dan Suciu
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS Lecture 17 CS2110 Spring 2018.
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
Presentation transcript:

1 CSE 332: Graphs Richard Anderson Spring 2016

2 Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss Guest lecture, Paul Beame

3 Graphs A formalism for representing relationships between objects Graph G = (V,E) –Set of vertices: V = {v 1,v 2,…,v n } –Set of edges: E = {e 1,e 2,…,e m } where each e i connects one vertex to another (v j,v k ) For directed edges, (v j,v k ) and (v k,v j ) are distinct. (More on this later…) A B C V = {A, B, C, D} E = {(C, B), (A, B), (B, A) (C, D)} D

4 Graphs Notation |V| = number of vertices |E| = number of edges v is adjacent to u if (u,v) ∈ E –neighbor of = adjacent to –Order matters for directed edges It is possible to have an edge (v,v), called a loop. –We will assume graphs without loops. V = {A, B, C, D} E = {(C, B), (A, B), (B, A) (C, D)} A B C D

5 Examples of Graphs For each, what are the vertices and edges? The web Facebook Highway map Airline routes Call graph of a program …

6 Directed Graphs In directed graphs (a.k.a., digraphs), edges have a direction: Thus, (u,v) ∈  E does not imply (v,u) ∈  E. I.e., v adjacent to u does not imply u adjacent to v. In-degree of a vertex: number of inbound edges. Out-degree of a vertex : number of outbound edges. or 2 edges here A B C D A B C D

7 Undirected Graphs In undirected graphs, edges have no specific direction (edges are always two-way): Thus, (u,v) ∈ E does imply (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. (Same as number of adjacent vertices.) A B C D

8 Weighted Graphs Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton Each edge has an associated weight or cost.

9 Paths and Cycles A path is a list of vertices {w 1, w 2, …, w q } such that (w i, w i+1 ) ∈ E for all 1 ≤  i < q A cycle is a path that begins and ends at the same node Dallas Seattle San Francisco Chicago Salt Lake City P = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

10 Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge Seattle San Francisco Dallas Chicago Salt Lake City For path P: length(P) = 5 cost(P) = 11.5 How would you ensure that length(p)=cost(p) for all p?

11 Simple Paths and Cycles A simple path repeats no vertices (except that the first can also be the last): P = {Seattle, Salt Lake City, San Francisco, Dallas} P = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A cycle is a path that starts and ends at the same node: P = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} P = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle} A simple cycle is a cycle that is also a simple path (in undirected graphs, no edge can be repeated).

12 Paths/Cycles in Directed Graphs Consider this directed graph: Is there a path from A to D? Does the graph contain any cycles? A B C D

13 Undirected Graph Connectivity Undirected graphs are connected if there is a path between any two vertices: A complete undirected graph has an edge between every pair of vertices: (Complete = fully connected) Connected graphDisconnected graph

14 Directed graphs are strongly connected if there is a path from any one vertex to any other. Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction. A complete directed graph has a directed edge between every pair of vertices. (Again, complete = fully connected.) Directed Graph Connectivity

15 Trees as Graphs A tree is a graph that is: – undirected – acyclic – connected Hey, that doesn’t look like a tree! A B DE C F HG

16 Rooted Trees We are more accustomed to: Rooted trees (a tree node that is “special”) Directed edges from parents to children (parent closer to root). A B DE C F HG A B DE C F HG A B DE C F HG A rooted tree (root indicated in red) drawn two ways Rooted tree with directed edges from parents to children. Characteristics of this one?

17 Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no (directed) cycles. main() add() access() mult() read() Aside: If program call- graph is a DAG, then all procedure calls can be in- lined

18 |E| and |V| How many edges |E| in a graph with |V| vertices? What if the graph is directed? What if it is undirected and connected? Can the following bounds be simplified? –Arbitrary graph: O(|E| + |V|) –Arbitrary graph: O(|E| + |V| 2 ) –Undirected, connected: O(|E| log|V| + |V| log|V|) Some (semi-standard) terminology: –A graph is sparse if it has O(|V|) edges (upper bound). –A graph is dense if it has  (|V| 2 ) edges.

19 What’s the data structure? Common query: which edges are adjacent to a vertex

20 Representation 2: Adjacency List A list (array) of length |V| in which each entry stores a list (linked list) of all adjacent vertices Space requirements? Best for what kinds of graphs? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge? A B C D A B C D

21 Representation 1: Adjacency Matrix A |V| x |V| matrix M in which an element M[u,v] is true if and only if there is an edge from u to v Space requirements? Best for what kinds of graphs? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge? A B C D ABC A B C D D

22 Representing Undirected Graphs What do these reps look like for an undirected graph? ABC A B C D D A B C D A B C D Adjacency matrix: Adjacency list:

23 Some Applications: Bus Routes in Downtown Seattle If we’re at 3 rd and Pine, how can we get to 1 st and University using Metro? How about 4 th and Seneca?

24 Application: Topological Sort Given a graph, G = (V,E), output all the vertices in V sorted so that no vertex is output before any other vertex with an edge to it. CSE 142CSE 143 CSE 321 CSE 341CSE 378 CSE 326CSE 370 CSE 403 CSE 421CSE 467 CSE 451 CSE 322 Is the output unique? CSE 303CSE 457 What kind of input graph is allowed?

25 Topological Sort: Take One 1.Label each vertex with its in-degree (# inbound edges) 2.While there are vertices remaining: a.Choose a vertex v of in-degree zero; output v b.Reduce the in-degree of all vertices adjacent to v c.Remove v from the list of vertices Runtime:

26 CSE 142CSE 143 CSE 321 CSE 341 CSE 378 CSE 326 CSE 370 CSE 403 CSE 421 CSE 467 CSE 451 CSE 322 CSE 303 CSE

27 void Graph::topsort(){ Vertex v, w; labelEachVertexWithItsInDegree(); for (int counter=0; counter < NUM_VERTICES; counter++){ v = findNewVertexOfDegreeZero(); v.topologicalNum = counter; for each w adjacent to v w.indegree--; }

28 Topological Sort: Take Two 1.Label each vertex with its in-degree 2.Initialize a queue Q to contain all in-degree zero vertices 3.While Q not empty a.v = Q.dequeue; output v b.Reduce the in-degree of all vertices adjacent to v c.If new in-degree of any such vertex u is zero Q.enqueue(u) Runtime: Note: could use a stack, list, set, box, … instead of a queue

29 void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; labelEachVertexWithItsIn-degree(); q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()){ v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } intialize the queue get a vertex with indegree 0 insert new eligible vertices

Find a topological order for the following graph E F D A C B K J G H I L

If a graph has a cycle, there is no topological sort Consider the first vertex on the cycle in the topological sort It must have an incoming edge B A D E F C

Lemma: If a graph is acyclic, it has a vertex with in degree 0 Proof: Pick a vertex v 1, if it has in-degree 0 then done If not, let (v 2, v 1 ) be an edge, if v 2 has in-degree 0 then done If not, let (v 3, v 2 ) be an edge... If this process continues for more than n steps, we have a repeated vertex, so we have a cycle