MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang I.The Seven Bridges of Königsberg Graph theory [图论] is a branch of mathematics which started with a famous puzzle that was posed by the townsfolk of Königsberg, Prussia in the early 1700’s. Königsberg is now known as Kaliningrad, a seaport and the administrative center of Kaliningrad Oblast, the Russian exclave between Poland and Lithuania on the Baltic Sea. The former East Prussia town was built largely on an island in the Pregel river, which sits near where two branches of the river join. The borders of the town spread over to the banks of the river as well as a nearby peninsula. Between these four land masses, seven bridges had been erected. Figure 1: The town of Königsberg and its seven bridges. Figure 2: The city of Kaliningrad today from Google Earth. Many of the seven bridges are gone and some are replaced by modern structures. 1 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang For a long while, the townspeople of Königsberg posed the question: “Is it possible to take a walk through town, crossing each of the seven bridges just once, and ending up wherever you started?” The “Seven Bridges of Königsberg” problem [柯尼斯堡七桥问题] is a famous historical problem in mathematics. Leonhard Paul Euler [萊昂哈德·歐拉] (1707- 1783) was a pioneering Swiss mathematician, who spent most of his life in Russia and Germany. Euler (pronounced as OILER) heard of the problem and solved it, thereby laid the foundation of very vast and important field of graph theory. 2 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Figure 3: Portrait of Leonhard Euler In 1736 Euler proved that it was not possible to walk through all seven bridges exactly just one time. In coming to this conclusion, Euler formulated the problem in terms of graph theory. He abstracted the case of Königsberg by eliminating all unnecessary features. He drew a picture consisting of “dots” that represented the landmasses and the line-segments representing the bridges that connected those land masses. To start with, Euler pointed out that the only important feature of a route is the sequence of bridges crossed. This allowed him to reformulate the problem in abstract terms eliminating all features except the list of land masses and the bridges connecting them. In modern terms, one replaces each land mass with an abstract "vertex" or node, and each bridge with an abstract connection, an "edge", which only serves to record which pair of vertices (land masses) is connected by that bridge. The resulting mathematical structure is called a graph. Figure 4: How Euler solved the “Seven Bridges of Königsberg” problem by abstraction. 3 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang The resulting picture might have looked somewhat similar to the figure shown below. Figure 5: The graph that corresponds to the “Seven Bridges of Königsberg” in Euler’s abstracted version. This simplifies the problem to great extent. Now, the problem can be merely seen as the way of tracing the graph with a pencil without actually lifting it. One can try it in all possible ways, but you will soon figure out, it is not possible. But Euler not only proved that it is not possible, but also explained why and what should be the characteristic of the graphs, so that its edge could be traversed exactly once. He came out with the then new concept of the degree of a node, which is defined as the number of edges touching a given node. Euler 4 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang proposed the following theorem: any given graph can be traversed with each edge traversed exactly once if and only if it had zero or exactly two nodes with odd degrees. We can easily infer this theorem. Exactly two nodes are required (and must be) to start and end of your trip. If it has even nodes than we can easily come and leave the node without repeating the edge twice or more. In actual case of seven bridges of Königsberg, once the situation was presented in terms of graph, the case was simplified as the graph had just 4 nodes, with each node having odd degree. So, Euler concluded that these bridges cannot be traversed exactly once. A graph is a mathematical structures used to model the notion of connection (pairwise relations between objects from a certain collection). A "graph" in this context consists of a non-empty set of points (vertices) and a set of lines (edges) connecting the vertices. The number of edges linked to a vertex is called the degree of that vertex. Graph theory is the branch of mathematics that devotes to the study of graphs. Mathematically, a graph is an ordered pair G = (V, E) comprising a set V of vertices or nodes together with a set E of edges or lines. Figure 6: A simple graph with vertex set V = {1, 2, 3, 4, 5, 6} and edge set E = {{1,2}, {1,5}, {2,3}, {2,5}, {3,4}, {4,5}, {4,6}}. Example of a graph a b c d Number of edges = Number of vertices = 4 Vertices are a, b, c and d. Edges are (ab), (ac), (ad) and (cd). 4 5 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Glossary of graph theory: route A sequence of edges and nodes from one node to another. Any given edge or node might be used more than once. path A route that does not pass any edge more than once. If the path does not pass any node more than once, it is a simple path. connected If some route exists from every node to every other, the graph is connected. Note that some graphs are not connected. A diagram of an unconnected graph may look like two or more unrelated diagrams, but all the nodes and edges shown are considered as one graph. Are the following paths? (referring to the previous example) (bacd) is a path. (abcd) is not a path since the is no edge between b and c. Circuit is a path start and end at the same vertex. For example, (cdac) Is the following graph a connected graph? a b is a circuit. NO c d e f Is the following graph a connected graph? a b c Yes d e f In graph theory, an Eulerian path is a path which passes through every edge once and only once. An Euler path with the same starting and ending node is an Euler circuit or an Euler cycle. If the starting and ending nodes are different, it is an open Euler trail. Any graph that possesses an Euler circuit is called an Euler graph. An open Euler trail is possible if and only if there are exactly two vertices of odd degree. An Euler circuit is possible if and only if every vertex is of even degree. The Königsberg Bridges 6 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang problem was an attempt to find an open Euler trail or an Euler circuit. Euler Theorem: A connected graph has an Euler path if and only if it contains none or two odd degree vertices. OR (1) A connected graph has an Euler circuit if and only if all vertices are even degree. (2) A connected graph has an open Euler trail if and only if just 2 vertices are odd degree. Figure 7: Every vertex of this graph has an even degree, therefore this is an Eulerian graph. Following the edges in alphabetical order gives an Eulerian circuit/cycle. The degree of a vertex is the number of edges connecting the vertex. a b c d e deg (a) = 2 deg (b) = f 3 deg(c) = 1 A graph may be undirected, meaning that there is no distinction between the two vertices associated with each edge, or its edges may be directed from one vertex to another. Structures that can be represented as graphs are ubiquitous, and many problems of practical interest can be represented by graphs. The link structure of a website could be represented by a directed graph: the vertices are the web pages available at the website and a directed edge from page A to page B exists if and only if A contains a link to B. A similar approach can be taken to problems in travel, biology, computer chip design, and many other fields. The graphs studied in graph theory should not be confused with "graphs of functions" and other kinds of graphs. II.The Chinese Postman Problem 7 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang A similar problem is called Chinese Postman Problem[中国邮递员问题] (after the Chinese mathematician, Mei-Ku Kuan[管梅谷], who discovered it in early 1960's). The background of Chinese postman problem is interesting. It is the problem that the Chinese Postman faces: he wishes to travel along every road in a city in order to deliver letters, with the least possible distance. The problem is how to find a shortest closed walk of the graph in which each edge is traversed at least once, rather than exactly once. In graph theory, finding a shortest closed path/circuit that visits every edge of an (connected) undirected graph is called the Chinese Postman problem. Alan Goldman of the U.S. National Bureau of Standards first coined the name 'Chinese Postman Problem' for this problem. For the practical situation, the problems like delivery of mail or newspaper, trash pick-up, and snow removal can be modeled by Chinese Postman Problem. If a graph possesses an Euler circuit, then the Chinese postman problem is solved. If a graph has no Euler circuit, then at least one edge must be crossed more than once. Figure 1 shows an Euler graph with 4 Euler circuits, while figure 2 shows a graph with no Euler circuit. Route 1: (s,1), (1,2), (2,3), (3,4), (4,2), (2,s) Route 2: (s,1), (1,2), (2,4), (4,3), (3,2), (2,s) Route 3: (s,2), (2,3), (3,4), (4,2), (2,1), (1,s) Route 4: (s,2), (2,4), (4,3), (3,2), (2,1), (1,s) When the graph has an Euler circuit, that circuit is an optimal solution for the Chinese Postman problem. 8 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang 1. Does the following graph have an Euler circuit? a NO b c d e 2. f Does the following graph have an Euler circuit? YES How can we find the Euler circuit? We can find an Euler circuit by taking the following steps. Take any circuit from the graph and then remove it from the graph until the graph is empty And then concatenate all the circuits together. 3. Find any Euler circuit in the following graph. a b c d e f Ans: Take and remove (aeca) from the graph. Then the graph becomes a b c d e f 9 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Take and remove (abfa) from the graph. Then the graph becomes: a b c d e f Take and remove (dfebd) from the graph. Then the graph becomes empty. Concatenating (aeca) and (abfa), we have (aecabfa). 4. Concatenating (aecabfa) and (dfebd), we have (aecabfa) + (bdfeb) = (aeca(bdfeb)fa) Find any Euler circuit in the following graph. Ans. After take and removing circuits (abeihga) and (defkjhd) from the graph, there is only one circuit (cmlhec) left. Concatenating (abeihga) and (defkjhd), we have (abeihga) + (efkjhde) = (ab(efkjhde)ihga) Concatenating (abefkjhdeihga) and (cmlhec), we have (abefkjhdeihga) and (ecmlhe) = (ab(ecmlhe)fkjhdeihga) Eulerization In the Chinese postman problem, the postman wishes to pass through all the streets without repeating the same street twice. We know it is impossible to pass all the streets without repeating unless the streets are arranged like an Euler graph. So, our problem now is to find any way to pass through all the streets (we don’t mind to repeat the same street if the graph is non-Euler). In mathematics, we can interpret this problem as to find any circuit passing through all the edges at least once. We can add some edges to make the graph Eulerian if it is not already 10 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang one. This process is called Eulerization. Any connected graph can be “eulerized” by taking the following steps: Select any two vertices with odd degree. Select any path between these two vertices. Duplicate the edges on this path. Repeat the above until all the vertices have even degree. 5. Eulerize the following graph. a b c d e f Ans. Select a and b. Duplicate edges on (afb), we have a b c d e f Select vertices e and f. Duplicate edges on (ebf), we have a b c d e f Adjacent matrix All graphs can be represented by adjacent matrices. For example, below is an adjacent matrix representing the above graph. vertex a b c d e f A 0 0 1 0 1 2 B 0 0 0 1 2 3 C 1 0 0 0 1 0 D 0 1 0 0 0 1 11 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang E 1 2 1 0 0 0 F 2 3 0 1 0 0 6. Given the following adjacency matrix, draw the corresponding graph. Eulerize it and then find an Euler circuit. vertex a b c d e f A 0 0 2 0 0 1 B 0 0 0 2 1 0 C 2 0 0 1 1 0 D 0 2 1 0 0 1 E 0 1 1 0 0 0 F 1 0 0 1 0 0 Ans a b c d e f Odd degree vertices: a and b Duplicate edges: ac, cd, db. a b c d e f Take and remove circuit (acdbdca). The graph becomes a b c d e f There is only one circuit left (acebdfa). Concatenate (acdbdca) and (acebdfa), we have (acdbdca cebdfa). III.Hamilton circuit and Traveling salesman Problem A Hamilton path is a path that travels through every vertex of a graph once and only once; a Hamilton circuit is a Hamilton path that starts and ends at the same vertex. For example, two Hamilton circuits in the following graph are (abdfec), (acebdf) 12 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang a b c d e f Recall: an Euler path is a path that travels through every edge of a graph once and only once; an Euler circuit is an Euler path that starts and ends at the same vertex. Unlike Euler circuit, there is no easy way to check if there is a Hamilton circuit or not. Complete graph A graph is completed if there is an edge between every pair of vertexes. A complete graph is a simple graph in which every pair of distinct vertices is connected by a unique edge. Note that every complete graph has a Hamilton circuit. Figure 1: A complete graph with 5 vertices. Each vertex has an edge to every other vertex. If a complete graph has 2 vertices, then it has 1 edge. If a complete graph has 3 vertices, then it has 1+2=3 edges. If a complete graph has 4 vertices, then it has 1+2+3=6 edges. If a complete graph has N vertices, then it has 1+2+3+...+(N-1)=(N-1)*N/2 edges. The complete graph with 4 vertices is written K4, etc. The complete graph with N vertices, KN, has (N-1)! Hamilton circuits. 10!= 3628800 20!= 2432902008176640000 30!= 265252859812191058636308480000000 40!= 815915283247897734345611269596115894272000000000 Notice how quickly these numbers become huge! 13 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang 7. Do you think the following graphs are complete graph? a b Yes No e f a b c d e f Example: Find Hamiltonian circuit on a dodecahedron – 12 identical pentagonal faces – 20 vertices (representing 20 cities) Problem: starting from one city, tour the world by visiting each city exactly once and return to the starting city Hamiltonian paths and circuit are named after William Rowan Hamilton who invented the Icosian game, now also known as Hamilton's puzzle, which involves finding a Hamiltonian circuit in the edge graph of the dodecahedron. Figure 2: A Hamiltonian circuit in a dodecahedron. 14 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Weighted graph In a weighted graph, each edge is assigned with a number. For example: 5 a b 4 2 4 3 e f 2 Minimum-weight Hamilton circuit Minimum-weight Hamilton circuit is a Hamilton circuit having minimum weight amount all the Hamilton circuit. The problem of finding the Minimum-weight Hamilton circuit is also called Traveling salesman Problem (TSP). In these problems, you want to travel to several locations (these are the vertices). You can get from each location to every other location, and you pick out one best way to do that (this will be the edge connected those two vertices). Attached to each edge is a number, called the “weight” of that edge, representing how “good” that route is (“smaller is better”). The total weight of a Hamilton circuit is the sum of the weights of all the edges in that circuit. So the “traveling-salesman problem”, TSP for short, is to find the Hamilton circuit with the 15 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang smallest total weight. Traveling salesman Problem (TSP) A salesman wants to visits all the cities once and only once. He wants to minimize the traveling expenses. What route he should go? We can solve a TSP by listing all Hamilton circuits, calculating the total weight of each one, and then choosing the one which has least total weight. This is called the brute-force algorithm. We see that the third family of circuits has the least total weight. This method is good if we only have a few vertices, but we’ve seen that the number of Hamilton circuits grows very rapidly as we increase the number of vertices. It quickly becomes impossible to do the calculation by hand. Even with a computer, this method becomes impractical with fairly small numbers of vertices (around 20). 16 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Here is another method which takes a lot less work: The nearest-neighbor algorithm: 1. Choose any starting vertex. 2. Whenever you reach any vertex, look at the weights of all the edges that lead to vertices you haven’t visited yet. Choose the one of least weight. 3. Once you’ve reached the last vertex, go back to the starting point. If we start at A, the “nearest neighbor” is C, then B, then we have no choice but to go to D, then A. That gives us the “optimal” Hamiltonian circuit A,C,B,D,A of total weight 21. If we start at C, we go to A, B, D, C. This is a “non-optimal” Hamiltonian circuit of total weight 23. So the nearest-neighbor algorithm takes less work, but doesn’t necessarily give the best answer. However, it usually produces an answer which is close to best. Minimum Spanning Trees A tree is a connected graph with no circuit. More mathematically, a tree is a graph in which any two vertices are connected by exactly one simple path. A sub-graph is a graph contains only some vertices and edges of the graph. Given a connected, undirected graph, a spanning tree of that graph is a sub-graph which is a 17 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang tree and connects all the vertices together. A single graph can have many different spanning trees. In some situation we want to assign a weight to each edge, which is a number representing how unfavorable it is, and use this to assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree. A minimum spanning tree (MST) or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree. Figure 2:The minimum spanning tree of a planar graph. Each edge is labeled with its weight, which here is roughly proportional to its length. One example of the application of minimum spanning tree would be a cable TV company laying cable to a new neighborhood. If it is constrained to bury the cable only along certain paths, then there would be a graph representing which points are connected by those paths. Some of those paths might be more expensive, because they are longer, or require the cable to be buried deeper; these paths would be represented by edges with larger weights. A spanning tree for that graph would be a subset of those paths that has no cycles but still connects to every house. There might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost. Another application is the following. Computer Connection Problem A computer engineer wants to connect some computers so that there is a direct or indirect connection between each pair of computers. The costs of connection between computers are given. Find the cheapest way to make this connection. Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for 18 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang a connected weighted graph. • Keep adding edges with cheapest cost to the circuit. • Ensure not to close up a circuit. Example of Kruskal's algorithm: The original graph with numbers near the arcs indicate their weight. None of the arcs are highlighted. Step 1: AD and CE are the shortest arcs, with length 5, and AD has been arbitrarily chosen, so it is highlighted. Step 2: CE is now the shortest arc that does not form a cycle, with length 5, so it is highlighted as the second arc. Step 3: The next arc, DF with length 6, is highlighted using much the same method. Step 4: The next-shortest arcs are AB and BE, both with length 7. AB is chosen arbitrarily, and is highlighted. The arc BD has been highlighted in red, because there already exists a path (in green) between B and D, so it would form a cycle (ABD) if it were chosen. 19 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Step 5: The process continues to highlight the next-smallest arc, BE with length 7. Many more arcs are highlighted in red at this stage: BC because it would form the loop BCE, DE because it would form the loop DEBA, and FE because it would form FEBAD. Step 6: Finally, the process finishes with the arc EG of length 9, and the minimum spanning tree is found. Prim's algorithm: another algorithm that finds a minimum spanning tree for a connected weighted undirected graph The algorithm continuously increases the size of a tree, one edge at a time, starting with a tree consisting of a single vertex, until it spans all vertices. Input: A non-empty connected weighted graph with vertices V and edges E (the weights can be negative). Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew = {} Repeat until Vnew = V: o Choose an edge (u, v) with minimal weight such that u is in Vnew and v is not (if there are multiple edges with the same weight, any of them may be picked) o Add v to Vnew, and (u, v) to Enew Output: Vnew and Enew describe a minimal spanning tree 20 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E and F are connected to D through a single edge. A is the vertex nearest to D and will be chosen as the second vertex along with the edge AD. The next vertex chosen is the vertex nearest to either D or A. B is 9 away from D and 7 away from A, E is 15, and F is 6. F is the smallest distance away, so we highlight the vertex F and the arc DF. The algorithm carries on as above. Vertex B, which is 7 away from A, is highlighted. 21 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang In this case, we can choose between C, E, and G. C is 8 away from B, E is 7 away from B, and G is 11 away from F. E is nearest, so we highlight the vertex E and the arc BE. Here, the only vertices available are C and G. C is 5 away from E, and G is 9 away from E. C is chosen, so it is highlighted along with the arc EC. Vertex G is the only remaining vertex. It is 11 away from F, and 9 away from E. E is nearer, so we highlight it and the arc EG. Now all the vertices have been selected and the minimum spanning tree is shown in green. In this case, it has weight 39. 22 MATH1020 Math for Non-Science-Chap1: Graph theory—Dr. Tsang 23