Course Notes Set 7 Review of Graphs Computer

  • Slides: 16
Download presentation
Course Notes Set 7: Review of Graphs Computer Science and Software Engineering Auburn University

Course Notes Set 7: Review of Graphs Computer Science and Software Engineering Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -0

Graphs • Graph theory is a branch of topology, so is a mathematically rigorous

Graphs • Graph theory is a branch of topology, so is a mathematically rigorous subject. • For our purposes, however, we will only need to understand graphs at the level presented in an undergraduate data structures and algorithms course. • Although undirected graphs are the more general concept, we will only discuss directed graphs. Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -1

Directed Graphs • A directed graph (or digraph) G = (V, E) consists of

Directed Graphs • A directed graph (or digraph) G = (V, E) consists of a finite set V = {n 1, n 2, …, nm} of nodes and a finite set E = {e 1, e 2, …, ep} of edges, where each edge ek = {ni, nj} is an ordered pair (start-node, terminal-node) of nodes from V. n 1 e 1 n 2 n 3 e 3 V = {n 1, n 2, n 3, n 4, n 5, n 6, n 7} e 4 e 2 n 4 n 5 e 6 n 6 E = {e 1, e 2, e 3, e 4, e 5, e 6} = {(n 1, n 2), (n 1, n 4), (n 3, n 4), (n 2, n 5), (n 4, n 6), (n 6, n 3)} n 7 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -2

Nodes and Degrees • • • Indegree(node)= number of distinct edges that have the

Nodes and Degrees • • • Indegree(node)= number of distinct edges that have the node as a terminal node. Outdegree(node)= number of distinct edges that have the node as a start node. Source node= a node with indegree 0. Sink node= a node with outdegree 0. Transfer node= a node with indegree != 0 and outdegree != 0. n 1 e 1 n 2 e 4 e 2 n 3 e 3 n 4 n 5 e 6 n 7 Auburn University Computer Science and Software Engineering Indegree(n 1) = 0 Indegree(n 2) = 1 Indegree(n 4) = 2 Indegree(n 7) = 0 Outdegree(n 1) = 2 Outdegree(n 2) = 1 Outdegree(n 4) = 1 Outdegree(n 7) = 0 COMP 6710 Course Notes Slide 7 -3

Paths and Semi-Paths • • Directed Path= a sequence of edges such that, for

Paths and Semi-Paths • • Directed Path= a sequence of edges such that, for any adjacent pair of edges ei, ej in the sequence, the terminal node of the first edge is the start node of the second edge. Directed Semi-path= a sequence of edges such that, for at least one adjacent pair of edges ei, ej in the sequence, the initial node of the first edge is the initial node of the second edge, or the terminal node of the first edge is the terminal node of the second edge. n 1 e 1 n 2 e 4 e 2 n 3 e 3 n 4 n 5 There is is a a path from n 1 to n 6. semi-path between n 1 and n 3. semi-path between n 2 and n 4. semi-path between n 5 and n 6. e 5 e 6 n 7 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -4

Connectedness Two nodes ni and nj in a directed graph are: • 0 -connectediff

Connectedness Two nodes ni and nj in a directed graph are: • 0 -connectediff there is no path or semi-path between ni and nj. • 1 -connectediff there is a semi-path but no path between ni and nj. • 2 -connectediff there is a path between ni and nj. • 3 -connectediff there is a path from ni to nj and a path from nj to ni. A graph is connectediff all pairs of nodes are either 1 -connected or 2 connected. A graph is strongly connected iff all pairs of nodes are 3 connected. n 1 e 1 n 2 e 4 e 2 n 3 e 3 n 4 n 5 e 6 n 1 and n 7 n 2 and n 6 n 1 and n 6 n 3 and n 6 The graph are 0 -connected. are 1 -connected. are 2 -connected. are 3 -connected. is not strongly connected. n 7 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -5

Connectedness n 1 e 1 n 2 e 4 e 2 n 3 n

Connectedness n 1 e 1 n 2 e 4 e 2 n 3 n 1 e 3 n 4 n 5 n 6 e 8 e 7 n 7 Connected, but not strongly connected. Auburn University Computer Science and Software Engineering n 3 e 9 n 2 e 4 e 2 e 5 e 6 e 1 e 3 n 4 n 5 e 6 n 6 e 8 e 7 n 7 Strongly connected. COMP 6710 Course Notes Slide 7 -6

Strong Components • A strong component of a directed graph is a maximal set

Strong Components • A strong component of a directed graph is a maximal set of 3 -connected nodes; that is, a maximal set of strongly connected nodes. n 1 e 1 n 2 e 2 n 3 e 3 {n 3, n 4, n 6} is a strong component. {n 7} is a strong component. e 4 n 5 e 6 n 7 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -7

Cyclomatic Number • The cyclomatic number of a strongly connected directed graph G =

Cyclomatic Number • The cyclomatic number of a strongly connected directed graph G = (V, E) is given by v(G) = |E| - |V| + p, where p is the number of strong components in the graph. • v(G) is also equal to the number of bounded areas defined by the graph. n 1 e 4 e 2 n 3 e 9 n 2 e 3 n 4 n 5 e 6 n 6 v(G) = 9 – 7 + 1 = 3 e 8 e 7 n 7 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -8

Program Graphs • Given a program written in an imperative programming language, its program

Program Graphs • Given a program written in an imperative programming language, its program graph is a directed graph in which nodes are either entire statements or fragments of a statement, and edges represent flow of control. • At the module level, program graphs should be connected. Loops will define strongly connected components. Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -9

Graphs for Control Constructs if-then Sequence { m 1(); m 2(); m 3(); m

Graphs for Control Constructs if-then Sequence { m 1(); m 2(); m 3(); m 2 c m 1 m 2 m 3 } if-then-else if (c) { m 1(); } else { m 2(); } m 3(); if (c) { m 1(); } m 2(); Switch/multi-way decision c c m 1 m 2 m 3 … mn m 3 Auburn University Computer Science and Software Engineering mm COMP 6710 Course Notes Slide 7 -10

Graphs for Control Constructs Post-test Loop Pre-test Loop while (c) { m 1(); }

Graphs for Control Constructs Post-test Loop Pre-test Loop while (c) { m 1(); } m 2(); c m 1 m 2 Auburn University Computer Science and Software Engineering do { m 1(); } while (c); m 2(); m 1 c m 2 COMP 6710 Course Notes Slide 7 -11

Program Graph Example s 1(); if (c 1) { s 2(); } else {

Program Graph Example s 1(); if (c 1) { s 2(); } else { s 3(); } s 4(); while (c 2) { s 5(); } s 6(); s 1 c 1 s 2 s 3 s 4 c 2 s 5 s 6 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -12

Program Graph Example ÏÏÏ ¬¹¹¹¹¹ 2 ÏÏÞßàpublic static String triangle (int a, int b,

Program Graph Example ÏÏÏ ¬¹¹¹¹¹ 2 ÏÏÞßàpublic static String triangle (int a, int b, int c) { ÏÏϪ˹¹¹¹ 3 ÏÏÏϨ¹íÏString result; 4 ÏÏÏϨ¹íÏboolean is. ATriangle; 5 ÏÏÏϨ¹³´if ( ( a < b + c ) && ( b < a + c ) && ( c < a + b ) ) { 6 ÏÏÏÏ§Ï 6¾¹¹Ïis. ATriangle = true; 7 ÏÏÏÏ§Ï 6Ï} 8 ÏÏÏϧÏö´else { 9 ÏÏÏϧϸ¾¹¹Ïis. ATriangle = false; 10 ÏÏÏϧÏÈÏ} 11 ÏÏÏϨ¹³´if ( is. ATriangle ) { 12 ÏÏÏÏ§Ï 6¾¹³´if ( ( a == b ) && ( b == c ) ) { 13 ÏÏÏÏ§Ï 6ÏÏ 6¾¹¹Ïresult = "Triangle is equilateral. "; 14 ÏÏÏÏ§Ï 6Ï} 15 ÏÏÏÏ§Ï 6ÏÏ÷´else if ( ( a != b ) && ( a != c ) && ( b != c ) ) { 16 ÏÏÏÏ§Ï 6ÏÏ 6¾¹¹Ïresult = "Triangle is scalene. "; 17 ÏÏÏÏ§Ï 6Ï} 18 ÏÏÏÏ§Ï 6ÏÏö´else { 19 ÏÏÏÏ§Ï 6Ïϸ¾¹¹Ïresult = "Triangle is isosceles. "; 20 ÏÏÏÏ§Ï 6ÏÏÈÏ} 21 ÏÏÏÏ§Ï 6Ï} 22 ÏÏÏϧÏö´else { 23 ÏÏÏϧϸ¾¹¹Ïresult = "Not a triangle. "; 24 ÏÏÏϧÏÈÏ} 25 ÏÏ ¹Ä¹¹Ïreturn result; 26 ÏÏÏÏ©} 51 52 53 6 9 11 121 151 122 153 23 13 16 19 25 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -13

Condensation Graph • Given a graph G = (V, E), its condensation graph is

Condensation Graph • Given a graph G = (V, E), its condensation graph is formed by replacing some components with a condensing node. n 1 e 4 e 2 n 3 e 3 Condensation Graph: n 2 n 1 n 4 n 5 e 5 n 2 Results from condensing strong components. e 4 e 2 C 1 e 6 n 6 e 1 n 5 n 7 C 2 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -14

Condensation Graph 51 52 Condense compound conditions C 1 53 6 6 9 11

Condensation Graph 51 52 Condense compound conditions C 1 53 6 6 9 11 121 151 122 152 9 C 2 23 C 3 13 C 4 16 19 153 23 13 16 19 25 25 Auburn University Computer Science and Software Engineering COMP 6710 Course Notes Slide 7 -15