16.8 KB · commit a9602da
To work with graphs using computers, we need efficient ways to represent them. In this section we will introduce three important ways to represent graphs: adjacency lists, adjacency matrices, and incidence matrices. We will also study graph isomorphism, which is the problem of determining whether two graphs have the same structure.
One way to represent a graph without multiple edges is to list all the vertices adjacent to each vertex of the graph. Such a representation is called an adjacency list.
Use adjacency lists to describe the simple graph given in Figure 1.
Solution: Table 1 lists those vertices adjacent to each of the vertices of the graph. ◀
Figure 1 — A Simple Graph.
| Vertex | Adjacent Vertices |
|---|---|
| a | b,c,e |
| b | a |
| c | a,d,e |
| d | c,e |
| e | a,c,d |
| TABLE 1 — An Adjacency List for a Simple Graph. |
To represent a directed graph using adjacency lists, we list the terminal vertices of the edges that start at each vertex of the graph, as Example 2 illustrates.
Figure 2 — A Directed Graph.
| Initial Vertex | Terminal Vertices |
|---|---|
| a | b,c,d,e |
| b | b,d |
| c | a,c,e |
| d | |
| e | b,c,d |
| TABLE 2 — An Adjacency List for a Directed Graph. |
Represent the directed graph shown in Figure 2 by listing all the vertices that are the terminal vertices of edges starting at each vertex of the graph.
Solution: Table 2 represents the directed graph shown in Figure 2. ◀
Carrying out graph algorithms using the representation of graphs by lists of edges, or by adjacency lists, can be cumbersome if there are many edges in the graph. To simplify computation, graphs can be represented using matrices. Two types of matrices commonly used for this purpose will be introduced here. One is based on the adjacency of vertices, and the other is based on the incidence of vertices and edges.
Suppose that G=(V,E) is a simple graph where ∣V∣=n. Suppose that the vertices of G are listed arbitrarily as v1,v2,…,vn. The adjacency matrix A (or AG) of G, with respect to this listing of the vertices, is the n×n zero–one matrix with 1 as its (i,j)th entry when vi and vj are adjacent, and 0 as its (i,j)th entry when they are not adjacent. In other words, if its adjacency matrix is A=[aij], then aij={10if {vi,vj} is an edge of G,otherwise.
Use an adjacency matrix to represent the graph shown in Figure 3.
Figure 3 — A Simple Graph.
Solution: We order the vertices as a,b,c,d. The matrix representing this graph is
0111101011001000.◀Draw a graph with the adjacency matrix
0110100110010110with respect to the ordering of vertices a,b,c,d.
Solution: A graph with this adjacency matrix is shown in Figure 4. ◀
Figure 4 — A Graph with the Given Adjacency Matrix.
The adjacency matrix of a simple graph is symmetric, that is, aij=aji, because both of these entries are 1 when {vi,vj} is an edge of G and both are 0 when {vi,vj} is not an edge of G. Furthermore, because a simple graph has no loops, each entry on the main diagonal is zero, that is, aii=0 for i=1,2,…,n.
Adjacency matrices can also be used to represent undirected graphs with loops and with multiple edges. A loop at the vertex vi is represented by a 1 at the (i,i)th position of the adjacency matrix. When multiple edges connect the same pair of vertices vi and vj, the (i,j)th entry of the adjacency matrix equals the number of edges connecting them.
Use an adjacency matrix to represent the pseudograph shown in Figure 5.
Figure 5 — A Pseudograph.
Solution: The adjacency matrix using the ordering of vertices a,b,c,d is
0302301101122120.◀The adjacency matrix for a directed graph G=(V,E) has a 1 in its (i,j)th position if there is an edge from vi to vj, where v1,v2,…,vn is a list of the vertices. In other words, if A=[aij] is the adjacency matrix of G, then aij={10if (vi,vj) is an edge of G,otherwise. The adjacency matrix of a directed graph does not have to be symmetric, because there may not be an edge from vj to vi when there is an edge from vi to vj. Adjacency matrices can also be used to represent directed multigraphs, with aij equal to the number of directed edges from vi to vj.
Another common way to represent graphs is to use incidence matrices. Let G=(V,E) be an undirected graph with vertices v1,v2,…,vn and edges e1,e2,…,em. The incidence matrix with respect to this ordering of V and E is the n×m matrix M=[mij], where mij={10when edge ej is incident with vi,otherwise.
Represent the graph shown in Figure 6 with an incidence matrix.
Figure 6 — An Undirected Graph.
Solution: The incidence matrix representing this graph is
v1v2v3v4v5e1e2e3e4e5e6100011001001010010010011001100.◀Incidence matrices can also be used to represent multiple edges and loops. Multiple edges are represented in the incidence matrix using columns with identical entries, because these edges are incident with the same pair of vertices. Loops are represented using a column with exactly one entry that is 1, corresponding to the vertex that is incident with this loop.
Represent the pseudograph shown in Figure 7 using an incidence matrix.
Figure 7 — A Pseudograph.
Solution: The incidence matrix representing this graph is
v1v2v3v4v5e1e2e3e4e5e6e7e81000011000110000110000101010010101000010.◀We often need to know whether two graphs have the same structure. For example, if two graphs represent circuits on chips, we might want to know whether they carry out the same function.
In other words, when two simple graphs are isomorphic, there is a one-to-one correspondence between vertices of the two graphs that preserves the adjacency relationship.
Show that the graphs G=(V,E) and H=(W,F), displayed in Figure 8, are isomorphic.
Figure 8 — The Graphs G and H.
Solution: The function f with f(u1)=v1, f(u2)=v4, f(u3)=v3, and f(u4)=v2 is a one-to-one correspondence between V and W. To see that this correspondence preserves adjacency, note that adjacent vertices in G are u1 and u2, u1 and u3, u2 and u4, and u3 and u4, and each of the pairs f(u1)=v1 and f(u2)=v4, f(u1)=v1 and f(u3)=v3, f(u2)=v4 and f(u4)=v2, and f(u3)=v3 and f(u4)=v2 consists of adjacent vertices in H. ◀
It is often difficult to determine whether two simple graphs are isomorphic. There are n! possible one-to-one correspondences between the vertex sets of two simple graphs with n vertices. Testing each such correspondence by checking whether it preserves adjacencies is impractical for graphs with even modest numbers of vertices, because n! grows extremely rapidly.
Instead of trying all possible mappings, we can show that two graphs are not isomorphic by finding a graph invariant—a property preserved by isomorphism—that one graph has and the other does not. Some useful graph invariants include:
Show that the graphs displayed in Figure 9 are not isomorphic.
Solution: Both G and H have five vertices and six edges. However, the degrees of the vertices in G are 2, 3, 2, 2, 3, and in H they are 2, 2, 3, 3, 2. Both graphs have two vertices of degree three and three vertices of degree two.
However, in G, the two vertices of degree three, namely c and e, are adjacent. In H, the two vertices of degree three, namely a and c, are adjacent as well. But in G, the vertex of degree two a is adjacent to two vertices of degree two (namely b and e, where deg(e)=3 and deg(b)=2), while in H each vertex of degree two is adjacent to at least one vertex of degree three. Specifically, in G, deg(a)=2 and its neighbors are b (degree 2) and e (degree 3). In H, the vertices of degree two are b,d,e. Vertex b has neighbors a (degree 3) and c (degree 3); vertex d has neighbors a (degree 3) and c (degree 3); vertex e has neighbor a (degree 3). Because no vertex of degree two in H is adjacent to another vertex of degree two, G and H cannot be isomorphic. ◀
Figure 9 — The Graphs G and H.
Figure 10 — The Graphs G and H.
Determine whether the graphs shown in Figure 10 are isomorphic.
Solution: Both graphs have eight vertices and ten edges. Both have four vertices of degree two and four vertices of degree three. However, the subgraphs induced by vertices of degree three are not isomorphic. In G, the vertices of degree three are b,d,f, and h. The subgraph of G induced by these vertices consists of four vertices and two disjoint edges {b,f} and {d,h}. In H, the vertices of degree three are s,v,w, and z. The subgraph of H induced by these vertices has four edges, connecting these vertices in a cycle of length four: {s,w},{w,z},{z,v}, and {v,s}. These subgraphs are displayed in Figure 11. Because these subgraphs are not isomorphic, G and H are not isomorphic. ◀
Figure 11 — The Subgraphs of G and H Made Up of Vertices of Degree Three and the Edges Connecting Them.
Determine whether the graphs G and H displayed in Figure 12 are isomorphic.
Figure 12 — Graphs G and H.
Solution: Both G and H have six vertices and seven edges. Both have four vertices of degree two and two vertices of degree three. It is also easy to see that the subgraphs of G and H consisting of all vertices of degree two and the edges connecting them are isomorphic (as the reader should verify). Because G and H agree with respect to these invariants, it is reasonable to try to find an isomorphism f.
We now will define a function f and then determine whether it is an isomorphism. Because deg(u1)=2 and because u1 is not adjacent to any other vertex of degree two, the image of u1 must be either v4 or v6, the only vertices of degree two in H not adjacent to a vertex of degree two. We arbitrarily set f(u1)=v6. [If we found that this choice did not lead to isomorphism, we would then try f(u1)=v4.] Because u2 is adjacent to u1, the possible images of u2 are v3 and v5. We arbitrarily set f(u2)=v3. Continuing in this way, using adjacency of vertices and degrees as a guide, we set f(u3)=v4, f(u4)=v5, f(u5)=v1, and f(u6)=v2. We now have a one-to-one correspondence between the vertex set of G and the vertex set of H, namely, f(u1)=v6,f(u2)=v3,f(u3)=v4,f(u4)=v5,f(u5)=v1,f(u6)=v2.
To see whether f preserves edges, we examine the adjacency matrix of G,
AG=010100101001010100101010000101010010,and the adjacency matrix of H with the rows and columns labeled by the images of the corresponding vertices in G,
AH=010100101001010100101010000101010010.Because AG=AH, it follows that f preserves edges. Hence, f is an isomorphism, so G and H are isomorphic. ◀
Graph isomorphism has many practical applications. We describe two important ones here.
Chemists use multigraphs, known as molecular graphs, to model chemical compounds. In these graphs, vertices represent atoms and edges represent chemical bonds between these atoms. Two structural isomers, molecules with identical molecular formulas but with atoms bonded differently, have nonisomorphic molecular graphs. When a potentially new chemical compound is synthesized, a database of molecular graphs is checked to see whether the molecular graph of the compound is the same as one already known.
Electronic circuits are modeled using graphs in which vertices represent components and edges represent connections between them. Modern integrated circuits, known as chips, are miniaturized electronic circuits, often with millions of transistors and connections between them. Because of the complexity of modern chips, automation tools are used to design them. Graph isomorphism is the basis for the verification that a particular layout of a circuit produced by an automated tool corresponds to the original schematic of the design. Graph isomorphism can also be used to determine whether a chip from one vendor includes intellectual property from a different vendor. This can be done by looking for large isomorphic subgraphs in the graphs modeling these chips.