Presentation is loading. Please wait.

Presentation is loading. Please wait.

241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms.

Similar presentations


Presentation on theme: "241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms."— Presentation transcript:

1 241-303 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). 241-303, Semester 1 2014-2015 5. Graph Theory

2 241-303 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

3 241-303 Discrete Maths: Graphs/5 3 7.Depth First Search 8.Finding the Shortest Path 9.Planar Graphs 10.More Information

4 241-303 Discrete Maths: Graphs/5 4 1. Introduction Muddy Gap Casper Douglas Gillettte Buffalo Sheridan Greybull Worland Shoshoni Lander Part of Wyoming’s (a USA State) Road System

5 241-303 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?

6 241-303 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

7 241-303 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.

8 241-303 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

9 241-303 Discrete Maths: Graphs/5 9 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?

10 241-303 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

11 241-303 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.

12 241-303 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.

13 241-303 Discrete Maths: Graphs/5 13 1.2. 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

14 241-303 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

15 241-303 Discrete Maths: Graphs/5 15 1.3. Parallel Edges, Loops, Isolation A graph with parallel edges, a loop, and an isolated vertex: v1 v3 v4 v2 e1 e2 e4 e3 continued

16 241-303 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.

17 241-303 Discrete Maths: Graphs/5 17 1.4. 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”.

18 241-303 Discrete Maths: Graphs/5 18 Sheet Metal Hole Drilling Problem: minimise the moving time of the drill over the metal sheet. continued

19 241-303 Discrete Maths: Graphs/5 19 A Weighted Graph Version Add edge numbers (weights) to indicate the movement time between any two holes. 12 9 4 4 3 5 6 8 6 a d e c b 2

20 241-303 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

21 241-303 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.

22 241-303 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)

23 241-303 Discrete Maths: Graphs/5 23 2. 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

24 241-303 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

25 241-303 Discrete Maths: Graphs/5 25 2.1. Konigsberg Bridge Problem continued The bridges are red.

26 241-303 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 1736. D A B C Briver

27 241-303 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

28 241-303 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.

29 241-303 Discrete Maths: Graphs/5 29 2.2. 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.

30 241-303 Discrete Maths: Graphs/5 30 2.3. 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.

31 241-303 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

32 241-303 Discrete Maths: Graphs/5 32 3. 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.

33 241-303 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

34 241-303 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

35 241-303 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.

36 241-303 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

37 241-303 Discrete Maths: Graphs/5 37 3.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

38 241-303 Discrete Maths: Graphs/5 38 3.2. 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).

39 241-303 Discrete Maths: Graphs/5 39 3.3. 1) 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

40 241-303 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

41 241-303 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

42 241-303 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

43 241-303 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.

44 241-303 Discrete Maths: Graphs/5 44 3.4. 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.

45 241-303 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 3 2 3 3 11

46 241-303 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.

47 241-303 Discrete Maths: Graphs/5 47 4. 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

48 241-303 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

49 241-303 Discrete Maths: Graphs/5 49 5. 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)

50 241-303 Discrete Maths: Graphs/5 50 5.1. 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

51 241-303 Discrete Maths: Graphs/5 51 5.2. Example ProgramNo. linesNo. returnsNo. calls 1 66 20 1 2 41 10 2 3 68 5 8 4 90 34 5 5 75 12 14

52 241-303 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)

53 241-303 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

54 241-303 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)

55 241-303 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

56 241-303 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.

57 241-303 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 v2 5.4. The Similarity Graph

58 241-303 Discrete Maths: Graphs/5 58 6. Implementing Graphs Adjacency matricies – –singular: matrix Adjacency lists

59 241-303 Discrete Maths: Graphs/5 59 6.1. An Adjacency Matrix ab c ed Graph 0 1 0 0 1 a b c d e 1 0 1 0 1 0 1 1 0 1 0 0 0 0 1 1 1 1 1 0 a b c d e Adjacency Matrix

60 241-303 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

61 241-303 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

62 241-303 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.

63 241-303 Discrete Maths: Graphs/5 63 Example ab c ed 0 1 0 1 0 a b c d e 1 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 a b c d e A =

64 241-303 Discrete Maths: Graphs/5 64 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 A 2 = 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 = 2 0 2 0 1 a b c d e 0 3 1 2 1 2 1 3 0 1 0 2 0 2 1 1 1 1 1 2 a b c d e

65 241-303 Discrete Maths: Graphs/5 65 Consider row a, column c in A 2 : ( 0 1 0 1 0 ) a bd c 0 1 0 1 1 b d = 0*0 + 1*1 + 0*0 + 1*1 + 0*1 = 2 Why it Works... continued a-b-c a-d-c

66 241-303 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

67 241-303 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

68 241-303 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

69 241-303 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

70 241-303 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.

71 241-303 Discrete Maths: Graphs/5 71 Representing Directed Graphs A directed graph: 01 3 2 4

72 241-303 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. 1 1 1 0 0 0 0 0 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 1 2 3 4 1 2 3 4 finish start

73 241-303 Discrete Maths: Graphs/5 73 6.2. Adjacency Lists Usually used for representing a directed graph. So, for the last example: 012 3 014 24 1 0 1 2 3 4 successors[] means NULL size of array = no. of nodes (n) no. of cells == no. of edges (a)

74 241-303 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.

75 241-303 Discrete Maths: Graphs/5 75 6.3. 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

76 241-303 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

77 241-303 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

78 241-303 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)

79 241-303 Discrete Maths: Graphs/5 79 6.3.1. 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)

80 241-303 Discrete Maths: Graphs/5 80 6.3.2. 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)

81 241-303 Discrete Maths: Graphs/5 81 6.3.3. 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

82 241-303 Discrete Maths: Graphs/5 82 6.3.4. 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.

83 241-303 Discrete Maths: Graphs/5 83 6.5. 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

84 241-303 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

85 241-303 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

86 241-303 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

87 241-303 Discrete Maths: Graphs/5 87 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. Problem: cycles Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked nodes.

88 241-303 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.

89 241-303 Discrete Maths: Graphs/5 89 7.1. Directed Graph Example a d fe c b Graph G

90 241-303 Discrete Maths: Graphs/5 90 7.2. 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];

91 241-303 Discrete Maths: Graphs/5 91 7.3. 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; } }

92 241-303 Discrete Maths: Graphs/5 92 7.4. 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

93 241-303 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

94 241-303 Discrete Maths: Graphs/5 94 7.5. 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

95 241-303 Discrete Maths: Graphs/5 95 7.6. 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

96 241-303 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)

97 241-303 Discrete Maths: Graphs/5 97 7.7. 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 continued

98 241-303 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

99 241-303 Discrete Maths: Graphs/5 99 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.

100 241-303 Discrete Maths: Graphs/5 100 8.1. Example Weighted Graph a b c z gf de 2 2 14 3 2 4 73 5 6 1 Problem: find the shortest path from vertex a to vertex z.

101 241-303 Discrete Maths: Graphs/5 101 8.2. 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

102 241-303 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!

103 241-303 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

104 241-303 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 }

105 241-303 Discrete Maths: Graphs/5 105 Edsger Wybe Dijkstra In 1997, aged 67

106 241-303 Discrete Maths: Graphs/5 106 8.3. Processing the Example a b c z gf de 2 2 14 3 2 4 73 5 6 1 continued

107 241-303 Discrete Maths: Graphs/5 107 Initialisation a b c z gf de 2 2 14 3 2 4 73 5 6 1 continued Inf = temporary score 0

108 241-303 Discrete Maths: Graphs/5 108 First Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 continued 2 Inf 1 0 = changed temporary = permanent score

109 241-303 Discrete Maths: Graphs/5 109 Second Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 continued 2 Inf 4 6 0 1

110 241-303 Discrete Maths: Graphs/5 110 Third Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 4 Inf 4 6 6 0 1 2 continued

111 241-303 Discrete Maths: Graphs/5 111 Fourth Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 5 6 6 0 1 24 4 Could have chosen ‘d’ instead. continued

112 241-303 Discrete Maths: Graphs/5 112 Fifth Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 5 6 6 0 1 24 4 Choosing ‘d’ is a waste of time. continued

113 241-303 Discrete Maths: Graphs/5 113 Sixth (and final) Iteration a b c z gf de 2 2 14 3 2 4 73 5 6 1 6 6 0 1 2 4 4 5 Finished! Shortest path from ‘a’ to ‘z’ is length 5.

114 241-303 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.

115 241-303 Discrete Maths: Graphs/5 115 8.4. 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

116 241-303 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

117 241-303 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?

118 241-303 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 ?

119 241-303 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

120 241-303 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

121 241-303 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

122 241-303 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.

123 241-303 Discrete Maths: Graphs/5 123 8.5. Execution Time (worst case) The graph has n verticies. T is the set of verticies with temporary scores – –initially it contains all n verticies

124 241-303 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

125 241-303 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

126 241-303 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

127 241-303 Discrete Maths: Graphs/5 127 Dijkstra’s algorithm has the same running time in the worst case, so it is optimal in that situation.

128 241-303 Discrete Maths: Graphs/5 128 9. 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?

129 241-303 Discrete Maths: Graphs/5 129 9.1. 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.

130 241-303 Discrete Maths: Graphs/5 130 9.2. A Connected Planar Graph 12 3 4 5 6 B C A D continued

131 241-303 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

132 241-303 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.

133 241-303 Discrete Maths: Graphs/5 133 9.3. 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

134 241-303 Discrete Maths: Graphs/5 134 9.4. Showing a Graph is not Planar Consider the cities example: ParisBerlinBonn Madrid RomeGeneva continued v Assume there are f faces, e edges.

135 241-303 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

136 241-303 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*(9 - 6 + 2) 18 >= 20,which is a contradiction So the graph is not planar.

137 241-303 Discrete Maths: Graphs/5 137 9.5. 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

138 241-303 Discrete Maths: Graphs/5 138 10. Further Information DM: Chapter 6 “Graph Theory”


Download ppt "241-303 Discrete Maths: Graphs/5 1 Discrete Maths Objective – –introduce graph theory (e.g. Euler and Hamiltonian cycles), and discuss some graph algorithms."

Similar presentations


Ads by Google