710 likes | 1.23k Views
Discrete Maths. 242-213 , Semester 2, 2013-2014. Objective introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path). 11 . Graph Theory. Overview. Introduction Directed Graphs Other Graph Terminology
E N D
Discrete Maths 242-213, Semester 2, 2013-2014 • Objective • introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path). 11. Graph Theory
Overview • Introduction • Directed Graphs • Other Graph Terminology • Sheet Metal Hole Drilling • Hamiltonian Cycles • The Travelling Salesman Problem • Depth First Search • Finding the Shortest Path • More Information
Part of Wyoming’s (a USA State) Road System 1. Introduction Sheridan Greybull Gillettte Buffalo Worland Shoshoni Lander Casper Douglas Muddy Gap
Problem • The Wyoming Road Inspector lives in Greybull, and must check every road. • He must check the roads as quickly as possible • by travelling each road only once • starting from Greybull and returning there • Is this travel plan possible?
(Undirected) Graph Version Gre She e1 e3 e2 e4 Buf Gil Wor e5 e6 e7 e8 e10 e9 Sho Cas Dou e13 e11 Mud Lan e12 continued
Dots = vertices/nodes (singular: vertex) • Lines = edges/arcs • An undirected graph is one where the edges have no direction (no arrows) on them.
Equivalent Graph Wor e6 Gre Sho e2 e11 Lan e9 e12 e4 e1 Mud Gil e13 e8 e5 e3 Dou e10 Cas She e7 Buf
1.1. Paths • A path is a sequence of edges between two verticies • e.g. the path from Sheridan (She) to Muddy Gap (Mud) is: {She, Buf, Cas, Mud} • Road Inspector Problem: • is there a path from Gre (Greybull) to Gre which uses all the edges only once?
1.2. Cycles • In a directed graph, a cycleis a path that begins and ends at the same node. • e.g. {Gre, She, Buf, Wor, Gre} • A simple cycle is a path with no repeated verticies, except the ends. • e.g. {Wor, Buf, Gil, Buf, Wor} is not a simple cycle continued
In an undirected graph, a simple cycle must have two or more different nodes. • e.g. the cycle {Wor, Wor} is excluded 2 nodes • Road Inspector Problem again: • is there a simple cycle from Gre (Greybull) to Gre which uses all the edges only once?
Answer: No • Consider Worland (Wor): the inspector must use every edge connected to Wor only once. Wor Gre Sho e6 e2 e4 Buf continued
But to travel through Wor requires 2 edges (one in, one out). • So there is no way to use the third edge to visit Wor without using one of the other edges again. • This argument is the basis of the first mathimatical result in graph theory...
The Euler Cycle • If a graph G has a simple cycle from vertex v to v, which uses every edge exactly once, only if there are an even number of edges connected to each vertex. • The first graph theory result, proved by by Leonhard Euler in 1736 for the Konigsberg Bridge Problem. Leonhard Euler 1707 - 1783
1.3. Practical Uses of Euler Cycles • In computer networks, edge traversal (i.e. moving between network nodes) is expensive. • Euler’s definition is a very fast algorithm for checking whether a graph (network) can be traversed efficiently.
Example • Can this network be traversed efficiently (e.g. by a Web search engine collecting information)? • e.g. start at a, finish at a, travel each edge only once? a b c f e d g h i
2. Directed Graphs v1 • A directed graph = vertices/nodes and arcs. • An arc = a directed edge (one with an arrow). e1 e3 v3 v2 e4 e5 e2 v6 v4 e6 v5 e7
A Calling Graph • A Calling Graph for a small program: main makeList printList mergeSort 4 examples of direct recursion split merge
3. Other Graph Terminology • A graph with parallel edges, a loop, and an isolated vertex: e3 v2 v4 e4 e1 v3 e2 v1 continued
Parallel Edges e1, e2 = (v1, v2)A loop e3 = (v2, v2)Isolated (or unconnected) vertex: v4 • A graph with no loops and no parallel edges is a simple graph. • A graph with no isolated verticies is a connected graph.
Path Length • The length of a path = the number of edges that it uses. • If edges/arcs are labeled with numbers then we can sum the values along a path to get a “distance”.
4. Sheet Metal Hole Drilling • Problem: minimise the moving time of the drill over the metal sheet. continued
A Weighted Graph Version • Add edge numbers (weights) to indicate the movement time between any two holes. 8 b a 6 2 6 4 c 3 d 5 9 12 4 e
Simplified Problem • Assume that the drilling must start at vertex ‘a’ and end at vertex ‘e’. • What is the path with the shortest length between ‘a’ and ‘e’? • length = sum of weights on the path’s edges
Listing all Possibilities • Path Lengtha,b,c,d,e 21a,b,d,c,e 28a,c,b,d,e 24a,c,d,b,e 26a,d,b,c,e 27a,d,c,b,e 22 The shortestlength path.
Is there a Better Way? • Finding an answer by listing all the paths is very time consuming for real problems. • But there is no known approach/algorithm which is faster for general graphs! • finding a better algorithm would be a major breakthrough in maths (and engineering)
5. Hamiltonian Cycles • Sir William Rowan Hamilton’s puzzle (1850’s) • it made him very rich • Each corner is labelled with a city name. • The shape is a dodecahedron. My version uses country names. William Rowan Hamilton (1805 – 1865) continued
Problem: start at any city (letter), travel along the edges, visit each city exactly once, and return to the starting city. • Note: not all edges need to be used
Graph of Hamilton’s Puzzle b a g f h p t i o q r s j c n e m k l d
Hamiltonian Cycle Formalised • In a graph G, find a cycle that contains each vertex exactly once, except for the starting/ending vertex that appears twice.
A Solution not all edges used b a g f h p t i o q r s j c n e m k l d
5.1. Hamilton vs. Euler? • A Euler cycle visits each edge once. • A Hamiltonian cycle visits each vertex once. • They sound similar, but mathematicians have much harder problems with Hamiltonian cycles • e.g. it is easy to check for a Euler cycle, but there is no simple test for a Hamiltonian cycle
Algorithms for Finding Cycles • There are algorithms for finding a Euler cycle in a graph which take O(a) time • a is the number of edges in the graph • Algorithms for finding a Hamiltonian cycle are O(ea) or O(a!) in the worst case • much too slow for real graphs continued
For that reason, algorithms designed for real-world data only generate near-minimum length cycles • they are less time consuming, but may not give the best answer
5.2. Some Properties of Hamiltonian Cycles • 1) If a graph has N verticies, then the Hamiltonian cycle must use N edges. • e.g. s a t b w v d c u • 2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used).
5.3. 1) Proving there is no H.C (easy) • The graph has 5 verticies and 6 edges. v4 and v2 have degree 3. • Must include the two edges connected to v1 and v3. • This creates a loop, but without v5, so not a H.C. v1 v2 v5 v4 v3 continued
2) Proving there is no H.C (hard) b c a e d f i k g h j l m continued
Assume that the graph does have a H.C. • Edges (a,b), (a,g), (b,c), (c,k) must be in the H.C. since verticies ‘a’, ‘c’ have degree 2. • Therefore, edges (b,d), (b,f) must not be in the H.C. since ‘b’ is fully used already. continued
Therefore, edges (g,d), (d,e), (e,f), (f,k) must be in the H,C,. since that is the only way to have ‘d’ and ‘f’ in the H.C. • But there is now a cycle: • {a, b, c, k, f, e, d, g, a} • We cannot connect any more edges to g, e, k since their degree is 2 already • so it is not possible to create a H.C.
6. The Travelling Salesman Problem • This problem is related to the Hamiltonian cycle, but the graph is weighted • see the sheet metal hole drilling example • Given a weighted graph G, find a minimum length Hamiltonian cycle in G.
Example • Answer: {a,b,c,d,a} with minimum length 11. • Proof: try replacing any edge (e.g. (d,c) by either of the ‘long’ edges. b a 2 11 3 3 d 3 c 11
Why Travelling Salesman? • Think of the verticies as cities, edge weights as distances. • The problem becomes: find a shortest route in which a salesman (or woman) can visit each city once, starting and ending at the same city.
7. Depth First Search (DFS) • Depth First Search (DFS) is a graph searching method, useful for directed graphs • e.g. the Web • DFS uses recursion to explore all the successors of a node, and backtrackingto choice points. • Problem: cycles • Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked nodes.
DFS is “depth first” because it always fully explores down a path away from a vertex v before it backtracks to looks at other paths (other choices) leaving v.
Directed Graph Example a b d e f c Graph G
DFS Tree • Since nodes are marked, the graph is searched as if it were a tree: a/1 b/2 d/4 c/3 e/5 f/6 c
Running Time • The time taken to search from a node is proportional to the no. of successors of that node. • Total search time for all nodes = O(n).Total search time for all successors = time to search all edges = O(a). • Total running time is O(n + a) continued
If the graph is dense, a >> n, the O(n) term can be ignored • in that case, the total running time = O(a)
Uses of DFS • Finding cycles in a graph • e.g. for finding recursive dependencies in a calling graph • Reachability detection • i.e. can a vertex v be reached from vertex u? • useful for e-mail routing
8. Finding the Shortest Path • A weighted graph has values (weights)assigned to its edges. • The length of a path = the sum of the weights of the edges in the path • w(i,j) = weight of edge (i,j) • The shortest path between two verticies = the path having the minimum length.
8.1. Example Weighted Graph c b 2 2 4 1 3 2 z d e a 4 3 7 1 6 f 5 g Problem: find the shortest path from vertex a to vertex z.