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

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

Embed Size (px)

Citation preview

Page 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

COMP 6710 Course NotesSlide 7-1Auburn UniversityComputer Science and Software Engineering

Course Notes Set 7:

Review of Graphs

Computer Science and Software EngineeringAuburn University

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

COMP 6710 Course NotesSlide 7-2Auburn UniversityComputer 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.

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

COMP 6710 Course NotesSlide 7-3Auburn UniversityComputer Science and Software Engineering

Directed Graphs

• A directed graph (or digraph) G = (V, E) consists of a finite set V = {n1, n2, …, nm} of nodes and a finite set E = {e1, e2, …, ep} of edges, where each edge ek = {ni, nj} is an ordered pair (start-node, terminal-node) of nodes from V.

n1 n2

n4 n5

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)}

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

COMP 6710 Course NotesSlide 7-4Auburn UniversityComputer 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.

n1 n2

n4 n5

n7n6

n3

e1

e2

e3

e4

e5

e6

Indegree(n1) = 0Indegree(n2) = 1Indegree(n4) = 2Indegree(n7) = 0Outdegree(n1) = 2Outdegree(n2) = 1Outdegree(n4) = 1Outdegree(n7) = 0

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

COMP 6710 Course NotesSlide 7-5Auburn UniversityComputer Science and Software Engineering

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.

n1 n2

n4 n5

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.

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

COMP 6710 Course NotesSlide 7-6Auburn UniversityComputer Science and Software Engineering

ConnectednessTwo nodes ni and nj in a directed graph are:

•0-connected iff there is no path or semi-path between ni and nj.

•1-connected iff there is a semi-path but no path between ni and nj.

•2-connected iff there is a path between ni and nj.

•3-connected iff there is a path from ni to nj and a path from nj to ni.

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.

n1 n2

n4 n5

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.

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

COMP 6710 Course NotesSlide 7-7Auburn UniversityComputer Science and Software Engineering

Connectedness

n1 n2

n4 n5

n7n6

n3

e1

e3

e4

e5

e6

e2

e7

e8

e9n1 n2

n4 n5

n7n6

n3

e1

e3

e4

e5

e6

e2

e7

e8

Connected, but notstrongly connected.

Strongly connected.

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

COMP 6710 Course NotesSlide 7-8Auburn UniversityComputer 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.

n1 n2

n4 n5

n7n6

n3

e1

e2

e3

e4

e5

e6

{n3, n4, n6} is a strong component.{n7} is a strong component.

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

COMP 6710 Course NotesSlide 7-9Auburn UniversityComputer 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

n1 n2

n4 n5

n7n6

n3

e1

e3

e4

e5

e6

e2

e7

e8

e9

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

COMP 6710 Course NotesSlide 7-10Auburn UniversityComputer 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.

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

COMP 6710 Course NotesSlide 7-11Auburn UniversityComputer Science and Software Engineering

Graphs for Control Constructs

m1

m2

m3

Sequence{ m1(); m2(); m3();}

c

m1

m2

if-thenif (c) { m1();}m2();

c

m1 m2

m3

if-then-elseif (c) { m1();}else { m2();}m3();

c

m1 m2 m3 mn

mm

Switch/multi-way decision

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

COMP 6710 Course NotesSlide 7-12Auburn UniversityComputer Science and Software Engineering

Graphs for Control Constructs

c

m1

m2

Pre-test Loopwhile (c) { m1();}m2();

m1

c

m2

Post-test Loopdo { m1();} while (c);m2();

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

COMP 6710 Course NotesSlide 7-13Auburn UniversityComputer Science and Software Engineering

Program Graph Example

s1();if (c1) { s2();}else { s3();}s4();while (c2) { s5();}s6();

s1

c1

s2 s3

s4

c2

s5

s6

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

COMP 6710 Course NotesSlide 7-14Auburn UniversityComputer 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 ÏÏÏÏ©}

51

52

53

6 9

11

23

121

122

151

152

153

16

25

13 19

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

COMP 6710 Course NotesSlide 7-15Auburn UniversityComputer 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.

n1 n2

n4 n5

n7n6

n3

e1

e2

e3

e4

e5

e6

Condensation Graph:

n1 n2

n5

C2

e1

e2 e4

C1

Results from condensingstrong components.

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

COMP 6710 Course NotesSlide 7-16Auburn UniversityComputer Science and Software Engineering

Condensation Graph51

52

53

6 9

11

23

121

122

151

152

153

16

25

13 19

C1

6 9

C2

23

C3 C4

16

25

13 19

Condense compound conditions