class Graph:
    ''' 
    A graph represented using an adjacency matrix    
    '''
    
    def __init__(self, num_vertices, is_directed = False):
        ''' create a new empty graph '''
        self.num_vertices = num_vertices
        self.matrix = [[0]*num_vertices for i in range(num_vertices)]                
        self.is_directed = is_directed
        
    def add_edge(self, v1, v2):
        ''' 
        Add edge from v1 to v2 and v2 to v1. Note, vertices 
        are assumed to be integers in the range 0 to num_vertices-1.         
        ''' 
        # we set both v1->v2 and v2->v1 to 1 since the graph is undirected
        self.matrix[v1][v2] = 1            
        if not self.is_directed: 
            self.matrix[v2][v1] = 1

    def get_neighbours(self, v):
        ''' 
        Return a copy of vertices that are adjacent to vertex v. Note, v
        is assumed to be an integer in the range 0 to num_vertices-1.
        ''' 
        ret = []
        for i in range(self.num_vertices):
            if self.matrix[v][i] == 1:
                ret.append(i)
        return ret
        
    def has_edge(self, v1, v2):
        ''' 
        Return True if edge exists between v1 and v2, False otherwise. 
        Note, vertices are assumed to be integers in the range 0 to
        num_vertices-1.
        '''
        return self.matrix[v1][v2] == 1
        

if __name__ == '__main__':
    
    # create a new undirected Graph with 10 vertices
    g = Graph(10)
    g.add_edge(0,1)
    g.add_edge(1,2)
    g.add_edge(0,2)
    
    # list all the edges in the graph

    # the following has time complexity O(|V|^2)
    
    for i in range(g.num_vertices):
        for j in range(g.num_vertices):
            if g.has_edge(i, j):
                print "(",i,",",j,")"
                
    # alternatively:

    # the following also has time complexity O(|V|^2)
    
    print "------------------------"
    
    for i in range(g.num_vertices):
        adjv = g.get_neighbours(i)
        for j in adjv:
            print "(",i,",",j,")"
                
    
                
        
