7 Determining the Degree of Each Vertex

If the graph G=V,EG=(V,E) is represented by its adjacency list A⁢GA(G) and vertex list V⁢GV(G), Algorithm 13 develops a mapping λ\lambda whose domain is V⁢GV(G) and whose range is the degree of V⁢GV(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 k≠e⁢o⁢lk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(VV)
while k≠e⁢o⁢lk\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 V⁢GV(G) and edge list E⁢GE(G), Algorithm 14 maps each vertex in V⁢GV(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 k≠e⁢o⁢lk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(EE)
while k≠e⁢o⁢lk\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 k≠e⁢o⁢lk\neq eol
   map(λ,v,0\lambda,v,0)
   k=k=next(V,kV,k)
k=k=first(EE)
while k≠e⁢o⁢lk\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 O⁢1O(1), creating λ\lambda is linear, i.e. has complexity O⁢V+EO\left(|V|+|E|\right).