1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Graph Implementation

Discussion in 'C' started by ihatec, Oct 26, 2010.

  1. ihatec

    ihatec New Member

    Joined:
    Sep 1, 2010
    Messages:
    20
    Likes Received:
    3
    Trophy Points:
    0
    Hi, I have to implement a graph as a adjacent matrix for my algorithms & data structure course and almost everything works correct besides method given below. The method should removes given vertex from the graph and all the edges containing it.
    Returns: 0 if no error occurs or -1 otherwise
    It does not produce the proper output(does not remove the given vertex and edges). I even draw and analize this step by step on a paper and I thing there is a problem with the part

    if(i==vertex||j==vertex)
    continue;
    array[j]=matrix[j];

    but I am not sure that this is the problem, maybe it is somewhere else. Please help me with this.

    Code:
    count - size of the adjacent matrix(number of vertices) 
    adjacent[i][j] - matrix representing the graph.(from this graph vertex should be removed)
    
    int AdjacencyMatrixGraph::removeVertex(int vertex)
    {
        if(vertex>=count||vertex<0)
            return -1;
        if(count==0)
            return -1;
        else
        {
            int **tablica=new int*[count-1];
            for(int i=0;i<count-1;i++)
                tablica[i]=new int[count-1];
            
            if(!tablica)
                return -1;
            
            for(int i=0;i<count-1;i++)
                for(int j=0;j<count-1;j++)
                    tablica[i][j]=0;
    
            for(int i=0;i<count-1;i++)
                for(int j=0;j<count-1;j++)
                {
                    if(i==vertex || j==vertex)
                        continue;
                    tablica[i][j]=adjacent[i][j];
                    
                }
    
            for(int i=0; i<count;i++)
            {
                delete [] adjacent[i];
            }
            delete [] adjacent;
            
            adjacent=tablica;
            count--;
            return 0;
        }
    }
     
    Last edited by a moderator: Oct 27, 2010

Share This Page