4.1 Introduction to Graphs • Graph – consists of vertices and edges. Each edge must start and end at a vertex. • Example: 4.1 Introduction to Graphs • Examples of non-graphs: (edge splits in two pieces, no vertex at end) 4.1 Introduction to Graphs • Path – connects one vertex to another by a sequence of edges V1 • Example: V1- V3- V2- V4 V2 V4 V3 V5 4.1 Introduction to Graphs • Connected Graph – there is a path from every vertex to every other vertex • Example of graph that is not connected: V1 V2 V4 V3 V6 V5 4.1 Introduction to Graphs • Degree of a vertex - number of edges connected to it • Example: V4 has degree = 3 V2 V4 V1 V3 V5 4.1 Introduction to Graphs • Total degree of a graph - sum of the degrees of all the vertices. Note: If a graph has n edges, the total degree = 2n. • Example: graph has total degree = 10 V1 V4 V2 V3 4.2 Edge Paths • Konigsberg Bridge Problem: Can you walk across each bridge exactly once? North Bank River Island Island South Bank 4.2 Edge Paths • In 1736, Euler showed that the Konigsberg bridge problem was unsolvable by using graph theory. • Another example – trace without lifting your pencil: 4.2 Edge Paths • Edge Path – a path that goes through each edge of the graph exactly once To determine if a graph has an edge path, count the number of vertices of odd degree. Note that this number will always be even (0,2,4,…) • A graph has an edge path if it has: – 2 vertices of odd degree 4.2 Edge Paths • Selecting the starting/ending vertices: Given 2 vertices of odd degree – start at one of the vertices and end at the other 4.2 Fleury’s Method for Finding an Edge Path 1. Select a starting vertex. 2. Select and number a connecting edge if it is the only remaining unused edge, or, select it if the edge does not cut off or isolate a portion of the graph. 3. Repeat step 2 until all edges have been selected. 4.3 Edge Cycles • Cycle – a path that starts and ends on the same vertex with no repeated edges • Edge Cycle – a cycle that includes every edge in the graph • Theorem: A graph has an edge cycle it is connected and every vertex is of even degree 4.3 Edge cycles • Selecting the starting/ending vertices for edge cycles: – no vertices of odd degree – start at any vertex and end at the same vertex 4.3 Edge Cycles • Edge cycles are different from edge paths: – Edge Cycle – every vertex is of even degree – Edge Path – exactly 2 vertices are of odd degree 4.3 Edge Cycles • Eulerizing – adding edges where they already exist to create an edge cycle • Method – add edges to connect the vertices of odd degree. This changes both vertices to an even degree. Make sure you only connect one edge to each odd vertex! • Note: Eulerizing is useful in museums, gardens, street systems with non-Euler cycles that force you to retrace your path 4.4 Vertex Cycles • Cycle – a path that starts and ends on the same vertex with no repeated edges • Vertex Cycle – a cycle that contains every vertex • Unsolved problem: Identifying exactly when a vertex cycle exists. 4.4 Vertex Cycles • Dirac’s Theorem – if the following are all true for a graph then a vertex cycle must exist: – Graph is connected – Graph has 3 or more vertices – Each vertex is adjacent to at least half of the vertices • Note: Dirac’s conditions may not be satisfied and yet a vertex cycle still exists. 4.4 Vertex Cycles • Complete Graph – every vertex is connected to every other vertex • Definition: n! n (n 1) (n 2) ... 3 2 1 • Theorem: A complete graph with n vertices has (n-1)! vertex cycles (n 1)! • Note: This becomes if reverse cycles 2 are not included. 4.5 Traveling Salesman Problem • Not the “how to get rid of a traveling salesman” problem • Problem: How to schedule visits to cities in the cheapest way possible – using graph theory • Weighted Graph – a weight (number) is assigned to each edge • Graph theory problem: Finding the vertex cycle with the smallest total weight 4.5 Traveling Salesman Problem • Solution – Brute Force Method: – List all vertex cycles (reverse cycles not included) – Find the total weight of each cycle – Pick the cycle with the smallest weight (n 1)! 2 • Problem with this approach: cycles must be checked – gets large in a hurry 4.5 Traveling Salesman Problem • Lower Bound – Add the weights of the n shortest edges where n = # of vertices • Applications of traveling salesman problem: – Deliveries (e.g. Federal Express) – Collections(e.g. Trash Pickup) – Manufacturing(drilling holes in circuit boards – need to minimize the movement of the drill) 4.6 Approximations to Traveling Salesman Problem • Nearest Neighbor Approximation: 1. Select the starting vertex. 2. Select the unused connecting edge with the smallest weight. 3. Repeat step 2 until all vertices are reached. 4. Select the edge that goes back to the starting point to complete the cycle 4.6 Approximations to Traveling Salesman Problem • Cheapest Link Approximation: 1. Select the edge with the smallest weight. 2. Select the unused edge with the smallest weight unless it completes a cycle or one vertex becomes connected to 3 edges. Break ties arbitrarily. 3. Repeat step 2 until all vertices have been included in a cycle. 4.6 Approximations to Traveling Salesman Problem - Comparison • Nearest Neighbor Approximation: 1. Starting vertex must be specified. 2. Order selected is always sequential – starting vertex is always the end vertex of the previous edge. • Cheapest Link Approximation: 1. Order selected is not necessarily sequential. 2. Weights are always increasing. 4.7 Trees • Tree – a graph that is connected and has no cycles. Examples: family tree, organizational chart • Properties of trees: – For any 2 vertices there is a unique path between them – A tree with n vertices has n-1 edges – A tree has the least number of edges possible for a connected graph 4.7 Trees • Determining whether a graph is a tree - A graph with n vertices and n-1 edges must be a tree • Spanning Tree of a Graph – a tree that is a subgraph that contains all the vertices of the graph • Minimal Spanning Tree – the spanning tree of a graph with the smallest weight 4.7 Trees • Kruskal’s method for finding the minimal spanning tree: 1. Find the edge with the smallest weight 2. Find the next smallest unused edge that does not form a cycle 3. Repeat step 2 until all vertices are included. Remember - do not complete a cycle! 4.8 Instant Insanity • Instant Insanity - uses 4 blocks with colors on the sides (red, white, blue, green). • Goal – to stack the cubes so that all 4 colors occur exactly once on each side. • There are 331,776 possibilities with trial and error. • The puzzle can be solved using graph theory. 4.8 Instant Insanity • Cubes must be represented in 2 dimensions • Convert each cube to a graph representation by connecting colors of opposite sides R B G W R W B G W G 4.8 Instant Insanity • Consolidate to one graph, labeling paths by cube number 2 R R B WR GB WR G B WR W GB 2 3 4 G 1 B W 3 1 2 1 4 G 4 3 4.8 Instant Insanity • Solution to consolidated graph – pick 2 subgraphs such that – Each vertex has degree 2 – Each cube is represented exactly once in each subgraph (1, 2, 3, and 4) – 2 subgraphs have no edge in common (same number/edge in both subgraphs) • Note: forms of solutions are given in book 4.8 Instant Insanity • Solution subgraphs for the example: 2 R R W R 1 2 4 B W 1 2 4 3 G 3 B 1 34 2 W 3 1 2 1 4 G B G 4 3