reserve G, G1, G2 for _Graph, H for Subgraph of G;

theorem Th167:
  for G being connected _Graph, e being object
  st e in the_Edges_of G \ G.loops()
  ex T being plain spanning Tree-like Subgraph of G
  st e in the_Edges_of T
proof
  let G be connected _Graph, e be object;
  assume e in the_Edges_of G \ G.loops();
  then A1: e in the_Edges_of G & not e in G.loops() by XBOOLE_0:def 5;
  set T1 = the plain spanning Tree-like Subgraph of G;
  per cases;
  suppose A2: e in the_Edges_of T1; :: trivial case
    take T1;
    thus thesis by A2;
  end;
  suppose A3: not e in the_Edges_of T1;
    :: the idea is to add the specified edge but remove another
    :: edge from the cycle that will be formed, so that the
    :: resulting graph T3 is a spanning tree containing the specified edge
    set v = (the_Source_of G).e, w = (the_Target_of G).e;
    set G2 = the addEdge of T1,v,e,w;
    e DJoins v,w,G by A1, GLIB_000:def 14;
    then reconsider G2 as Subgraph of G by GLIBPRE1:50;
    :: show that G2 contains only one cycle
    v is Vertex of G & w is Vertex of G by A1, FUNCT_2:5;
    then A4: v is Vertex of T1 & w is Vertex of T1 by GLIB_000:def 33;
    then A5: G2 is non acyclic & for W1, W2 being Walk of G2
        st W1 is Cycle-like & W2 is Cycle-like holds W1.edges() = W2.edges()
      by A3, GLIBPRE1:51;
    then consider W being Walk of G2 such that
      A6: W is Cycle-like by GLIB_002:def 2;
    :: show that the cycle contains another edge e0
    A7: e in W.edges()
    proof
      assume not e in W.edges();
      then reconsider W1 = W as Walk of T1 by A4, GLIB_006:109;
      W1 is Cycle-like by A6, GLIB_006:24;
      hence contradiction by GLIB_002:def 2;
    end;
    G2.loops() c= G.loops() by GLIB_009:48;
    then not e in G2.loops() by A1;
    then consider e0 being object such that
      A8: e0 in W.edges() & e0 <> e by A6, A7, GLIBPRE1:29;
    reconsider e0 as set by TARSKI:1;
    :: T3 is the desired spanning tree
    set T3 = the plain removeEdge of G2,e0;
    set w1 = (the_Source_of G2).e0, w2 = (the_Target_of G2).e0;
    A9: G2 is addEdge of T3,w1,e0,w2 by A8, GLIB_008:37;
    A10: e0 Joins w1,w2,G2 by A8, GLIB_000:def 13;
    e0 in {e0} by TARSKI:def 1;
    then not e0 in the_Edges_of G2 \ {e0} by XBOOLE_0:def 5;
    then A11: not e0 in the_Edges_of T3 by GLIB_000:51;
    w1 in the_Vertices_of G2 & w2 in the_Vertices_of G2 by A10, GLIB_000:13;
    then A12: w1 is Vertex of T3 & w2 is Vertex of T3 by GLIB_000:def 33;
    :: show that T3 is connected
    now
      let v1, v2 be Vertex of T3;
      reconsider u1 = v1, u2 = v2 as Vertex of G2 by GLIB_000:def 33;
      consider W2 being Walk of G2 such that
        A13: W2 is_Walk_from u1,u2 by GLIB_002:def 1;
      set P2 = the Path-like Subwalk of W2;
      A14: P2 is_Walk_from u1,u2 by A13, GLIB_001:160;
      per cases;
      suppose not e0 in P2.edges(); :: trivial case
        then reconsider W3 = P2 as Walk of T3 by GLIB_001:172;
        take W3;
        thus W3 is_Walk_from v1,v2 by A14, GLIB_001:19;
      end;
      suppose A15: e0 in P2.edges();
        :: from the cycle in G2 we can get a path avoiding e0
        consider P4 being Path of G2 such that
          A16: P4 is_Walk_from w1,w2 & P4.edges() = W.edges()\{e0} and
          not e0 in G2.loops() implies P4 is open by A6, A8, A10, GLIBPRE1:32;
        e0 in {e0} by TARSKI:def 1;
        then A17: not e0 in P4.edges() by A16, XBOOLE_0:def 5;
        A18: P4.first() = w1 & P4.last() = w2 by A16, GLIB_001:def 23;
        :: replace e0 in P2 by the avoiding path
        :: the distinction of cases comes from the orientation of e0 in P2
        per cases by A10, A15, GLIB_006:28;
        suppose A19: G2.walkOf(w1,e0,w2) is_odd_substring_of P2, 0;
          set W4 = P2.replaceEdgeWith(e0,P4);
          e0 Joins P4.first(),P4.last(),G2 &
            G2.walkOf(P4.first(),e0,P4.last()) is_odd_substring_of P2, 0
            by A10, A18, A19;
          then not e0 in W4.edges() by A17, GLIB_006:40;
          then reconsider W3 = W4 as Walk of T3 by GLIB_001:172;
          take W3;
          W4 is_Walk_from u1,u2 by A14, GLIB_006:47;
          hence W3 is_Walk_from v1,v2 by GLIB_001:19;
        end;
        suppose A20: G2.walkOf(w2,e0,w1) is_odd_substring_of P2, 0;
          set W4 = P2.replaceEdgeWith(e0,P4.reverse());
          A21: not e0 in P4.reverse().edges() by A17, GLIB_001:107;
          P4.reverse().first() = w2 & P4.reverse().last() = w1
            by A18, GLIB_001:22;
          then e0 Joins P4.reverse().first(),P4.reverse().last(),G2 &
            G2.walkOf(P4.reverse().first(),e0,P4.reverse().last())
              is_odd_substring_of P2, 0 by A10, A20, GLIB_000:14;
          then not e0 in W4.edges() by A21, GLIB_006:40;
          then reconsider W3 = W4 as Walk of T3 by GLIB_001:172;
          take W3;
          W4 is_Walk_from u1,u2 by A14, GLIB_006:47;
          hence W3 is_Walk_from v1,v2 by GLIB_001:19;
        end;
      end;
    end;
    then T3 is connected by GLIB_002:def 1;
    :: since G2 only contains one cycle and T3 is connected, it is a tree
    then A22: T3 is Tree-like by A5, A9, A11, A12, GLIBPRE1:52;
    the_Vertices_of T3 = the_Vertices_of G2 by GLIB_000:def 33
      .= the_Vertices_of T1 by A4, GLIB_006:102
      .= the_Vertices_of G by GLIB_000:def 33;
    then reconsider T3 as plain Tree-like spanning Subgraph of G
      by A22, GLIB_000:def 33, GLIB_000:43;
    take T3;
    not e in {e0} by A8, TARSKI:def 1;
    then e in the_Edges_of G2 \ {e0} by A7, XBOOLE_0:def 5;
    hence e in the_Edges_of T3 by GLIB_000:51;
  end;
end;
