7 Determining the Degree of Each Vertex

If the graph G=V,EG=(V,E) is represented by its adjacency list AGA(G) and vertex list VGV(G), Algorithm 13 develops a mapping λ\lambda whose domain is VGV(G) and whose range is the degree of VGV(G). It assumes the buffer vv provides communication with the vertex list.

Algorithm 13: Determine Vertex Degree Using Adjacency List
k=k=first(VV)
while keolk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(VV)
while keolk\neq eol
   for all vertices ww adjacent to vv
      n=n=evaluate(λ,w\lambda,w)+1+1
      map(λ,v,n\lambda,v,n)
   k=k=next(V,kV,k)

If GG is a directed graph, represented by its vertex list VGV(G) and edge list EGE(G), Algorithm 14 maps each vertex in VGV(G) to its degree. It is assumed that each edge is represented by an ordered pair of vertices v,w(v,w) and the buffer ee provides communication with the edge list.

Algorithm 14: Determine Vertex Degree of Directed Graph Using Edges
k=k=first(VV)
while keolk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(EE)
while keolk\neq eol
   n=n=evaluate(λ,e.v\lambda,e.v)+1+1
   map(λ,e.v,n\lambda,e.v,n)
   k=k=next(E,kE,k)

If GG is an undirected graph, Algorithm 15 performs the same operation. It is assumed that each edge is represented by an unordered pair of vertices v,w(v,w).

Algorithm 15: Determine Vertex Degree of Undirected Graph Using Edges
k=k=first(VV)
while keolk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(EE)
while keolk\neq eol
   n=n=evaluate(λ,e.v\lambda,e.v)+1+1
   map(λ,e.v,n\lambda,e.v,n)
   n=n=evaluate(λ,e.w\lambda,e.w)+1+1
   map(λ,e.w,n\lambda,e.w,n)
   k=k=next(E,kE,k)

If all the elementary operations are O1O(1), creating λ\lambda is linear, i.e. has complexity OV+EO\left(|V|+|E|\right).