数据结构之图论算法伪代码(伪代码是一种思想可对照伪代码的实用代码学习算法设计)

/*简单的拓扑排序伪代码*/
void Graph::topsort()
{
  for( int counter = 0; counter    {
       vertex v = findNewVertexOfIndegreeZero();
        if( v == NOT_A_VERTEX )
              throw CycleFoundException();
          v.topNum = counter;
            for each Vertex w adjacent to v
                  w.indegree--;
   }
}

/*施行拓扑排序的伪代码*/
void Graph::topsort()
{
  Queue q;
  int counter = 0;

  q.makeEmpty();
  for each Vertex v
        if( v.indegree == 0 )
            q.enqueue( v );

  while( !q.isEmpty() )
{
   Vertex v = q.dequeue();
    v.topNum = ++counter;

    for each Vertex w adjacent to v
        if( --w.indegree == 0 )
             q.enqueue( w );
}

if( counter != NUM_VERTICES )
     throw CycleFoundException();
}


/*无权最短路径算法的伪代码*/

void Graph:: unweighted( Vertex s )
{
   for each Vertex v
   {
     v.dist = INFINITY;
     v.known = false;
   }
   s.dist = 0;

   for( int currDist = 0; currDist < NUM_VERTICES; currDist++ )
      for each Vertex v
         if( !v.know && v.dist == currDist )
              {
                     v.known = ture;
                   for each Vertex w adjacent to v
                        if( w.dist == INFINITY )
                            {
                               w.dist = currDist + 1;
                               w.path = v;
                            }
                }
}
/*无权最短路径算法的伪代码*/
void Graph::unweighted( Vertex s )
{
  Queue q;
 
  for each Vertex v
        v.dist = INFINITY;

  s.dist = 0;
  q.enqueue( s );
 
   while( !q.isEmpty() )
    {
      Vertex v = q.dequeue();

       for each Vertex w adjacent to v
             if( w.dist == INFINITY )
                  {
                   w.dist = v.dist + 1;
                   w.path = v;
                   q.enqueue( w );
                   }
   }
}

/*Dijkstra算法中的Vertex类(伪代码)*/
/*Pseudocode sketch of the vertex structure.
* in real C++,path would be of type vertex *, and many of the code fragments that we describe
* require either a dereferencing * or use the -> operator instead of the . operator.
*  Needless to say,this obscures the basic algorithmic ideas.
*/
struct Vertex
{
  List adj; //Adjacency list
  bool known;
  DistType dist; //DistType is probably int
  Vertex path;  // Probably Vertex *, as mentioned above
         // Other data and member functions as needed
};

/* 显示实际最短路径的例程*/
/* print shortest path to v after dijkstra has run.
*  Assume that the path exists.
*/
void Graph::printPath(Vertex v )
{
  if( v.path != NOT_A_VERTEX )
  {
     printPath( v.path );
      cout << " to " ;
  }
  cout << v;
}

/* Dijkstra 算法的伪代码*/
Void Graph::dijkstra( Vertex s )
{
  for each Vertex v
   {
     v.dist = INFINITY;
     v.known = false;
   }
  s.dist = 0;

  for( ; ;)
   {
     Vertex v = smallest unknown distance vertex;
      if( v == NOT_A_VERTEX )
         break;
       v.known = true;

        for each Vertex w adjacent to v
             if( !w.known )
                  if(v.dist + cvw < w.dist )
                      {
                        //Update w
                            decrease( w.dist to v.dist + cvw );
                                 w.path = v;
                      }
   }
}

/*查找字梯的C++ 的C++程序*/
/*Run the shortest path calculation from the adjacency map,returning a map that contains the "pre" entries for each word in the graph.*/
map findChain( const map > & adjacentWords,const string&first )
{
  map previousWord;
  queue q;
 
  q.push( first );
  while( !q.empty() )
  {
    string current = q.front(); q.pop();
    
     map >::const_iterator itr;
      itr = adjacentWord.find( current );
       
      const vector & adj = itr->second;
      for( int i=0;i            if( previousWord[adj[i] ] ==" ")
                {
                   previousWord[adj[i]] = current;
                      q.push( adj[i] );
                }
    }
   previousWord[ first ] = " ";

    return previousWord;
}
// After the shortest path calculation has run,computes the vector that
// contains the swquence of word changes to get from first to second.
vector getChainFromPreMap( const map & previous, const sting&second )
{
   vector result;
   map & prev = const_cast & (previous);

   for( string current = second; current != " "; current = prev[ current ] )
         result.push_back( current );

     reverse( result.begin(),result.end() );

      return result;
}

/*Kruskal算法的伪代码*/
void Graph::kruskal()
{
   int edgesAccepted = 0;
   DisjSet ds( NUM_VERTICES );
   priorityQueue pq( getEdges() );
   Edge e;
   Vertex u,v;

   while(edgesAccepted < NUM_VERTICES - 1 )
   {
     pq.deleteMin( e ); //Edge e = (u,v)
     SetType uset = ds.find( u );
     SetType vset = ds.find( v );
      if ( uset != vset )
        {
          // Accept the edge
           edgesAccepted++;
            ds.unionSets( uset, vset );
        }
    }
}

/*深度优先搜索模板(伪代码)*/
void Graph::dfs( Vertex v )
{
  v.visited = true;
  for each Vertex w adjacent to v
        if( !w.visited )
             dfs( w );
}

/*Fig10-40 计算斐波那契数的低效算法*/
/* Compute Fibonacci numbers as described in Chapter 1.*/
int fib( int n )
{
  if( n<= 1 )
     return 1;
   else
      return fib(n-1) + fib( n-2 );
}
/* Fig10-41 计算斐波那契数的线性算法*/
/* Compute Fibonacci numbers as described in Chapter 1. */
int fibonacci( int n )
{
  if( n <= 1 )
    return 1;
   
  int last = 1;
  int nextToLast = 1;
  int answer = 1;
  for( int i =2 ; i<= n; i++ )
  {
    answer = last + nextToLast;
    nextToLast = last;
    last = answer;
  }
  return answer;
}

你可能感兴趣的:(科学技术)