V. Adamchik 1 Graph Theory Victor Adamchik Fall of 2005 Plan 1. Basic Vocabulary 2. Regular graph 3. Connectivity 4. Representing Graphs Introduction A.Aho and J.Ulman acknowledge that “Fundamentally, computer science is a science of abstraction.” Computer scientists must create abstractions of real-world problems that can be represented and manipulated in a computer. Sometimes the process of abstraction is simple. For example, we use a logic to design a computer circuits. Another example - scheduling final exams. For successful scheduling we have to take into account associations between courses, students and rooms. Such set of connections between items is modeled by graphs. Let me reiterate, in our model the set of items (courses, students and rooms) won't be much helpful. We also have to have a set of connections between pairs of items, because we need to study the relationships between connections. The basic idea of graphs were introduced in 18th century by the great Swiss mathematician Leonhard Euler. He used graphs to solve the famous Königsberg bridge problem. Here is a picture (taken from the internet) V. Adamchik 21-127: Concepts of Mathematics German city of Königsberg (now it is Russian Kaliningrad) was situated on the river Pregel. It had a park situated on the banks of the river and two islands. Mainland and islands were joined by seven bridges. A problem was whether it was possible to take a walk through the town in such a way as to cross over every bridge once, and only once. Here is the graph model of the problem A graph is a set of points (we call them vertices or nodes) connected by lines (edges or arcs). The simplest example known to you is a linked list. V. Adamchik The Web: The entire Web is a graph, where items are documents and the references (links) are connections. Networks: A network consist of sites that send and recieve messages of various types. Program Structure: A compiler builds a graph to represent relationships between classes. The items are classes; connections are represented by possibility of a method of one class to call a method from another class Basic Vocabulary A substantial amount of definitions is associated with graphs. We start with introduction to different types of graphs A graph that have nonempty set of vertices connected at most by one edge is called simple When simple graphs are not efficient to model a cituation, we consider multigraphs. They allow multiple edges between two vertices. If that is not enough, we consider pseudographs. They allow edges connect a vertex to itself. What do these three types of graphs have in common? The set of edges is unordered. All such graphs are called undirected. A directed graph consist of vertices and ordered pairs of edges. Note, multiple edges in the same direction are not allowed. 3 V. Adamchik 21-127: Concepts of Mathematics If multiple edges in the same direction are allowed, then a graph is called directed multigraph. Usually by a graph people mean a simple undirected graph. No directions, no self-loops, no multiple edges. Be careful and watch out! An edge may also have a weight or cost associated with it. If a, b is an edge we might denote the cost by c a, b In the example below, c a, b c b, a 7. Two vertices are called adjacent if there is an edge between them. The degree of a vertex in an undirected graph is the number of edges associated with it. If a vertex has a loop, it contributes twice. 5 V. Adamchik In the above picture, the degree of vertex a is 2, and the degree of vertex c is 4. Theorem (The handshaking theorem). Let G be an undirected graph (or multigraph) with V vertices and N edges. Then 2N deg v v V Example, Exercise. Suppose a simple graph has 15 edges, 3 vertices of degree 4, and all others of degree 3. How many vertices does the graph have? 3*4 + (x-3)*3 = 30 In a directed graph terminology reflects the fact that each edge has a direction. In a directed graph vertex v is adjacent to u, if there is an edge leaving v and coming to u. In a directed graph the in-degree of a vertex denotes the number of edges coming to this vertex. The out-degree of a vertex is the number of edges leaving the vertex. V. Adamchik 21-127: Concepts of Mathematics Theorem. Let G be a directed graph (or multigraph) with V vertices and N edges. Then indeg v N v V outdeg v v V Regular graph A graph in which every vertex has the same degree is called a regular graph. Here is an example of two regular graphs with four vertices that are of degree 2 and 3 correspondently The following graph of degree 3 with 10 vertices is called the Petersen graph (after Julius Petersen (1839-1910), a Danish mathematician.) V. Adamchik Exercise. Given a regular graph of degree d with V vertices, how many edges does it have? The complete graph on n vertices, denoted Kn , is a simple graph in which there is an edge between every pair of distinct vertices. Here are K4 and K5 : Exercise.How many edges in Kn ? Connectivity A path is a sequence of distinctive vertices connected by edges. Vertex v is reachable from u if there is a path from u to v. A graph is connected, if there is a path between any two vertices. Exercise. Given a graph with 7 vertices; 3 of them of degree two and 4 of degree one. Is this graph is connected? No, the graph have 5 edges. 7 V. Adamchik 21-127: Concepts of Mathematics A directed graph is strongly connected if there is a path from u to v and from v to u for any u and v in the graph. A directed graph is weakly connected if the underlying undirected graph is connected Representing Graphs Theorem. In an undirected simple graph with N vertices, there are at most N N 1 2 edges. Proof. By induction on the number of vertices. V = 1, there are no edges V = n, there are n n 1 2 edges We need to prove that if V n 1 then a graph has n n nn 1 2 n nn 1 2 edges 1 2 Exercise. What is the maximum number of edges in a simple disconnected graph with N vertices? For all graphs, the number of edges E and vertices V satisfies the inequality E If the number of edges is close to V V 2. log V , we say that this is a dense graph, it has a large number of edges. Otherwise, this is a sparse graph E is relatively sparse. There are two standard ways to represent a graph: · as a collection of adjacency lists V log V. In most cases, the graph V. Adamchik 9 · or as an adjacency matrix An adjacency list representation is used for representation of the sparse graphs. An adjacency matrix representation may be preferred when the graph is dense. The adjacency-list representation of a graph G consists of an array of linked lists, one for each vertex. Each such list contains all vertices adjacent to a chosen one. Here is an adjacency-list representation: Vertices in an adjacency list are stored in an arbitrary order. A potential disadvantage of the adjacency-list representation is that there is no quicker way to determine if there is an edge between two given vertices. This disadvantage is eliminated by an adjacency matrix representation. The adjacency matrix is a matrix of size V x V such that Mi j 1, if there is an edge between i and j 0, otherwise V. Adamchik 21-127: Concepts of Mathematics Trees A tree is a connected simple graph without cycles. A cycle is a sequence of distinctive adjacent vertices that begins and ends at the same vertex. A tree with V vertices must have V 1 edges. A connected graph with V vertices and V 1 edges must be a tree. A rooted tree is a tree with one vertex designated as a root. A forest is a graph without cycles. In orther words, a forest is a set of trees. A spanning tree of a graph is a subgraph, which is a tree and contains all vertices of the graph. In the figure below, the right picture represents a spanning tree for the graph on the left. A spanning tree is not unique. V. Adamchik Famous Problems on Graphs The Euler cycle (or tour) problem: Is it possible to traverse each of the edges of a graph exactly once, starting and ending at the same vertex? The Hamiltonian cycle problem: Is it possible to traverse each of the vertices of a graph exactly once, starting and ending at the same vertex? The traveller salesman problem: Find the shortest path in a graph that visits each vertex at least once, starting and ending at the same vertex? The planar graph: Is it possible to draw the edges of a graph in such a way that edges do not cross? The four coloring problem: Is it possible to color the vertices of a graph with at most 4 colors such that adjacent vertices get different color? The marriage problem (or bipartite perfect matching): At what condition a set of boys will be marrying off to a set of girls such that each boy gets a girl he likes? 11 V. Adamchik 1 Graph Theory Victor Adamchik Fall of 2005 Plan 1. Graph Isomorphism 2. Graph Enumeration 3. Planar Graphs Graphs Isomorphism There are different ways to draw the same graph. Consiser the following two graphs. You probably feel that these graphs do not differ from each other. What about this pair? Another pair V. Adamchik 21-127: Concepts of Mathematics Are they the same? To answer this question, we need to look at the most essential property of a graph. Recall the graph representation. We discuss two models - an adjacency list and an adjacency matrix. So, we can say that a notion about which pair of vertices are adjacent and which are not is the most important property. Base on this, we can say that two graphs above must be identical - check the pair of adjacent vertices. Definition. Graphs G1 V1 , E1 and G2 V2 , E2 are isomorphic if 1. there is a bijection (one-to-one correspondence) f from V1 to V2 and 2. there is a bijection g from E1 to E2 that maps each edge v, u to f v , f u Example of non-isomorphic graphs Exercise. Are these two graphs isomorphic? V. Adamchik 3 In practice, it is not a simple task to prove that two graphs are isomorphic. It is much simpler to show that two graphs are not isomorphic by showing an invariant property that one has and other does not. An invariant is a property such that if a graph has it all isomorphic graphs have it. Some of such properties are the number of vertices, the number of edges, degree of a vertex and some others. Note, a complete set of such invariants is unknown. Theorem. Two graphs are isomorphic for some ordering of their vertices their adjacency matri- ces are equal. Question. How many different adjacency matrices do a graph with n vertices have? Answer. It's equal to the number of permutations of n elements, which is n . Graph Enumeration The subject of graph enumeration is concerned with the problem of finding out how many non-isomorphic graphs there are which posses a given property. Main Question of this section: How many are there simple undirected non-isomorphic graphs with n vertices? We will try to answer this question into two steps. First, we count all labelled graphs. How many are there simple undirected labelled graphs with n vertices? Here is a set of all such graphs for n 3 V. Adamchik 21-127: Concepts of Mathematics There are only two choices for each edge, it either exists or it does not. Therefore, since the maximum number of edges is n , the total number of undirected labelled graphs is 2 2 n 2 Next, we will count all unlabelled graphs. We observe that in the above picture there are some isomorphic graphs. Excluding them, we obtain only four unlabelled graphs (with 3 vertices) Therefore, to count all unlabelled graphs we need to count the number of equivalent classes. Here are some partciluar cases vertices 1 2 3 4 5 num of graphs 1 2 4 11 34 Counting the number of equivalent classes is far out of this course. 5 V. Adamchik Planar graphs An undirected graph is called a planar graph if it can be drawn on a paper without having two edges cross. We say that a graph can be embedded in the plane, if it planar. A planar graph divides the plane into regions (bounded by the edges), called faces. The following planar graph has 4 faces. Theorem (Euler's formula) For any connected planar graph G V , E , the following formula holds V F E 2 where F stands for the number of faces. Proof (by induction on the number of faces) Base step. G is connected and has only one face. It is a tree, so E V 1 E V 1 and therefore 2. Suppose the formula holds for a connected graph with n faces. Prove it for n 1 faces. V. Adamchik 21-127: Concepts of Mathematics Choose an edge e connecting two different faces of G, and remove it. The graph remains connected. This removal decreases both the number of faces and edges by one. So, we can use inductive hypothesis. V E F V E 1 F 1 2. QED. Theorem (Euler's formula for a disconnected graph) For any planar graph G V , E with k components, the following formula holds V F E 1 k Proof. Choose an edge e within the same face and remove it. The graph becomes disconnected. This removal decreases the number of edges by one and increase the number of componenets by one. Since the formula holds for G QED e, it also holds for G. V. Adamchik 21-127: Concepts of Mathematics Graph Theory Victor Adamchik Fall of 2005 Plan 1. Euler Cycles 2. Hamiltonian Cycles Euler Cycles Definition. An Euler cycle (or circuit) is a cycle that traverses every edge of a graph exactly once. If there is an open path that traverse each edge only once, it is called an Euler path. The left graph has an Euler cycle: a, c, d, e, c, b, a and the right graph has an Euler path b, a, e, d, b, e V. Adamchik 2 Theorem A connected undirected graph has an Euler cycle each vertex is of even degree. Proof. Each time the path passes through a vertex it contributes two to the vertex's degree, except the starting and ending vertices. If the path terminates where it started, it will contribute two to that degree as well. We start with any vertex and choose a next vertex from all adjacent vertices in random order. We continues in this manner. After finite number of steps, we will end in the vertex we started with. If we traverse all edges, we build an Euler cycle, Otherwise, consider a subgraph H obtained from the original graph by deleting that cycle. Each vertex of H has even degree. In the above left pcture graph H might consist of vertices c, d, e. The original graph and H must have one vertex in common. We start with this vertex and repeat the procedure. We repeat all these until all edges are traversed. QED. The proof to the theorem produces an algorithm. Algorithm (constructing Euler cycles) G is a connected graph with even edges We start at a proper vertex and construct a cycle. We decrease the vertex degree each time we visit it. If this cycle contains all edges of the graph, stop. Otherwise, select a vertex of degree greater than 0 (that belongs to the graph as well as to the cycle!) and construct another cycle. Next, splice these two cycles into one. If a join cycle contains all edges of the graph, stop. If not, select a common vertex of degree greater than 0 and construct a cycle, and so on. Consider the following example. On each step we maintain an array of degree V. Adamchik 21-127: Concepts of Mathematics 1. Start at 3 and discover cycle (3,5,4,3). 2. Start at 3 and discover cycle (3,2,1,3) 3. splice them in one (3, 5, 4, 3, 1, 2, 3) 4. start at 1 and discover (1,0,6,5,1) 5. splice, to get a final cycle (3, 5, 4, 3, 1, 0, 6, 5, 1, 2, 3) Theorem A connected undirected graph has an Euler path (not a cycle) if it has exectly two vertices of odd degree. Consider the graph. It has two vertices of odd degrees, since the graph has an Euler path. Theorem A digraph has an Euler cycle all vertices A graph below is not Eulerian. if it strongly connected and indeg vk outdeg vk for V. Adamchik Hamiltonian Cycles Definition. A Hamiltonian cycle is a cycle that traverses every vertex of a graph exactly once. If there is an open path that traverses each vertex only once, it is called a Hamiltonian path. The terminoogy came from the Icosian puzzle, invented by Hamilton in 1857. See http://www.puzzlemuseum.com/month/picm02/200207icosian.htm There was a wooden "schematic" dodecahedron with a peg at each vertex labeled with different cities. The goal was to start at a city and travel along edges, visiting each of the other 19 cities exactly ones. There are no known conditions which are both necessary and sufficient. There are a few sufficient considitons. For a graph to have a Hamiltonian cycle the degree of each vertex must be two or more. 4 V. Adamchik 21-127: Concepts of Mathematics In general, the more edges a graph has, the more likely it is to have a Hamiltonian cycle. The Petersen graph does not have a Hamiltonian cycle. Theorem (Dirac) Let G be a simple graph with n 3 vertices. If every vertex has degree at least n 2, then G has a Hamiltonian cycle. The best known algorithm for finding a Hamiltonian cycle has an exponential worst-case complexity. Definition. The line graph L G of graph G has a vertex for each edge of G, and two of these vertices are adjacent iff the corresponding edges in G have a common vertex. V. Adamchik 6 Eulerian cycles of a graph G translate into Hamiltonian cycles of L G . Exercise 1. If an edge has a vertex of degree d1 at one end and a vertex of degree d2 at the other, what is the degree of its corresponding vertex in the line graph? Answer. d1 d2 2 Exercise 2. Suppose a graph G is regular of degree d. Is L G necessarily a regular graph? and if so of what degree? Answer. L G is a regular graph of degree 2 d 1 Exercise 3. Why the line graph of a regular graph, if connected, always has an Euler cycle? V. Adamchik 21-127: Concepts of Mathematics Answer. L G is a regular graph of even degree. Exercise 4. Given G LG V, E with degree sequence d1 , d2 , ..., dV . Count the number of edges in E, X . Answer. Each edge in L G corresponds to a pair of adjacent edges in G. Therefore, we have to count pair of edges associated with each vertex in G V X k 0 dk 2 1 V. Adamchik Graph Theory Victor Adamchik Fall of 2005 Plan 1. The Chinese Postman Problem 2. The Traveling Salesman Problem 3. Graph Coloring The Chinese Postman Problem The Chinese Postman Problem (CPP) is a close cousin to finding an Euler cycle. Given a connected weighted graph or digraph G the CPP is the problem of finding the shortest cycle that uses each edge in G at least once. The name comes from the fact that a Chinese mathematician, Mei-Ko Kwan (1962), developed the first algorithm to solve this problem for a rural postman. We will develop a method for solving this type of problem. In the best situation, where each vertex has even degree, any Euler circuit solves the problem. Consider a case when a graph has two vertices of odd degree. V. Adamchik 21-127: Concepts of Mathematics 3 A H 5 2 2 B 2 5 D 1 C II I 4 5 G 9 2 F 1 E This picture immitates the mail delivery. A number assigned to an edge represents the time needed to travel along the edge. Our job is to find a shortest route starting and ending at vertex A. The graph does not have an Euler cycle but an Euler path, because there are two vertices of odd degrees. Therefore in order to cover all edges we will have to retrace edges IC and CF. This will increrase the total time by 10. 3 A H 5 2 2 B 2 5 D 1 C II I 4 5 G The total time is 2 9 5 4 9 2 F 1 2 1 5 5 1 5 5 E 2 3 49 Is there faster route? Yes, we do not retrace CF, but rather use FE+ED+DC which is shorter by 1. V. Adamchik 3 Generally, it might be not so simple to find a shortest path between two vertices. In this case we might use Dijkstra's algorithm (15-211). What if the graph has more than two vertices of odd degree? A B C D E F G H I In this graph there are four vertices of odd degree B, D, F, H. If we connect two pairs of these four vertices by two edges, the new graph will have an Euler cycle. It follows that we need to find the two paths (connecting chosen pairs) with total weight as small as possible. First we need to list all the ways to put the four odd vertices in two pairs. Pairing Path Weight Path Weight BD && FH B-A-D 8 F-E-H 10 BF && DH B-E-F 7 D-E-H 12 BH && DF B-E-H 9 D-E-F 10 Then, for each set of two pairs we find the shortest path joining the two vertices in each of the two pairs. Thje first pairing has the samllest total time 18. To find a specific route, take the given graph and add the retraced streets as multiple edges. Then, find an Euler circuit for this multigraph. V. Adamchik 21-127: Concepts of Mathematics A B C D E F G H I The Traveling Salesman Problem The Traveling Salesman Problem (TSP) is a close cousin to finding an Hamiltonian cycle. Given a weighted graph G, you want to find the shortest cycle (may be non-simple) that visits all the vertices. Consider the following graph: One cycle is a b c f e d a that has a total weight 26. Is there a shorter cycle? The approximation algorithm. This algorithm does not solve the problem but rather yeilds a solution which is within a factor of 2 of optimal (in the worst-case). Here is the alghorithm Step 1: find a MST. Step 2: do a DFS of the MST. V. Adamchik 5 Graph Coloring Your mission color the entire map of South America. 1. No country may touch another country of the same color. 2. You will be charged each time you use a color 3. You must color the map as cheaply as possible. We start with the graph representation: vertices represent countries. Two vertices are adjacent if two countries have a common boarder. So we reduce a problem to vertex coloring. Adjacent vertices must be colored in different colors. Definition. The chromatic number of a graph is the least number of colors required to do a coloring of a graph. V. Adamchik 21-127: Concepts of Mathematics What is the largest possible chromatic number? This question has puzzled mathaticians for a very long time. It has now been proved that any planar graph will have a chromatic number of at most 4. This is called the Four Color Problem. The problem was solved in 1976 by Appel and Heken. They developed an algorithm that solves the problem. The Four Colour Theorem is the first major theorem to be proved using a computer, The disadvantage of this computer aid approach is that the proof cannot be verified by hand. Theorem In any connected planar graph G (with at least 3 vertices), E 3V 6. By means of this theorem we can prove that K5 is not planar. Indeed, K5 has 5 vertices and 10 edges. By the theorem 10 15 6 9 which is not true. Proof. a) If the graph has no cycles, E E V V E 1 V V . Also, V 2V 6 3 or 2 V E 3V 6 0 6 b) If the graph has a cycle. We will count the number of pairs (edge, face). e1, F1 , e1, F2 , e2, F1 , e2, F2 , e3, F1 , e3, F2 7 V. Adamchik On one hand, each face is bounded by at least 3 edges. So edge, face 3F k On other hand, each edge is associated with at most two faces, edge, face 2E k Combining these together, yeilds 3F edge, face 2E k or 3F 2E By the Euler formula 2 V F E 6 3V 3F 3E 6 3V 2E 3E 6 3V E QED Corollary V, E has a vertex of degree less than 6. Every simple planar graph G Proof. Using the handshaking theorem combined with the previous theorem deg v v V Find the average degree 2 E 2 3V 6 V. Adamchik 21-127: Concepts of Mathematics 1 V deg v 6V 12 V v V 6 12 V 6 So, at least one vertex has a degree less than 6. QED Theorem. (Six Color Theorem) Any simple planar graph G can be colored with 6 colors. Proof (by induction on number of vertices). If G has six or less vertices, then the result is obvious. Suppose that all such graphs with n 1 vertices are 6-colorable. We have proved (see Corollary in the previous chapter) that any planar graph has at least one vertex of degree less than 6. Let us call it v. Remove this vertex v from the graph. By inductive hypothesis, that new graph G v is 6-colorable. Then, since there are at most 5 adjacent vertices to v, there must be a color available to use. This completes the proof. QED. Theorem. (Heawood) Every simple planar graph can be colored with less than or equal to 5 colors. Proof. We going to repeat the previous theorem proof to the point where it fails. The proof fails when a picked vertex v has degree 5. In this case the proof goes as follows. Label the vertices adjacent to v as x1 , x2 , x3 , x4 and x5 . Assume that x4 and x5 are not adjacent to each other. (the fact that not all xk joined by edges follows from the observation that if the are then the graph of these 6 vertices will be K5 which is not planar). V. Adamchik Next we remove edges v, x1 , v, x2 and v, x3 and then contract edges v, x4 and v, x5 . In the procces of contraction, vertices v, x4 , x5 will be replaced by a new vertex y, and all neighbors of v, x4 , x5 will be neighbors of y. We obtain a new graph H with two less vertices. By inductive hypothesis we can color it with 5 colors. Next we color vertices in our original graph G with the same coloring as for H, except vertices v, x4 , x5 . We assign y's color to vertices x4 and x5 (remember they are not adjacent). We give v a color different from all colors used on the four vertices x1 , x2 , x3 and y QED. 9 V. Adamchik 21-127: Concepts of Mathematics Graph Theory Victor Adamchik Fall of 2005 Plan 1. Bipartite matchings Bipartite matchings In this section we consider a special type of graphs in which the set of vertices can be divided into two disjoint subsets, such that each edge connects a vertex from one set to a vertex from another subset. Personnel Problem. You are the boss of a company. The company has M workers and N jobs. Each worker is qualified to do some jobs, but not others. How will you assign jobs to each worker? Marriage Problem. There are X men and Y women who desire to get married. Participants indicate who among V. Adamchik 2 the opposite sex would be acceptable as a potential spouse. Every woman can be married to at most one man, and every man to at most one woman. How could we marry everybody to someone they liked? Definition. A simple graph G disjoint subsets V b V, E is called bipartite if its vertex set can be partitioned into two V1 V2 , such that every edge has the form e V2 . Note, that no vertices both in V1 or both in V2 are connected. Exercise 1. Which of the following graphs are bipartite? C4 and C6 are bipartite. This is C6 : Theorem (König, 1936) A graph is bipartite it does not have an odd length cycle. a, b where a V1 and V. Adamchik 21-127: Concepts of Mathematics Proof. . Fix a vertex v. Define two sets of vertices A w V odd length path from v to w B w V even length path from v to w These sets provide a bipartition. If there is an odd length cycle, a vertex will be present in both sets. For example, consider C6 and fix vertex 1, then A 2, 4, 6 amd B 1, 3, 5 QED. Definition. A complete bipartite graph Km,n is a bipartite graph that has each vertex from one set adjacent to each vertex to another set. Exercise 2. Find a formula for the number of edges in Km,n . Answer. m*n Exercise 3. If Km,n is regular, what can you say about m and n? V. Adamchik 4 Answer. m = n Exercise 4. Is K2,3 planar? Answer. yes Exercise 5. How many faces does K2,3 have? Answer. V+F-E=2, F = 2 +m*n-(m+n), so K2,3 has 3 faces. Exercise 6. Is K3,3 planar? Answer. no Definition. A subset of edges M E is a matching if no two edges have a common vertex. In the picture below, the matching set of edges is in red: Definition. A matching M is maximum, if it has a largest number of possible edges. In this example, blue lines represent a matching and red lines represent a maximum matching. V. Adamchik 21-127: Concepts of Mathematics Exercise 6. What is the maximum number of edges in the maximum matching of a bipartite graph with n vertices? Answer. n 2 Definition. A matching M is perfect, if it matches all vertices. We must have V1 V2 in order for a perfect matching to possibly exist. How do we find a perfect matching? Consider an example. Blue edges represent a matching that is not perfect We start at 4, since it has no matching edge, and go to 5. Next we doubt that 2-5 is a part of perfect matching and pair 2 with 8. Again we question 3-8 and pair 3 with 6. One more time, we void 1-6 and instead we pair 1 with 7. Here is our new matching - we change (along the path) blue edges to black and black edges to red to produce the graph on the right. V. Adamchik Since the number of edges is equal to the number of vertices (in one of the disjoint sets), we have a perfect matching. Definition. An alternating path is a path whose edges alternate between matched and unmatched edges. Can we always improve on a matching if we find an alternating path? Yes, the argument is as it follows. An alternating path consists of matched and unmatched edges. The number of unmatched edges exceeds the number of matched edges by one. Therefore, an alternating path always increase the matching by one. Matching Algorithm (Hungarian algorithm). Step 1. Start at unmatched vertex. Step 2. Find an alternating path. Step 3. If it exists, change matching edges to nonmatching edges and conversely. If it does not exist, choose another unmatched vertex. Step 4. If the number of edges equals V/2 stop, otherwise proceed to step 1 and repeat as long all vertices have been examined without finding any alternating paths. Example 2. Given a bipartite graph with a matching. Find a perfect matching 6 V. Adamchik First iteration: 2 - 9 - 5 - 10 Second iteration: 3 - 7 - 1 - 9 - 2 - 8 Third iteration: 4 - 10 - 5 - 9 - 1 - 6 21-127: Concepts of Mathematics V. Adamchik We stop, since the matching has five edges. Typical Word Problem. Erdös is on committees A and E. Petersen is on committees A and D. Polya is on committee B. Hamilton is on committees C and D. Euler is on committees A and E. You need to pick five members for the National Academy Council, each from a different committee. Is this possible? Complexity of the Matching Algorithm The upper bound for the number of iteration is O V . The upper bound for finding an alternating path using BFS is O E . Therefore, the total time is O V E . 8