import random


def ER_undirected_generator2(nodes, edge_prob):
    """
    generates a random undirected graph with
    given nodes and edge creation probability
    
    Slightly modified from above algorithm where
    a complete graph generation is not necessary.
    This results in a faster generation of graph.
    """
    
    generated_graph = {}
    
    # fill in each node with blank first
    for node in range(nodes):
        generated_graph[node] = set([])
        
    for tail in range(nodes):
        # iterate over all "head" nodes larger than "tail"
        # each edge possibility is considered only once
        for head in range(tail + 1, nodes):
            if random.random() < edge_prob:
                # make sure to add edge to both nodes
                generated_graph[tail].add(head)
                generated_graph[head].add(tail)

    return generated_graph