Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 6710 Course NotesSlide 7-0 Auburn University Computer Science and Software Engineering Course Notes Set 7: Review of Graphs Computer Science and Software."— Presentation transcript:

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

2 COMP 6710 Course NotesSlide 7-1 Auburn University Computer Science and Software Engineering 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.

3 COMP 6710 Course NotesSlide 7-2 Auburn University Computer Science and Software Engineering Directed Graphs A directed graph (or digraph) G = (V, E) consists of a finite set V = {n 1, n 2, …, n m } of nodes and a finite set E = {e 1, e 2, …, e p } of edges, where each edge e k = {n i, n j } is an ordered pair (start-node, terminal-node) of nodes from V. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 V = {n1, n2, n3, n4, n5, n6, n7} E = {e1, e2, e3, e4, e5, e6} = {(n1, n2), (n1, n4), (n3, n4), (n2, n5), (n4, n6), (n6, n3)}

4 COMP 6710 Course NotesSlide 7-3 Auburn University Computer Science and Software Engineering 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. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 Indegree(n1) = 0 Indegree(n2) = 1 Indegree(n4) = 2 Indegree(n7) = 0 Outdegree(n1) = 2 Outdegree(n2) = 1 Outdegree(n4) = 1 Outdegree(n7) = 0

5 COMP 6710 Course NotesSlide 7-4 Auburn University Computer Science and Software Engineering Paths and Semi-Paths Directed Path = a sequence of edges such that, for any adjacent pair of edges e i, e j 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. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 There is a path from n1 to n6. There is a semi-path between n1 and n3. There is a semi-path between n2 and n4. There is a semi-path between n5 and n6.

6 COMP 6710 Course NotesSlide 7-5 Auburn University Computer Science and Software Engineering Connectedness Two nodes n i and n j in a directed graph are: 0-connected iff there is no path or semi-path between n i and n j. 1-connected iff there is a semi-path but no path between n i and n j. 2-connected iff there is a path between n i and n j. 3-connected iff there is a path from n i to n j and a path from n j to n i. A graph is connected iff all pairs of nodes are either 1-connected or 2- connected. A graph is strongly connected iff all pairs of nodes are 3- connected. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 n1 and n7 are 0-connected. n2 and n6 are 1-connected. n1 and n6 are 2-connected. n3 and n6 are 3-connected. The graph is not connected. The graph is not strongly connected.

7 COMP 6710 Course NotesSlide 7-6 Auburn University Computer Science and Software Engineering Connectedness n1n2 n4n5 n7n6 n3 e1 e3 e4 e5 e6 e2 e7 e8 e9 n1n2 n4n5 n7n6 n3 e1 e3 e4 e5 e6 e2 e7 e8 Connected, but not strongly connected. Strongly connected.

8 COMP 6710 Course NotesSlide 7-7 Auburn University Computer Science and Software Engineering 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. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 {n3, n4, n6} is a strong component. {n7} is a strong component.

9 COMP 6710 Course NotesSlide 7-8 Auburn University Computer Science and Software Engineering 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. v(G) = 9 – 7 + 1 = 3 n1n2 n4n5 n7n6 n3 e1 e3 e4 e5 e6 e2 e7 e8 e9

10 COMP 6710 Course NotesSlide 7-9 Auburn University Computer Science and Software Engineering 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.

11 COMP 6710 Course NotesSlide 7-10 Auburn University Computer Science and Software Engineering Graphs for Control Constructs m1 m2 m3 Sequence { m1(); m2(); m3(); } c m1 m2 if-then if (c) { m1(); } m2(); c m1m2 m3 if-then-else if (c) { m1(); } else { m2(); } m3(); c m1 m2m3 mn mm … Switch/multi-way decision

12 COMP 6710 Course NotesSlide 7-11 Auburn University Computer Science and Software Engineering Graphs for Control Constructs c m1 m2 Pre-test Loop while (c) { m1(); } m2(); m1 c m2 Post-test Loop do { m1(); } while (c); m2();

13 COMP 6710 Course NotesSlide 7-12 Auburn University Computer Science and Software Engineering Program Graph Example s1(); if (c1) { s2(); } else { s3(); } s4(); while (c2) { s5(); } s6(); s1 c1 s2s3 s4 c2 s5 s6

14 COMP 6710 Course NotesSlide 7-13 Auburn University Computer Science and Software Engineering Program Graph Example ÏÏÏ ¬¹¹¹¹¹¹¹¹¹ 2 ÏÏÞßà public static String triangle (int a, int b, int c) { ÏÏϪ˹¹¹¹¹¹¹¹ 3 ÏÏÏϨ¹íÏ String result; 4 ÏÏÏϨ¹íÏ boolean isATriangle; 5 ÏÏÏϨ¹³´ if ( ( a < b + c ) && ( b < a + c ) && ( c < a + b ) ) { 6 ÏÏÏϧÏ6¾¹¹Ï isATriangle = true; 7 ÏÏÏϧÏ6Ï } 8 ÏÏÏϧÏö´ else { 9 ÏÏÏϧϸ¾¹¹Ï isATriangle = false; 10 ÏÏÏϧÏÈÏ } 11 ÏÏÏϨ¹³´ if ( isATriangle ) { 12 ÏÏÏϧÏ6¾¹³´ if ( ( a == b ) && ( b == c ) ) { 13 ÏÏÏϧÏ6ÏÏ6¾¹¹Ï result = "Triangle is equilateral."; 14 ÏÏÏϧÏ6ÏÏ6Ï } 15 ÏÏÏϧÏ6ÏÏ÷´ else if ( ( a != b ) && ( a != c ) && ( b != c ) ) { 16 ÏÏÏϧÏ6ÏÏ6¾¹¹Ï result = "Triangle is scalene."; 17 ÏÏÏϧÏ6ÏÏ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 ÏÏÏÏ© } 5151 5252 5353 6 9 11 23 12 1 12 2 15 1 15 2 15 3 16 25 13 19

15 COMP 6710 Course NotesSlide 7-14 Auburn University Computer Science and Software Engineering Condensation Graph Given a graph G = (V, E), its condensation graph is formed by replacing some components with a condensing node. n1n2 n4n5 n7n6 n3 e1 e2 e3 e4 e5 e6 Condensation Graph: n1n2 n5 C2 e1 e2 e4 C1 Results from condensing strong components.

16 COMP 6710 Course NotesSlide 7-15 Auburn University Computer Science and Software Engineering Condensation Graph 5151 5252 5353 6 9 11 23 12 1 12 2 15 1 15 2 15 3 16 25 13 19 C1 6 9 C2 23 C3C4 16 25 13 19 Condense compound conditions


Download ppt "COMP 6710 Course NotesSlide 7-0 Auburn University Computer Science and Software Engineering Course Notes Set 7: Review of Graphs Computer Science and Software."

Similar presentations


Ads by Google