Graphs.

Slides:



Advertisements
Similar presentations
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Advertisements

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 Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Chapter 12 Graphs Andreas Savva. 2 Definition A graph consists of a set of vertices together with a set of edges. If e = (v,w) is an edge.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –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.
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 Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
© 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 Chapter 12.
 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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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.
COSC 2007 Data Structures II
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. 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:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs and Shortest Paths Using ADTs and generic programming.
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
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.
Graphs.
CS212: Data Structures and Algorithms
Introduction to Graphs
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
CMSC 341 Lecture 21 Graphs (Introduction)
Comp 245 Data Structures Graphs.
Graph Operations And Representation
Refresh and Get Ready for More
Graphs Graph transversals.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Graphs.
Graph Operations And Representation
Graph Theory By Amy C. and John M..
Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
ITEC 2620M Introduction to Data Structures
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs.
Graphs.
Graph Operations And Representation
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright  1997 Oxford University Press All Rights Reserved
Graphs.
Chapter 9: Graphs Basic Concepts
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Graphs

Chapter 12 Overview Graphs Graphs provide a rich and powerful way to model relations.

Chapter Objectives 1. To review basic graph vocabulary and concepts. 2. To develop classes that model various kinds of graphs. 3. To learn some useful algorithms for manipulating graphs.

Graphs Graphs represent many real-world problems They do so by showing complex relationships between objects

Course prerequisites

Prerequisites in Graphical Form

Tables and graphs One is commonly used to represent the other. Both have a two-dimensional quality Both can be used to represent relationships between objects

Graph Terminology Graph - represents relationship among nodes Vertex - a node in a graph Edge - link connecting 2 nodes (relationship) Adjacent - 2 nodes connected by an edge Undirected Graph - a graph in which the edges have no directional component

Terminology (con’t) Network - a graph in which the edges are weighted Directed graph (digraph) - a graph in which the edges have directional components (like the course prerequisites example)

Terminology (con’t) Path - a sequence of distinct vertices, each adjacent to the next Cycle - an undirected path containing at least three vertices such that the last vertex is adjacent to the first Connected graph - an undirected graph in which there is a path from any vertex to any other vertex

Types of graphs

Terminology (con’t) Disconnected Graph - an undirected graph in which it is impossible to get to at least one node from any other vertex Free tree - a connected, undirected graph with no cycles

examples

Directed cycle - a directed path which is cyclical

Strongly connected - a digraph in which there is a directed path from any vertex to any other vertex

Weakly connected - a digraph in which it is not possible to go from any vertex to any other

Graph theory Graphs may be most conveniently defined in terms of sets. First, we have a set of vertices (V) Second, we have a set of edges (E) A graph is a set of V and E

Adjacency If there is an edge connection two nodes then they are adjacent. Note: if the edge is directed, then there is only adjacency one way. Let Av be the set of all vertices adjacent to a given vertex v

Adjacency For the subsets Av, we can construct the edges as ordered pairs by the rule: The pair (v,w) is an edge if and only if w is a member of Av.

An Example Digraph

Vertices Each vertex is part of the set V of vertices in the graph

Edges Each edge can be represented as a pair of vertices

Graphs and set theory We can keep track of all the edges in a graph by keeping track, for all vertices v, of the set of edges containing v (E). Similarly, we could keep track of the set A of all vertices adjacent to v.

Pairs adjacent to vertex 0 (A0) The vertices adjacent to vertex 0 are the set A0 = {3}

New graph definition This leads us to a new definition of a graph: A graph G consists of a set V, called the vertices of G, and, for all v in V, a subset A of V, consisting of the set of vertices adjacent to v.

Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {3} A1 = {0,2,4}

Undirected graphs This definition works with undirected graphs as well: An undirected graph is one which satisfies the following symmetry property: if w is a member of Av, this implies that v is a member of Aw, for all v and w such that v,w are members of V

Undirected Graph

Vertices Each vertex is part of the set V of vertices in the graph

Edges Each edge can be represented as a pair of vertices

Pairs adjacent to 0 (A0) The vertices adjacent to vertex 0 are the set

Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {1,3,4} A1 = {0,2,4}

How to represent graphs Since graphs are sets and subsets, they need a two dimensional structure One dimension is the vertices The other dimension is the edges (or the vertices adjacent to the one we are on)

What might do with graphs? Neighborhood queries For a vertex, find all edges connected to other vertices Edge member queries Determine whether an edge is in the graph

Analysis If there are n vertices and m edges it may take as many as n*m operations to traverse every relationship represented in a graph Depending on our representation of the graph we may be able to streamline the searching process

List representation One method of implementing a graph is to employ a List of vertices, such that from each vertex we can have access to those which are adjacent. A list of linked lists is one method of doing this.

An Adjacency List

Adjacency Lists Linked implemetation Advantages No wasted memory space Disadvantages More complicated. Sequential access only. Search would be O(n) where n is the number of possible vertices adjacent to any given vertex.

Contiguous implementation Advantages Less awkward than linked Allows for random access

Adjacency Matrix

Adjacency Tables Advantages Easy to interpret. A[v,w] is true only if vertex v is adjacent to vertex w. If the graph is directed then this indicates an edge from v to w. If the graph is undirected then the adjacency table is symmetric and A[w,v] will also be true.

Disadvantages Mirrored across the diagonal for undirected graphs Not sparse.

UAL GraphADT Characteristics An Undirected Adjacency List Graph G = (V,E) stores an undirected graph so that vertex neighbors can be found efficiently. The number of vertices in the graph, n = |V|, is fixed when the graph is created. The vertices are labeled 0…n-1.

Operations:

vertexSize() int vertexSize() Precondition: None. Postcondtion: None. Returns: The number of vertices in the graph, |V|.

edgeSize() int edgeSize() Precondition: None. Postcondtion: None. Returns: The number of edges in the graph, |E|.

addEdge() void addEdge(i,j) Precondition: Postcondition:

nextNeighbor() int nextNeighbor(i) Precondition: Postcondition: If the current iterator position is within the neighbor list, it’s advanced to the next neighbor; if it’s at the end of the neighbor list, it’s reset to the beginning. Returns: The next neighbor of i, or n if at the end of the list.

DAL Graph ADT Characteristics A Directed Adjacency List Graph G = (V,E) stores an directed graph so that vertex neighbors can be found efficiently. The number of vertices in the graph, n = |V|, is fixed when the graph is created. The vertices are labeled 0…n-1.

vertexSize() and edgeSize() int vertexSize() Precondition: None. Postcondition: None. Returns: The number of vertices |V|. int edgeSize() Returns: The number of edges |E|.

addEdge() void addEdge(i,j) Precondition: Postcondition:

nextNeighbor() int nextNeighbor(i) Precondition: Postcondition: If the current iterator position is within the neighbor list, it’s advanced to the next neighbor; if it’s at the end of the neighbor list, it’s reset to the beginning. Returns: The next neighbor of i, or n if at the end of the list.

UAM Graph ADT Characteristics An Undirected Adjacency Matrix Graph G = (V,E) stores an undirected graph so that vertex connectivity can be queried efficiently. The number of vertices in the graph, n =|V|, is fixed when the graph is created. The vertices are labeled 0…n-1.

Operations: int vertexSize() Precondition: None. Postcondition: None. Returns: The number of vertices in graph, |V| int edgeSize() Returns: The number of edges in the graph, |E|

addEdge() void addEdge(i,j) Precondition: Postcondition:

edgeConnected() bool edgeConnected(i,j) Precondition: Postcondition: None. Returns: True if and only if Note that by convention

DAM Graph ADT Characteristics A Directed Adjacency Matrix Graph G = (V,E) stores an directed graph so that vertex connectivity can be queried efficiently. The number of vertices in the graph, n = |V|, is fixed when the graph is created. The vertices are labeled 0…n-1.

Operations: int vertexSize() Precondition: None. Postcondition: None. Returns: The number of vertices in graph, |V|. int edgeSize() Returns: The number of edges in graph, |E|.

addEdge() void addEdge(i,j) Precondition: Postcondition:

edgeConnected() bool edgeConnected(i,j) Precondition: Postcondition: None. Returns: True if and only if Note that by convention

Undirected graph for Exercise 12-5

Directed graph for Exercise 12-6

Adjacency List for Exercise 12-7

Adjacency Matrix for Exercise 12-8