directed path

ECE 453 – CS 447 – SE 465
Software Testing &
Quality Assurance
Instructor
Kostas Kontogiannis
1
Overview
Discrete Mathematics for Testers
• Set Theory
• Functions
• Relations
• Probability
• Graphs
–
–
–
–
Representation
Paths
Directed Graphs
Graphs for Testing
2
Set Definition Format
• Explicit enumeration:
– Example: Y = {10, 20, 30, 40}
• Using decision rules:
– Examples:
•
•
•
•
Y = {item_number | 80 < item_number < 100}
Y = {item_number : 80 < item_number <100}
Y = {x | x is positive integer}
Y = {x | PI(x)}
• Empty set: A set with no elements
• Y = {year : 2012 < year < 1812} = {} = O
3
Venn Diagrams
• Diagrams that illustrate diferent set relationships or
illustrate a set with respect to the domain (U)
• Set operations:
–
–
–
–
–
Union : A U B = { x : x in A or x in B}
Intersection: A ∩ B = {x : x in A and x in B}
Complement: A* = {x : x not in A}
Relative complement: A – B = {x : x in A and x not in B}
Symmetric difference: A + B = {x: x in A + x in B} =
= (A U B) – (A ∩ B)
4
Set Relations and Partitions
•
•
•
A Subset B: a in A  a in B
A Proper Subset B: A subset B and (B – A) ≠ O
Equal sets: A = B iff A subset B and B subset A
•
Given a set B and a set of subsets A1, A2, An of B,
the subsets are a partition of B iff
1. A1 U A2 U .. An = B
2. For i ≠ j Ai ∩Aj = O
5
Functions, Domains, Ranges
• Given two sets A and B, a function f :A  B is a
subset of the product A X B such that for any of ai,
aj in A, there exist bi, bj in B such that f(ai) = bi,
and f(aj) = bj.
• The set A is called the domain of f and B is called
the range of f.
• The function f can be represented as a set of pairs.
6
Function Types
• Functions are further described by the particulars of
the mapping
•
•
•
•
•
e.g. f(A) = {bi in B : bi = f(ai) for some ai in A}
This set is sometimes called the image of A under f
Types of functions that relate the image and the range are:
Function f is a function of A onto B iff f(A) = B
Function f is a function of A into B iff f(A) proper subset B
Function f is a one-to-one function from A to B iff
for all ai, aj in A, s.t. ai ≠aj implies that f(ai) ≠ f(aj)
7
Function Composition
• Suppose we have sets and functions such that the
range of one is the domain of the next:
– f: A  B
– g: B  C
– h: C  D
• We can define a new function w:A  D such that
w(a) = (h o g o f) (a) = h(g(f(a)))
• So, if f(a) = b, and g(b) = c, and h(c) = d then w(a)
= h(g(f(a))) = h(g(b)) = h(c) = d
8
Application Snapshot
• Causal Flow
a
f
b
g
c
• Non-Causal Flow
a
f
g
b
b
c
b
B
9
Relations
• Given two sets A, B a relation R is a subset of the
Cartesian product A X B
• Functions are a special case of a relation:
– Both are subsets of some Cartesian product
– Functions have the additional requirement that a
domain element can not be associated with more than
one range element.
• When a relation is not a function then is also
called a mapping.
• A relation has a property called cardinality
10
Relations among Sets
• Given two sets A, B, and a relation R among these
sets, the cardinality of R is:
– one-to-one iff R is a one-to-one function from A to B
– many-to-one: iff R is a many-to-one function from A to
B
– one-to-many: iff at least one element a in A is in two
ordered pairs in R, that is (a, bi) in R and (a, bj) in R.
– many-to-many: iff at least one element a in A is in two
ordered pairs in R, that is (a, bi) in R and (a, bj) in R and
one element b in B is in two ordered pairs in R, that is
(ak, b) in R and (an, b) in R
11
Participation of a Relation
• Let R be a relation s.t. R is a subset of A X B
• The participation of the relation R can be:
– total participation: iff every element of A is in some
ordered pair in R
– partial participation: iff some element of A is not in
any ordered pair in R
– onto participation: iff every element of B is in some
ordered pair in R
– into participation: iff some element of B is not in some
element in R
12
Relations on a Single Set
• Let R be a relation defined in the same set A X A
• A relation R is:
– reflexive: iff for all a in A <a, a> in R
– symmetric: iff <a,b> in R then <b,a> in R
– anti-symmetric: if <a,b> in R and <b,a> in R then it implies that
a=b
– transitive: iff <a,b> in R and <b,c> in R then <a,c> in R
• Such a relation R is an ordering relation if it is reflexive,
anti-symmetric and, transitive.
• Similarly, R is an equivalence relation if it is reflexive,
symmetric, and transitive.
13
Propositional Logic
• Propositions are sentences specified in propositional logic
notation that can be evaluated to True or False.
• A simple proposition (e.g. p, q, r) is a valid sentence
• Valid sentences can be constructed by other sentences
using logical operators (and, or, not, implies, exclusive_or)
• Two proposition logic sentences are equivalent iff their
truth tables are identical (e.g. S= p  q) is equivalent with
the sentence W = ~p or q)
• A proposition is a tautology if it is always true
(e.g. S = a or ~a)
• A proposition is a contradiction if it is always false
(e.g. S = a and ~a)
14
Probability Theory
• Events: things that happen
• Universe of Discourse: all events that can possibly happen, and is
denoted by U.
• Let p is a proposition about elements in U:
– The truth set of a proposition p, written T(p), is the set of all elements in
U for which p is true (note T(p) U T(~p) = U)
– The probability that a proposition p is true is defined as Pr(p) = |T(p)| / |U|
• The definition of U is crucial on determining the value of Pr(p)
• Facts:
– Pr(~p) = 1 – P(p)
– Pr(p and q) = Pr(p) * Pr(q)
– Pr(p or q) = Pr(p) + Pr(q) – Pr(p and q)
15
Graph Theory for Testers
• A graph G = (V, E) is composed of a finite
and non empty set V = {n1, n2, nm) of nodes
and a set E = {e1, e2, ep} of edges, where
each edge ek = {ni, nj}, for some nodes ni, nj
in V.
16
Example
n1
e1
e2
n3
e3
V = {n1, n2, n3, n4, n5, n6, n7}
n2
e4
n5
n4
E = {e1, 2, e3, e4, e5}
= {{n1, n2}, {n1, n4}, {n3, n4},
{n2, n5}, {n4, n6}}
e5
n6
n7
Degree of a node: the number of edges that have the node as an
endpoint. Example: deg(n1) = 2, deg(n7) = 0, deg(n4) = 3. 17
Incidence Matrices
• An incidence matrix
of a graph G = (V, E)
with m nodes and n
edges is an m X n
matrix where the
elements in row i, and
column j is 1 iff node i
is an endpoint of edge
j, otherwise is 0.
n1
n2
n3
n4
n5
n6
n7
e1
1
1
0
0
0
0
0
e2
1
0
0
1
0
0
0
e3
0
0
1
1
0
0
0
e4
0
1
0
0
1
0
0
e5
0
0
0
1
0
1
0
18
Adjacency matrix
• The adjacency matrix
of a graph G = (V, E)
with m nodes, and n
edges, is an m X m
matrix, where the
elements in row i and
column j is 1 iff there
is an edge between
node i and node j,
otherwise is 0.
n1 n2 n3 n4 n5 n6 n7
n1 0
1
0
1
0
0
0
n2 1
0
0
0
1
0
0
n3 0
0
0
1
0
0
0
n4 1
0
1
0
0
1
0
n5 0
1
0
0
0
0
0
n6 0
0
0
1
0
0
0
n7 0
0
0
0
0
0
0
19
Paths
• A path is a sequence of edges such that, for
any adjacent pair of edges ei, ej in the
sequence, the edges share a common node
as an endpoint.
• Paths can be described either as a sequence
of edges, or as a sequence of nodes.
• Paths are defined between a source node
and a target node.
20
Example Paths
Path (from-to)
Node sequence Edge sequence
n1 – n5
n1, n2, n5
e1, e4
n6 – n5
n6, n4, n1, n2, n5
e5, e2, e1, e4
n3 – n2
n3, n4, n1, n2
e3, e2, e1
21
Connectedness
• Nodes ni, nj are connected iff there is a path from
node ni to node nj
• “Connectedness” is an equivalence relation on the
node set of a graph
• “Connectedness” defines a partition on the node
set of a graph.
• A component of a graph is a maximal set of
connected nodes
• Example: Two components in the sample graph
C1 = {n1, n2, n3, n4, n5, n6}, and C2 = :n7}
22
Condensation Graphs
• Given a graph G = (V, E), its condensation
graph is formed by replacing each
component by a condensing node.
C1
n7
23
Complexity of a Graph
• A measure of the complexity of a graph is
its cyclomatic complexity measure.
• The cyclomatic complexity of a graph G, is
a number V(G) = e – n + 2, where e is the
number of edges in the graph, and n is the
number of vertices (nodes) in the graph.
24
Directed Graphs
• A directed graph (or digraph) D = (V, E),
consists of a finite set of vertices V = {n1,
n2, … nm}, and a set of edges E = {e1, e2,
ep}, where each edge ek = <ni, nj> is an
ordered pair of nodes in V.
25
Example Directed Graph
n1
e1
e2
n3
e3
V = {n1, n2, n3, n4, n5, n6, n7}
n2
e4
n5
n4
E = {e1, 2, e3, e4, e5}
= {<n1, n2>, <n1, n4>, <n3, n4>,
<n2, n5>, <n4, n6>}
e5
n6
n7
26
Properties of Directed Graphs
• The indegree (or fan-in) of a node in a directed graph is the number of
distinct edges that have the node as a terminal node (target node).
Example: indeg(n1) = 0, indeg(n4) = 2
• The outdegree (or fan-out) of a node in a directed graph is the number
of distinct edges that have the node as a start node (source node).
Example: outdeg(n1) = 2, outdeg(n6) = 0
• A node with in indegree = 0 is called a source node (n1, n3, n7)
• A node with outdegree = 0 is called a sink node (n5, n6, n7)
• A node with indegree ≠ 0 and outdeg ≠ 0 is called a transfer node (n2,
n4)
• A node that is both a source and a sink is an isolated node
• The source and sink nodes in a graph constitute the external boundary
of the graph
27
Adjacency Matrix of a Directed Graph
• The adjacency matrix of a directed graph D = (V,
E) with m nodes is an m X m matrix which in row i
and column j there is an entry equal to 1 iff there
is an edge from node i to node j, otherwise the
entry is equal to 0
• The sum of a row is the outdegree of a node
• The sum of a column is the indegree of a node
28
Paths and Semi-Paths
• A directed path is a sequence of edges such that, for any adjacent set
of edges ei, ej in the sequence, the terminal node of ei is the initial node
of ej
• A cycle is a directed path that begins and ends in the same node
• A directed semi-path is 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
• Directed paths are also called chains
• Example: 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
29
Reachability Matrix
• The reachability matrix of a
directed graph D = (V, E) with
m nodes is an m X m matrix R =
(r(i, j)), where the entry r(i, j) =
1 iff there is a path from node i
to node j, otherwise the entry is
equal to 0
• The reachability matrix R can
be computed as
R= I + A + A2 + … + Ak
where k is the length of the
longest path in D
n1 n2 n3 n4 n5 n6 n7
n1 0
1
0
1
1
1
0
n2 0
0
0
0
1
0
0
n3 0
0
0
1
0
1
0
n4 0
0
0
0
0
1
0
n5 0
0
0
0
0
0
0
n6 0
0
0
0
0
0
0
n7 0
0
0
0
0
0
0
30
N-Connectedness
• Two nodes ni and nj in a directed graph are:
– 0-connected: iff there is no path from ni to nj
– 1-connected: iff there is a semi-path but no path
between ni, nj
– 2-connected: iff there is a path between ni, nj
– 3-connected: iff there is a path from ni to nj and a path
from nj to ni
• A strong component of a directed graph is the
maximal set of 3-connected nodes
31
Example
n1
e1
n2
e2
n3
e3
e4
n5
n4
n1 and n7 are 0-connected
n2 and n6 are 1-connected
n1 and n6 are 2-connected
n3 and n6 are 3-connected
e5
e6
n6
n7
32
Condensation Graph of a DiGraph
• A condensation graph is a a graph
D'=(V',E') based on the directed graph
D=(V,E) where each vertex in V'
corresponds to a strongly connected
component in D and edge (u,v) is in E' if
and only if there exists an edge in E
connecting any of the vertices in the
component of u to any of the vertices in the
component of v.
33
Example Condensation DiGraph
n1
e1
n2
e2
C1
e4
n5
n7
34
Example Condensation Graph
Condensation Graph
35
Program Graphs
• Given a program written in an imperative
programming language, its program graph is a
directed graph in which:
– Nodes are program statements (or statement fragments),
and edges represent flow of control
• Control flow of imperative programs is defined by
sequencing, iteration, and choice constructs
36
Digraphs of the Structured
Programming Constructs
s2
s2
s2
s2
s1
s1
s1
s1
s2
Sequence if-then-else
s2
s2
s2
s2
s2
s2
case
if-then
s1
s1
s2
s2
loop-pre-test loop-post-test
37
Finite State Machines
• A Finite State Machine (FSM) is a directed
graph (S, T, Ev, Act, Guard), in which S is a
set of nodes, T is a set of edges, and Ev,
Act, Guard are sets of events, actions, and
guards associated with the transitions in T.
38
Example FSM (with events, actions
and guards)
bid [value < 100] /reject
Selling
bid [value >= 200] /sell
Happy
bid [(value >= 100) & (value < 200)] /sell
Unhappy
39
Petri-Nets
• A Petri-Net is a bipartite graph (P, T, In, Out), in which P,
T are disjoint sets of nodes and In and Out are sets of
edges, where In is a subset of P X T, and out is a subset of
TXP
• A marked Petri-Net is a 5-tuple (P, T, In, Out, M) in which
(P, T, In, Out) is a Petri-Net and M is a set of mappings of
places to positive integers (number of tokens in a Place)
• A transition in a Petri-Net is enabled when there is at least
one token in each input place
• When an enabled transition fires one token is removed
from each of its input places and is added in each of the
output places
40
Example
p1
p5
p2
t1
t3
p4
p3
P = {p1, p2, p3, p4, p5}
T = {t1, t2, t3}
In = { <p1, t1>, <p5, t1>,
<p5, p3>, <p2, p3>,
<p3, t2>}
Out = {<t1, p3>, <t2, p4>,
<t3, p4>}
M = <1, 1, 1, 2, 0>
t2
41
Example Petri Net
42