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.

Slides:



Advertisements
Similar presentations
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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. Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges”
Breadth-First and Depth-First Search
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
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.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Introduction to Graph  A graph consists of a set of vertices, and a set of edges that link together the vertices.  A graph can be: Directed: Edges are.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
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 & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Social Media Mining Graph Essentials.
CS261 Data Structures DFS and BFS – Edge List Representation.
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 CSC 211 Data Structures Lecture 28 Dr. Iftikhar Azim Niaz 1.
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.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
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.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
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.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
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.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CSE 373 Topological Sort Graph Traversals
CS120 Graphs.
Chapter 22: Elementary Graph Algorithms I
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs: Definitions How would you represent the following?
Lecture 10 Graph Algorithms
Graphs G = (V,E) V is the vertex set.
Presentation transcript:

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 objects (relationship = edge, object = node) Examples Physical Networks: Communication, Information, Transportation, Mazes Social Networks: Facebook, LinkedIn, Google+ Dependency Networks: Flow charts, State machines, Bayes Nets, Markov Random Fields, Regulatory Networks 1

Directed Graph A directed graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E  V  V is a set of edges For any e i = (u, v)  E“ คู่อันดับ – ordered pairs” u  V is the source node v  V is the target node (aka destination node) Note: In some graphs we allow self-edges i.e., e i = (u, u) We use directed edges to represent asymmetrical relationships Examples: One-way streets, Causal relationship, Regulatory relationship, Web page links 2

Undirected Graph An undirected graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E = { {u,v} | u, v  V } is a set of edges Here E is a set of คู่ไม่อันดับ – unordered pairs We use undirected edges to represent symmetrical relationships Examples: Two-way streets Network traffic Mazes Six degrees of separation (and Six degrees of Kevin Bacon) Six degrees of separation Six degrees of Kevin Bacon 3

Weighted Graph An weighted graph, G = (V, E, W), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges (directed or undirected) W: E   (i.e., a function from E to  ) Examples: Distances on a map 4

Properties of graphs |V| = number of nodes |E| = number of edges If we allow self-edges In a directed graph, |E|  |V| 2 In an undirected graph, |E|  |V|*(|V|-1)/2 + |V| A sparse graph is one where |E| = O(|V|) or less Most ‘real world’ graphs are sparse Transportation networks, www, mazes, Facebook A dense graph is one where |E| = O(|V| 2 ) or more 5

Properties of graphs The degree of a node is the number of edges coming in or out of that node In undirected graphs Degree(u) = | { v  V : {u,v}  E } | Thus, Degree(u) is the number of “neighbors” of u In directed graphs In-degree(u) = | { v  V : (v,u)  E } | Out-degree(u) = | { v  V : (u,v)  E } | Degree(u) = In-degree(u) + Out-degree(u) 6

Representing Graphs Directed, unweighted Adjacency matrixAdjacency List abcdabcd b cb a a a b c d abcdabcd source target d

Representing Graphs Directed, weighted Adjacency matrixAdjacency List abcdabcd b,2 c,3 a,-1 a,9 a b c d abcdabcd source target d,8

Representing Graphs Undirected, unweighted Adjacency matrixAdjacency List 9 abcdabcd a b c d abcdabcd source target

Representing Graphs Undirected, weighted Adjacency matrixAdjacency List 10 abcdabcd a b c d abcdabcd source target

Mini-Quiz Undirected or Directed Graphs Space Complexity Adjacency Matrix Representation: O(?) Adjacency List Representation: O(?) Which is the better choice if the graph is sparse? How expensive is it to check whether (u,v)  E ? Adjacency Matrix Representation: O(?) Adjacency List Representation: O(?) Undirected Graphs How expensive is it to compute the degree of a node? Adjacency Matrix Representation: O(?) Adjacency List Representation: O(?) 11

Mini-Quiz Directed Graphs How expensive is it to compute the out-degree of a node? Adjacency Matrix Representation: O(?) Adjacency List Representation: O(?) How expensive is it to compute the in-degree of a node? Adjacency Matrix Representation: O(?) Adjacency List Representation: O(?) 12

Paths and Cycles Let G = (V,E) be a graph, a path is a sequence of m vertices v 1, v 2, v 3, …, v m-1, v m such that (v i, v i+1 )  E for 1  i  m-1 v 1 is the source vertex of the path v m is the target vertex of the path The length of the path is m-1 A cycle is a path where v 1 == v m 13

Simple Paths and Simple Cycles A path is simple if there is at most one copy of each vertex in the path What is the maximum length of a simple path in a graph with |V| nodes? A cycle is simple if there is at most one copy of each vertex, except the source/target, in the cycle What is the maximum length of a simple cycle in a graph with |V| nodes? 14

Reachability One of the most common tasks for computing over graphs is to find the subset of nodes reachable from some particular node u This is called “reachability problem” Special case: determine whether there is a path from some particular node u to some particular node t There are two fundamental algorithms for answering this question Depth-first search (DFS) Breadth-first search (BFS) 15

Depth-First Search (DFS) DFS Algorithm on a node u, Make sure to keep track of nodes that has been visited For each edge (u, v i ) If v i has not yet been visited Include v i to a list of nodes reachable from u Recursively perform DFS on v i Note: We have to keep track of visited nodes because there might be cycles in the graph 16

Breadth-First Search (BFS) BFS Algorithm on a node u, Make sure to keep track of nodes that has been visited Initialization: Enqueue u to a queue Add u to a list of nodes reachable from u While the queue isn’t empty Dequeue node v from front of queue For each edge (v,w) If w hasn’t been visited Enqueue w to a queue Add w to a list of nodes reachable from u 17

Depth-First Search (BFS) … again DFS Algorithm on a node u, Make sure to keep track of nodes that has been visited Initialization: Push u to a stack Add u to a list of nodes reachable from u While the stack isn’t empty Pop node v from the stack For each edge (v,w) If w hasn’t been visited Push w to a stack Add w to a list of nodes reachable from u 18

Analyzing BFS, DFS algorithms What’s the total cost of a DFS/ BFS? Hints How many times do we process each node in a DFS/BFS? How many times do we cross each edge in a DFS/BFS? 19