1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#ifndef STACK_GRAPH_H #define STACK_GRAPH_H #define hca(x) (fTime(x)) #include "Stack.h" #include "Queue.h"
using VStatus = enum { UNDISCOVERED, DISCOVERED, VISITED }; using EType = enum { UNDETERMINED, TREE, CROSS, FORWARD, BACKWARD };
template <typename Tv, typename Te> class Graph { private: void reset() { for ( int i = 0; i < n; i++ ) { status ( i ) = UNDISCOVERED; dTime ( i ) = fTime ( i ) = -1; parent ( i ) = -1; priority ( i ) = INT_MAX; for ( int j = 0; j < n; j++ ) if ( exists ( i, j ) ) type ( i, j ) = UNDETERMINED; } } void BFS(int,int&); void DFS(int,int&); bool TSort(int,int&,Stack<Tv>*); public: int n; virtual int insert ( Tv const& ) = 0; virtual Tv remove ( int ) = 0; virtual Tv& vertex ( int ) = 0; virtual int inDegree ( int ) = 0; virtual int outDegree ( int ) = 0; virtual int firstNbr ( int ) = 0; virtual int nextNbr ( int, int ) = 0; virtual VStatus& status ( int ) = 0; virtual int& dTime ( int ) = 0; virtual int& fTime ( int ) = 0; virtual int& parent ( int ) = 0; virtual int& priority ( int ) = 0; int e; virtual bool exists ( int, int ) = 0; virtual void insert ( Te const&, int, int, int ) = 0; virtual Te remove ( int, int ) = 0; virtual EType & type ( int, int ) = 0; virtual Te& edge ( int, int ) = 0; virtual int& weight ( int, int ) = 0; void bfs ( int ); void dfs ( int ); Stack<Tv>* tSort ( int )
};
template <typename Tv, typename Te> void Graph<Tv, Te>::bfs ( int s ) { reset(); int clock = 0; int v = s; do if ( UNDISCOVERED == status ( v ) ) BFS ( v, clock ); while ( s != ( v = ( ++v % n ) ) ); }
template <typename Tv, typename Te> void Graph<Tv, Te>::BFS ( int v, int& clock ) { Queue<int> Q; status ( v ) = DISCOVERED; Q.enqueue ( v ); while ( !Q.empty() ) { int v = Q.dequeue(); dTime ( v ) = ++clock; for ( int u = firstNbr ( v ); -1 < u; u = nextNbr ( v, u ) ) if ( UNDISCOVERED == status ( u ) ) { status ( u ) = DISCOVERED; Q.enqueue ( u ); type ( v, u ) = TREE; parent ( u ) = v; } else { type ( v, u ) = CROSS; } status ( v ) = VISITED; } }
template <typename Tv, typename Te> void Graph<Tv, Te>::dfs ( int s ) { reset(); int clock = 0; int v = s; do if ( UNDISCOVERED == status ( v ) ) DFS ( v, clock ); while ( s != ( v = ( ++v % n ) ) ); }
template <typename Tv, typename Te> void Graph<Tv, Te>::DFS ( int v, int& clock ) { dTime ( v ) = ++clock; status ( v ) = DISCOVERED; for ( int u = firstNbr ( v ); -1 < u; u = nextNbr ( v, u ) ) switch ( status ( u ) ) { case UNDISCOVERED: type ( v, u ) = TREE; parent ( u ) = v; DFS ( u, clock ); break; case DISCOVERED: type ( v, u ) = BACKWARD; break; default: type ( v, u ) = ( dTime ( v ) < dTime ( u ) ) ? FORWARD : CROSS; break; } status ( v ) = VISITED; fTime ( v ) = ++clock; }
template <typename Tv, typename Te> Stack<Tv>* Graph<Tv, Te>::tSort ( int s ) { reset(); int clock = 0; int v = s; Stack<Tv>* S = new Stack<Tv>; do { if ( UNDISCOVERED == status ( v ) ) if ( !TSort ( v, clock, S ) ) { while ( !S->empty() ) S->pop(); break; } } while ( s != ( v = ( ++v % n ) ) ); return S; }
template <typename Tv, typename Te> bool Graph<Tv, Te>::TSort ( int v, int& clock, Stack<Tv>* S ) { dTime ( v ) = ++clock; status ( v ) = DISCOVERED; for ( int u = firstNbr ( v ); -1 < u; u = nextNbr ( v, u ) ) switch ( status ( u ) ) { case UNDISCOVERED: parent ( u ) = v; type ( v, u ) = TREE; if ( !TSort ( u, clock, S ) ) return false; break; case DISCOVERED: type ( v, u ) = BACKWARD; return false; default: type ( v, u ) = ( dTime ( v ) < dTime ( u ) ) ? FORWARD : CROSS; break; } status ( v ) = VISITED; S->push ( vertex ( v ) ); return true; }
#endif
|