12/05/2002

CPS130, Lecture 25-26; and Algorithms Continued

1. Introduction

We introduce directed graphs and their use as orientations of (undirected) graphs. We show that a bridgeless graph had a strongly connected orientation that can be found by depth first search (DFS). DFS is a powerful strategy, and its further facilitates the design of algorithms to solve connectivity problems in both undirected and directed graphs. We discuss some matrices associated with graphs. We use the vertex-edge incidence matrix as an example to derive the linear equations arising from the resistive electrical network problem.

2. Digraphs

A directed graph or digraph is a pair D = (X,E) with a (nonempty) set X of vertices and a set of directed edges, E. Each edge in E is of the form e = (x,y) with x ≠ y; x is called the initial vertex, y is the terminal vertex and the edge is directed from x to y. Sometimes we still write e = xy, but xy ≠ yx. Notice that we have excluded loops, which would be edges of the form xx; should we want to allow this type of edge we will call D a directed graph with loops.

An x-y path of length k is a distinct set of vertices p = [x = x0, x1, x2, …, xk = y]

with xxii+1∈≤E,0 i≤k−1, except possibly for x = y and k ≥ 2 and then p is a (graphs with loops would also allow xx to be a cycle). A graph is strongly connected if for every distinct pair x, y in X, there is an x-y path.

Example 1.

4 1 2 5

7 3 6

This digraph is not strongly connected since there is no 6-x or 7-x path for any x. In fact, the graph is acyclic; there are no directed cycles. If we were to redirect edges 13 to 31 and 36 to 63, we would have a graph with three strongly connected components. A

1 strongly connected (or strong component) is a maximal strongly connected vertex induced subgraph. This is a subgraph, D(Xs) = (Xs, Es) where Xs ⊆ X and Es = {e = xy∈E | x∈Xs, y∈Xs} (vertex induced), that is strongly connected but not contained in any larger vertex induced strongly connected subgraph D(Xt) with Xs ⊂ Xt (maximal). A new directed graph, DC = (C, EC), can be made from D once we have identified the connected components, say, Ci. We let each such Ci be a vertex in C; then there is a single edge, CiCj ∈ EC if there is an edge in E directed from a vertex in Ci to a vertex in Cj. DC is called the condensation of D.

HW 10.1. Show that DC is acyclic by using the maximal property.

Example 2. Example1 digraph with redirected edge and 4 strong components. EC = {C1C2, C1C3, C2C3}(big arrows).

C2 4 1 2 5

C3 7 3 6

C1

Given a digraph, D = (X,E) we can undirect the edges to produce the graph G(D) = (X,F) such that xy∈F iff xy∈E. Similarly, given a graph, G = (X,E) we can direct the edges to produce the graph D(G) = (X,F) such that xy or yx∈F iff xy∈E. Hence D(G) is not unique while G(D) is unique. Sometimes D(G) is called an orientation of G. The notions of semicycle and semiwalk in D can be defined as a corresponding cycle or walk in G(D). We can also say that D is weakly connected if G(D) is connected.

Example 3: one way street problem

Suppose a graph G is connected and represents the streets (edges) and intersections (vertices) in (part of) a city and that city transportation planners are convinced that the traffic flow will be more efficient if they orient G to D(G) so that each directed edge represents a one way street. We would clearly want D(G) to be strongly connected, and it is clear that G must be connected. Also, there can be no bridges in G, which are edges whose removal leaves G-e disconnected; otherwise, we will be about to go from one component to the other but not back. Interestingly, these two conditions is the total solution as seen in the following theorem. First another

HW 10.2. Show, as in quiz2, problem1, part3, that removing any bridge in a connected graph leaves a graph with two connected components.

2

Theorem 1. A graph has a strongly connected orientation, D(G), iff G is connected and has no bridges.

proof;

Connectivity and no bridges are immediately necessary for, if either condition fails, there is no way we can orient G to make D(G) strongly connected.

To show sufficiency, we first note than any edge in a bridgeless connected graph must be on some cycle. For if we remove any edge, e = xy, not on a cycle, there no longer any xy path so G is disconnected and e would be a bridge.

Now let G(X1), X1 ⊆ X, be a maximal induced subgraph such that there exists a strongly connected orientation, D(G(X1)). We show X1 = X.

path

xj x 1 x e 2

X1 X2

Figure 1: picture idea of proof

Suppose there is some X2 = X – X1 ≠ ∅ . Since G is connected, there is an edge e = x1x2 with x1∈ X1 and x2 ∈ X2. Also, since e is not a bridge, there is a cycle, c = [x1, x2, …, x1]. If xj is the first vertex on the cycle in X1 (possibly x1), we orient the edges xixi+1 from xi to xi+1, 1i≤≤j−1. We form new digraph with these additionally j1−−j1 vertices and newly oriented edges, which is D1 = Xx1i∪∪,E1xixi+1, where E1 are i1==i1 the oriented edges of D(G(X1)). By construction D1 remains strongly connected. If we now orient any remaining edges in the induced subgraph G(X1 ∪ xi) arbitrarily, we will have produced a larger strongly connected orientation, so G(X1) could not be maximal. Thus X1 = X.

3

3. Graphs and Matrices

Recall that, in L23-24, we defined the adjacency matrix of a graph, G = (X,E), with X ordered as X = {1, 2, 3, …, | X | = n}, as the n x n matrix

(GM1) A(i,j) = 1 if ij ∈E; A(i,j) = 0, otherwise.

We can use the identical definition when D is a digraph. In the graph case, A is symmetric, A = AT, but not in the digraph case.

Example 4. Let D be the directed graph of example1 and G be the graph obtained by undirecting the edges of D. We see that

0111000 0111000   100 0110 000 0110 100 0011 000 0011   AG = 1000001 and AD = 0000001 0100010 0000010   0110100 0000000   0011000 0000000 .

Notice that AD is strictly upper triangular, A(i,j) = 0 for i ≤ j. There are two reasons for this. First D is acyclic so there always is such an ordering that makes AD strictly upper triangular. The fact that this particular order works is due to the use of BFS in example2 of L23-24 to get D as an orientation of an undirected graph. The BSF ordering makes the matrix strictly upper triangular. If we then direct the nontree edges from lower number to higher number, we maintain the triangular property.

HW10.3. Verify the statements made about the use of BSF to give an acyclic orientation of a connected graph G and, hence, an AD that is strictly upper triangular.

For an vertex x of a directed graph, let indeg(x) = | {y | yx ∈ E} | and outdeg(x) = | {y | xy ∈ E} | .

4

The following proposition is also relevant.

Proposition 1. Let D be an acyclic digraph. Then there are vertices x and y such that indeg(x) = outdeg(y) = 0. proof; Let x and y be the end vertices of the longest (directed) path in D. Then indeg(x) = outdeg(y) = 0; otherwise there is either a longer path or a cycle. endproof;

To find the strictly upper triangular ordering we first order a vertex .x with indeg(x) = 0, and then remove x and its incident edges. Then we find a new vertex with indeg = 0 and order it and continue the process. Such an ordering of an acyclic digraph is often called a topological ordering.

We now present a little result about the matrix powers of either AG or AD. If we write An we will use ordinary integer arithmetic; bool(An) means the arithmetic is Boolean (1 + 1 = 1; 1 + 0 = 1; 1·1 = 1, 1·0 = 0).

Proposition 2. Let A be the adjacency matrix of G or D.

(1) bool(An(i,j)) = 1 iff there is a path of length l ≤ n from i to j; bool(An(i,j)) = 0, otherwise. (2) An(i,j) is the number of walk of length n from i to j.

HW 10.4. Prove both parts of proposition2 by induction on n.

If the edges of D or G have weights ≥ 0, we can use the adjacency matrix for an algorithms to find all shortest paths between vertices. See Large, L21. We will use another matrix, the vertex-edge incidence matrix, in the next section.

4. The Resistive Electrical Network Problem

The resistive electrical network problem is defined on a connected graph, G, with orientation, D. Suppose there are (n + nb) vertices and m edges. Associated with each edge, e will be a conductivity, ce > 0 (or resistance, r = 1/c), an edge voltage, Ve, and a current, Ie. The edge voltage for an oriented edge e = xy will be Ve = ux - uy, and this is why we need to orient G; ux and uy are node (vertex voltages). We will see that the orientation is arbitrary (we are essentially establishing a “sign” convention). We will partition the nodes as X = [U, B]. The nodes in U are called unknown (or internal) since we must solve for the corresponding node voltages, while the nodes in B have

5 known boundary values. There must be a boundary edge, e = ub, u∈U, b∈B for each boundary node. Often (in EE), B contains only one node called “ground” and other set voltages are handled by voltage source elements, but our formulation has some advantages. We allow each node in U to have specified current influx (or current source).

There are two physical laws that specify the problem as a set of equations to be solved. We will specify V, I, and c to be vectors of length m, u will be the (unknown) vector of length n, and b will be a vector of length m, but will be nonzero only for the boundary edges. We first have Ohm’s Law:

(OL) Ik = ck Vk = ck(ui – uj) for ordered directed edge ij and ordered nodes i and j.

We also enforce Kirchoff’s current conservation law:

(KL) ∑ Iki= j= si for all vertices i∈U where j∈adj(i). j

In order to simplify these laws we need a new matrix, the vertex-edge incidence matrix. Let G = (X, E) or D = (X,E). Then the vertex-edge incidence matrix, A0, is an | X | x | E | matrix defined, for undirected G, by

(MG) A0(i,k) = A0(j,k) = 1 for edge k = ij, and A0(p,k) = 0 for p ≠ i or j.

For directed D, the matrix is defined by

(MD) A0(i,k) = 1, A0(j,k) = -1 for directed edge k = ij, and A0(p,k) = 0 for p ≠ i or j.

We will use the directed case in this discussion. Note that each column has a 1 and a –1, so the rows add to zero and, hence, are not independent in the matrix (linear) algebra sense. This reflects the physical necessity to specify at least one boundary vertex. We let A be the n x m submatrix of the (n + nb) x m matrix of the directed graph, D, defining the resistive network (in the first paragraph of this section) obtained by deleting the rows corresponding to boundary nodes. This means, that by the e = ub convention for the directed boundary edges, these columns of A have a single 1 in the row j = u. A is called the reduced incidence matrix.

Example 5. 1 8 2 4 1 2 3 4 9 10 3

5 6 7 b=0 5 6 7

Let’s form the reduced incidence matrix for this digraph. We see (leaving out the zeros)

6

111  11− 1 −11  A = −11 −11  −−11 1  −−111−1.

It is no accident the first 7 x 7 submatrix is a lower triangular matrix with 1s on the diagonal. It could also be upper triangular. That is, there is always both an ordering of the vertices and edges and an orientation such that the initial n x n submatrix , corresponding to the internal nodes, is triangular with 1’s on the diagonal by ordering the edges in a spanning containing the internal nodes consistent.

HW 10.5. Verify the assertion in the preceding sentence.

This means that the rows of A are independent, so our ultimate matrix equation will have a unique solution.

Now, referring to example5 for specification, let’s reformulate (OL) and (KL) using A and the m x m diagonal matrix C = diag(c) ( C(j,j) = c(j); zero, otherwise). The matrix form of Ohm’s Law becomes

(OLM) I = C V = C(AT u - b).

This follows since the multiplication of u by AT takes the appropriate difference in the u vector, and we need to fix things up for boundary nodes in the vector b. The matrix form is Kirchoff’s law is equally simple:

(KLM) A I = s, since multiplication by A just sums the node currents in the appropriate way. Combining (OLM) and (KLM) gives an n x n matrix (equation)

(RN1) ACAT u = s + AC b.

The AC b term on the right gives the equivalent currents induced by our boundary values for the internal nodes connected to the boundary.

7

The matrix

(RN2) M = ACAT is the nodal admittance matrix. Since the rows of A are independent and all ci > 0, M is symmetric (obvious), positive definite and, hence, nonsingular. This means that there is a unique solution u.

HW 10.6. Show that the linear system for example5, assuming all ci =1 and all si = 1, is:

31−−−1 1 1  −−13 1 −1 1 −−12 1 1  −−121u = 1. −−12 1 1  −−113−11  −−11 −14 1

One could use the sparse matrix features of Matlab to assemble (build) the matrix M from A. However, we must then decide how to enter A as data as well as the quantities in C and b. The code resistorsim in the appendix builds M resistor by resistor and also assembles the total equivalent current vector. Enjoy!

5. Depth First Search Orientation on a Graph

We now consider the depth first search of a connected graph. We will put the tree edges in T and orient them. We will also orient the nontree edges and put them in B. If G is connected and bridgeless, then DFS will produce a strongly connected orientation; this is an algorithmic proof of Theorem 1.

Pick any vertex x and set order = 0. The following algorithm produces the DFS from vertex x.

8 DFS(x, order) mark(x); order(x) = order + 1; for y ∈ adj(x) if y unmarked and order(y) ≠ order(x) -1 push y else add oriented yx to B endif; endfor; while stack is nonempty pop = p from stack; add oriented xp to T; DFS(p, order); endwhile;

Example 6. DSF(1, 0)

2 1 3 4

7 5 6 oriented tree edges

8 9 10

1 1 vertices and order number 2 5 oriented tree edges 6 3 and back edges

10 4 7 9 4 5 8 8 3 6 7 9 10 2

9

An important property of the DFS construction is the following: if xy is a backedge y is an ancestor of x; that is, there is a directed x-y path in T. If G is connected, all vertices will be visited and all edges will be tree edges in T or backedges in B. Consider any leaf, t, in the DFS tree. Let y be the vertex in adj(x) on the directed x-t path, say, p. If min (order(v) | v∈ p-x) > 1, e = xy in G must be a bridge since there is no v-x path in G-e for any v∈ p-x. Therefore, if G is bridgeless, there is an v-x path for any vertex, and the orientation is strongly connected.

10

Appendix: Matlab Code for resistorsim

(save as resistorsim.m in a matlab path to run) function [nodev,currents,M] = resistorsim(rcirc, bdary, source, printsys) % % Solves resistive electrical network equations to give a vector % node voltages (nodev) and an array of edge currents. There are % m directed edges (i,j), nb boundary edges, and n nonboundary (internal) % nodes. The undirected graph of the network must be connected with at % least one edge from a nonboundary node to a boundary node. rcirc is % an mx3 array with each row of the form (i,j, conductivity) for % nodes i,j and conductivity of the edge (i,j) resistor; or, when n=1, % rcirc=[]. bdary is a nb x 3 array of internal nodes with % their boundary edge to a boundary node with fixed node value; % each row has the form (i, bvalue, conductivity). source is an % ns x 2 array of current sources with each row of the form % (i, sourcevalue) unless source = []. The sign convention is: % (i,j) current from i (positive) into j (negative); (i, boundarynode) % from i (positive) to boundarynode (negative); source currents go % into internal nodes (negative). If printsys is the string 'yes', % resistorsim prints the linear system. % % Get dimensions. sm = size(rcirc); m = sm(1); if m ~= 0 n = max(max(rcirc(:,1)), max(rcirc(:,2))); else n=1; end snb = size(bdary(:,1)); nb = snb(1); szs = size(source); ns = szs(1); % % Initialize matrix M and right hand side b to zero. % % M = zeros(n,n); M = sparse(n,n); b = zeros(n,1); % % Assemble internal node matrix elements into M. % for k = [1: m] i = rcirc(k,1); j = rcirc(k,2); cvalue = rcirc(k,3); M(i,i) = M(i,i) + cvalue; M(j,j) = M(j,j) + cvalue; M(i,j) = M(i,j) - cvalue; M(j,i) = M(j,i) - cvalue; end

11 % % Assemble boundary node (diagonal) matrix elements into M. % for k = [1: nb] i = bdary(k,1); cvalue = bdary(k,3); M(i,i) = M(i,i) + cvalue; end % % Assemble source into b. % for k = [1: ns] i = source(k,1); svalue = source(k,2); b(i) = b(i) + svalue; end % % Assemble boundary value * boundary conductivity into b. % for k = [1: nb] i = bdary(k,1); b(i) = b(i) + bdary(k,2) * bdary(k,3); end % % Print matrix M and rhs b if printsys is 'yes'. % if printsys == 'yes' disp(' System Matrix M'); M disp(' System right hand side b'); b end % % Compute node voltages. % nodev = M\b; % % Create and compute (m + nb) x 3 array , currents, whose rows % have the form (i, j, ijcurrent) or the form (i, bvalue, bcurrent). % currents = []; for k = [1:m] i = rcirc(k,1); j = rcirc(k,2); ijcurrent = rcirc(k,3) * (nodev(i) - nodev(j)); currents(k,:) = [i , j, ijcurrent]; end for k = [1: nb]; i = bdary(k,1); bval = bdary(k,2); ibcurrent = bdary(k,3) * (nodev(i)-bval); currents = [currents ; i, bval, ibcurrent]; end % v 1/16/01

12

Quiz Appendix

Possible Grade 110 108 105 A+ 100 100 ______96 94 A 92 90 90 90 90 ______88 87 97 87 87 A- 86 ______85 median = 85 A- - or B+ + 84 84 ______average = 83.18 B+ 83 81 ______75 73 69 68 68 66 64 B to C+ 63 62 59 56 55

13