FIND SHORTEST PATHS IN A NETWORK REPRESENTED BY A COST-ADJACENCY MATRIX:

        int cost[numVertices][numVertices];
        int dist[numVertices];

        void shortestPath(int start)    
        /* Store in dist[i] the cost of the shortest path from v to i */
          {
            int i, j, w, min;
            bool pathFound[numVertices];
        
            /* Initialize dist to either the cost of a direct path from v to i,
               if there is one, or maxint if there is not */
        
            for (i = 0; i < numVertices; i ++)
                dist[i] = cost[v][i];
        
            /* Initialize pathsFound */
        
            for (i = 0; i < numVertices; i ++)
                pathFound[i] = false;
            pathFound[v] = true;
        
            /* Generate all numVertices -1 shortest paths */
        
            for (i = 0; i < numVertices - 1; i ++)
              {
        
                /* Find the vertex w such that dist[w] is minimum, and 
                   pathFound[w] is false. This will be the terminal of our 
                   newest path */
        
                min = maxint;
                for (j = 0; j < numVertices; j ++)
                  if (! pathFound[j] && dist[j] < min)
                    { w = j; min = dist[j]; }
        
                pathFound[w] = true;
        
                /* Can we find shorter paths to our other vertices through w? */
        
                for (j = 0; j < numVertices; j ++)
                  if (! pathFound[j] && dist[w] + cost[w][j] < dist[j])
                        dist[j] = dist[w] + cost[w][j]
              }
          }

        TOPOLOGICAL SORT ON A DIGRAPH REPRESENTED BY ADJACENCY LISTS:

        struct
          { int count;
            bool visited;
            Node * edgeList;
          } vertex[numVertices];

        void topsort();
        /* Outputs a list of the vertices of g in topological order */
          {
            int i, j;
            Node * p;
            bool found;

            /* Initialize the count and visited field of each vertex *
        
            for (i = 0; i < numVertices; i ++)
              { vertex[i].count = 0; vertex[i].visited = false; }

            for (i = 0; i < numVertices; i ++)
              {
                Node * p = vertex[i].edgeList;
                while (p != NULL)
                  {
                    vertex[p -> head].count ++;
                    p = p -> link;
                  }
              }
        
            /* Do the sort */
        
            for (i = 0; i < numVertices; i ++)
              { 
                /* Find an unvisited vertex which can now be visited */

                found = false; j = 0;
                while (! found && j < numVertices)
                    if (! vertex[j].visited && vertex[j].count = 0)
                        found = true;
                    else
                        j ++;
                if (! found) 
                  { cerr << "Cycle in graph" << endl; return; }
        
                /* Visit it */
        
                cout << j << end;
                vertex[j].visited : true;
        
                /* Reduce count of all its successors */
        
                p = vertex[j].edgeList;
                while (p != NULL)
                  { vertex[p -> head].count --;
                    p = p -> link;
                  }
              }
          }

Copyright ©1998 - Russell C. Bjork