function [ vtoe_pointer, vtoe ] = mesh_vtoe ( e_order, e_num, etov, v_num )

%*****************************************************************************80
%
%% mesh_vtoe() determines vertex-to-element information.
%
%  Discussion:
%
%    The input information from ETOV:
%
%    E_ORDER = 3
%    E_NUM = 16
%    V_NUM = 13
%
%    E          ETOV
%    --------   ---------------
%     1         3      4      1
%     2         3      1      2
%     3         3      2      8
%     4         2      1      5
%     5         8      2     13
%     6         8     13      9
%     7         3      8      9
%     8        13      2      5
%     9         9     13      7
%    10         7     13      5
%    11         6      7      5
%    12         9      7      6
%    13        10      9      6
%    14         6      5     12
%    15        11      6     12
%    16        10      6     11
%
%    The output information in VTOE_POINTER(V_NUM+1), VTOE(E_ORDER*E_NUM):
%
%    V   VTOE_POINTER   VTOE
%    --  ------------   -------
%
%     1         1        1
%                        2
%                        4
%     2         4        2
%                        3
%                        4
%                        5
%                        8
%     3         9        1
%                        2
%                        3
%                        7
%     4        13        1
%     5        14        4
%                        8
%                       10
%                       11
%                       14
%     6        19       11
%                       12
%                       13
%                       14
%                       15
%                       16
%     7        25        9
%                       10
%                       11
%                       12
%     8        29        3
%                        5
%                        6
%                        7
%     9        33        6
%                        7
%                        9
%                       12
%                       13
%    10        38       13
%                       16
%    11        40       15
%                       16
%    12        42       14
%                       15
%    13        44        5
%                        6 
%                        8
%                        9
%                       10
%              49
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 September 2017
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer E_ORDER, the order of the elements.
%
%    integer E_NUM, the number of elements.
%
%    integer ETOV(E_ORDER,E_NUM), the vertices that 
%    make up each element.
%
%    integer V_NUM, the number of vertices.
%
%  Output:
%
%    integer VTOE_POINTER(V_NUM+1), a pointer into the
%    array VTOE.  Information for node V is stored in VTOE entries
%    VTOE_POINTER(V) through VTOE_POINTER(V+1)-1.
%
%    integer VTOE(E_ORDER*E_NUM), the elements that contain each vertex.
%

%
%  Step 1.
%  From ETOV, create a list of pairs (V,E).
%
  ve = zeros ( e_order * e_num, 2 );

  k = 0;
  for e = 1 : e_num
    for o = 1 : e_order
      k = k + 1;
      ve(k,1) = etov(o,e);
      ve(k,2) = e;
    end
  end
%
%  Sort the rows of VE.
%
  ve = sortrows ( ve ); 
%
%  STEP 2:
%  Create the pointer array.
%  (Some of this coding is done this way to allow for the case where
%  some nodes are not used.  They still need an entry VTOE_POINTER.)
%
  vtoe_pointer = zeros ( v_num + 1, 1 );

  old = 0;
  for k = 1 : e_order * e_num
    new = ve(k,1);
    if ( new ~= old )
      for v = old + 1 : new
        vtoe_pointer(v) = k;
      end
      old = new;
    end
  end
  vtoe_pointer(v_num+1) = e_order * e_num + 1;
%
%  STEP 3:
%  Create VTOE.
%
  vtoe = zeros ( e_order * e_num, 1 );

  vtoe(1:e_order * e_num,1) = ve(1:e_order * e_num,2);

  return
end

