Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Maths 11. Graph Theory Objective

Similar presentations


Presentation on theme: "Discrete Maths 11. Graph Theory Objective"— Presentation transcript:

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

2 1. Graph Terms (Strongly) Connected Component Vertices/Nodes Sub Graph
Complete Graph Directed Acyclic Graph (DAG) Euler/Hamiltonian Cycle Vertices/Nodes Edges Un/Weighted Un/Directed In/Out Degree Self-Loop/Multiple Edges Sparse/Dense Path, Cycle Isolated, Reachable Contest Algorithms: 6. Graph Intro

3 A graph has two parts (V, E), where: V are the nodes, called vertices
E are the links between vertices, called edges 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA

4 Directed graph the edges are directed e.g., bus cost network Undirected graph the edges are undirected e.g., road network

5 A weighted graph adds edge numbers.
8 b a 6 2 6 4 c 3 d 5 9 12 4 e

6 End vertices (or endpoints) of an edge
U and V are the endpoints Edges incident on a vertex a, d, and b are incident Adjacent vertices U and V are adjacent Degree of a vertex X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j

7 Path Simple path Examples sequence of alternating vertices and edges
begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct Examples P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y

8 Cycle Simple cycle Examples
circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices and edges are distinct Examples C1=(V,b,X,g,Y,f,W,c,U,a) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs

9 A graph is connected if there is a path between every pair of vertices
Connected graph Non connected graph with two connected components

10 Strong Connectivity Each vertex can reach all other vertices a g c d e
b e f g Graphs

11 Complete Graphs All pairs of vertices are connected by an edge.
No. of edges |E| = |V| (|V-1|)/2 = O(|V|2)

12 Directed Acyclic Graph (DAG)
A DAG has no cycles Some algorithms become simpler when used on DAGs instead of general graphs, based on the principle of topological ordering find shortest paths and longest paths by processing the vertices in a topological order Contest Algorithms

13 2. 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.

14 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.

15 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

16 3. Hamiltonian Cycle 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. continued

17 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

18 Graph of Hamilton’s Puzzle

19 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.

20 A Solution not all edges used b a g f h p t i o q s r j c n e m k l d

21 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

22 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

23 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

24 Some Properties of Hamiltonian Cycles
1) If a graph has N verticies, then the Hamiltonian cycle must use N edges. e.g. s a b t w v c d u 2) Every vertex in a Hamiltonian cycle has a degree of 2 (some edges may not be used).

25 Proving there is no H.C 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

26 Proving there is no H.C b c a e d f i k g h j l m continued

27 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

28 But there is now a cycle:
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.

29 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.

30 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

31 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.

32 5. 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 backtracking to choice points. Problem: cycles Solution: DFS ‘marks’ nodes as it visits them, and never revisits marked nodes.

33 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.

34 Directed Graph Example
b d e f c Graph G

35 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

36 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

37 If the graph is dense, a >> n, the O(n) term can be ignored
in that case, the total running time = O(a)

38 Uses of DFS Finding cycles in a graph Reachability detection
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

39 6. 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.

40 Example Weighted Graph
b 2 c 2 4 1 2 3 z d e a 4 3 7 1 6 f 5 g Problem: find the shortest path from vertex a to vertex z.

41 Dijkstra’s Shortest Path Algorithm
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

42 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!

43 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) // S(x) = Inf; // 3 T = all verticies; // 4 : continued

44 // find shortest path to z while (z in T) {
// find shortest path to z while (z in T) { // choose v from T with min S(v); // T = T without v; // for (each x in T adjacent to v) // S(x) = smaller_of( S(x), S(v)+w(v,x) ); // 9 } // 10 return S(z); // return shortest path }

45 Processing the Example
b 2 c 2 4 1 2 3 z d e a 4 3 7 1 6 f 5 g continued

46 Initialisation = temporary score Inf b 2 c Inf 2 4 1 2 3 Inf Inf z Inf
Inf Inf z Inf d e a 4 3 7 1 6 f 5 g Inf Inf continued

47 First Iteration = permanent score = changed temporary 2 b 2 c Inf 2 4
1 2 3 Inf Inf d e Inf z a 4 3 7 1 6 f 5 g Inf 1 continued

48 Second Iteration 2 b 2 c Inf 2 4 1 2 3 4 Inf d e Inf z a 4 3 7 1 6 f 5
4 Inf d e Inf z a 4 3 7 1 6 f 5 g 6 1 continued

49 Third Iteration b 2 c 4 2 4 1 2 3 4 6 z Inf d e a 4 3 7 1 6 f 5 g 6
4 6 z Inf d e a 4 3 7 1 6 f 5 g 6 1 continued

50 Fourth Iteration b 2 c 2 4 1 2 3 4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6
4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 1 Could have chosen ‘d’ instead. continued

51 Fifth Iteration b 2 c 2 4 1 2 3 z 5 d e 6 a 4 3 7 1 6 f 5 g 6
4 z 5 d e 6 a 4 3 7 1 6 f 5 g 6 1 Choosing ‘d’ is a waste of time. continued

52 Sixth (and final) Iteration
2 4 b 2 c 2 4 1 2 3 4 z 5 d e 6 a 4 3 7 1 6 Finished! Shortest path from ‘a’ to ‘z’ is length 5. f 5 g 6 1

53 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.

54 Execution Time (worst case)
The graph has n verticies. T is the set of verticies with temporary scores initially it contains all n verticies

55 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

56 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(n2) Total execution time of function: O(n) + O(n2) = O(n2), for large n

57 7. Further Information Discrete Mathematics and its Applications Kenneth H. Rosen McGraw Hill, 2007, 7th edition chapter 10, sections 10.1, 10.2, 10.5, 10.6


Download ppt "Discrete Maths 11. Graph Theory Objective"

Similar presentations


Ads by Google