Graphs, Connectivity, and Traversals
Total Page:16
File Type:pdf, Size:1020Kb
Graphs, Connectivity, and Traversals Definitions Like trees, graphs represent a fundamental data structure used in computer science. We often hear about cyber space as being a new frontier for mankind, and if we look at the structure of cyberspace, we see that it is structured as a graph; in other words, it consists of places (nodes), and connections between those places. Some applications of graphs include • representing electronic circuits • modeling object interactions (e.g. used in the Unified Modeling Language) • showing ordering relationships between computer programs • modeling networks and network traffic • the fact that trees are a special case of graphs, in that they are acyclic and connected graphs, and that trees are used in many fundamental data structures 1 An undirected graph G = (V; E) is a pair of sets V , E, where • V is a set of vertices, also called nodes. • E is a set of unordered pairs of vertices called edges, and are of the form (u; v), such that u; v 2 V . • if e = (u; v) is an edge, then we say that u is adjacent to v, and that e is incident with u and v. • We assume jV j = n is finite, where n is called the order of G. •j Ej = m is called the size of G. • A path P of length k in a graph is a sequence of vertices v0; v1; : : : ; vk, such that (vi; vi+1) 2 E for every 0 ≤ i ≤ k − 1. { a path is called simple iff the vertices v0; v1; : : : ; vk are all distinct. { a path is called a cycle iff its length is at least 3, and the start and end vertices are the same: i.e. v0 = vk. • a geometrical representation of a graph is obtained by treating the vertices as points on a plane, and edges e = (u; v) as smooth arcs which terminate at both u and v. • the degree of a vertex v, denoted as deg(v), equals the number of edges that are incident with v. • Handshaking Theorem. P deg(v) = 2jEj. v2V 2 Example 1. Let G = (V; E), where V = fSD; SB; SF; LA; SJ; OAKg are cities in California, and E = f(SD; LA); (SD; SF ); (LA; SB); (LA; SF ); (LA; SJ); (LA; OAK); (SB; SJ)g are edges which represent flights between two cities. Provide the following for the graph: • the order and size • a geometrical representation • a path of length 8 • the longest simple path • the largest cycle • the degrees of each vertex 3 A directed graph is a graph G = (V; E) whose edges have direction. In this case, given (u; v) 2 E, u is called the start vertex and v is called the end vertex. Moreover, the in-degree of vertex v, denoted deg+(v), is the number of edges for which v is the end vertex. And the out-degree of vertex v, denoted deg−(v), is the number of edges for which v is the start vertex. Similar to the handshaking theorem X X deg+(v) = deg−(v) = jEj: v2V v2V An undirected graph can be made into a directed graph by orienting each edge of the graph; that is, by assigning a direction to each edge. Conversely, each directed graph has associated with it an underlying undirected graph which is obtained by removing the orientation of each edge. Example 2. Redraw the graph from Example 1, but now with oriented edges. 4 Graph Connectivity and Traversals Recall that a path in a graph G = (V; E) (either directed or undirected) from vertex u to vertex v is a sequence of vertices u = v0; v1; : : : ; vn = v for which (vi; vi+1) 2 E for all i = 0; 1; : : : ; n − 1. We then say that G is connected provided there is a path from every vertex u 2 V to every other vertex v 2 V . In what follows we present algorithms that pertain to the degree of connectivity of a graph. To begin, we consider two different ways of traversing a graph; i.e. visiting each node of the graph. The first algorithm makes use of a FIFO queue data structure, and is called a breadth-first traversal, while the second uses a stack data structure, and is called a depth-first traversal. Breadth-First Graph Traversal Algorithm. Let G = (V; E) be a graph (either directed or undirected). Initialize FIFO queue Q as being empty. Initialize each vertex of G as being unmarked. While there exist unmarked vertices: Let u be one such unmarked vertex. Mark u and place it in Q. While Q is nonempty: Remove node u from front of Q. For every v that is a neighbor/child of u: If v is unmarked, then mark v and place it in Q. In addition to visiting each node, this procedure implicitly yields a spanning forest of trees (or a spanning tree if the forest has only one tree) whose edges are comprised of those of the form (u; v) where u was the node that was removed from Q and led to v being marked. For undirected graphs, a breadth-first traversal partitions the edges of G into those that are used in the forest, and those that are not used. The latter edges are called cross edges, since they always connect nodes that are on different branches of one of the (spanning) trees of the spanning forest. 5 Example 3. For the graph G = (V; E), where V = fa; b; c; d; e; f; g; h; i; j; kg and the edges are given by E = f(a; b); (a; c); (b; c); (b; d); (b; e); (b; g); (c; g); (c; f); (d; f); (f; g); (f; h); (g; h); (i; j); (i; k); (j; k)g: Show the forest that results when performing a breadth-first traversal of the G. Assume that all adjacency lists follow an alphabetical ordering. 6 Depth-First Graph Traversal Algorithm. Let G = (V; E) be a graph (either directed or undirected). Initialize stack S as being empty. Initialize each vertex of G as being unmarked. While there exist unmarked vertices: Let u be one such unmarked vertex. Mark u and push it on to S. While S is nonempty: Let u be at the front of S. Let v be the first unmarked neighbor/child of u. If v does not exist: Pop u from S. Otherwise: Mark v and push v on to S. In addition to visiting each node, this procedure also implicitly yields a spanning forest of trees (or a spanning tree if the forest has only one tree) whose edges are comprised of those of the form (u; v) where u was the node from the front of S that reached v and caused it to be marked. For undirected graphs, a depth-first traversal partitions the edges of G into those that are used in the forest, and those that are not used. The latter edges are called backward edges, since they always connect nodes that are on the same tree branch (i.e., the edge connects a descendant to an ancestor). 7 Example 4. For the graph G = (V; E), where V = fa; b; c; d; e; f; g; h; i; j; kg and the edges are given by E = f(a; b); (a; c); (b; c); (b; d); (b; e); (b; g); (c; g); (c; f); (d; f); (f; g); (f; h); (g; h); (i; j); (i; k); (j; k)g: Show the forest that results when performing a breadth-first traversal of the G. Assume that all adjacency lists follow an alphabetical ordering. 8 Theorem 1. Undirected graph G = (V; E) is connected iff a breadth-first traversal of G yields a forest with exactly one tree. Proof of Theorem 1. Suppose a breadth-first traversal of G yields a forest with exactly one tree T . Then G is connected since T is connected (by definition a tree is connected and acyclic), and is a spanning tree for G. Now suppose G is connected. Suppose u is the root of the first tree. Let v be any other vertex of G. Then there is a path P : u = v0; v1; : : : ; vn−1 = v from u to v. Notice that, in the first iteration of the outer while loop, v1 has the opportunity to be marked when v0 is removed from the queue. And inductively, assuming that vi−1 will be entered in the queue (during the first outer-loop iteration), vi will then have the opportunity to be marked. Therefore, by induction, v will be marked in the first iteration of the outer while loop. Since v was arbitrary, it follows that all vertices of G belong to the first and only tree (each iteration of the outer loop corresponds with a new tree). Corollary 1. The size of the forest generated in a breadth-first traversal of a undirected graph equals the number of connected components of that graph. Moreover, each tree in the forest represents a spanning tree for one of the connected components; in other words, a tree that contains each vertex of a component. A Connectivity Algorithm for Directed Graphs For directed graphs, first note that there are two kinds of connectivity to consider. The first is called strong connectivity, and requires that all paths follow the orientation of each directed edge. The other kind is called weak connectivity and allows for paths that ignore edge orientation. Of course, weak connectivity can be tested using the breadth-first traversal algorithm described above. On the other hand, the (strong) connectivity of a directed graph G = (V; E) can be determined by making two depth-first traversals of the graph.