Nội dung Text: Algorithms and Data Structures in C part 8

The solution to the simple recurrence relation yields, assuming a general form of C(n) = λn
followed by a constant to obtain the particular solution
Applying the boundary condition C (1) = 1 and C (2) = 6 one obtains
Figure 2.4 Recursive Model for Boolean Function Evaluation
2.4 Graphs and Trees
This section presents some fundamental definitions and properties of graphs.
Definition 2.7
A graph is a collection of vertices, V, and associated edges, E, given by the pair
A simple graph is shown in Figure 2.5.
In the figure the graph shown has
Figure 2.5 A Simple Graph
Definition 2.8
The size of a graph is the number of edges in the graph
Definition 2.9

The order of a graph G is the number of vertices in a graph
For the graph in Figure 2.5 one has
Definition 2.10
The degree of a vertex (also referred to as a node), in a graph, is the number of edges containing
the vertex.
Definition 2.11
In a graph, G = (V, E), two vertices, v1 and v2, are neighbors if
(v1,v2) E or (v1,v2) E
In the graph in Figure 2.5 v1 and v2 are neighbors but v1 and v3 are not neighbors.
Definition 2.12
If G = (V1, E1) is a graph, then H = (V2, E2) is a subgraph of G written if and .
A subgraph of the graph in Figure 2.5 is shown in Figure 2.6.
Figure 2.6 Subgraph of Graph in Figure 2.5
The subgraph is generated from the original graph by the deletion of a single edge (v2, v3).

Definition 2.13
A path is a collection of neighboring vertices.
For the graph in Figure 2.5 a valid path is
Definition 2.14
A graph is connected if for each vertex pair (vi,vj) there is a path from vi to vj.
The graph in Figure 2.5 is connected while the graph in Figure 2.6 is disconnected.
Definition 2.15
A directed graph is a graph with vertices and edges where each edge has a specific direction
relative to each of the vertices.
An example of a directed graph is shown in Figure 2.7.
Figure 2.7 A Directed Graph
The graph in the figure has G = (V, E) with
In a directed graph the edge (vi, vj) is not the same as the edge (vj, vi) when i ≠ j. The same
terminology G = (V, E) will be used for directed and undirected graphs; however, it will always
be stated whether the graph is to be interpreted as a directed or undirected graph.

Notice for the directed cyclic graph in Figure 2.9 that the double arrow notations between nodes
v2 and v4 indicate the presence of two edges (v2, v4) and (v4, v2). In this case it is these edges which
form the cycle.
Definition 2.17
A tree is an acyclic connected graph.
Examples of trees are shown in Figure 2.10.
Definition 2.18
An edge, e, in a connected graph, G = (V, E), is a bridge if G′ = (V, E′) is disconnected where
Figure 2.10 Trees
If the edge, e, is removed, the graph, G, is divided into two separate connected graphs. Notice
that every edge in a tree is a bridge.
Definition 2.19
A planar graph is a graph that can be drawn in the plane without any edges intersecting.
An example of a planar graph is shown in Figure 2.11. Notice that it is possible to draw the
graph in the plane with edges that cross although it is still planar.
Definition 2.20
The transitive closure of a directed graph, G = (V1, E1) is a graph, H = (V2, E2), such that,

Figure 2.11 Planar Graph
where f returns a set of edges. The set of edges is as follows:
Thus in Eq. 2.45, . Transitive closure is illustrated in Figure 2.12.
Figure 2.12 Transitive Closure of a Graph
2.5 Parallel Algorithms
This section presents some fundamental properties and definitions used in parallel processing.
2.5.1 Speedup and Amdahls Law
Definition 2.21
The speedup of an algorithm executed using n parallel processors is the ratio of the time for
execution on a sequential machine, TSEQ, to the time on the parallel machine, TPAR:
If an algorithm can be completely decomposed into n parallelizable units without loss of
efficiency then the Speedup obtained is
If however, only a fraction, f, of the algorithm is parallelizable then the speedup obtained is

which yields
This is known as Amdahl's Law. The ratio shows that even with an infinite amount of computing
power an algorithm with a sequential component can only achieve the speedup in Eq. 2.50. If an
algorithm is 50% sequential then the maximum speedup achievable is 2. While this may be a
strong argument against the merits of parallel processing there are many important problems
which have almost no sequential components.
Definition 2.22
The efficiency of an algorithm executing on n processors is defined as the ratio of the speedup to
the number of processors:
Using Amdahl's law
with
2.5.2 Pipelining
Pipelining is a means to achieve speedup for an algorithm by dividing the algorithm into stages.
Each stage is to be executed in the same amount of time. The flow is divided into k distinct
stages. The output of the jth stage becomes the input to the (j + 1) th stage. Pipelining is
illustrated in Figure 2.13. As seen in the figure the first output is ready after four time steps Each
subsequent output is ready after one additional time step. Pipelining becomes efficient when
more than one output is required. For many algorithms it may not be possible to subdivide the
task into k equal stages to create the pipeline. When this is the case a performance hit will be
taken in generating the first output as illustrated in Figure 2.14.

Figure 2.13 A Four Stage Pipeline
Figure 2.14 Pipelining
In the figure TSEQ is the time for the algorithm to execute sequentially. TPS is the time for each
pipeline stage to execute. TPIPE is the time to flow through the pipe. The calculation of the time
complexity sequence to process n inputs yields
for a k-stage pipe. It follows that TPIPE (n) < TSEQ (n) when
The speedup for pipelining is
Example 2.6 Order
which yields
In some applications it may not be possible to keep the pipeline full at all times. This can occur
when there are dependencies on the output. This is illustrated in Example 2.7. For this case let us
assume that the addition/subtraction operation has been set up as a pipeline. The first statement
in the pseudo-code will cause the inputs x and 3 to be input to the pipeline for subtraction. After
the first stage of the pipeline is complete, however, the next operation is unknown. In this case,
the result of the first statement must be established. To determine the next operation the first
operation must be allowed to proceed through the pipe. After its completion the next operation
will be determined. This process is referred to flushing the pipe. The speedup obtained with
flushing is demonstrated in Example 2.8.

Figure 2.15 Full Crossbar Topology
Because of the large number of edges the topology is impractical in design for large n.
2.5.3.2 Rectangular Mesh
A rectangular mesh topology is illustrated in Figure 2.16. From an implementation aspect the
topology is easily scalable. The degree of each node in a rectangular mesh is at most four. A
processor on the interior of the mesh has neighbors to the north, east, south, and west. There are
several ways to implement the exterior nodes if it is desired to maintain that all nodes have the
same degree. For an example of the external edge connection see Problem 2.5.
2.5.3.3 Hypercube
A hypercube topology is shown in Figure 2.17. If the number of nodes, n, in the hypercube
satisfies n = 2d then the degree of each node is d or log (n). As a result, as n becomes large the
number of edges of each node increases. The magnitude of the increase is clearly more
manageable than that of the full crossbar but it can still be a significant problem with hypercube
architectures containing 64K nodes. As a result the cube-connected cycles, described in the next
section, becomes more attractive due to its fixed degree.
The vertices of an n dimensional hypercube are readily described by the binary ordered pair
Figure 2.16 Rectangular Mesh
With this description two nodes are neighbors if they differ in their representation in one location
only. For example for an 8 node hypercube with nodes enumerated
processor (0, 1, 0) has three neighbors: