discrete maths objective introduce graph theory (e.g. euler and hamiltonian cycles), and discuss...

67
Discrete Maths Objective introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path). 242-213, Semester 2, 2014-2015 11. Graph Theory 1

Upload: gilbert-booth

Post on 27-Dec-2015

228 views

Category:

Documents


2 download

TRANSCRIPT

Discrete Maths

Discrete MathsObjectiveintroduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstras shortest path).242-213, Semester 2, 2014-201511. Graph Theory1OverviewIntroductionDirected GraphsOther Graph TerminologySheet Metal Hole DrillingHamiltonian CyclesThe Travelling Salesman ProblemDepth First SearchFinding the Shortest PathMore Information

21. IntroductionMuddy GapCasperDouglasGillettteBuffaloSheridanGreybullWorlandShoshoniLanderPart of Wyomings (a USA State) Road System3ProblemThe Wyoming Road Inspector lives in Greybull, and must check every road.

He must check the roads as quickly as possibleby travelling each road only oncestarting from Greybull and returning there

Is this travel plan possible?4(Undirected) Graph VersionLanShoWorMudCasDouGilBufSheGree1e2e4e3e6e7e9e11e12e13e10e5e8continued5Dots = vertices/nodes (singular: vertex)Lines = edges/arcsAn undirected graph is one where the edges have no direction (no arrows) on them.

6Equivalent GraphCasShoLanMudWorGreSheBufGilDoue9e6e12e4e1e2e3e7e8e5e10e13e1171.1. PathsA path is a sequence of edges between two verticiese.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?81.2. CyclesIn a directed graph, a cycle is 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 cyclecontinued9In an undirected graph, a simple cycle must have two or more different nodes.e.g. the cycle {Wor, Wor} is excluded2 nodes10Road Inspector Problem again:is there a simple cycle from Gre (Greybull) to Gre which uses all the edges only once?

Answer: NoConsider Worland (Wor): the inspector must use every edge connected to Wor only once.ShoWorGreBufe6e4e2continued11But 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...12The Euler CycleIf 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.13

Leonhard Euler1707 - 17831.3. Practical Uses of Euler Cycles In computer networks, edge traversal (i.e. moving between network nodes) is expensive.

Eulers definition is a very fast algorithm for checking whether a graph (network) can be traversed efficiently.14ExampleCan 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?abcdefghi152. Directed GraphsA directed graph = vertices/nodes and arcs.An arc = a directed edge (one with an arrow).v5v2v1v3v6v4e2e1e3e6e5e4e716A Calling GraphA Calling Graph for a small program:mainprintListmergeSortmergesplitmakeList4 examples ofdirect recursion173. Other Graph TerminologyA graph with parallel edges, a loop, and an isolated vertex:v1v3v4v2e1e2e4e3continued18Parallel 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.19Path LengthThe 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.204. Sheet Metal Hole DrillingProblem: minimise the moving time of the drill over the metal sheet.continued21

A Weighted Graph VersionAdd edge numbers (weights) to indicate the movement time between any two holes.1294435686adecb222Simplified ProblemAssume 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 paths edges23Listing all PossibilitiesPathLengtha,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 22The shortestlength path.24Is 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)255. Hamiltonian CyclesSir William Rowan Hamiltons puzzle (1850s)it made him very richEach corner is labelled with a city name.The shape is a dodecahedron.

continuedMy versionuses countrynames.26

William Rowan Hamilton(1805 1865)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 used27Graph of Hamiltons Puzzleabcdefghijklmnopqrst28Hamiltonian Cycle FormalisedIn a graph G, find a cycle that contains each vertex exactly once, except for the starting/ending vertex that appears twice.29A Solutionabcdefghijklmnopqrstnot all edges used305.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 cyclese.g. it is easy to check for a Euler cycle, but there is no simple test for a Hamiltonian cycle 31Algorithms for Finding CyclesThere are algorithms for finding a Euler cycle in a graph which take O(a) timea is the number of edges in the graph

Algorithms for finding a Hamiltonian cycle are O(ea) or O(a!) in the worst casemuch too slow for real graphscontinued32For that reason, algorithms designed for real-world data only generate near-minimum length cyclesthey are less time consuming, but may not give the best answer

335.2. Some Properties of Hamiltonian Cycles1) If a graph has N verticies, then the Hamiltonian cycle must use N edges.e.g.abcdstuvw2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used).345.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.v1v2v3v4v5continued352) Proving there is no H.C (hard)abckmghjldeficontinued36Assume 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.continued37Therefore, 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 alreadyso it is not possible to create a H.C.386. The Travelling Salesman ProblemThis problem is related to the Hamiltonian cycle, but the graph is weightedsee the sheet metal hole drilling example

Given a weighted graph G, find a minimum length Hamiltonian cycle in G.39ExampleAnswer: {a,b,c,d,a} with minimum length 11.

Proof: try replacing any edge (e.g. (d,c) by either of the long edges.abcd3233111140Why 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.417. Depth First Search (DFS)Depth First Search (DFS) is a graph searching method, useful for directed graphse.g. the WebDFS uses recursion to explore all the successors of a node, and backtracking to choice points.

Problem: cyclesSolution: DFS marks nodes as it visits them, and never revisits marked nodes.

42DFS 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.43Directed Graph ExampleadfecbGraph G44DFS TreeSince nodes are marked, the graph is searched as if it were a tree:d/4f/6e/5cb/2a/1c/345Running TimeThe 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)continued46If the graph is dense, a >> n, the O(n) term can be ignoredin that case, the total running time = O(a)47Uses of DFSFinding cycles in a graphe.g. for finding recursive dependencies in a calling graph

Reachability detectioni.e. can a vertex v be reached from vertex u?useful for e-mail routing

488. Finding the Shortest PathA weighted graph has values (weights)assigned to its edges.

The length of a path = the sum of the weights of the edges in the pathw(i,j) = weight of edge (i,j)

The shortest path between two verticies = the path having the minimum length.498.1. Example Weighted Graphabczgfde221432473561Problem: find the shortest path from vertex a to vertex z.508.2. Dijkstras Shortest Path AlgorithmAssign scores to verticies:S(v) = score of vertex v (some integer)there are temporary and permanent scoresall verticies start with a temporary score of infinity (Inf)continued51

Edsger Wybe Dijkstra(1930 2002)The algorithm uses a set T, which contains all the nodes with temporary scoresinitially that is all n nodes

When the score of a vertex v is made permanent, it is also the length of the shortest path from vertex a to vertex vthis is what we want!52Algorithm (as C-like pseudocode)int dijkstra(vertex a, vertex z)// find the shortest path from a to z{ // initialisation S(a) = 0; // 1 for (all verticies x != a) // 2 S(x) = Inf; // 3

T = all verticies; // 4:continued53 // find shortest path to z while (z in T) {// 5 choose v from T with min S(v);// 6 T = T without v;// 7 for (each x in T adjacent to v)// 8 S(x) = smaller_of( S(x), S(v)+w(v,x) );// 9 }// 10 return S(z); // return shortest path}548.3. Processing the Exampleabczgfde221432473561continued55Initialisationabczgfde221432473561continuedInfInfInfInfInfInfInf= temporary score056First Iterationabczgfde221432473561continued2InfInfInfInfInf10= changed temporary= permanent score57Second Iterationabczgfde221432473561continued2InfInf4Inf60158Third Iterationabczgfde2214324735614Inf466012continued59Fourth Iterationabczgfde22143247356156601244Could have chosend instead.continued60Fifth Iterationabczgfde22143247356156601244Choosing d is awaste of time.continued61Sixth (and final) Iterationabczgfde22143247356166012445Finished!Shortest pathfrom a toz is length 5.62NotesOn each iteration:one score becomes permanent

the vertex with the new permanent score changes its adjacent verticies temporary scores if they can be made smaller

The algorithm eventually reaches every vertex.638.4. Execution Time (worst case)The graph has n verticies.

T is the set of verticies with temporary scoresinitially it contains all n verticies64Consider the AlgorithmThe loop on lines 2-3 is executed n-1 timesit has O(n) execution time

The loop on lines 5-10 is executed n times.

The choose statement on line 6 will require a search through all of T in the worst case:so it has O(n) execution timecontinued65The loop on lines 8-9 looks through all of T:O(n) execution time

Total execution time of loop on lines 5-10:O(n * (n+n))= O(n2)

Total execution time of function:O(n) + O(n2)= O(n2), for large n669. Further InformationDiscrete Mathematics and its ApplicationsKenneth H. RosenMcGraw Hill, 2007, 7th editionchapter 10, sections 10.1, 10.2, 10.5, 10.667