1 / 81

Graphs

Graphs. Trees Minus Restrictions:. We studied binary trees . Recently we learned that as we lift off more and more of the restrictions that make a binary tree, it becomes a “ free tree .”

celina
Download Presentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs

  2. Trees Minus Restrictions: We studied binary trees. Recently we learned that as we lift off more and more of the restrictions that make a binary tree, it becomes a “free tree.” In our description of free trees two of the restrictions included the absence of cyclic paths and the requirement that all vertices be connected. If we lift these two particular restrictions, we no longer have a tree, but rather have a new structure called a “graph.” All trees are a restricted form of graphs. Although graphs include all trees, not all graphs are trees.

  3. General graphs differ from trees • need not have a root node • no implicit parent-child relationship • may be several (or no) paths from one vertex to another.

  4. Identify trees and graphs 1 2 3 4 5 6

  5. Identify trees and graphs • All are graphs • only last three are trees

  6. The formal definition of a graph A graph G consists of a set of vertices V and a set of edges E where the elements of E are pairs of vertices taken from V.

  7. edge If e=(v,w)  E then v,w  V, and the edge “e” is said to be “incident” to v and w. Furthermore, we say that v is adjacent to w.

  8. example graph G={V,E} V={v,w,x,y,z} E={e,f,g,h} where: e=(v,w) f=(w,x) g=(x,y) h=(y,z)

  9. undirected graph ordinary graph: “v is adjacent to w”  “w is adjacent to v” i.e. “undirected” graph

  10. directed graph directed graph: edges are expressed as ordered pairs where the order is significant: i.e. e=(v,w)  E  v is adjacent to w, but w is not adjacent to v unless e=(w,v) is also  E called “digraph” for short.

  11. Directed Graphs

  12. Graph definitions 1. A graph G consists of a set V , whose members are called the vertices of G, together with a set E of pairs of distinct vertices from V . 2. The pairs in E are called the edges of G. 3. If e = (v, w) is an edge with vertices v and w, then v and w are said to lie on e, and e is said to be incident (attached) with v and w. 4. If the pairs are unordered, G is called an undirected graph. 5. If the pairs are ordered, G is called a directed graph. The term directed graph is often shortened to digraph, and the unqualied term graph usually means undirected graph.

  13. Graph definitions 6. Two vertices in an undirected graph are called adjacent if there is an edge from the first to the second. 7. A path is a sequence of distinct vertices, each adjacent to the next. 8. A cycle is a path containing at least three vertices such that the last vertex on the path is adjacent to the first. 9. A graph is called connected if there is a path from any vertex to any other vertex. 10. A free tree is defined as a connected undirected graph with no cycles.

  14. Graph definitions 11. In a directed graph a path or a cycle means always moving in the direction indicated by the arrows. Such a path (cycle) is called a directed path (cycle). 12. A directed graph is called strongly connected if there is a directed path from any vertex to any other vertex. If we suppress the direction of the edges and the resulting undirected graph is connected, we call the directed graph weakly connected. 13. The valence of a vertex is the number of edges on which it lies, hence also the number of vertices adjacent to it.

  15. Use of graphs • Directed graphs (direction associated with links) are useful in modeling • communication networks • networks in which signals, electrical pulses, etc. flow from one node to another along various paths. • In networks where there may be no direction associated with the links, • model using undirected graphs, or simply graphs.

  16. Use of graphs There are physical situations in which graphs are an ideal structure to use to represent such situations. The most popular of such a situation known as the “Travelling Salesman Problem.” A problem related to physical electronic circuit design in which graphs may be used to represent the physical wiring on an electronic printed circuit board.

  17. Travelling Salesman Problem: If we know the distance via each road between each city, what is the shortest route for a salesman who has to visit all the cities?

  18. Automatic printed circuit board generation: Given a number of electronic components, how can we route the required printed circuit board traces so that they do not cross?

  19. Message transmission in a network

  20. Selected South Pacific air routes

  21. Various kinds of undirected graphs

  22. Examples of directed graphs

  23. Figure 12-1

  24. Figure 12-2

  25. Graph as an ADT In conjuction with the graph definition we need a set of operations that can be performed on elements of the abstract data type.

  26. Graph Methods

  27. Figure 12-3

  28. Figure 12-4

  29. Figure 12-5

  30. Figure 12-6

  31. Figure 12-7

  32. Computer Representation • If we are to write programs for solving problems concerning graphs, then we must first find ways to represent the mathematical structure of a graph as some kind of data structure.

  33. Computer Representation • There are several methods in common use, which differ fundamentally in the choice of abstract data type used to represent graphs, and • there are several variations depending on the implementation of the abstract data type. • In other words, we begin with one mathematical system (a graph), then • we study how it can be described in terms of other abstract data types (sets, tables, and lists can all be used, as it turns out), and • finally we choose implementations for the abstract data type that we select.

  34. The Set Representation • Graphs are defined in terms of sets, and it is natural to look first to sets to determine their representation as data. • First, we have a set of vertices, and, second, we have the edges as a set of pairs of vertices. • Rather than attempting to represent this set of pairs directly, we divide it into pieces by considering the set of edges attached to each vertex separately. • In other words, we can keep track of all the edges in the graph by keeping, for all vertices v in the graph, the set Ev of edges containing v, or, equivalently, the set Av of all vertices adjacent to v. • In fact, we can use this idea to produce a new, equivalent definition of a graph:

  35. new, equivalent definition of a directed graph: A digraph G consists of a set V , called the vertices of G, and, for all v V ,a subset Av of V, called the set of vertices adjacent to v.

  36. new, equivalent definition of a graph: • From the subsets Avwe can reconstruct the edges as ordered pairs by the following rule: The pair (v, w) is an edge if and only if wAv . • It is easier, however, to work with sets of vertices than with pairs.

  37. new, equivalent definition of a undirected graph: • This new definition, moreover, works for both directed and undirected graphs. • The graph is undirected means that it satisfies the following symmetry property: w Av implies v Aw for all v, w V . • This property can be restated in less formal terms: It means that an undirected edge between v and w can be regarded as made up of two directed edges, one from v to w and the other from w to v.

  38. Removal from a Binary Search Tree A undirected graph G consists of a set V , called the vertices of G, and, for all v, w V , a subset Av of V, called the set of vertices adjacent to v and a subset Aw of V, called the set of vertices adjacent to w.

  39. “set implementation” of a graph The advantage of a set implementation is that it follows naturally from the definitions of a graph which are set based. The disadvantage to such implementations is that not all computer languages support sets.

  40. Set Implementation: (in pascal) Label all vertices 1 to MAX type vertex = 1 .. MAX adjacency_set = set of vertex; graph = record n:0 .. max; A: array[vertex] of adjacency_set end; ... var G:graph; … G.n := 3; G.A[1] := (); G.A[2] := (); G.A[3] := (1, 2); Resulting Graph:

  41. Example graph with adjacency sets G.n:=5; G.A[1]:=(3); G.A[2]:=(4,5); G.A[3]:=(1,4,5); G.A[4]:=(2,3,5); G.A[5]:=(2,3,4);

  42. Example graph with adjacency sets G.n:=5; G.A[1]:=(3); G.A[2]:=(4,5); G.A[3]:=(1,4,5); G.A[4]:=(2,3,5); G.A[5]:=(2,3,4);

  43. implement sets of vertices in C++ There are two general ways for us to implement sets of vertices in C++. • One way is to represent the set as a list of its elements. • The other implementation, often called a bit string, keeps a Boolean value for each potential element of the set to indicate whether or not it is in the set. • For simplicity, we shall consider that the potential elements of a set are indexed with the integers from 0 to max_set - 1, where max_set denotes the maximum number of elements that we shall allow. • This latter strategy is easily implemented either with the standard template library class std :: bitset<max_set> • or with our own class template that uses a template parameter to give the maximal number of potential members of a set.

  44. Set as a bit string: template <int max set> struct Set { bool is element[max set]; }; • sets as arrays the structure Set is essentially implemented as an array of bool entries. Each entry indicates whether or not the corresponding vertex is a member of the set.

  45. Digraph as a bit-string set: template <int max size> class Digraph { int count; //number of vertices, at mostmax size Set<max size> neighbors[max size]; }; • In this implementation, the vertices are identified with the integers from 0 to count - 1. • If v is such an integer, the array entry neighbors[v] is the set of all vertices adjacent to the vertex v.

  46. Adjacency Tables The array neighbors in the definition of class Graph is basically a two-dimensional boolean array and can be changed to an array of arrays, that is, to a two-dimensional array, as follows: Digraph as an adjacency table: template <int max size> class Digraph { int count; //number of vertices, at mostmax size bool adjacency[max size][max size]; };

  47. Digraph as an adjacency table: The adjacency table has a natural interpretation: • adjacency[v][w] is true if and only if vertex v is adjacent to vertex w. • If the graph is directed, we interpret adjacency[v][w] as indicating whether or not the edge from v to w is in the graph. • If the graph is undirected, then the adjacency table must be symmetric; that is, adjacency[v][w] = adjacency[w][v] for all v and w. • The representation of a graph by adjacency sets and by an adjacency table is illustrated in Figure 12.4.

  48. Adjacency set and an adjacency table

  49. Adjacency Lists • Another way to represent a set is as a list of its elements. • For representing a graph, we shall then have both a list of vertices and, for each vertex, a list of adjacent vertices. • We can consider implementations of graphs that use either contiguous lists or simply linked lists. • For more advanced applications, however, it is often useful to employ more sophisticated implementations of lists as binary or multiway search trees or as heaps. • Note that, by identifying vertices with their indices in the previous representations, we have ipso facto (by the fact itself)implemented the vertex set as a contiguous list, but now we should make a deliberate choice concerning the use of contiguous or linked lists.

  50. list-based implementations • We obtain list-based implementations by replacing our earlier sets of neighbors by lists. • This implementation can use either contiguous or linked lists. • The contiguous version is illustrated in part (b) of Figure 12.5, and • the linked version is illustrated in part (c) of Figure 12.5.

More Related