Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms (e.g. Dijkstra’s shortest path) , Semester Graph Theory
Discrete Maths: Graphs/5 2 Overview 1. Introduction 2. Cycles 3.Hamiltonian Cycles 4.Algorithms for Finding Cycles 5.Similarity Graphs 6.Implementing Graphs continued
Discrete Maths: Graphs/5 3 7.Depth First Search 8.Finding the Shortest Path 9.Planar Graphs 10.More Information
Discrete Maths: Graphs/ Introduction Muddy Gap Casper Douglas Gillettte Buffalo Sheridan Greybull Worland Shoshoni Lander Part of Wyoming’s (a USA State) Road System
Discrete Maths: Graphs/5 5 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?
Discrete Maths: Graphs/5 6 (Undirected) Graph Version Lan Sho Wor Mud CasDou Gil Buf She Gre e1 e2 e4 e3 e6e7 e9 e11 e12 e13 e10 e5 e8 continued
Discrete Maths: Graphs/5 7 Dots = vertices/nodes (singular: vertex) Lines = edges/arcs An undirected graph is one where the edges have no direction (no arrows) on them.
Discrete Maths: Graphs/5 8 Equivalent Graph Cas Sho Lan Mud Wor Gre She Buf Gil Dou e9 e6 e12e4e1 e2 e3 e7 e8 e5 e10 e13 e11
Discrete Maths: Graphs/ 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?
Discrete Maths: Graphs/5 10 Answer: No Consider Worland (Wor): the inspector must use every edge connected to Wor only once. Sho Wor Gre Buf e6 e4 e2 continued
Discrete Maths: Graphs/5 11 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.
Discrete Maths: Graphs/5 12 The General Theory If a graph G has a path from vertex v to v, which uses every edge exactly once, then an even number of edges must be connected to each vertex. More on this in section 2.
Discrete Maths: Graphs/ A Directed Graph A directed graph = vertices/nodes and arcs. An arc = a directed edge (one with an arrow). v5 v2 v1 v3 v6 v4 e2 e1 e3 e6 e5 e4 e7
Discrete Maths: Graphs/5 14 A Calling Graph A Calling Graph for a small program: main printList mergeSort mergesplit makeList 4 examples of direct recursion
Discrete Maths: Graphs/ Parallel Edges, Loops, Isolation A graph with parallel edges, a loop, and an isolated vertex: v1 v3 v4 v2 e1 e2 e4 e3 continued
Discrete Maths: Graphs/5 16 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.
Discrete Maths: Graphs/ 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”.
Discrete Maths: Graphs/5 18 Sheet Metal Hole Drilling Problem: minimise the moving time of the drill over the metal sheet. continued
Discrete Maths: Graphs/5 19 A Weighted Graph Version Add edge numbers (weights) to indicate the movement time between any two holes a d e c b 2
Discrete Maths: Graphs/5 20 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
Discrete Maths: Graphs/5 21 Listing all Possibilities PathLength a,b,c,d,e 21 a,b,d,c,e 28 a,c,b,d,e 24 a,c,d,b,e 26 a,d,b,c,e 27 a,d,c,b,e 22 The shortest length path.
Discrete Maths: Graphs/5 22 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)
Discrete Maths: Graphs/ Cycles In 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 cycle continued
Discrete Maths: Graphs/5 24 In an undirected graph, a simple cycle must have two or more different nodes. – –e.g. the cycle {Wor, Wor} is excluded 2 nodes
Discrete Maths: Graphs/ Konigsberg Bridge Problem continued The bridges are red.
Discrete Maths: Graphs/5 26 The Bridges from Above Problem: walk over each bridge exactly once; ending back at the starting point. A classic example of a general graph problem. Solved by Leonhard Euler in D A B C Briver
Discrete Maths: Graphs/5 27 Graph Model Find a cycle in the graph that includes all the edges and all the vertices – –but use each edge exactly once Called a Euler cycle. A C D B
Discrete Maths: Graphs/5 28 A Solution Use the same technique as in the Wyoming road inspection problem. Consider vertex A: since it has 3 edges connected to it, then the graph cannot have a Euler Cycle.
Discrete Maths: Graphs/ Euler Cycle Formalised A graph G has a Euler cycle when all the nodes are connected and every vertex has an even degree. The degree of a vertex v = number of edges connected to v.
Discrete Maths: Graphs/ Practical Uses 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.
Discrete Maths: Graphs/5 31 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 bc def g hi
Discrete Maths: Graphs/ 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. continued My version uses country names.
Discrete Maths: Graphs/5 33 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
Discrete Maths: Graphs/5 34 Graph of Hamilton’s Puzzle a b c d e f g h i j k l m n o p q r s t
Discrete Maths: Graphs/5 35 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.
Discrete Maths: Graphs/5 36 A Solution a b c d e f g h i j k l m n o p q r s t not all edges used
Discrete Maths: Graphs/ 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
Discrete Maths: Graphs/ Some Properties of Hamiltonian Cycles 1) If a graph has N verticies, then the Hamiltonian cycle must use N edges. – –e.g. a b cd s t u v w v 2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used).
Discrete Maths: Graphs/ ) Proving there is no H.C (easy) The graph has 5 verticies and 6 edges. v4 and v2 have degree 3. Start creating the cycle without using the edges (v4, v1) and (v2,v3). v1 v2 v3 v4 v5 continued
Discrete Maths: Graphs/5 40 But now there are only 4 edges -- not enough for a H.C involving 5 verticies. v1 v2 v3 v4 v5
Discrete Maths: Graphs/5 41 2) Proving there is no H.C (hard) a b c k m g h j l d e f i continued
Discrete Maths: Graphs/5 42 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
Discrete Maths: Graphs/5 43 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.
Discrete Maths: Graphs/ 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.
Discrete Maths: Graphs/5 45 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. a b c d
Discrete Maths: Graphs/5 46 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.
Discrete Maths: Graphs/ 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(e a ) or O(a!) in the worst case – –much too slow for real graphs continued
Discrete Maths: Graphs/5 48 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
Discrete Maths: Graphs/ Similarity Graphs A very common coding requirement is to collect ‘similar’ things into groups based on their properties. e.g.: – –text recognition (group characters) – –weather forecasting (find clouds/rainfall) – –program analysis (find similar programs)
Discrete Maths: Graphs/ Grouping ‘Similar’ Programs Similar student programs are often collected by teachers looking for students who have worked together illegally. In our example, we examine the properties: – –no. of lines in the program – –no. of return statements – –no. of function calls
Discrete Maths: Graphs/ Example ProgramNo. linesNo. returnsNo. calls
Discrete Maths: Graphs/5 52 A vertex v is made from three properties, and so can be viewed as a kind of 3D coordinate (p1,p2,p3). p1 p2 p3 v (10,15,30)
Discrete Maths: Graphs/5 53 Define a dissimilarity function s() between two verticies v = (v1, v2, v3) and w = (w1, w2, w3): s(v, w) = |v1-w1| + |v2-w2| + |v3-w3| v and w are similar if s(v,w) is close to 0. – –because v and w are closer together in space 5.3. Dissimilarity Function
Discrete Maths: Graphs/5 54 Using v1, v2, v3, v4, v5 for the five programs from the table: v1 = (66, 20, 1) v2 = (41, 10, 2) v3 = (68, 5, 8) v4 = (90, 34, 5) v5 = (75, 12, 14)
Discrete Maths: Graphs/5 55 Dissimilarity values: s(v1,v2) = 36s(v1,v3) = 24 s(v1,v4) = 42s(v1,v5) = 30 s(v2,v3) = 38s(v2,v4) = 76 s(v2,v5) = 48s(v3,v4) = 54 s(v3,v5) = 20s(v4,v5) = 46
Discrete Maths: Graphs/5 56 We place an edge between two verticies v and w only if s(v,w) < S We decide on S -- the smaller it is, the ‘closer’ v and w have to be.
Discrete Maths: Graphs/5 57 Let s(v,w) < 25 for v1, v2,...v5 The resulting edges collect the programs into 3 groups: – –{1,3,5}, {2}, and {4} Conclusion: the students who wrote programs 1, 3, and 5 may have worked together. – –and was student no.3 the boss? v1 v3 v5 v4 v The Similarity Graph
Discrete Maths: Graphs/ Implementing Graphs Adjacency matricies – –singular: matrix Adjacency lists
Discrete Maths: Graphs/ An Adjacency Matrix ab c ed Graph a b c d e a b c d e Adjacency Matrix
Discrete Maths: Graphs/5 60 Properties The degree of a vertex v (of a simple graph) = sum of row v or sum of column v – –e.g. a has degree 2 since it is connected to b and e An adjacency matrix can represent loops – –e.g. vertex c on the previous slide continued
Discrete Maths: Graphs/5 61 An adjacency matrix cannot represent parallel edges, but that is possible if nonnegative integers are allowed as matrix entries – –ij th entry = no. of edges between vertex i and j Not an efficient data structure since information is duplicated around the main diagonal – –most data appears twice
Discrete Maths: Graphs/5 62 The Number of Paths If an adjacency matrix A is multiplied by itself repeatedly: – –A, A 2, A 3,..., A n Then the ij th entry in matrix A n is equal to the number of paths from i to j of length n.
Discrete Maths: Graphs/5 63 Example ab c ed a b c d e a b c d e A =
Discrete Maths: Graphs/ A 2 = = a b c d e a b c d e
Discrete Maths: Graphs/5 65 Consider row a, column c in A 2 : ( ) a bd c b d = 0*0 + 1*1 + 0*0 + 1*1 + 0*1 = 2 Why it Works... continued a-b-c a-d-c
Discrete Maths: Graphs/5 66 A non-zero product means there is at least one vertex connecting a and c. The sum is 2 because of: – –(a, b, c)and (a, d, c) – –2 paths of length two
Discrete Maths: Graphs/5 67 The Degree of Verticies The entries on the main diagonal of A 2 give the degrees of the verticies (when A is a simple graph). Consider vertex c: – –degree of c = 3 since it is connected to the edges (c,b), (c,d), and (c,e). continued
Discrete Maths: Graphs/5 68 In A 2 these become paths of length 2: – –(c,b,c), (c,d,c), and (c,e,c) So the number of paths of length 2 for c = the degree of c – –this is true for all verticies
Discrete Maths: Graphs/5 69 Coding Adjacency Matricies #define NUMNODESn int arcs[NUMNODES][NUMNODES]; arcs[u][v] == 1 if there is an edge (u,v); 0 otherwise The implementation may also need a way to map node names (strings) to array indicies. continued
Discrete Maths: Graphs/5 70 If n is large then the array will be very large, with almost half of it being unnecessary. If the nodes are lightly connected then most of the array will contain 0’s, which is a further waste of memory.
Discrete Maths: Graphs/5 71 Representing Directed Graphs A directed graph:
Discrete Maths: Graphs/5 72 Its Adjacency Matrix Not symmetric; all the array may be necessary. Still a waste of space if nodes are lightly connected finish start
Discrete Maths: Graphs/ Adjacency Lists Usually used for representing a directed graph. So, for the last example: successors[] means NULL size of array = no. of nodes (n) no. of cells == no. of edges (a)
Discrete Maths: Graphs/5 74 Data Structures struct cell { /* for a linked list */ Node nodeName; struct cell *next; }; struct cell *successors[NUMNODES]; successors[u] points to a linked list of cells which give the names of the nodes connected to u.
Discrete Maths: Graphs/ Matricies or Lists? Which representation is better for graphs? The answer varies, depending on the operations that will be applied to the graph. We will consider three operations: – –is there an edge between u and v? – –find the successors of u (in a directed graph) – –find the predecessors of u (in a directed graph) continued
Discrete Maths: Graphs/5 76 The answer also depends on the number of edges (a) between the n nodes – –a sparse graph has few edges (a << n) – –a dense graph has many edges(a >> n) continued
Discrete Maths: Graphs/5 77 In the most dense graph, a graph of n nodes will have n(n-1)/2 edges. In that case, for large n, a = O(n 2 ) nodes = 5 edges = (5*4)/2 = 10
Discrete Maths: Graphs/5 78 Proof that a graph of n nodes has n(n-1)/2 edges. Write as S(n) = n(n-1)/2 Basis. S(2) = 1. True. Inductive Case. – –assume S(n) = n(n-1)/2(1) – –try to show S(n+1) = (n+1)n/2(2) – –we know: S(n+1) = S(n) + n which is – –S(n+1) = n(n-1)/2 + n which is – –S(n+1) = (n+1)n/2which is (2)
Discrete Maths: Graphs/ Is there an edge (u,v)? Adjacency matrix: O(1) to read arcs[u][v] Adjacency list: O(1 + a/n) – –O(1) to get to successors[u] – –length of linked list is on average a/n – –if a sparse graph (a O(1) – –if a dense graph (a~=n 2 ): O(1+ a/n) => O(n)
Discrete Maths: Graphs/ Find u’s successors (u->v) Adjacency matrix: O(n) since must examine the entire row for vertex u Adjacency list: O(1+(a/n)) since must look at entire list pointed to by successors[u] – –if a sparse graph (a O(1) – –if a dense graph (a~=n 2 ): O(1+a/n) => O(n)
Discrete Maths: Graphs/ Find u’s predecessors (t->u) Adjacency matrix: O(n) since must examine the entire column for vertex u – –a 1 in the row for ‘t’ means that ‘t’ is a predecessor Adjacency list: O(a) since must examine every list pointed to by successors[] – –if a sparse graph (a<<n): O(a) is fast – –if a dense graph (a~=n 2 ): O(a) is slow
Discrete Maths: Graphs/ Summary: which is faster? OperationDense GraphSparse Graph Find edgeAdj. MatrixEither Find succ.EitherAdj. list Find pred.Adj. MatrixEither As a graph gets denser, an adjacency matrix has better execution time than an adjacency list.
Discrete Maths: Graphs/ Why bother with an Adj. List? An adjacency list might use less storage space than an adjacency matrix for the same graph. The size of an adjacency matrix for a graph of n nodes is: – –n 2 bits (assuming 0 and 1 are stored as bits) continued
Discrete Maths: Graphs/5 84 An adjacency list cell uses: – –32 bits for the integer, 32 bits for the pointer – –so, cell size = 64 bits Total no. of cells = total no. of edges, a – –so, total size of lists = 64a bits successors[] has n entries (for n nodes) – –so, array size is 32n bits Total size of an adjacency list data struct: 64a + 32n
Discrete Maths: Graphs/5 85 Size Comparison An adjacency list will use less storage than an adjacency matrix when: 64a + 32n < n 2 – –which is: a < n 2 /64 - n/2 When n is large, ignore the n/2 term: a < n 2 /64 continued
Discrete Maths: Graphs/5 86 n 2 is (roughly) the maximum number of edges. So if the actual number of edges in a graph is 1/64 of the maximum number of edges, then an adj. list representation will be smaller than an adj. matrix coding – –but the graph must be quite sparse
Discrete Maths: Graphs/ 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. Problem: cycles Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked nodes.
Discrete Maths: Graphs/5 88 DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v.
Discrete Maths: Graphs/ Directed Graph Example a d fe c b Graph G
Discrete Maths: Graphs/ Data Structures enum MARKTYPE {VISITED, UNVISITED}; struct cell { /* adj. list */ NODE nodeName; struct cell *next; }; typedef struct cell *LIST; struct graph { enum MARKTYPE mark; LIST successors; }; typedef struct graph GRAPH[NUMNODES];
Discrete Maths: Graphs/ The dfs() Function void dfs(NODE u, GRAPH G) // recursively search G, starting from u { LIST p; // runs down adj. list of u NODE v; // node in cell that p points at G[u].mark = VISITED; // visited u p = G[u].successors; while (p != NULL) { // visit u’s succ’s v = p->nodeName; if (G[v].mark == UNVISITED) dfs(v, G); // visit v p = p->next; } }
Discrete Maths: Graphs/ Calling dfs(a,G) CallVisited d(a){a} d(a)-d(b){a,b} d(a)-d(b)-d(c){a,b,c} Skip b, return to d(b) d(a)-d(b)-d(d){a,b,c,d} Skip c d(a)-d(b)-d(d)-d(e){a,b,c,d,e} Skip c, return to d(d) continued call it d(a) for short
Discrete Maths: Graphs/5 93 d(a)-d(b)-d(d)-d(f){a,b,c,d,e,f} Skip c, return to d(d) d(a)-d(b)-d(d){a,b,c,d,e,f} Return to d(b) d(a)-d(b){a,b,c,d,e,f} Return to d(a) d(a){a,b,c,d,e,f} Skip d, return
Discrete Maths: Graphs/ DFS Tree Since nodes are marked, the graph is searched as if it were a tree: d/4 f/6e/5 c b/2 a/1 c/3
Discrete Maths: Graphs/ dfs() 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
Discrete Maths: Graphs/5 96 If the graph is dense, a >> n, the O(n) term can be ignored – –in that case, the total running time = O(a)
Discrete Maths: Graphs/ 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 routing continued
Discrete Maths: Graphs/5 98 Finding a topological order for an acyclic graph – –e.g. used to order tasks where u->v means do task u then task v a b c d e f a b/3 c/5 d/6 e/4 f topological order /1 /2
Discrete Maths: Graphs/ 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.
Discrete Maths: Graphs/ Example Weighted Graph a b c z gf de Problem: find the shortest path from vertex a to vertex z.
Discrete Maths: Graphs/ Dijkstra’s Shortest Path Algorithm Due to Edsger W. Dijkstra (1959, when 29). Assign scores to verticies: – –S(v) = score of vertex v (some integer) – –there are temporary and permanent scores – –all verticies start with a temporary score of infinity (Inf) continued
Discrete Maths: Graphs/5 102 The algorithm uses a set T, which contains all the nodes with temporary scores – –initially 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 v – –this is what we want!
Discrete Maths: Graphs/5 103 Algorithm (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 : continued
Discrete Maths: Graphs/5 104 // 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 }
Discrete Maths: Graphs/5 105 Edsger Wybe Dijkstra In 1997, aged 67
Discrete Maths: Graphs/ Processing the Example a b c z gf de continued
Discrete Maths: Graphs/5 107 Initialisation a b c z gf de continued Inf = temporary score 0
Discrete Maths: Graphs/5 108 First Iteration a b c z gf de continued 2 Inf 1 0 = changed temporary = permanent score
Discrete Maths: Graphs/5 109 Second Iteration a b c z gf de continued 2 Inf
Discrete Maths: Graphs/5 110 Third Iteration a b c z gf de Inf continued
Discrete Maths: Graphs/5 111 Fourth Iteration a b c z gf de Could have chosen ‘d’ instead. continued
Discrete Maths: Graphs/5 112 Fifth Iteration a b c z gf de Choosing ‘d’ is a waste of time. continued
Discrete Maths: Graphs/5 113 Sixth (and final) Iteration a b c z gf de Finished! Shortest path from ‘a’ to ‘z’ is length 5.
Discrete Maths: Graphs/5 114 Notes On 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.
Discrete Maths: Graphs/ Proof of the Algorithm Inductive statement T(i): – –on the ith iteration of the loop on lines 5-10 of the algorithm, the score S(v) for vertex v is made permanent – –s(v) is the minimum of the temp scores – –that score, S(v), is the shortest path from vertex a to vertex v continued
Discrete Maths: Graphs/5 116 Basis (i = 1). – –initially S(a) = 0, and all other S()’s = Inf – –so vertex a will be chosen – –S(a) is the shortest path from vertex a to a – –T(1) is true. continued
Discrete Maths: Graphs/5 117 Induction Case. – –Assume that T(k) is true for all k < i. – –On the k+1 iteration, select node v. Is S(v) the shortest path from a to v?
Discrete Maths: Graphs/5 118 Diagram of Induction Case Permanent Scores Temporary Scores a w u vChoose v. Is s(v) the shortest path from a to v ?
Discrete Maths: Graphs/5 119 Proof by Contradiction Assume that S(v) is not the shortest path from a to v. That means there must be another path: a ->... -> w -> u ->... -> v which is shorter. continued something not in permanent
Discrete Maths: Graphs/5 120 This means that: path_length(a ->... -> w -> u ->... -> v)... -> v) which means that: path_length(a ->... -> w -> u)... -> v) which means that: S(u) < S(v) continued
Discrete Maths: Graphs/5 121 But, if S(u) < S(v) then u should have been selected instead of v on the k+1 th iteration. Since v was chosen, it means that: S(u) >= S(v), for all u So S(v) is the shortest path. continued
Discrete Maths: Graphs/5 122 The induction case was: – –On the k+1 iteration, select node v. Is S(v) the shortest path from a to v? – –The answer is yes. – –So, T(k+1) is true. Since the basis and induction cases are true, T(i) is true for all i.
Discrete Maths: Graphs/ Execution Time (worst case) The graph has n verticies. T is the set of verticies with temporary scores – –initially it contains all n verticies
Discrete Maths: Graphs/5 124 Consider the Algorithm The loop on lines 2-3 is executed n-1 times – –it 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 time continued
Discrete Maths: Graphs/5 125 The 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(n 2 ) Total execution time of function: – –O(n) + O(n 2 ) = O(n 2 ), for large n
Discrete Maths: Graphs/5 126 Is Dijkstra’s Algorithm Optimal? Optimal = the fastest running time (for the given assumptions). In the worst case, a graph with n nodes has n(n-1)/2 edges. Finding the shortest path for a node in the worst case requires looking at all the edges: – –O( n(n-1)/2 )= O(n 2 ) continued
Discrete Maths: Graphs/5 127 Dijkstra’s algorithm has the same running time in the worst case, so it is optimal in that situation.
Discrete Maths: Graphs/ Planar Graphs ParisBerlinBonn Madrid RomeGeneva Can the cities be connected by roads without using bridges (or intersections)? – –i.e. the roads should not cross Is this graph planar?
Discrete Maths: Graphs/ Definition A graph is planar if it can be drawn in the plane (e.g. on paper, on a computer screen) without its edges crossing.
Discrete Maths: Graphs/ A Connected Planar Graph B C A D continued
Discrete Maths: Graphs/5 131 A connected planar graph divides the plane (the paper) into distinct regions called faces. A face is defined by a cycle in the graph’s nodes. continued
Discrete Maths: Graphs/5 132 The example has 4 faces: A, B, C, and D – –FaceCycle A(5,2,3,4,5) B(1,5,4,6,1) C(1,2,5,1) D(1,2,3,4,6,1) D is the outer face.
Discrete Maths: Graphs/ Euler’s Formula Euler (1752) proved that for any connected graph, where: f = no. of faces e = no. of edges v = no. of verticies/nodes then the formula holds: f = e - v + 2
Discrete Maths: Graphs/ Showing a Graph is not Planar Consider the cities example: ParisBerlinBonn Madrid RomeGeneva continued v Assume there are f faces, e edges.
Discrete Maths: Graphs/5 135 Each cycle uses at least 4 edges, so a face is bounded by at least 4 lines. – –total no. of lines used for all faces >= 4f (1) A line is an edge used at most twice, so: – –total no. of lines for all faces <= 2e(2) Combining (1) and (2): 2e >= 4f(3) continued
Discrete Maths: Graphs/5 136 Replace the f value in (3) with Euler’s formula (assuming the graph is planar): 2e >= 4(e - v + 2) From the graph, we see that e = 9 and v = 6. So: 2*9 >= 4*( ) 18 >= 20,which is a contradiction So the graph is not planar.
Discrete Maths: Graphs/ Uses of Planarity For drawing nice graphics – –display diagrams without crossing lines Integrated circuit (IC) design: – –ICs can have several layers, but on a given layer the circuit cannot have components that cross over each other
Discrete Maths: Graphs/ Further Information DM: Chapter 6 “Graph Theory”