650 likes | 851 Views
Graphs. Chapter 12 Graphs Overview 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.
E N D
Chapter 12 • Graphs • Overview • 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
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
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
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.
Vertices Each vertex is part of the set V of vertices in the graph V = {0,1,2,3,4}
Edges Each edge can be represented as a pair of vertices E = {{0,3}, {1,0}, {1,2}, {1,4}, {2,1}, {4,0},{4,2}}
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} A2 = {1} A3 = {} A4 = {0,2}
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
Vertices Each vertex is part of the set V of vertices in the graph V = {0,1,2,3,4}
Edges Each edge can be represented as a pair of vertices E = {{0,1}, {0,3}, {0,4}, {1,2}, {1,4}, {2,4}}
Pairs adjacent to 0 (A0) The vertices adjacent to vertex 0 are the set A0 = {1,3,4}
Graph definition G = V + A, V = {0,1,2,3,4}, A0 = {1,3,4} A1 = {0,2,4} A2 = {1} A3 = {0} A4 = {0,1}
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.
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 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.
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() • Precondition: None. • Postcondition: None. • Returns: The number of edges |E|.