GRAPHS Lecture 17 CS 2110 — Spring 2019.

Slides:



Advertisements
Similar presentations
Graph-02.
Advertisements

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.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 03 / 25 / 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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Subchap 22.1 – Representation of Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Data Structures Week 9 Introduction to Graphs Consider the following problem. A river with an island and bridges. The problem is to see if there is a way.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Data Structures & Algorithms Graphs
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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,
Graphs Upon completion you will be able to:
Copyright © Curt Hill Graphs Definitions and Implementations.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs.
Graphs Lecture 19 CS2110 – Spring 2013.
GRAPHS Lecture 16 CS2110 Fall 2017.
Graphs ORD SFO LAX DFW Graphs Graphs
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Depth-First Search.
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Operations And Representation
CS200: Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Modeling and Simulation NETW 707
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.
Chapter 9: Graphs Basic Concepts
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graph Operations And Representation
Chapter 11 Graphs.
GRAPHS Lecture 17 CS2110.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs By Rajanikanth B.
Graphs Chapter 7 Visit for more Learning Resources.
Graphs G = (V, E) V are the vertices; E are the edges.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS Lecture 17 CS2110 Spring 2018.
Graphs: Definitions How would you represent the following?
Lecture 10 Graph Algorithms
Chapter 9: Graphs Basic Concepts
Graphs G = (V,E) V is the vertex set.
GRAPHS.
Heaps Chapter 6 Section 6.9.
Presentation transcript:

GRAPHS Lecture 17 CS 2110 — Spring 2019

JavaHyperText Topics “Graphs”, topics 1-3 1: Graph definitions 2: Graph terminology 3: Graph representations

Charts (aka graphs)

Graphs Graph: Graphs generalize trees [charts] Points connected by curves [in CS] Vertices connected by edges Graphs generalize trees Graphs are relevant far beyond CS…examples… 5 4 2 7 8 9

Vertices: people “from” Edges: friendships https://medium.com/@johnrobb/facebook-the-complete-social-graph-b58157ee6594

Vertices: subway stops Edges: railways

Vertices: stations Edges: cables Submarine fibre optic cables carry 99% of international data. Source: https://www.newsweek.com/undersea-cables-transport-99-percent-international-communications-319072 Vertices: stations Edges: cables https://www.submarinecablemap.com/

Vertices: State capitals Edges: “States have shared border” http://www.cs.cmu.edu/~bryant/boolean/maps.html

Graphs as mathematical structures

Undirected graphs An undirected graph is a pair (V, E) where V is a set Element of V is called a vertex or node We’ll consider only finite graphs Ex: V = {A, B, C, D, E}; |V| = 5 E is a set Element of E is called an edge or arc An edge is itself a two-element set {u, v} where {u, v} ⊆ V Often require u ≠ v (i.e., no self-loops) Ex: E = {{A, B}, {A, C}, {B, C}, {C, D}}, |E| = 4 A B C D E

Directed graphs A directed graph is similar except the edges are pairs (u, v), hence order matters A B C D E V = {A, B, C, D, E} E = {(A, C), (B, A), (B, C), (C, D), (D, C)} |V| = 5 |E| = 5

Convert undirected  directed? Right question is: convert and maintain which properties of graph? Convert undirected to directed and maintain paths? Any path from A to B in undirected graph would be reversible to a path from B to A, which does not exist in original graph

Paths A path is a sequence v0,v1,v2,...,vp of vertices such that for 0 ≤ i < p, Directed: (vi, vi+1) ∈ E Undirected: {vi, vi+1} ∈ E The length of a path is its number of edges Path: A,C,D A B C D E

Convert undirected  directed? Right question is: convert and maintain which properties of graph? Convert undirected to directed and maintain paths: Nodes unchanged Replace each edge {u,v} with two edges {(u,v), (v,u)} Convert directed to undirected and maintain paths: Can’t: Any path from A to B in undirected graph would be reversible to a path from B to A, which does not exist in original graph A B

Labels Whether directed or undirected, edges and vertices can be labeled with additional data A B C D E 5 -3 1 2 Edges now labeled with integers Nodes already labeled with characters

Discuss How could you represent a maze as a graph? Vertex for each point where there is more than one path to take Edges connect according to paths between those points Algorithms, 2nd ed., Sedgewick, 1988

Announcement A4: See time distribution and comments @735 Spending >16 hours is a problem; talk to us or a TA about why that might be happening Comments on the GUI: “GUI was pretty awesome.” “I didn't see the relevance of the GUI.” Hints: “Hints were extremely useful and I would've been lost without them.” “Hints are too helpful. You should leave more for people to figure out on their own.” Adjectives: “Fun” (x30), “Cool” (x19) “Whack”, “Stressful”, “Tedious”, “Rough”

Graphs as data structures

Graph ADT Operations could include: Add a vertex Remove a vertex Search for a vertex Number of vertices Add an edge Remove an edge Search for an edge Number of edges You aren’t going to find a standard Graph ADT in the Java library. Usually these are custom-built for the graphs you want to represent. Much like trees. Demo

Graph representations Two vertices are adjacent if they are connected by an edge Common graph representations: Adjacency list Adjacency matrix 1 2 3 4 running example (directed, no edge labels)

Adjacency “list” Maintain a collection of the vertices For each vertex, also maintain a collection of its adjacent vertices Vertices: 1, 2, 3, 4 Adjacencies: 1: 2, 3 2: 4 3: 2, 4 4: none 1 2 3 4 Could implement these “lists” in many ways…

Adjacency list implementation #1 Map from vertex label to sets of vertex labels 1 ↦ {2, 3} 2 ↦ {4} 3 ↦ {2, 4} 4 ↦ {none} 1 2 3 4 Recall, no order implied on sets or in map

Adjacency list implementation #2 Linked list, where each node contains vertex label and linked list of adjacent vertex labels 1 2 3 1 2 3 4 2 4 Definitely an order now 3 2 4 4 Demo

Adjacency list implementation #3 Array, where each element contains linked list of adjacent vertex labels 1 2 3 4 1 2 3 2 4 3 2 4 Definitely an order now 4 Requires: labels are integers; dealing with bounded number of vertices

Adjacency “matrix” Given integer labels and bounded # of vertices… Maintain a 2D Boolean array b Invariant: element b[i][j] is true iff there is an edge from vertex i to vertex j 1 2 3 4 F T 1 2 3 4

Adjacency list vs. Adjacency matrix 1 2 3 4 F T 1 2 3 2 4 3 2 4 4 Efficiency: Space to store? O(|V| + |E|) O(|V|2)

Adjacency list vs. Adjacency matrix 1 2 3 4 F T 1 2 3 2 4 3 2 4 4 Efficiency: Time to visit all edges? O(|V| + |E|) O(|V|2)

Adjacency list vs. Adjacency matrix 1 2 3 4 F T 1 2 3 2 4 3 2 4 4 Efficiency: Time to determine whether edge from v1 to v2 exists? O(|V| + |E|) O(1) Tighter: O(|V| + # edges leaving v1)

More graph terminology Vertices u and v are called the source and sink of the directed edge (u, v), respectively the endpoints of (u, v) or {u, v} The outdegree of a vertex u in a directed graph is the number of edges for which u is the source The indegree of a vertex v in a directed graph is the number of edges for which v is the sink The degree of a vertex u in an undirected graph is the number of edges of which u is an endpoint A B C D E 1 2 A B C D E 2

Adjacency list vs. Adjacency matrix 1 2 3 4 F T 1 2 3 2 4 3 2 4 4 Efficiency: Time to determine whether edge from v1 to v2 exists? O(|V| + |E|) O(1) Tighter: O(|V| + outdegree(v1))

Adjacency list vs. Adjacency matrix Property Matrix O(|V| + |E|) Space O(|V|2) Time to visit all edges O(|V| + od(v1)) Time to find edge (v1,v2) O(1)

Adjacency list vs. Adjacency matrix Property Matrix O(|V| + |E|) Space O(|V|2) Time to visit all edges O(|V| + od(v1)) Time to find edge (v1,v2) O(1) Max # edges = |V|2 Sparse Dense

Adjacency list vs. Adjacency matrix Property Matrix O(|V| + |E|) Space O(|V|2) Time to visit all edges O(|V| + od(v1)) Time to find edge (v1,v2) O(1) Max # edges = |V|2 Sparse: |E| ≪ |V|2 Dense: |E| ≈ |V|2

Adjacency list vs. Adjacency matrix Property Matrix O(|V| + |E|) Space O(|V|2) Time to visit all edges O(|V| + od(v1)) Time to find edge (v1,v2) O(1) Sparse graphs Better for Dense graphs Max # edges = |V|2 Sparse: |E| ≪ |V|2 Dense: |E| ≈ |V|2