Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
Advertisements

CSE332: Data Abstractions Lecture 15: Introduction to Graphs Dan Grossman Spring 2010.
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.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
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.
Chapter 9: Graphs Basic Concepts
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
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,
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
CSE332: Data Structures & Algorithms Lecture 14: Introduction to Graphs Aaron Bauer Winter 2014.
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
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE 373 Topological Sort Graph Traversals
CSE332: Data Structures & Algorithms Introduction to Graphs
Introduction to Graphs
Lecture 11 Graph Algorithms
Basic Concepts Graphs For more notes and topics visit:
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CS 3343: Analysis of Algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
Instructor: Lilian de Greef Quarter: Summer 2017
CMSC 341 Lecture 21 Graphs (Introduction)
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.
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Paul Beame in lieu of Richard Anderson
Graphs CSE 2011 Winter November 2018.
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.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Graphs.
Graph Operations And Representation
Directed Graph Algorithms
Based on slides of Dan Suciu
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graphs.
Graphs.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Graphs.
Important Problem Types and Fundamental Data Structures
GRAPHS Lecture 17 CS 2110 — Spring 2019.
Graphs.
Lecture 10 Graph Algorithms
Chapter 9: Graphs Basic Concepts
Lecture 20: Implementing Graphs
Chapter 9 Graph algorithms
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 14: Introduction to Graphs Instructor: Lilian de Greef Quarter: Summer 2017

Today Overview of Midterm Introduce Graphs Mathematical representation Undirected & Directed Graphs Self edges Weights Paths & Cycles Connectedness Trees as graphs DAGs Density & Sparsity

Midterm: Statistics and Distribution Remember: it’s curved 20% of grade → can pass class with even a 0 on exam Mean 31.2 /43 Std. dev. 5.48 Median 32.5 /43 Mode 34 /43 Max 41 /43

Midterm: Distribution by Problem

Hash Tables There is a hash table implemented with linear probing that doubles in size every time its load factor is strictly greater than 1⁄2. What is the worst-case condition for insert in this table? What is the asymptotic worst-case running time to insert an item? (let n = # items in table) What is the amortized running time to insert an item to this table?

Hash Tables Now we have a hash table implemented with separate chaining in which each chain stores its keys in sorted order. What is the worst-case condition for insert in this table? What is the asymptotic worst-case running time to insert an item into this table?

Introducing: Graphs Vertices, edges, and paths (oh my!)

Introductory Example This representation is called a In this example, locations (Seattle, Bainbridge Island, the East Side, and Mercer Island) are the And the roads, bridges, and ferry lines are the East Side Bainbridge Island Seattle Mercer Island

Graphs A graph is a formalism for representing relationships among items Very general definition because very general concept A graph is a pair A set of vertices, also known as V = {v1,v2,…,vn} A set of edges E = {e1,e2,…,em} An edge “connects” the vertices Each edge ei is a pair of vertices Graphs can be directed or undirected Bainbridge (B) Seattle (S) Mercer Island (M) East Side (E) V = {S,M,E,B} E = {(S,B), (S,E), (S,M), (M,E)}

Another Example: (V = { characters }, E = { romances }) Source: http://www.webhostingbuzz.com/blog/2015/02/10/superlove-marvels-romantic-relationships-mapped/

Undirected Graphs In undirected graphs, edges have no specific direction Edges are always Thus, (u,v)  E implies (v,u)  E Only one of these edges needs to be in the set The other is implicit, so normalize how you check for it Degree of a vertex: number of edges containing that vertex Put another way: the number of adjacent vertices Bainbridge (B) Seattle (S) Mercer Island (M) East Side (E) B S M E degree(S) = degree(B) =

Directed Graphs In directed graphs (sometimes called digraphs), edges have a direction Thus, (u,v) E does not imply (v,u) E. Let (u,v) E mean u → v 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 B S M E B S M E or In-degree(E) = Out-degree(B) =

Self-Edges, Connectedness 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 (often therefore implicit, but we will be explicit) A node can have a degree / in-degree / out-degree of A graph does not have to be connected Even if every node has non-zero degree

More notation For a graph G = (V,E): |V| is the number of vertices V = {S,M,E,B} E = {(S,B), (S,E), (S,M), (M,E)} For a graph G = (V,E): |V| is the number of vertices |E| is the number of edges Minimum? Maximum for undirected? Maximum for directed? If (u,v)  E Then v is a neighbor of u, i.e., v is adjacent to u Order matters for directed edges u is not adjacent to v unless (v,u)  E (assuming self-edges allowed, else subtract |V|) max for undirected: every vertex has degree v+1 (self-edges allowed), v vertices, edges shared between two, so divide in half max for directed: every vertex (v of them) has edge to every other vertex (v of them) Is M adjacent to B? Is S adjacent to B? Is B adjacent to S?

Examples Which would… Use directed edges? Have self-edges? Be connected? Have 0-degree nodes? Web pages with links Facebook friends Methods in a program that call each other Road maps (e.g., Google maps) Airline routes Family trees Course pre-requisites Web pages with links: directed, no self-edges (unless link to self), not connected, could have 0 degree nodes Facebook friends: undirected, no self-edges (unless link to self), not connected, could have 0 degree nodes Methods in a program that call each other: directed, self-edges possible (recursive), not connected, could have 0 degree nodes Road maps (e.g., Google maps): undirected, self-edges, not connected, Airline routes: directed, no self edges, connected, no 0-degree nodes Family trees: directed, no self-edges, weakly connected Course pre-requisites, directed, no self-edges, not connected, could have 0 degree nodes

Weighted Graphs In a weighed graph, each edge has a weight a.k.a. cost Typically numeric (most examples use ints) Some graphs allow negative weights; many do not B S M E

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

Paths and Cycles A path is a list of vertices [v0,v1,…,vn] such that (vi,vi+1) E for all 0  i < n. Said as “a path from v0 to vn” A cycle is a path that begins and ends at the same node (v0 == vn) Seattle San Francisco Dallas Chicago Salt Lake City Example: [Seattle, ] Salt Lake City, Chicago, Dallas, San Francisco, Seattle

Path Length and Cost length(P) = cost(P) = Path length: Number of edges in a path Path cost: Sum of weights of edges in a path Example: let P = [Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle] Chicago Seattle San Francisco Dallas Salt Lake City 3.5 2 2.5 3 length(P) = cost(P) =

Simple Paths and Cycles A simple path repeats no vertices, except the first might be the last e.g. [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 e.g. [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 e.g. [Seattle, Salt Lake City, San Francisco, Dallas, Seattle]

Paths and Cycles in Directed Graphs B S M E Example: Is there a path from B to M ? Does the graph contain any cycles?

Undirected-Graph Connectivity 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 graph Connected graph

Directed-Graph Connectivity 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 plus self edges

Practice Time! Let graph G = (V, E) where V = {a, b, c, d} E = {(a,b), (b,c), (a,c), (b,d)} How connected is G? Disconnected Weakly Connected Strongly Connected Complete / Fully Connected

Trees as Graphs When talking about graphs, D E C F H G When talking about graphs, we say a tree is a graph that is: Connected Acyclic when you treat edges as undirected Note that Edges can be undirected All trees are graphs, but not all graphs are trees A B D E C F H G

Rooted Trees We are more accustomed to rooted trees where: We identify a unique root We think of edges as directed: parent to children Given a tree, picking a root gives a unique rooted tree The tree is just drawn differently A B D E C F H G A B D E C F H G redrawn

Rooted Trees We are more accustomed to rooted trees where: We identify a unique root We think of edges as directed: parent to children Given a tree, picking a root gives a unique rooted tree The tree is just drawn differently F G H C A B D E A B D E C F H G redrawn

Directed Acyclic Graphs (DAGs) 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 DAG? DAG? DAG?

Examples Which of our directed-graph examples do you expect to be a DAG? Web pages with links Methods in a program that call each other Airline routes Family trees Course pre-requisites Family trees Course pre-requisites

Density / Sparsity Recall: In an undirected graph, 0 ≤ |E| < |V|2 Recall: In a directed graph: 0 ≤ |E| ≤ |V|2 So for any graph, O(|E|+|V|) is Another 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 |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 If |E| is O(|V|) we say the graph is sparse More sloppily, sparse means “most possible edges if connected at least as many edges as vertices -1

What is the Data Structure? So graphs are really useful for lots of data and questions For example, “what’s the lowest-cost path from x to y” But we need a data structure that represents graphs The “best one” 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 Adjacency Matrix and Adjacency List Different trade-offs, particularly time versus space

Adjacency Matrix E S B M 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 1 2 3 B S M E (0) (1) (2) (3) 1 M[row][column] Does this graph have self edges? 2 3

Adjacency Matrix Properties 1 2 3 Running time to: Get a vertex’s out-edges: Get a vertex’s in-edges: Decide if some edge exists: Insert an edge: Delete an edge: Space requirements: Best for sparse or dense graphs? F T B S M E (0) (1) (2) (3)

Adjacency Matrix Properties How will the adjacency matrix vary for an undirected graph? Undirected will be symmetric around the diagonal How can we adapt the representation for weighted graphs? Instead of a Boolean, store a number in each cell Need some value to represent ‘not an edge’ In some situations, 0 or -1 works

Adjacency List E S B M Assign each node a number from 0 to |V|-1 An array of length |V| in which each entry stores a list of all adjacent vertices (e.g., linked list) B S M E (0) (1) (2) (3) 1 2 3

Adjacency List Properties 1 2 3 / Running time to: Get all of a vertex’s out-edges: where d is out-degree of vertex Get all of a vertex’s in-edges: (but could keep a second adjacency list for this!) Decide if some edge exists: where d is out-degree of source Insert an edge: (unless you need to check if it’s there) Delete an edge: Space requirements: O(|V|+|E|) B S M E (0) (1) (2) (3) Best for sparse or dense graphs?