Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructing and Using

Similar presentations


Presentation on theme: "Constructing and Using"— Presentation transcript:

1 Constructing and Using
Flow Graphs Constructing and Using Copyright © Curt Hill

2 Flow Charts In program design and documentation we often use a flow chart You will remember flow charts from earlier courses There are several shapes connected by arrows Circle – start or stop Rectangle – processing block Diamond – a decision Notice the flow of control structures in the next picture Copyright © Curt Hill

3 Flow Chart Example Copyright © Curt Hill

4 Notes Flow charts were developed in the 1920s to describe processes
They were very popular in the early computer years (1950s to 1970s) In this presentation we are more interested in flow graphs Flow graphs are based graph theory Thus they are able to use numerous theorems from graph theory Copyright © Curt Hill

5 Flow Graphs AKA Control Flow Graph (CFG)
A flow graph is a simplified flow chart Looks more like a graph theory diagram Both flow charts and flow graphs are directed graphs Only two constructs A node which is a circle An arrow which is an edge between two nodes Copyright © Curt Hill

6 Flow Graph Example Copyright © Curt Hill

7 Charts and Graphs The rectangles of a flow chart typically represent one or more sequential flow statements Thus we often have several rectangles one leading to another This is fine for some things but not what we want in flow graphs that we are currently considering Copyright © Curt Hill

8 Interest We are not interested in sequential statements
We wish to collapse them into a block This is also a common thing for a compiler In our normal flow graph any adjacent set of non-flow statements is collapsed into one Even if one is a control flow node Copyright © Curt Hill

9 Better Example 11 1 11 1 2,3 2 6 3 4,5 4 7 8 7 6 8 From Pressman 5 9 10 Copyright © Curt Hill

10 Observations When we collapse these nodes the following becomes true
Thus for every arc A->B Outdegree of A > 1 or indegree of B > 1 or both Next we take another detour through graph theory Copyright © Curt Hill

11 Mathematical definitions
Nodes or Vertices A location or point May have a number of properties Arcs or Edges Connector of nodes Directional or bidirectional May also have other properties, eg. cost 2 1 3 6 4 5 Copyright © Curt Hill

12 Mathematical definitions
2 Indegree of a node Number of arcs ending at node Outdegree Number of arcs starting at node Directed or digraph Arcs are one directional only Undirected Node indegree = node outdegree Not of interest today 1 3 6 4 5 Copyright © Curt Hill

13 Directed graph Flow graphs are directed
Flow must start at a node and finish at a node We denote these with arrows We cannot back up in a program Each node may point at zero or more arcs Each node may be pointed at by zero or more arcs Copyright © Curt Hill

14 Connectivity 2 A and B together is a disconnected graph with two components A is strongly connected There is a path from every node to every other node B is weakly connected No path leads to 7 1 3 6 4 5 A B 9 8 7 Copyright © Curt Hill

15 Graph Theory Concepts Path Reachability Domination
A series of edges from one node to another Reachability A path exists from one node to another If no path exists from the start node that part of the graph can be removed – AKA dead code Domination If all paths from A to C go through B then B dominates C EG. a loop header dominates the loop Copyright © Curt Hill

16 Consider this code int f1, f2, total=0, counter=1; while(!infile){ cin >> f1 >> f2 if(f1==0){ total+=f1; counter++; } else if(f2 == 0){ cout << total<<counter; counter = 0; } else total -= f2; cout << f1 << f2; } // end while cout << counter; Copyright © Curt Hill

17 Flow Graph 9 1 4 3 6 5 7 8 2 Copyright © 2001- 2017 Curt Hill
From Pressman 7 8 Copyright © Curt Hill

18 Construction There is only one start and one end
Any return statements make an arc to the one end Predicates include the following: If, while, for, case Breaks make an arc to the first statement after a loop/switch Continue makes an arc to then end of the loop Copyright © Curt Hill

19 More Construction An if may will be merged with prior sequential statements Loops will not – there is a branch to the beginning A for distinguishes between initialization and rest of loop The initialization may merge with preceding assignments It is the case of the switch which is the real predicate Multiple cases with one piece of code become one predicate Copyright © Curt Hill

20 Consider this code int f1, f2, total=0, counter=1; while(!infile){ cin >> f1 >> f2 if(f1==0){ total+=f1; counter++; } else if(f2 == 0){ cout << total<<counter; counter = 0; } else total -= f2; cout << f1 << f2; } // end while cout << counter; Copyright © Curt Hill

21 Flow Graph int f1, f2, total=0, counter=1; while(!infile){ cin >> f1 >> f2 if(f1==0){ total+=f1; counter++; } else if(f2 == 0){ cout << total<<counter; counter = 0; } else total -= f2; cout << f1 << f2; } // end while cout << counter; 9 1 2 4 3 5 6 From Pressman 7 8 Copyright © Curt Hill

22 Observation The cin and if were merged.
The two assignments in the then and else were also merged We may merge a predecessor with an if, but not a successor Copyright © Curt Hill

23 Conclusion Flow graphs are a common way to represent programs
Emphasis is on flow of control Allows us to apply graph theory to program flow This will be needed for measures such as Cyclomatic Complexity Copyright © Curt Hill


Download ppt "Constructing and Using"

Similar presentations


Ads by Google