Download presentation

Presentation is loading. Please wait.

0
**Mathematical Structures for Computer Science Chapter 5**

Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co. MSCS Slides Graphs and Trees

1
**Graphs and Their Representations**

Definitions of a Graph DEFINITION (INFORMAL): GRAPH A graph is a nonempty set of nodes (vertices) and a set of arcs (edges) such that each arc connects two nodes. Our graphs will always have a finite number of nodes and arcs. Example: The set of nodes in the airline map below is {Chicago, Nashville, Miami, Dallas, St. Louis, Albuquerque, Phoenix, Denver, San Francisco, Los Angeles}. There are 16 arcs; Phoenix–Albuquerque, ChicagoNashville, MiamiDallas, and so on. Section 5.1 Graphs and Their Representations

2
**Formal Definition of a Graph**

The informal definition of a graph works quite well if we have the visual representation of the graph before us to show which arcs connect which nodes. But without the picture, we need a concise way to convey this information. DEFINITION (FORMAL): GRAPH A graph is an ordered triple (N, A, g) where: N = a nonempty set of nodes (vertices) A = a set of arcs (edges) g = a function associating with each arc a an unordered pair x–y of nodes called the endpoints of a Section 5.1 Graphs and Their Representations

3
**Formal Definition of a Graph: Example**

Sketch a graph having nodes {1, 2, 3, 4, 5}, arcs {a1, a2, a3, a4, a5, a6}, and function g(a1) = 1–2, g(a2) = 1–3, g(a3) = 3–4, g(a4) = 3–4, g(a5) = 4–5, and g(a6) = 5–5. Section 5.1 Graphs and Their Representations

4
**Graphs and Their Representations**

Directed Graphs Directed graph: the arcs of a graph begin at one node and end at another. DEFINITION: DIRECTED GRAPH A directed graph (digraph) is an ordered triple (N, A, g) where: N = a nonempty set of nodes A = a set of arcs g = a function associating with each arc a an ordered pair (x, y) of nodes where x is the initial point and y is the terminal point of a A directed graph, therefore, has a direction associated with each arc. Section 5.1 Graphs and Their Representations

5
**Directed Graphs: Example**

In the example below there are four nodes and five arcs. The function g associating arcs with endpoints performs the mapping g(a1) = (1, 2), meaning that arc a1 begins at node 1 and ends at node 2. Also, g(a3) = (1, 3), but g(a4) = (3, 1). Section 5.1 Graphs and Their Representations

6
**Graphs and Their Representations**

Other Forms of Graphs Besides imposing direction on the arcs of a graph, we may want to modify the basic definition of a graph in other ways. Labeled graph:A graph whose nodes carry identifying information, such as the names of the cities in the map of airline routes. Weighted graph: A graph where each arc has some numerical value, or weight, associated with it. For example, a graph that indicates the distances of the various routes in the airline map. The term “graph” is used to mean an undirected graph. To refer to a directed graph, one always says “directed graph.” Section 5.1 Graphs and Their Representations

7
**Applications of Graphs**

Although the idea of a graph is very simple, an amazing number of situations have relationships between items that lend themselves to graphical representation. Graphical representations of partially ordered sets: (Hasse diagrams) were introduced in Chapter 4. A PERT chart (e.g., Figure 4.7) is a directed graph. The E-R diagram (e.g., Figure 4.10) is a graph. The commutative diagram illustrating composition of functions (see Figure 4.23) is a directed graph. Chapter 7 will introduce logic networks and represent them as directed graphs. Directed graphs will also be used to describe finite-state machines in Chapter 8. As seen earlier, the airline route map was a graph. Any representation of transportation routes is a graph (road maps, computer networks, water pipelines, etc.). Section 5.1 Graphs and Their Representations

8
**Graphs and Their Representations**

Graph Terminology Two nodes in a graph are adjacent if they are the endpoints associated with an arc. 1 and 3 are adjacent nodes, but 1 and 4 are not. A loop in a graph is an arc with endpoints n–n for some node n; arc a3 is a loop with endpoints 2–2. A graph with no loops is loop-free. Two arcs with the same endpoints are parallel arcs; arcs a1 and a2 are parallel. A simple graph is one with no loops or parallel arcs. An isolated node is adjacent to no other node; 5 is an isolated node. The degree of a node is the number of arc ends at that node. Nodes 1 and 3 have degree 3, node 2 has degree 5, node 4 has degree 1, and node 5 has degree 0. Section 5.1 Graphs and Their Representations

9
**Graphs and Their Representations**

Graph Terminology Because the function g that relates arcs to endpoints in the formal definition of a graph is indeed a function, each arc has a unique pair of endpoints. If g is a one-to-one function, then there is at most one arc associated with a pair of endpoints; such graphs have no parallel arcs. A complete graph is one in which any two distinct nodes are adjacent. A subgraph of a graph consists of a set of nodes and a set of arcs that are subsets of the original node set and arc set, respectively, in which the endpoints of an arc must be the same nodes as in the original graph. Below is a complete subgraph of the graph from the previous slide. Section 5.1 Graphs and Their Representations

10
**Graphs and Their Representations**

Graph Terminology A path from node n0 to node nk is a sequence n0, a0, n1, a1, ... , nk 1, ak 1, nk of nodes and arcs where, for each i, the endpoints of arc ai are ni–ni + 1. If such a path exists, then nk is reachable n0. In Figure seen here, one path from node 2 to node 4 consists of the sequence 2, al, 1, a2, 2, a4, 3, a6, 4. The length of a path is the number of arcs it contains; if an arc is used more than once, it is counted each time it is used. The length of the path described above from node 2 to node 4 is 4. A graph is connected if there is a path from any node to any other node. A cycle in a graph is a path from some node n0 back to n0, where no arc appears more than once in the path sequence, n0 is the only node appearing more than once, and n0 occurs only at the ends. A graph with no cycles is acyclic. Section 5.1 Graphs and Their Representations

11
**Bipartite Complete Graphs**

DEFINITION: BIPARTITE COMPLETE GRAPH A graph is a bipartite complete graph if its nodes can be partitioned into two disjoint nonempty sets N1 and N2 such that two nodes x and y are adjacent if and only if x N1 and y N2. If N1 = m and N2 = n, such a graph is denoted by Km,n. The figure below is not a complete graph because it is not true that every node is adjacent to every other node. However, the nodes can be divided into two disjoint sets, {1, 2} and {3, 4, 5}, such that any two nodes chosen from the same set are not adjacent but any two nodes chosen one from each set are adjacent. Section 5.1 Graphs and Their Representations

12
**Graphs and Their Representations**

Isomorphic Graphs Two graphs may appear quite different in their visual representation but still be the same graph according to our formal definition. The graphs in the figures below are the same—they have the same nodes, the same arcs, and the same arc-to-endpoint function. Structures that are the same except for relabeling are called isomorphic structures. Section 5.1 Graphs and Their Representations

13
**Graphs and Their Representations**

Isomorphic Graphs DEFINITION: ISOMORPHIC GRAPHS Two graphs (N1, A1, g1,) and (N2, A2, g2) are isomorphic if there are bijections f1: N1 → N2 and f2: A1 → A2 such that for each arc a ε A1, g1(a) = x–y if and only if g2[ f2(a)] = f1(x)–f1(y). The bijections for the isomorphic graphs below are: Section 5.1 Graphs and Their Representations

14
**Isomorphism in Simple Graphs**

Graph isomorphism is easier to establish if we restrict our attention to simple graphs. If one can find an appropriate function f1 mapping nodes to nodes, then a function f2 mapping arcs to arcs is trivial because there is at most one arc between any pair of endpoints. THEOREM ON SIMPLE GRAPH ISOMORPHISM Two simple graphs (N1, A1, g1) and (N2, A2, g2) are isomorphic if there is a bijection f: N1 → N2 such that for any nodes ni and nj of N1, ni and nj are adjacent if and only if f (ni) and f (nj) are adjacent. (The function f is called an isomorphism from graph 1 to graph 2.) Section 5.1 Graphs and Their Representations

15
**Proving That Graphs Are Not Isomorphic**

To prove that two graphs are not isomorphic, we must prove that the necessary bijection(s) do(es) not exist. Certain conditions under which it is clear that two graphs are not isomorphic are: One graph has more nodes than the other. One graph has more arcs than the other. One graph has parallel arcs and the other does not. One graph has a loop and the other does not. One graph has a node of degree k and the other does not. One graph is connected and the other is not. One graph has a cycle and the other does not. Section 5.1 Graphs and Their Representations

16
**Graphs and Their Representations**

Planar Graphs A planar graph is one that can be represented (on a sheet of paper, that is, in the plane) so that its arcs intersect only at nodes. Designers of integrated circuits want all components in one layer of a chip to form a planar graph so that no connections cross. A simple, connected, planar graph (when drawn in its planar representation, with no arcs crossing) divides the plane into a number of regions, including totally enclosed regions and one infinite exterior region. Leonhard Euler observed a relationship between the number n of nodes, the number a of arcs, and the number r of regions in such a graph. This relationship is known as Euler’s formula: n a + r = 2. Section 5.1 Graphs and Their Representations

17
**Graphs and Their Representations**

Planar Graphs THEOREM ON THE NUMBER OF NODES AND ARCS For a simple, connected, planar graph with n nodes and a arcs: If the planar representation divides the plane into r regions, then n a + r = 2. If n ≥ 3, then a ≤ 3n 6. If n ≥ 3 and there are no cycles of length 3, then a ≤ 2n – 4. We can use this theorem to prove that certain graphs are not planar. Section 5.1 Graphs and Their Representations

18
**Graphs and Their Representations**

Homeomorphic Graphs DEFINITION: HOMEOMORPHIC GRAPHS Two graphs are homeomorphic if both can be obtained from the same graph by a sequence of elementary subdivisions in which a single arc x–y is replaced by two new arcs xv–vy connecting to a new node v. The graphs in parts (b) and (c) of the figure below are homeomorphic because each can be obtained from part (a) by a sequence of elementary subdivisions. (However, neither can be obtained from the other by a sequence of elementary subdivisions.) Section 5.1 Graphs and Their Representations

19
**Graphs and Their Representations**

Nonplanar Graphs A graph that is planar cannot be turned into a nonplanar graph by elementary subdivisions. A graph that is nonplanar cannot be turned into a planar graph by elementary subdivisions. Homeomorphic graphs are either both planar or both nonplanar. KURATOWSKI THEOREM A graph is nonplanar if and only if it contains a subgraph that is homeomorphic to K5 or K3,3. If a graph has a subgraph homeomorphic to the nonplanar graphs K5 or K3,3, then the subgraph—and hence the entire graph—is nonplanar. Section 5.1 Graphs and Their Representations

20
**Graphs and Their Representations**

Adjacency Matrices The usual computer representations of a graph involve one of two data structures, either an adjacency matrix or an adjacency list. Suppose a graph has n nodes numbered n1, n2, ... , nn. This numbering imposes an arbitrary ordering on the set of nodes; recall that a set is an unordered collection. Having ordered the nodes, we can form an n x n matrix where entry i,j is the number of arcs between nodes ni and nj. This matrix is called the adjacency matrix A of the graph with respect to this ordering. Thus, aij = p where there are p arcs between ni and nj. For example, the following graph has a corresponding adjacency matrix. Section 5.1 Graphs and Their Representations

21
**Graphs and Their Representations**

Adjacency Lists Many graphs, far from being complete graphs, have relatively few arcs. Such graphs have sparse adjacency matrices; that is, the adjacency matrices contain many zeros. Yet if the graph has n nodes, it still requires n2 data items to represent the adjacency matrix, even if many of these items are zero. Any algorithm or procedure in which every arc in the graph must be examined requires looking at all n2 items in the matrix. A graph with relatively few arcs can be represented more efficiently by storing only the nonzero entries of the adjacency matrix. An adjacency list consists of a list for each node of all the nodes adjacent to it. Pointers are used to get us from one item in the list to the next. Such an arrangement is called a linked list. There is an array of n pointers, one for each node, to get each list started. Section 5.1 Graphs and Their Representations

22
**Adjacency Lists: Example**

The adjacency list for the graph of the figure on the left contains a four-element array of pointers, one for each node. The pointer for each node points to an adjacent node, which points to another adjacent node, and so forth. In the figure, the dot indicates a null pointer, meaning that there is nothing more to be pointed to or that the end of the list has been reached. Section 5.1 Graphs and Their Representations

Similar presentations

Presentation is loading. Please wait....

OK

CS203 Lecture 15.

CS203 Lecture 15.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google