bridge - algorithm

5
BRIDGE In graph theory, a bridge (also known as a cut-edge or cut arc or an isthmus) is an edge whose deletion increases the number of connected components. Equivalently, an edge is a bridge if and only if it is not contained in any cycle. A graph is said to be bridgeless if it contains no bridges. It is easy to see that this is equivalent to 2-edge-connectivity of each nontrivial component.

Upload: -sufi

Post on 22-Nov-2014

1.052 views

Category:

Documents


0 download

DESCRIPTION

BRIDGEIn graph theory, a bridge (also known as a cut-edge or cut arc or an isthmus) is an edge whose deletion increases the number of connected components. Equivalently, an edge is a bridge if and only if it is not contained in any cycle. A graph is said to be bridgeless if it contains no bridges. It is easy to see that this is equivalent to 2-edge-connectivity of each nontrivial component.A graph with 6 bridges (highlighted in red)Bridge-Finding AlgorithmAn O( | V | + | E | ) algorithm f

TRANSCRIPT

Page 1: Bridge - Algorithm

BRIDGEIn graph theory, a bridge (also known as a cut-edge or cut arc or an isthmus) is an edge whose deletion increases the number of connected components. Equivalently, an edge is a bridge if and only if it is not contained in any cycle.

A graph is said to be bridgeless if it contains no bridges. It is easy to see that this is equivalent to 2-edge-connectivity of each nontrivial component.

A graph with 6 bridges (highlighted in red)

Page 2: Bridge - Algorithm

Bridge-Finding Algorithm

An O( | V | + | E | ) algorithm for finding bridges in a connected graph was found by Tarjan in 1974. A distributed version of the algorithm also exists

Algorithm:

1. Find a spanning tree of G2. Create a rooted tree T from the spanning tree3. Traverse the tree T in preorder and number the nodes. Parent nodes in the tree now have

lower numbers than child nodes.4. For each node from v1 (the leaf nodes of the tree) to 1 (the root node of the tree) do:

1. Compute the number of descendants ND(v) for this node.2. Compute L(v) and H(v)

3. For each w such that : if and H(w) < w + ND(w) then (v,w) is a bridge.

Definitions: A non-tree edge between v and w is denoted by v − − w. An in-tree edge with v as the parent is denoted by .

where v is the parent node of w.

ND(v) is the number of descendants of v (including itself) in the rooted spanning tree.

L(v) and H(v) are the labels of the nodes with lowest and highest preorder label respectively reachable from v by travelling in the subtree rooted at v, along with at most one non-tree edge.

This algorithm works because LD(v), H(v) and L(v) can all be computed for a node v provided we know their values on all in-tree descendants of v. Also, if and only if the edge is a bridge, then it is clear that in the subtree rooted at w, it must be impossible to reach any node that is not a descendant of w. This is easy to check because the subtree rooted at w (that is, all

descendants of w) consists of the nodes so we can simply check if L(w),H(w) are in this set or not to check whether an edge is a bridge.

Page 3: Bridge - Algorithm

Bridge design pattern   demo The motivation is to decouple the Time interface from the Time implementation, while still allowing the abstraction and the realization to each be modelled with their own inheritance hierarchy. The implementation classes below are straight-forward. The interface classes are a little more subtle. Routinely, a Bridge pattern interface hierarchy “hasa” implementation class. Here the interface base class “hasa” a pointer to the implementation base class, and each class in the interface hierarchy is responsible for populating the base class pointer with the correct concrete implementation class. Then all requests from the client are simply delegated by the interface class to the encapsulated implementation class.

Code:#include <iostream.h>#include <iomanip.h>#include <string.h>

class TimeImp { public: TimeImp(int hr, int min) { hr_ = hr; min_ = min; } virtual void tell() { cout << "time is " << setw(2) << setfill(48) << hr_ << min_ << endl; } protected: int hr_, min_;};

class CivilianTimeImp: public TimeImp { public: CivilianTimeImp(int hr, int min, int pm): TimeImp(hr, min) { if (pm) strcpy(whichM_, " PM"); else strcpy(whichM_, " AM"); }

/* virtual */ void tell() { cout << "time is " << hr_ << ":" << min_ << whichM_ << endl; } protected: char whichM_[4];};

class ZuluTimeImp: public TimeImp { public: ZuluTimeImp(int hr, int min, int zone): TimeImp(hr, min) { if (zone == 5) strcpy(zone_, " Eastern Standard Time");

Page 4: Bridge - Algorithm

else if (zone == 6) strcpy(zone_, " Central Standard Time"); }

/* virtual */ void tell() { cout << "time is " << setw(2) << setfill(48) << hr_ << min_ << zone_ << endl; } protected: char zone_[30];};

class Time { public: Time(){} Time(int hr, int min) { imp_ = new TimeImp(hr, min); } virtual void tell() { imp_->tell(); } protected: TimeImp *imp_;};

class CivilianTime: public Time { public: CivilianTime(int hr, int min, int pm) { imp_ = new CivilianTimeImp(hr, min, pm); }};

class ZuluTime: public Time { public: ZuluTime(int hr, int min, int zone) { imp_ = new ZuluTimeImp(hr, min, zone); }};

int main() { Time *times[3]; times[0] = new Time(14, 30); times[1] = new CivilianTime(2, 30, 1); times[2] = new ZuluTime(14, 30, 6); for (int i = 0; i < 3; i++) times[i]->tell();}