Download presentation
Presentation is loading. Please wait.
1
Graphs
2
Chapter 12 Overview Graphs
Graphs provide a rich and powerful way to model relations.
3
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.
4
Graphs Graphs represent many real-world problems
They do so by showing complex relationships between objects
5
Course prerequisites
6
Prerequisites in Graphical Form
7
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
8
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
9
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)
10
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
11
Types of graphs
12
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
13
examples
14
Directed cycle - a directed path which is cyclical
15
Strongly connected - a digraph in which there is a directed path from any vertex to any other vertex
16
Weakly connected - a digraph in which it is not possible to go from any vertex to any other
17
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
18
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
19
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.
20
An Example Digraph
21
Vertices Each vertex is part of the set V of vertices in the graph
22
Edges Each edge can be represented as a pair of vertices
23
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.
24
Pairs adjacent to vertex 0 (A0)
The vertices adjacent to vertex 0 are the set A0 = {3}
25
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.
26
Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {3} A1 = {0,2,4}
27
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
28
Undirected Graph
29
Vertices Each vertex is part of the set V of vertices in the graph
30
Edges Each edge can be represented as a pair of vertices
31
Pairs adjacent to 0 (A0) The vertices adjacent to vertex 0 are the set
32
Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {1,3,4} A1 = {0,2,4}
33
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)
34
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
35
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
36
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.
37
An Adjacency List
38
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.
39
Contiguous implementation
Advantages Less awkward than linked Allows for random access
40
Adjacency Matrix
41
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.
42
Disadvantages Mirrored across the diagonal for undirected graphs
Not sparse.
43
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.
44
Operations:
45
vertexSize() int vertexSize() Precondition: None. Postcondtion: None.
Returns: The number of vertices in the graph, |V|.
46
edgeSize() int edgeSize() Precondition: None. Postcondtion: None.
Returns: The number of edges in the graph, |E|.
47
addEdge() void addEdge(i,j) Precondition: Postcondition:
48
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.
49
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.
50
vertexSize() and edgeSize()
int vertexSize() Precondition: None. Postcondition: None. Returns: The number of vertices |V|. int edgeSize() Returns: The number of edges |E|.
51
addEdge() void addEdge(i,j) Precondition: Postcondition:
52
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.
53
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.
54
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|
55
addEdge() void addEdge(i,j) Precondition: Postcondition:
56
edgeConnected() bool edgeConnected(i,j) Precondition:
Postcondition: None. Returns: True if and only if Note that by convention
57
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.
58
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|.
59
addEdge() void addEdge(i,j) Precondition: Postcondition:
60
edgeConnected() bool edgeConnected(i,j) Precondition:
Postcondition: None. Returns: True if and only if Note that by convention
61
Undirected graph for Exercise 12-5
62
Directed graph for Exercise 12-6
63
Adjacency List for Exercise 12-7
64
Adjacency Matrix for Exercise 12-8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.