//-------------------------------------------------------------
// Quadratic maximum contiguous subsequence sum algorithm.
// seqStart and seqEnd represent the actual best sequence.
//
//-------------------------------------------------------------
.....
template <class Comparable>
Comparable maxSubsequenceSum2( const vector<Comparable> & a,
int & seqStart, int &
seqEnd )
{
int n = a.size( );
Comparable maxSum = 0;
for( int i = 0; i < n; i++ )
{
Comparable thisSum = 0;
for( int j = i; j < n; j++ )
{
thisSum += a[ j ];
if( thisSum > maxSum )
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
}
}
return maxSum;
}
// Test program.
int main( void )
{
.....
vector<int> test; //Empty vector
int ss = -1, se = -1; // ss := SeqStart, se := SeqEnd
.....
.....
template <class Comparable>
class AATree;
template <class Comparable>
class AANode
{
public:
Comparable element;
AANode *left;
AANode *right;
int level;
AANode( ) : left( NULL ), right(
NULL ), level( 1 ) { }
AANode( const Comparable & e, AANode *lt, AANode *rt,
int lv = 1 )
: element( e ), left( lt ), right( rt ), level( lv ) { }
public:
void printPreOrder( ) const;
friend class AATree<Comparable>;
};
template <class Comparable>
class AATree
{
public:
AATree( );
AATree( const AATree & rhs );
~AATree( );
// Recursive traversals, with printing
void printPreOrder( ) const
{ if( root != NULL ) root->printPreOrder( ); }
.....
typedef AANode<Comparable> Node;
private:
Node *root;
Node *nullNode;
.....
Beispielsgraph Nr.1
Beispielsgraph Nr.2
struct Vertex;
struct Edge
{
// First vertex in edge is implicit
Vertex *dest; // Second vertex in edge
double cost; // Edge cost
Edge( Vertex *d = 0, double c = 0.0 )
: dest( d ), cost( c ) { }
};
// Basic info for each vertex.
struct Vertex
{
string name; // Vertex name
vector<Edge> adj; // Adjacent vertices (and costs)
double dist; // Cost
Vertex *prev; // Previous vertex on shortest path
int scratch; // Extra variable used in algorithm
Vertex( const string & nm ) : name( nm )
{ reset( ); }
void reset( )
{ dist = INT_MAX; prev = NULL; scratch = 0; }
};
// Initialize the vertex output info prior to running
// any shortest path algorithm.
void Graph::clearAll( )
{
for( vmap::iterator itr = vertexMap.begin( ); itr != vertexMap.end( ); ++itr )
(*itr).second->reset( );
}
// Single-source unweighted shortest-path algorithm.
void Graph::unweighted( const string & startName )
{
vmap::iterator itr = vertexMap.find( startName );
if( itr == vertexMap.end( ) ) {
cerr << " startName is not a vertex in this graph" << endl;
return;
}
clearAll( );
Vertex *start = (*itr).second;
list<Vertex *> q;
q.push_back( start ); start->dist = 0;
printName( (*itr).second ); //Control
while( !q.empty( ) )
{
Vertex *v = q.front( );
q.pop_front( );
for( int i = 0; i < v->adj.size( ); i++ )
{
Edge e = v->adj[ i ];
Vertex *w = e.dest;
if( w->dist == INT_MAX )
{
printName( e.dest ); //Control
w->dist = v->dist + 1;
cout << "ACTUAL DISTANCE = " << w->dist << endl; //Control
w->prev = v;
q.push_back( w );
}
}
}
}