topics: 1. finding a cycle in a graph 2. propagation delay - example 3. trees - properties מבנה...

30
Topics: 1. Finding a cycle in a graph 2. Propagation delay - example 3. Trees - properties בבבב- בבבב בבבבב2004 בבבבב3#

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Topics:1. Finding a cycle in a graph2. Propagation delay - example 3. Trees - properties

2004מבנה המחשב - אביב

3#תרגול

Checking if a Circuit is combinational• Mapping the circuit into a directed graph DG(V,E).

Each gate is a vertex.

Each net defines a star of edges emanating out of an output terminal.

OR

AND

NOT XOR OUT

IN

IN

Single net

Checking a Circuit (cont.)

• If an input terminal has two incoming edges, then it is fed by two nets, which is illegal. In that case we stop and return FALSE.

OR

AND

NOT XOR OUT

IN

IN

Two inputs to

one terminal !

Checking a Circuit (cont.)

• Is this condition enough for a circuit to be valid?

• No, we still need to check that the graph of the circuit contains no cycles.

Testing for Cycles

• Two classical algorithms:

• Topological sort based:– While “peeling off” sources, if there are no

sources but the set of non-sorted vertices is not empty, then return FALSE (proof: next slide).

• DFS based:– If a backwards edge is encountered, return

FALSE (proof: auxiliary slides at the end).

Existence of cycles• Claim: If a directed (sub-)graph G has no sources, then it

contains a cycle.• Proof (algorithmic):

– Note: No sources For all u, in(u) > 0 |E| > |V|-1.– Delete repeatedly all sinks (this does not create

sources)– Pick any vertex u and mark it.– Pick an edge (u,v)

• If v is marked: a cycle was found!!!• Else: pick an edge emanating from v…repeat

– Observation: The process must encounter a marked vertex (can’t have an infinite sequence of unmarked vertices). Hence, we close a cycle!

Propagation Delay

}{

pdpathsallpd

tMaxT

There is always at least one “critical path”.

What is the propagation delay of a circuit that is not acyclic?

Finding the Propagation Delay

)}'()'({)()( '

vtvtMaxvt pd

stableinputsvrpredecessov

stableinputs

acyclic graph topological order.

Each time updating the “inputs stable” time of the next vertex. This update is often called relaxation.

The total propagation delay is the maximal “inputs stable” time (assuming that we used output nodes).

This is also the delay of a critical path in DG(V,E).

Finding the Maximum Delay

0 0 OR AND NOT XOR OUT

A circuit with 2n/2 paths

2 options 2 options

Note: n/2 stages with 2 options each, resulting in 2n/2 paths.

There is no need to check all the paths, only the longest. This takes a linear time.

A circuit with 2n paths• …cannot be built!

• Why? A combinational circuit is a DAG, therefore we cannot reorder the gates to create different paths. Our only option is to include or exclude gates to create different paths.

• But, having n gates, we only have 2n such paths. Each gate can be included or excluded, therefore 2n.

• We cannot build this circuit since we will require an unbounded in-degree of the gates.

XOR is Associative•Define mod(a,q) to be the remainder of a/q.

•Claim:

– mod(a+b,q) = mod( mod(a,q) + mod(b,q),q)

– mod(a · b,q) = mod( mod(a,q) · mod(b,q),q)

•Proof:

– Let a = kq+j and b = sq+t. a+b = q(k+s) + j+t

mod(a+b,q) = mod(j+t,q) = mod(mod(a,q)+mod(b,q),q)

–The same proof for multiplication.

XOR is Associative (cont.)•Note that xor(x,y) = mod(x+y,2)

xor(xor(x,y),z) = mod(mod(x+y,2)+mod(z,2),2)

= mod((x+y)+z,2)

= mod(x+(y+z),2)

= mod(mod(x,2)+mod(y+z,2),2)

= xor(x,xor(y,z))

•Hence we may denote it by xor(x,y,z).

Tree: Basic properties•An undirected graph is a tree if it is:

•Connected, i.e., there is a path between every two vertices.

•Contains no cycles.

•One of these properties can be replaced with the following:

•|E| = |V|-1

Directed Tree: Basic properties•An directed graph is a directed tree if it is:

•In-degree = 1 for all v, besides the root.

•In degree of the root is 0.

•There is a directed path from the root to every node.

•Tree directed tree:

•Pick one of its vertices to be a root

•Direct all the edges out from the root

•Repeat it with the vertices as the roots.

Directed Tree (cont.)•A leaf is a node with out-degree zero.

•We define on the vertices of a directed tree:

•The parent of a node v. Note there is only one such vertices.

•A child of a node v (there may be many).

•Ancestor and Descendent of v.

The root (source): Ancestor for all x, but has no parent.

Internal node: has a parent, may have children.

A leaf (sink): has no children.

A claim on Trees•In a directed tree, any non-leaf is called an internal node.

•For a tree: a leaf is a node whose degree is 1.

•Claim: If the degree of any vertex in the tree 3, then the number of internal nodes the number of the leaves –2.

•Note: the underlying graph of a directed binary tree is such a tree, but since the root does not count as a leaf, we have in directed trees:

#internal nodes #internal leaves -1

A claim on Trees (cont.)•Proof (Induction on the number of nodes):

•Basis: trivial for n=1,2 , check for all trees with 3 nodes (there is only one such tree).

•Assumption: correct for all trees of size n.

•Step: prove for any tree of size n+1. Given such a tree, it must contain an internal node v.

•There exists two trees T1,T2 such that connecting them yields T.

•Define |Ti|=ni = mi+ki, (m is #leaf, k is # internal.

A claim on Trees (cont.)n = n1 + n2.

ki mi - 2 (From the induction hypothesis).

Note that the connection may reduce the number of leaves by at most two, the claim follows.

The next slides are for the curious students (home reading)

DFS Example

1

2

4

5

3

Stack

DFS

1

1

2

4

5

3

Stack

= Currently in the stack

DFS

1

2

1

2

4

5

3

Stack

DFS

1

2

3

1

2

4

5

3

Stack

DFS

1

2

3

1

2

4

5

3

4

Stack

Backwards Edge

This graph has a cycle!

Elements of the Proof

• The algorithm terminates regardless of the structure of the input graph.

• If there is a cycle in the graph, the algorithm will find it (return FALSE).

• If there is no cycle in the graph, the algorithm will return TRUE.

The Algorithm Terminates

• The algorithm passes through every vertex only once, therefore it will always terminate after visiting all of the vertices regardless of the edges.

The Proof

• Assume there is a cycle in the graph. At some point a first vertex that belongs to the cycle will be reached. All other vertices of the cycle have not been reached yet.

• Before that first vertex is popped out of the stack, the DFS procedure guarantees that an edge closing the cycle and entering that vertex will be tested.

• It is a backwards edge.

The Proof

• Immediate, but nevertheless:

• Assume there are no cycles in the graph.

• Backwards edges cannot exist since they require a path from a successor to a predecessor, which means there is a cycle.