DFS ON A DIGRAPH REPRESENTED AS AN ADJACENCY MATRIX:

        const
            maxvertices = ~~~;

        type
            vertexno = 1..maxvertices;

            graph = record
                novertices: vertexno;
                visited: array[vertexno] of boolean;
                edge: array[vertexno,vertexno] of boolean
            end;

        procedure DFS(var g: graph; v: vertexno);
        (* Visits vertices in g in depth first order starting at v.  As each
           vertex is visited, its number is written out *)

            var
                i: vertexno;

            procedure SearchFrom(v: vertexno);
            (* Actually does the visiting and searching *)

                var
                   j: vertexno;

                begin
                 with g do
                  begin

                     writeln(v);
                     visited[v] := true;
                     for j := 1 to novertices do
                        if edge[v,j] and not visited[j] then
                          SearchFrom(j)
                  end
                end;

            begin
             with g do
              begin

                (* Mark all vertices as not yet visited *)

                for i := 1 to NoVertices do
                    visited[i] := false;

                (* Do the search *)

                SearchFrom(v)

               end
            end;



        BFS ON A DIGRAPH REPRESENTED BY ADJACENCY LISTS:

        type
            (* vertexno, edgeptr, edgenode, graph as above *)

            vertextype = record
                visited: boolean;               (* new field since above *)
                edgelist: edgeptr
            end;

        procedure BFS(var g: graph; v: vertexno);
        (* Visits vertices in g in breadth first order starting at v.  As each
           vertex is visited, its name is written out *)

            var
                tail,head: vertexno;
                p: edgeptr;
                q: queue { of vertexno };

            begin
             with g do
              begin

                (* Mark all vertices as not yet visited *)

                for tail := 1 to NoVertices do
                    vertex[tail].visited := false;

                (* Do the search *)

                createq(q);
                insertq(v,q); vertex[v].visited := true;

                while not isemptyq(q) do
                  begin
                    removeq(q,tail);
                    writeln(tail)
                    p := vertex[tail].edgelist;
                    while p <> nil do
                      begin
                        head := p^.head;
                        if not vertex[head].visited then
                          begin
                            vertex[head].visited := true;
                            insertq(head,q)
                          end;
                        p := p^.link
                      end
                   end
               end
             end;

Copyright ©1999 - Russell C. Bjork