Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3

Similar presentations


Presentation on theme: "1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3"— Presentation transcript:

1 1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3 http://www.comp.nus.edu.sg/~cs1231y/lnotes/L13.ppt

2 2 A. Circuits 1.Definitions and examples of Closed Walk, Circuit, Simple Circuit 2.A special kind of circuit – Euler circuit 3.A special kind of simple circuit – Hamiltonian circuit

3 3 1. Closed Walk, Circuit, Simple Circuit n Definition: Given a graph G=(V,E), a closed walk is a walk that starts and ends at the same vertex: i.e. it is a sequence v 0 e 1 v 1 e 2 v 2 … e n-1 v n-1 e n v 0 where n  0, v i  V, e i ={v i-1,v i }  E (i  1..n-1) When obvious, we shorten it the closed walk to: v 0 v 1 v 2 … v n-1 v 0 n If all the edges in the closed walk are distinct, then the closed walk is a circuit. n If all the vertices in the closed walk are distinct (with exception of the first and last) then the closed walk is a simple circuit. n The length of the closed walk / circuit / simple circuit is the number of edges in the closed walk / circuit / simple circuit.

4 4 1. Closed Walk, Circuit, Simple Circuit Closed Walk Circuit (edges distinct) Simple Circuit (vertices distinct)

5 5 1. Closed Walk, Circuit, Simple Circuit 1 5 3 2 6 4 1 2 3 6 4 2No 5 3 6 5 1 3 5 YesNo 2 1 3 5 6 3 2 Yes No 6 5 1 2 3 6 Yes Closed Walk Circuit Simple Circuit Length of closed walk 6 6 5

6 6 A. Circuits 1.Definitions (Closed Walk, Circuit, Simple Circuit) and Examples 2.A special kind of circuit 2.1 Definition of Euler Circuit 2.2 Euler Circuit Theorem and Proof 2.3 Examples 2.4 (Related) Definition of Euler Path 2.5 Euler Path Theorem 2.6 Example 3.A special kind of simple circuit 3.1 Definition of Hamiltonian Circuit 3.2 Examples 3.3 Special Examples 3.4 (Related) Definition of Hamiltonian Path 3.5 Examples

7 7 2. Euler Circuit Leonhard Euler (pronounced OIL-er) 1707-1783, Swiss Mathematician “Is it possible for a person to take a walk around the town of Königsberg, starting and ending at the same location and crossing each of the seven bridges exactly once?”

8 8 2.1 Euler Circuit (Definition) n Definition: Let G=(V,E) be a graph. An EULER CIRCUIT for G is a circuit that contains every vertex in V and every edge in E. n Implications: –‘Circuit’: edges must be distinct… At most 1 traversal of each edge. –‘every edge in E’: … At least 1 traversal of each edge. –‘every vertex in V’: … At least 1 traversal of each vertex.

9 9 2.2 Euler Circuit Theorem n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. n Proof Strategy: –(  )Assume: G has an Euler Circuit Prove:(1) G is connected; (2)  v  V, deg(v) = 2k (for some k) –(  ) Assume: G is connected and  v  V, deg(v) = 2k (for some k) Prove: G has an Euler Circuit

10 10 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) To Prove: G is connected. (Defn of connected?) n Since G has an Euler Circuit, then let v 0 e 1 v 1 e 2 v 2 … e n-1 v n-1 e n v 0 …be the Euler Circuit. n Pick any 2 vertices x, y in V. n Now, x must be some v i and y must be some v j in the Euler circuit. ( Why? Since by definition, “An Euler circuit for G is a circuit that contains EVERY VERTEX in V…” ) If i  j then we have a walk from x = v i e i+1 v i+1 … e j v j = y If i  j then we have a walk from x = v i e i v i-1 … e j+1 v j = y n Therefore there is a walk from x to y in V. n Therefore G is connected (by definition of connected graph).

11 11 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) Before we prove (2)… Note that any vertex in a circuit can be the starting vertex 1 5 3 2 6 4 1 2 3 5 6 3 1 2 3 5 6 3 1 2 3 5 6 3 1 2 3 5 6 3 1 2 3 5 6 3 1 2 3 5 6 3 1 2 3 5 6 3

12 12 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) So in general, all these refer to the same circuit: v 0 e 1 v 1 e 2 v 2 … e n-1 v n-1 e n v 0 v 1 e 2 v 2 … e n-1 v n-1 e n v 0 e 1 v 1 ….. v n-1 e n v 0 e 1 v 1 e 2 v 2 … e n-1 v n-1 It just depends on which vertex you want to take as the starting vertex.

13 13 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) n Now, we prove (2)… n To show: that any vertex has an even degree. n Pick any vertex v in V. n Since G has an Euler circuit, then that circuit must pass through v (by defn of Euler circuit “… circuit that contains EVERY VERTEX in V…”) n Look at the circuit from the viewpoint of v as the starting vertex. n Each time v is exited through the use of one edge, it must be returned to by the use of another edge.

14 14 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) v Each time v is exited through the use of one edge, it must be returned to by the use of another edge. If v has some more edges incident on it, the process repeats again (Why? Because Euler Circuit traverses every edge in E).

15 15 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) v Each time v is exited through the use of one edge, it must be returned to by the use of another edge. If v has some more edges incident on it, the process repeats again (Why? Because Euler Circuit traverses every edge in E).

16 16 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume G=(V,E) has an Euler circuit. Show (1) G is connected, (2) (  v  V, deg(v) = 2k) v Each time v is exited through the use of one edge, it must be returned to by the use of another edge. Edges on v occur in exit / entry pairs! Degree of v must be a multiple of 2 => EVEN!!!

17 17 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. Proof: The proof that there is (  ) an Euler circuit, will be given by an ALGORITHM for finding the circuit. n We first present the algorithm. n We next show that the algorithm is correct. n (1) It will ALWAYS terminate; and n (2) If it terminates, it will ALWAYS find the circuit.

18 18 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. Step 1: Pick any vertex v  V as the starting vertex. n Step 2: Pick any sequence of adjacent vertices and edges starting and ending at v, and never repeating an edge. Let this circuit be C. C: v 1 2 3 v v 1 2 3 Proof: (The algorithm)

19 19 n Step 3: Does C contain every edge and vertex of G? n If ‘Yes’, then stop and output C, which is an Euler circuit. n If ‘No’… 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. v 1 2 3 6 4 5 C: v 1 2 3 v Proof: (The algorithm)

20 20 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3a: Remove the circuit C from G creating G’. v 1 2 3 4 5 6 C: v 1 2 3 v Proof: (The algorithm) G

21 21 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3a: Remove the circuit C from G creating G’. 1 3 4 5 6 C: v 1 2 3 v Proof: (The algorithm) G’

22 22 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3b: Pick any vertex u common to C and G’ 1 3 4 5 6 C: v 1 2 3 v u = Proof: (The algorithm)

23 23 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3c: Pick any sequence of adjacent vertices and edges of G’, starting and ending at u, never repeating an edge, creating another circuit C’. 1 3 4 5 6 C: v 1 2 3 v u = Proof: (The algorithm)

24 24 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3c: Pick any sequence of adjacent vertices and edges of G’, starting and ending at u, never repeating an edge, creating another circuit C’. 1 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = Proof: (The algorithm)

25 25 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: 1 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = Proof: (The algorithm)

26 26 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: 1 v 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v C’’: v Proof: (The algorithm)

27 27 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u C’’: v Proof: (The algorithm)

28 28 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u C’’: v 1 2 3 Proof: (The algorithm)

29 29 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u (iii) Follow C’ from u all the way back to u C’’: v 1 2 3 Proof: (The algorithm)

30 30 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u (iii) Follow C’ from u all the way back to u C’’: v 1 2 3 6 5 4 1 3 Proof: (The algorithm)

31 31 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u C’’: v 1 2 3 6 5 4 1 3 (iii) Follow C’ from u all the way back to u (iv) Return from u through untravelled portion of C to v Proof: (The algorithm)

32 32 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3d: PATCH C’ to C to form C’’: v 1 2 3 4 5 6 C: v 1 2 3 vC’: 3 6 5 4 1 3 u = (i) Start at v (ii) Follow C to u C’’: v 1 2 3 6 5 4 1 3 v (iii) Follow C’ from u all the way back to u (iv) Return from u through untravelled portion of C to v Proof: (The algorithm)

33 33 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Let C = C’’. Go back to Step 3: v 1 2 3 4 5 6 C : v 1 2 3 6 5 4 1 3 v Proof: (The algorithm)

34 34 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. n Step 3: Does C contain every edge and vertex of G? n If ‘Yes’, then stop and output C, which is an Euler circuit. n If ‘No’… (3a-3d) v 1 2 3 4 5 6 C : v 1 2 3 6 5 4 1 3 v Proof: (The algorithm)

35 35 2.2 Euler Circuit Theorem (Proof  ) The algorithm: Step 1: Pick any vertex v  V as the starting vertex. n Step 2: Pick any sequence of adjacent vertices and edges starting and ending at v, and never repeating an edge. Let this circuit be C. n Step 3: Does C contain every edge and vertex of G? If ‘Yes’, then stop and output C, which is an Euler circuit. If ‘No’, then… –Step 3a: Remove the circuit C from G creating G’. –Step 3b: Pick any vertex u common to C and G’ –Step 3c: Pick any sequence of adjacent vertices and edges of G’, starting and ending at u, never repeating an edge, creating another circuit C’. –Step 3d: PATCH C’ to C to form C’’: Start at v Follow C to u Follow C’ from u all the way back to u Return from u through untravelled portion of C to v Let C = C’’. Go to Step 3.

36 36 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. Proof: (1)IF the algorithm terminates, THEN it will give an Euler circuit. This is true because Step 3 checks whether all edges have been used, and step 3a ensures that no edge is repeated in the circuit. n Step 3: Does C contain every edge and vertex of G? If ‘Yes’, then stop and output C, which is an Euler circuit. If ‘No’, then… –Step 3a: Remove the circuit C from G creating G’. –Step 3b: Pick any vertex u common to C and G’ –Step 3c: Create another ciruit C’ startin from u. –Step 3d: PATCH C’ to C to form C’’:

37 37 2.2 Euler Circuit Theorem (Proof  ) Proof (  ): Assume (1) G is connected, (2) (  v  V, deg(v) = 2k). Show G=(V,E) has (  ) an Euler circuit. Proof: (2)The algorithm will always terminate, because… (a) The graph is finite, (b) Step 3b will always be successful since the graph is connected, any remaining edges must connect to the currently found circuit. (c) Step 2 and 3c will always be successful – a new circuit can always be found since the degree of the vertex u is even. n Step 3: Does C contain every edge and vertex of G? If ‘Yes’, then stop and output C, which is an Euler circuit. If ‘No’, then… –Step 3a: Remove the circuit C from G creating G’. –Step 3b: Pick any vertex u common to C and G’ –Step 3c: Create another circuit C’ starting from u –Step 3d: PATCH C’ to C to form C’’:

38 38 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i VertexDegree a2a2 b2b2 c2c2 d4d4 e4e4 f2f2 g2g2 h4h4 i2i2 j2j2 Even

39 39 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit?

40 40 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit? C = a b c d a

41 41 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit? C = a b c d a C’ = d e g h j i d C’’ = a b c d e g h j i d a

42 42 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit? C = a b c d e g h j i d a

43 43 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit? C = a b c d e g h j i d a C’ = e f h e C’’ = a b c d e f h e g h j i d a

44 44 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: Yes a b c d e f g h j i Q: Where is the Circuit? C = a b c d e f h e g h j i d a C is the Euler Circuit

45 45 2.3 Example n (Euler Circuit Theorem): Let G=(V,E) be a graph. G has an Euler circuit iff G is connected AND every vertex of G has even degree. Q: Does the graph have an Euler circuit A: No.

46 46 2.4. Euler Path (Definition) n Definition: Let G=(V,E) be a graph. An EULER PATH from v to u (v  u) for G is a path starting from v and ending at u and contains every vertex in V and every edge in E. n Implications: –‘Path’: edges must be distinct… At most 1 traversal of each edge. –‘every edge in E’: … At least 1 traversal of each edge. –‘every vertex in V’: … At least 1 traversal of each vertex.

47 47 2.5 Euler Path Theorem n (Euler Path Theorem): Let G=(V,E) be a graph. G has an Euler Path from v to u iff G is connected AND v and u have odd degree and every other vertex of G has even degree. n Euler Path theorem is a COROLLARY (a consequence, a result) of Euler Circuit theorem. n Proof is left as an exercise (You need to use the Euler circuit theorem to prove the Euler Path theorem).

48 48 2.6 Euler Path Theorem (Example) n (Euler Path Theorem): Let G=(V,E) be a graph. G has an Euler Path from v to u iff G is connected AND v and u have odd degree and every other vertex of G has even degree. Q: Does the graph have an Euler path from a to d? A: Yes. Degree a and d are odd. All other degree are even. a b cd ef gh j i k Q: Where is the Path?

49 49 2.6 Euler Path Theorem (Example) n (Euler Path Theorem): Let G=(V,E) be a graph. G has an Euler Path from v to u iff G is connected AND v and u have odd degree and every other vertex of G has even degree. a b cd ef gh j i Q: Does the graph have an Euler path from a to d? A: Yes. Degree a and d are odd. All other degree are even. k Q: Where is the Path? A: a g h i e h f a

50 50 2.6 Euler Path Theorem (Example) n (Euler Path Theorem): Let G=(V,E) be a graph. G has an Euler Path from v to u iff G is connected AND v and u have odd degree and every other vertex of G has even degree. a b cd ef gh j i Q: Does the graph have an Euler path from a to d? A: Yes. Degree a and d are odd. All other degree are even. k Q: Where is the Path? A: a g h i e h f ab f e k j d b c d

51 51 A. Circuits 1.Definitions (Closed Walk, Circuit, Simple Circuit) and Examples 2.A special kind of circuit 2.1 Definition of Euler Circuit 2.2 Euler Circuit Theorem and Proof 2.3 Examples 2.4 (Related) Definition of Euler Path 2.5 Euler Path Theorem 2.6 Example 3.A special kind of simple circuit 3.1 Definition of Hamiltonian Circuit 3.2 Examples 3.3 Special Examples 3.4 (Related) Definition of Hamiltonian Path 3.5 Examples

52 52 3.1. Hamiltonian Circuit (Definition) n Definition: Let G=(V,E) be a graph. A HAMILTONIAN CIRCUIT for G is a simple circuit that contains every vertex in V. (Sir William Hamilton 1805-1865, Irish) n Implications: –‘Simple circuit’: vertices must be distinct… –AT MOST ONE traversal of each vertex –‘every vertex in V’: … –AT LEAST ONE traversal of each vertex –‘Simple circuit’: (edges distinct of course!)

53 53 3.2 Hamiltonian Circuit (Examples) g b d a f e c h Q: Is there a Hamiltonian circuit? gbdafhecg A: Yes

54 54 3.3 Hamiltonian Circuit (Theorem?) n Q: Given ANY graph G=(V,E), is there a ‘nice property’ which is a sufficient condition to determine whether the graph has a Hamiltonian circuit? (Sufficient condition:) Property  Hamiltonian Circuit n A: Don’t know. No choice but to try all possible ways. n Q: Are there special types of graphs which definitely have Hamiltonian circuits? n A: Yes. n Q: What are they? n A: Graphs of some forms of geometric solids. (i) Platonic Solids; (ii) Archimedian solids

55 55 3.3 Hamiltonian Circuit (Special Cases) (i) Platonic Solids:

56 56 3.3 Hamiltonian Circuit (Special Cases) (ii) Archidemian Solids:

57 57 3.4. Hamiltonian Path (Definition) n Definition: Let G=(V,E) be a graph. A HAMILTONIAN Path for G is a simple path that contains every vertex in V. n Implications: –‘Simple path’: vertices must be distinct… –AT MOST ONE traversal of each vertex –‘every vertex in V’: … –AT LEAST ONE traversal of each vertex –‘Simple path’: (edges distinct of course!)

58 58 3.5 Hamiltonian Path (Example) n Example: The following graph does not have a Hamiltonian circuit. g b d a f e c h Q: But does it have a Hamiltonian Path from a to c? A: Yes: a e h f d b g c

59 59 3.5 Hamiltonian Path (Example) n Example: The following graph does not have a Hamiltonian circuit. g b d a f e c h Q: Does it have a Hamiltonian Path from a to g? A: No.

60 60 3.5 Hamiltonian Path (Example) n Example: The following graph does not have a Hamiltonian circuit. g b d a f e c h Q: Can there be Hamiltonian Paths starting from g? A: No.

61 61 3.5 Hamiltonian Path (Example) n Example: The following graph does not have a Hamiltonian circuit. g b d a f e c h Q: Can there be Hamiltonian Paths ending at d? A: No.

62 62 3.5 Hamiltonian Path (Example) n Example: The following graph does not have a Hamiltonian circuit. g b d a f e c h Q: Can there be a Hamiltonian Path ending at ‘a’? A: Yes. Example: b g c d f e h

63 63 B. Graph Representation 1.Graph Representation using Matrices 2.Using matrices to find information. 2.1 Q: Is the graph directed or undirected? 2.2 Q: How many connected components are there? 2.3 Q: Is there a walk of length n from v to u?

64 64 1. Graph Representation using matrices n There are 2 main ways to represent graphs in the computer: –Use a 2D array. –Use an adjacency lists. (Graph implementation taught in cs1102) n Using a 2D array corresponds to using a adjacency matrix representation. n IMPT: We assume you know (1) basic matrix terminology and (2) matrix multiplication (Please refer to p646-651 for your personal revision, if you need to).

65 65 1. Graph Representation using matrices n An m x n matrix A over a set S, is a rectangular array of elements of S arranged into m rows and n columns.

66 66 1. Graph Representation using matrices n A graph with n vertices can be represented using a n x n adjacency matrix. n Each entry a ij of the matrix is used to indicate the presence or absence of an edge from v i to v j. v1v1 v2v2 v3v3 v4v4 v5v5

67 67 2. Using matrices to find information n Let A be the adjacency matrix representation of some graph G. n Looking only at the matrix (i.e. do not draw the graph), how do we know… Q1: … whether the graph is directed or undirected? Q2: … how many connected components are there in the graph? Q3: … whether is there a walk of length w from v i to v j ?

68 68 2.1 Directed or undirected graph? n Q1: How do we know whether the graph is a directed or an undirected graph? A: The graph is undirected iff  i,j, a ij = a ji. v1v1 v2v2 v3v3 v4v4 v5v5 “Symmetric”

69 69 2.1 Directed or undirected graph? n Q1: How do we know whether the graph is a directed or an undirected graph? A: The graph is undirected iff  i,j, a ij = a ji. v1v1 v2v2 v3v3 v4v4 v5v5 “NOT Symmetric”

70 70 2.1 Directed or undirected graph? n Q1: How do we know whether the graph is a directed or an undirected graph? A: The graph is undirected iff  i,j, a ij = a ji. Why? If there is an edge from v i to v j, (a ij = 1) and since the graph is undirected, then there is also an edge from v j to v i (a ji = 1). Similarly, if there is no edge from v i to v j, (a ij = 0) then there is also no edge from v j to v i (a ji = 0).

71 71 2.2 How many connected components? n Q2: How do we know how many connected components are there in the graph? v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7

72 72 2.2 How many connected components? n Q2: How do we know how many connected components are there in the graph? v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7

73 73 2.2 How many connected components? n Q2: How do we know how many connected components are there in the graph? v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7

74 74 2.2 How many connected components? n Q2: How do we know how many connected components are there in the graph? v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7

75 75 2.2 How many connected components? n Q2: How do we know how many connected components are there in the graph? v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 3 connected components

76 76 2.2 How many connected components? n In general, if the graph has k connected components, each with n i vertices then there exist a way to swap the rows and columns of the matrix so that the vertices of the connected components are arranged together, resulting in a matrix of the form: Each A i is a adjacency matrix of n i vertices. Each O is a matrix with all entries as 0.

77 77 2.2 How many connected components? n ‘There exists a way’ doesn’t tell you how to deterministically find ‘the way’. (Not part of our syllabus). n There are algorithms which break a graph into connected components (which will be covered in other courses)

78 78 2.3 Walk from v i to v j of length w? n Q3: How do we know whether there is a walk from v i to v j of length w? v1v1 v2v2 v3v3 v4v4 v5v5 Q: Is there a walk from v 1 to v 2 of length 1? A: Yes. Q: Is there a walk from v 1 to v 2 of length 2? A: No. =

79 79 2.3 Walk from v i to v j of length w? n Q3: How do we know whether there is a walk from v i to v j of length w? v1v1 v2v2 v3v3 v4v4 v5v5 Q: Is there a walk from v 1 to v 2 of length 1? A: Yes. Q: Is there a walk from v 1 to v 2 of length 2? A: No. = Q: Is there a walk from v 2 to v 3 of length 2? A: Yes, 3 such walks.

80 80 2.3 Walk from v i to v j of length w? n Q3: How do we know whether there is a walk from v i to v j of length w? v1v1 v2v2 v3v3 v4v4 v5v5 Q: Is there a walk from v 1 to v 2 of length 3? A: Yes. Q: Is there a walk from v 2 to v 3 of length 3? A: No. = Q: How many of such walks? A: 6.

81 81 2.3 Walk from v i to v j of length w? Theorem: Let G = (V,E) and V = {v 1,v 2,…,v m } and let A be the adjacency matrix of G.  n >= 1, The i-j-th entry of A n = the number of walks of length n from v i to v j (  i,j  {1..m}) Proof by induction on n: Base case: (n=1) The i-j-th entry of A 1 = the number of walks of length 1 from v i to v j (  i,j  {1..m}) This is true since the i-j-th entry of A is 1 if there is an edge and 0 otherwise. If there is an edge, then there is a walk of length 1 from v i to v j.

82 82 2.3 Walk from v i to v j of length w? Theorem: Let G = (V,E) and V = {v 1,v 2,…,v m } and let A be the adjacency matrix of G.  n >= 1, The i-j-th entry of A n = the number of walks of length n from v i to v j (  i,j  {1..m}) Proof by induction on n: Inductive case: (k  k+1) Let a ij be used to denote elements in A. Let b ij be used to denote elements in A k. Let c ij be used to denote elements in A k+1. Assume: The i-j-th entry of A k = the number of walks of length k from v i to v j (  i,j  {1..m}) To Prove: The i-j-th entry of A k+1 = the number of walks of length k+1 from v i to v j (  i,j  {1..m})

83 83 2.3 Walk from v i to v j of length w? = AAkAk A k+1 i i jj a i1 b 1j + a i2 b 2j + a i3 b 3j + … + a im b mj = c ij a i1 a i2 a im b 1j b 2j b mj a i1 b 1j = # Walk of Length 1 from v i to v 1 x # Walk of Length k from v 1 to v j. (IH) = # Walk of Length k+1 from v i to v j with v 1 as 2 nd vertex

84 84 2.3 Walk from v i to v j of length w? = AAkAk A k+1 ii jj a i1 b 1j + a i2 b 2j + a i3 b 3j + … + a im b mj = c ij a i1 a i2 a im b 1j b 2j b mj a i2 b 2j = # Walk of Length 1 from v i to v 2 x # Walk of Length k from v 2 to v j.(IH) = # Walk of Length k+1 from v i to v j with v 2 as 2 nd vertex

85 85 2.3 Walk from v i to v j of length w? = AAkAk A k+1 ii jj a i1 b 1j + a i2 b 2j + a i3 b 3j + … + a im b mj = c ij a i1 a i2 a im b 1j b 2j b mj a i3 b 3j = # Walk of Length 1 from v i to v 3 x # Walk of Length k from v 3 to v j. (IH) = # Walk of Length k+1 from v i to v j with v 3 as 2 nd vertex

86 86 2.3 Walk from v i to v j of length w? = AAkAk A k+1 ii jj a i1 b 1j + a i2 b 2j + a i3 b 3j + … + a im b mj = c ij a i1 a i2 a im b 1j b 2j b mj a im b mj = # Walk of Length 1 from v i to v m x # Walk of Length k from v m to v j. (IH) = # Walk of Length k+1 from v i to v j with v m as 2 nd vertex

87 87 2.3 Walk from v i to v j of length w? = AAkAk A k+1 ii jj a i1 b 1j + a i2 b 2j + a i3 b 3j + … + a im b mj = c ij a i1 a i2 a im b 1j b 2j b mj All walks of length k+1 from v i to v j can only pass through v 1 to v m as their second vertex. (There are no other vertices to pass through). Therefore c ij will store the number of walks from v i to v j of length k+1. (Proven the k+1 case)

88 88 n End of Lecture


Download ppt "1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3"

Similar presentations


Ads by Google