import random


def approx_vertex_cover(graph_obj, random_selection=False):
    """Calculate a vertex cover for the graph using an approximation algorithm.

    Note: This function utilizes objects from the networkx package.
    Ref: Introduction to Algorithms 3rd. Ed. pg. 1108-1111

    :param graph_obj: Graph object (networkx).
    :return: Approximate vertex cover for the given graph.
    """

    cover = []
    edges = graph_obj.edges()

    while len(edges) > 0:

        # Choose edge in list of edges
        if random_selection:
            uv = edges[random.randint(0, len(edges) - 1)]
        else:
            uv = edges[0]

        # Add nodes to the VC
        cover.append(uv[0])
        cover.append(uv[1])

        # Remove any edges touching the currently chosen nodes.
        edges[:] = [e for e in edges if not (uv[0] in e or uv[1] in e)]

    return cover