540 likes | 750 Views
Graph Theory. Love Ekenberg. Graphs. A graph is a structure (V,E), where V is a finite set called the vertex set and E the set of edges. E consists of 2-tuples of elements in V. Example: V = {a,b,c,d,e}, E = {(a,b),(b,c),(d,e)}.
E N D
Graph Theory Love Ekenberg © Love Ekenberg
Graphs • A graph is a structure (V,E), where V is a finite set called the vertex set and E the set of edges. E consists of 2-tuples of elements in V. • Example: V = {a,b,c,d,e}, E = {(a,b),(b,c),(d,e)}. • Graphs can be complete, sparse, dense, or non of these. Sparse matrices contain few edges and dense ones contain many. • Different algorithms are suitable for processing different types of graphs. a d b c e © Love Ekenberg
Observation • 0 E 1/2V (V-1) • Think this through! © Love Ekenberg
Exercise G1 • Professor M and his wife A give a party at which there are four other married couples. Some couples shake hands when they meet, but naturally no couple shake hands with each other. At the end of the party the Professor asks everyone how many people they have shaken hands with, and he receives nine different answers. How many people shook hands with his wife? © Love Ekenberg
Solution G1 • Construct a graph in which vertices are the people at the party and there is an edge {x,y} iff x and y shook hands. The maximum number of handshakes in which any one person can be involved is eight. Therefore the answers are 0,1,2,3,4,5,6,7,8 since the answers are different. • Vertex 8 is joined (via edges) to all except one, which is his wife who answered 0. Thus 8 is joined to 1,2,3,4,5,6,7,M. • Note that 1 is joined to 8 and this is the only pair containing 1. 7 is thereby neither joined to 0 nor 1. This means that 7 is married to 1 since 0 is married to 8. Continuing in this way, we find that 6 is married to 2, and that 5 and 3 are married. Therefore M and 4 are married. © Love Ekenberg
Tables • Graphs can be represented using tables. The graph below has five vertices {a,b,c,d,e}. For example, vertex a is connected to vertex b and d. Now draw this graph! • a b c d e • b a d a b • d e c d • e • The complexity for representing this is O(V + E) © Love Ekenberg
Graphs can also be represented by a table where 1 means that there is an edge between the vertices: a b c d e a 1 1 0 1 0 b 1 1 0 0 1 c 0 0 1 1 0 d 1 0 1 1 1 e 0 1 0 1 1 The complexity of this representation is O(V^2) Note that the matrix is symetrical and less than half of the tuples need to be saved. The diagonal elements can be set to 0 or 1, depending on the situation. Tables © Love Ekenberg
Isomorphism • Two graphs G1 and G2 are isomorphic iff there is a bijection (a 1-1 relationship) B from the vertex set of G1 to the the vertex set of G2, such that {B(x),B(y)} is an edge in G2 iff {x,y} is an edge in G1. B is called and isomorphism. • Observations: • 1. If the vertex sets differ in size, then no isomorphism exists. • 2. If the edge sets differ in size, then no isomorphism exists. • Think about it! © Love Ekenberg
Valency The valency of vertex v in graph G = (V,E) is the number of edges from v. The valency of vertex v is often written d(v). A vertex is odd if the valency is odd. Similarly for even vertices. © Love Ekenberg
Observations • Three obsevartions about valency (which will not be proved here, but which are fairly easy to see): • 1. The sum of valencies of all vertices is double the number of edges, i.e: • 2. Denote vertices with even valency Ve and vertices with odd valency Vo yields: • therefore the number of odd vertices is even. Think this through! • 3. If B: V1 -> V2 is an isomorphism between G1 and G2; and B(v) = w, then each vertex that contains v is mapped to an edge that contains w. It therefore holds that d(v) = d(w). So if G1 has a vertex x with valency p and G2 has no vertex with valency p, then they cannot be isomorphic. © Love Ekenberg
Exercise G2 • Think through the following: • If G = (V,E) is a graph then the complement -G to G is the graph the vertex set of which is V and the edges of which connect those pairs of vertices that are not connected in G. If G has n vertices with valency d1,d2,…,dn, what is the valency of the the vertices in -G? © Love Ekenberg
Paths and Cycles A walk through a graph G = (V,E) is a sequence of vertices in V, v1,v2,…,vk, such that vi and vi+1 are adjacent, i.e, (vi,vi+1) is in E. If all vertices are different then then the walk is called a path through the graph. A path v1,v2,…,vr+1, where all vertices are different except that v1 = vr+1 is called a cycle with length r. Now draw this! Observation: Let G be a graph and let x y denote that x and y are connected via a path. This implies that is reflexive, symmetric och transitive, i.e, is an equivalence relation. An equivalence relation partitions the vertex set of the graph. © Love Ekenberg
Example • In the graph below {a,b,c,d} and {d,e,f,g} constitute partitions of the vertex set {a,b,c,d,e,f,g} • This can be important for dynamic analysis of state diagrams. The starting state determines which part of the state space is of interest for analysis. f c e g b d d a © Love Ekenberg
Cyclic Graphs • A graph is cyclic iff it can be written as a cycle. Cyclic grafs are usually written Cn, where n is the vertex set in the graph. • Observation • A cyclic graph (n >2) is always regular, i.e, the vertices have the same valency. (In this case 2.) © Love Ekenberg
Exercise G3 • Two tourists Herr A and Fru B are visiting Gotland. The intresting sites and the roads that connect them can be represented by the graph: • p q r s t u • q p p p p r • r r q q q s • s s s r r t • t t t t s • u u u • Herr A wants to visit all the sites and return to his starting point. Fru B wants to travel all roads exactly once. She would consider starting and finishing in different places. Can both their wishes be satisfied? © Love Ekenberg
Solution G3 • Herr A has many alternatives. One is p,q,t,s,u,r,p. Cycles that contain all vertices in a graph are called Hamilton cycles. • Fru B is less fortuitous. Set the starting vertex to x and the finishing vertex to y. Assume that x y. She then uses an edge from x when she starts and new edges when she returns are starts from x. So she must use an odd number of edges from x. The valency of x must be odd. Likewise the valency for y is odd. All other vertices must have even valency. In the given graph however, the valency is 4,4,5,5,5,3. So there are too many odd vertices. Assuming x = y, then all vertices must be even. © Love Ekenberg
Components in Graphs Let G = (V,E) be a graph and let the partition of V induced by be V = V1 V2 … Vr. Let Ej be the subset of E which contains edges which are both in Vi. The graphs Gi = (Vi,Ei) are then called components of G. If G only contains one component then it is connected. © Love Ekenberg
Let G = (V,E) be a connected graph and let G' = (V', E') where V'=V\v and E' = E\{(x,y) : x = v eller y=v)}. v is called an articulation point if G' is not connected. A graph without articulation points is called biconnected. Determining articulation points can easily be carried out with a search algorithm. Articulation Points and Biconnectivity © Love Ekenberg
A graph T = (V,E) is a tree iff: 1) T is connected 2) there are no cycles in T Observation If a new edge is added to a tree, a cycle is formed (since a tree is connected). Trees © Love Ekenberg
Observations about the Properties of Trees If T = (V,E) is a tree with a least two vetices, then following holds: 1. For each vertex x,y there is a unique path from x to y in T 2. The graph obtained by removing an edge from a tree has two components, both of which are trees. 3. E = V - 1 © Love Ekenberg
Proof If T = (V,E) is a tree with at least two vertices, then there is an unique path from x to y for all vertices x,y in T Proof: Since T is connected, there is a path from x to y: x = v0,v1,…,vr = y. Assume that another path exists x = u0,u1,…,ur = y. Now let i be the lowest index such that ui+1 vi+1. Since both paths finish with y, they will meet again and we define j to be the lowest index such that j>i and vj = uk for some k. But then vi,vi+1,…,vj,uk-1,…ui+1 is a cycle in T, which contradicts the assumptions. © Love Ekenberg
Proof If T = (V,E) is a tree with at least two vertices, then the graph obtained by removing an edge from the tree, has two components, both of which are trees. Proof: Let uv be an edge in T and let S = (V,E') be a graph with the same vertex set as T and edge set E' = E\uv. Now let V1 be the set of edges x in T for which a unique path from x to v in T passes through u. Such a path must terminate at uv. Otherwise T would contain a cycle. Further let V2 be the complement of V1 in V. Every vertex in V1 is connected with u via a path in S. There is no path from u to v in S. Each vertex in V2 is connected to v in S. From this it follows that V1 och V2 are the vertex sets of the two components in S. Each such componenct is connected (follows from the construction) and contains no cycles, since there are no cycles in T. © Love Ekenberg
If T = (V,E) is a tree then E = V - 1 Proof: The result is true when V = 1, since the only possible tree has no edges. Suppose the result is true for all trees with k or fewer vertices and let T be a tree where V = k + 1. Further let uv be an arbitrary edge in T. If T1 = (V1,E1) and T2 = (V2,E2) are trees obtained (2 above) by removing (u,v) from T, then this yields:V1 + V2 = VE1 + E2 = E - 1.According to the induction hypothesis the following holds: E = E1 + E2 + 1= V1 - 1 + V2 - 1 + 1= V - 1. Therefore the result is true for all positive values of k. Proof © Love Ekenberg
A forest is a graph without cycles. Show that if F= (V,E) is a forest with n komponents, then E = V - n. Exercise G4 © Love Ekenberg
Colouring Vertices in a Graph A vertex colouring of a graph G = (V,E) is a function c: V -> N, where c(x) c(y) whenever {x,y} E. The chromatic number of G (written f(G)) is MIN(k : there is a vertex colouring c from V to Nk}. © Love Ekenberg
Find the Chromatic Number of a Graph In order to find the chomatic number of a graph two things must be done. 1 Find a vertex colouring using k colours. 2. Show that no vertex colouring uses fewer than k colours. © Love Ekenberg
The Greedy-Algorithm for Colouring the Vertices of a Graph Assume that the edges of a graph are ordered: v1,v2,…,vn. First assign a colour to vertex v1. 1. For each vi 2 i n, define the set S of colours that have been assigned to vj, 1 j < i, and that are adjacent to vi. 2. Assign a colour to vi that is not in S c(v1) := 1 for i = 2 to n do for j = 1 to i - 1 do S := if vj is adjacent to vi then S := S {c(vj)} k := 1 while k S do k := k+1 c(vi) := k © Love Ekenberg
Observation The result of this algorthm depends on the how the vertices have been ordered. There are however, n! such orders. There is at least one such order that yeilds the chromatic number of a graph. © Love Ekenberg
Exercise G4 Show that there is an order of vertices that using the greedy algorithm, yields the chromatic number of an arbitrary graph. © Love Ekenberg
Rooted Trees and Levels A tree can be arranged in levels with a particular vertex called the root. The root is at level 0 and the adjacent vertices are at level 1, etc. For each k 2; level k contains those vertices that are adjacent to vertices at level k - 1. Except those that are already at k - 2 A vertex in a rooted tree is a leaf if it is at level i and is not adjacent to any vertex at level i+1. A vertex that is not a leaf is an internal vertex. The heights of a tree is MAX(k : level k is non-empty) © Love Ekenberg
The Height of a Tree Theorem The height of a m-ary rooted tree with k leaves is at least logmk. © Love Ekenberg
Minimal Spanning Trees Let G = (V,E) be a connected graph, and let T be a subset of E. T is a spanning tree iff: 1) each vertex in G belongs to an edge in T 2) the edges in T form a tree © Love Ekenberg
Write a simple algorithm that will find a spanning tree ST = (V,E') given a connected graph G = (V,E). Verify that it really does always do so. Exercise G5 © Love Ekenberg
Solution G5 V' := ; E' := ; For all (x,y) V if x V' then V' := V' {x}; E' := E' {(x,y)} else if y V' then V' := V' {y}; E' := E' {(x,y)} There is always a new vertex during the construction. Let V'' be the set of vertices in each sub-graph during the construction. If no edges existed with a vertex in V'' and the second in -V'', then neither would there be a path from a vertex in V'' to -V''. This would mean that G was not connected, which contradicts the assumption. © Love Ekenberg
Weighted Graphs A graph G = (V,E) and a function v from E to N, constitute a weighted graph. The function v is called a weight function. © Love Ekenberg
The MST Problem A number of roads are to be built between a number of cities so that all cities are connected. This must be done as cheaply as possible. All possible roads can be represented with a graph G. Furthermore, if the cost is the only relevant parameter, then the cheapest configuration must be a tree. The problem is thus to find a minimal spanning tree T = (V,E) for G so that the sum of the weights of the set of vertices is minimal. This is called the minimum spanning tree problem (MST problem) for the weighted graph G. © Love Ekenberg
Solution of the MST Problem There must be a solution to the MST problem since there is only a limited number of spanning trees T for G. Further each such tree yields a positive integer value of v(T). A simple algorithm can be constructed using the greedy-algorithm: At each step, add the cheapest edge that joins a new edge to the partial tree. It always works. © Love Ekenberg
Theorem Let G = (V,E) be a connected graph with the weight function v: E -> N. Now assume that T is a spanning tree for D which has been constructed using the greedy-algorithm. Then v(T) v(U), for any spanning tree U of G. © Love Ekenberg
Let e1,e2,…,en-1 denote the edges of T in the order that they are constructed by the greedy algorithm (GA). If U <> T then an edge exists in T that is not in U. Denote the first such edge with ek = xy. Denote the set of vertices e1,…ek-1 with K. Since U is a spanning tree there is a path S in U between x and y. In S there is then an edge e* which has a vertex in K and a vertex in i-K. Since ek was chosen by GA, then e* is a candidate (but one not chosen). Therefore v(e*) v(ek). It then follows that if e* is in T then its index must be larger than k. If we replace e* with ek in U we get a spanning tre U1 where:v(U1) = v(U) - v(e*) + v(ek) v(U). Furthermore it holds that the first edge in T that is in U1 has an index larger then k. The procedure can therefore be iterated to obtain U1, U2,…, where Ui+1 has a larger sub-sequence of e1,…,en-1 than Ui. The process terminates when we have recieved a spanning tree Ur =T. This yields: v(T) = v(Ur) v(Ur-1) … v(U1) v(U). Proof © Love Ekenberg
The Complexity of the Greedy Algorithm Let S be the set of vertices in the partial tree constructed using the greedy algorithm above. Construct a table: A B C x y v(x,y) … … … … … … Column A contains vertices that are not in K. B contains y such that y is in K and such that xy is one of the cheapest edges joining x and y. Column C shows the weights of edges xy. At the i:th step in the construction is S = i. And there are n - i vertices in column A. Now choose one of the least values in column C. Denote this element w(x0,y0). This entails n - i - 1 comparisons. © Love Ekenberg
The Complexity of the Greedy Algorithm (Cont.) Thereafter the table is updated by putting x0 in S, i.e, by deleting the rows that start with x0 and examining whether x0 can replace some element in B, i.e, examining whether w(xx0) < w(xy) for one of the remaining n - i - 1 vertices x. This entails a further n - i - 1 comparisons. The total number of comparisons is therefore: which is O(n2). © Love Ekenberg
Depth First Searches When examining the vertices of a graph there are two principle approaches. One of these is DFS: The vertices in a graph can be regarded as a queue Q. A vertex joins the queue when first found during a search and disappears when it is no longer needed (LIFO algorithm). Q:=(v) %v is the starting vertex W:={} %W is the set of edges under construction while Q <> () do x:=last(Q) if x is adjacent to a new corner y then Q:=Q (y); W:= W {xy} else Q:=Q - (x) T:=W © Love Ekenberg
Theorem T is a spanning tree for the component in the graph that contains v. © Love Ekenberg
Depth First Searches Example G = ({a,b,c,d,e},{(a,b),(b,c),(c,d),(c,e),(a,c),(b,d),(a,e)}) Q W a ab ab abc ab,bc abce ab,bc,ce abc ab,bc,ce abcd ab,bc,ce,cd abc ab,bc,ce,cd ab ab,bc,ce,cd a ab,bc,ce,cd ab,bc,ce,cd © Love Ekenberg
Breadth First Searches The other way to examine a graph is BFS: (FIFO algorithm) Q:=(v) %v is the starting vertex W:={} %W is the set of edges under construction while Q <> () do x:=first(Q) if x is adjacent to a new corner y then Q:=Q (y); W:= W {xy} else Q:=Q - (x) T:=W © Love Ekenberg
Theorem T is a spanning tree for the component in the graph that contains v. © Love Ekenberg
Breadth First Searches Example G = ({a,b,c,d,e},{(a,b),(b,c),(c,d),(c,e),(a,c),(b,d),(a,e)}) Q W a ab ab abc ab,ac abce ab,ac,ae bce ab,ac,ae bced ab,ac,ae,bd ced ab,ac,ae,bd ed ab,ac,ae,bd d ab,ac,ae,bd ab,ac,ae,bd © Love Ekenberg
Exercise 1. Use BFS (and DFS) to study the graph below. Is it connected? a b c d e f g h i e d e b a c b b a i g f g c e d d c h i h f i f © Love Ekenberg
Exercise Determine the complexity of the the greedy-algorithm for colouring vertices. c(v1) := 1 for i = 2 to n do for j = 1 to i - 1 do S := if vj is adjacent to vi then S := S {c(vj)} k := 1 while k S do k := k+1 c(vi) := k © Love Ekenberg
Exercise Show that there is an order for the vertices that using the greedy algorithm yields the chromatic number of an arbitrary graph. © Love Ekenberg