Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees."— Presentation transcript:

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

2 Section 5.1Graphs and Their Representations1 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, Chicago Nashville, Miami Dallas, and so on.

3 Section 5.1Graphs and Their Representations2 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

4 Section 5.1Graphs and Their Representations3 Formal Definition of a Graph: Example Sketch a graph having nodes {1, 2, 3, 4, 5}, arcs {a 1, a 2, a 3, a 4, a 5, a 6 }, and function g(a 1 ) = 1–2, g(a 2 ) = 1– 3, g(a 3 ) = 3–4, g(a 4 ) = 3–4, g(a 5 ) = 4–5, and g(a 6 ) = 5–5.

5 Section 5.1Graphs and Their Representations4 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.

6 Section 5.1Graphs and Their Representations5 In the example below there are four nodes and five arcs. The function g associating arcs with endpoints performs the mapping g(a 1 ) = (1, 2), meaning that arc a 1 begins at node 1 and ends at node 2. Also, g(a 3 ) = (1, 3), but g(a 4 ) = (3, 1). Directed Graphs: Example

7 Section 5.1Graphs and Their Representations6 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.

8 Section 5.1Graphs and Their Representations7 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.).

9 Section 5.1Graphs and Their Representations8 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 a 3 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 a 1 and a 2 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.

10 Section 5.1Graphs and Their Representations9 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.

11 Section 5.1Graphs and Their Representations10 Graph Terminology A path from node n 0 to node n k is a sequence n 0, a 0, n 1, a 1,..., n k 1, a k 1, n k of nodes and arcs where, for each i, the endpoints of arc a i are n i –n i + 1. If such a path exists, then n k is reachable n 0. In Figure seen here, one path from node 2 to node 4 consists of the sequence 2, a l, 1, a 2, 2, a 4, 3, a 6, 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 n 0 back to n 0, where no arc appears more than once in the path sequence, n 0 is the only node appearing more than once, and n 0 occurs only at the ends. A graph with no cycles is acyclic.

12 Section 5.1Graphs and Their Representations11 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 N 1 and N 2 such that two nodes x and y are adjacent if and only if x N 1 and y N 2. If N 1 = m and N 2 = n, such a graph is denoted by K m,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.

13 Section 5.1Graphs and Their Representations12 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 samethey 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.

14 Section 5.1Graphs and Their Representations13 Isomorphic Graphs DEFINITION: ISOMORPHIC GRAPHS Two graphs (N 1, A 1, g 1,) and (N 2, A 2, g 2 ) are isomorphic if there are bijections f 1 : N 1 N 2 and f 2 : A 1 A 2 such that for each arc a ε A 1, g 1 (a) = x– y if and only if g 2 [ f 2 (a)] = f 1 (x)–f 1 (y). The bijections for the isomorphic graphs below are:

15 Section 5.1Graphs and Their Representations14 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 f 1 mapping nodes to nodes, then a function f 2 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 (N 1, A 1, g 1 ) and (N 2, A 2, g 2 ) are isomorphic if there is a bijection f: N 1 N 2 such that for any nodes n i and n j of N 1, n i and n j are adjacent if and only if f (n i ) and f (n j ) are adjacent. (The function f is called an isomorphism from graph 1 to graph 2.)

16 Section 5.1Graphs and Their Representations15 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: 1. One graph has more nodes than the other. 2. One graph has more arcs than the other. 3. One graph has parallel arcs and the other does not. 4. One graph has a loop and the other does not. 5. One graph has a node of degree k and the other does not. 6. One graph is connected and the other is not. 7. One graph has a cycle and the other does not.

17 Section 5.1Graphs and Their Representations16 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 Eulers formula: n a + r = 2.

18 Section 5.1Graphs and Their Representations17 Planar Graphs THEOREM ON THE NUMBER OF NODES AND ARCS For a simple, connected, planar graph with n nodes and a arcs: 1. If the planar representation divides the plane into r regions, then n a + r = If n 3, then a 3n 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.

19 Section 5.1Graphs and Their Representations18 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.)

20 Section 5.1Graphs and Their Representations19 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 K 5 or K 3,3. If a graph has a subgraph homeomorphic to the nonplanar graphs K 5 or K 3,3, then the subgraphand hence the entire graphis nonplanar.

21 Section 5.1Graphs and Their Representations20 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 n 1, n 2,..., n n. 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 n i and n j. This matrix is called the adjacency matrix A of the graph with respect to this ordering. Thus, a ij = p where there are p arcs between n i and n j. For example, the following graph has a corresponding adjacency matrix.

22 Section 5.1Graphs and Their Representations21 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 n 2 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 n 2 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.

23 Section 5.1Graphs and Their Representations22 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.

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

Similar presentations

Ads by Google