Insertion Deletion of Vertices and Edges in Graph using Adjacency list

By | 13.05.2017

Insertion Deletion of Vertices and Edges in Graph


Write a C Program for Insertion Deletion of Vertices and Edges in Directed Graph using Adjacency list. Here’s simple Program for Insertion Deletion of Vertices and Edges in Graph using Adjacency list in C Programming Language.


Adjacency Matrix:


Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.

Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.

Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time.


Also Check : : C Program for Creation of Adjacency Matrix

Below is the source code for C Program for Insertion Deletion of Vertices and Edges in Graph using Adjacency list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Insertion Deletion of Vertices and Edges in Graph using Adjacency list */

#include<stdio.h>
#include<stdlib.h>

struct Edge;

struct Vertex
{
        int info;
        struct Vertex *nextVertex;  /*next vertex in the linked list of vertices*/
        struct Edge *firstEdge;  /*first Edge of the adjacency list of this vertex*/
}*start = NULL;

struct Edge
{
        struct Vertex *destVertex;  /*Destination vertex of the Edge*/
        struct Edge *nextEdge; /*next Edge of the adjacency list*/
};

struct Vertex *findVertex(int u);
void insertVertex(int u);
void insertEdge(int u,int v);
void deleteEdge(int u,int v);
void deleteIncomingEdges(int u);
void deleteVertex(int u);
void display();

int main()
{
        int choice,u,origin,destin;

        while(1)
        {
                printf("\n1.Insert a Vertex\n");
                printf("2.Insert an Edge\n");
                printf("3.Delete a Vertex\n");
                printf("4.Delete an Edge\n");
                printf("5.Display\n");
                printf("6.Exit\n");
                printf("\nEnter your choice : ");
                scanf("%d",&choice);

                switch(choice)
                {
                 case 1:
                        printf("\nEnter a vertex to be inserted : ");
                        scanf("%d",&u);
                        insertVertex(u);
                        break;
                 case 2:
                        printf("\nEnter an Edge to be inserted : ");
                        scanf("%d %d",&origin,&destin);
                        insertEdge(origin,destin);
                        break;
                 case 3:
                        printf("\nEnter a vertex to be deleted : ");
                        scanf("%d",&u);
                        /*This function deletes all edges coming to this vertex*/
                        deleteIncomingEdges(u);
                        /*This function deletes the vertex from the vertex list*/
                        deleteVertex(u);
                        break;
                 case 4:
                        printf("\nEnter an edge to be deleted : ");
                        scanf("%d %d",&origin,&destin);
                        deleteEdge(origin,destin);
                        break;
                 case 5:
                        display();
                        break;
                 case 6:
                        exit(1);
                 default:
                        printf("\nWrong choice\n");
                        break;
                 }/*End of switch*/
        }/*End of while*/

        return 0;
}/*End of main()*/

void insertVertex(int u)
{
        struct Vertex *tmp,*ptr;
        tmp = malloc(sizeof(struct Vertex));
        tmp->info = u;
        tmp->nextVertex = NULL;
        tmp->firstEdge = NULL;

        if(start == NULL)
        {
                start = tmp;
                return;
        }
        ptr = start;
        while(ptr->nextVertex!=NULL)
                ptr = ptr->nextVertex;
        ptr->nextVertex = tmp;
}/*End of insertVertex()*/

void deleteVertex(int u)
{
        struct Vertex *tmp,*q;
        struct Edge *p,*temporary;
        if(start == NULL)
        {
                printf("\nNo vertices to be deleted\n");
                return;
        }
        if(start->info == u)/* Vertex to be deleted is first vertex of list*/
        {
                tmp = start;
                start = start->nextVertex;
        }
        else /* Vertex to be deleted is in between or at last */
        {
        q = start;
                while(q->nextVertex != NULL)
                {
                        if(q->nextVertex->info == u)
                                break;
                        q = q->nextVertex;
                }
                if(q->nextVertex==NULL)
                {
                        printf("Vertex not found\n");
                        return;
                }
                else
                {
                        tmp = q->nextVertex;
                        q->nextVertex = tmp->nextVertex;
                }
        }
        /*Before freeing the node tmp, free all edges going from this vertex */
        p = tmp->firstEdge;
        while(p!=NULL)
        {
                temporary = p;
                p = p->nextEdge;
                free(temporary);
        }
        free(tmp);
}/*End of deleteVertex()*/

void deleteIncomingEdges(int u)
{
        struct Vertex *ptr;
        struct Edge *q,*tmp;

        ptr = start;
        while(ptr!=NULL)
        {
                if(ptr->firstEdge == NULL)   /*Edge list for vertex ptr is empty*/
                {
                        ptr = ptr->nextVertex;
                        continue; /* continue searching in other Edge lists */
                }

                if(ptr->firstEdge->destVertex->info == u)
                {
                        tmp = ptr->firstEdge;
                        ptr->firstEdge = ptr->firstEdge->nextEdge;
                        free(tmp);
                        continue; /* continue searching in other Edge lists */
                }
                q = ptr->firstEdge;
                while(q->nextEdge!= NULL)
                {
                        if(q->nextEdge->destVertex->info == u)
                        {
                                tmp = q->nextEdge;
                                q->nextEdge = tmp->nextEdge;
                                free(tmp);
                                continue;
                        }
                        q = q->nextEdge;
                }
                ptr = ptr->nextVertex;
        }/*End of while*/

}/*End of deleteIncomingEdges()*/


struct Vertex *findVertex(int u)
{
        struct Vertex *ptr,*loc;
        ptr = start;
        while(ptr!=NULL)
        {
                if(ptr->info == u )
                {
                        loc = ptr;
                        return loc;
                }
                else
                        ptr = ptr->nextVertex;
        }
        loc = NULL;
        return loc;
}/*End of findVertex()*/

void insertEdge(int u,int v)
{
        struct Vertex *locu,*locv;
        struct Edge *ptr,*tmp;

        locu = findVertex(u);
        locv = findVertex(v);

        if(locu == NULL )
        {
                printf("\nStart vertex not present, first insert vertex %d\n",u);
                return;
        }
        if(locv == NULL )
        {
                printf("\nEnd vertex not present, first insert vertex %d\n",v);
                return;
        }
        tmp = malloc(sizeof(struct Edge));
        tmp->destVertex = locv;
        tmp->nextEdge = NULL;

        if(locu->firstEdge == NULL)
        {
                 locu->firstEdge = tmp;
         return;
        }
        ptr = locu->firstEdge;
        while(ptr->nextEdge!=NULL)
                ptr = ptr->nextEdge;
        ptr->nextEdge = tmp;

}/*End of insertEdge()*/

void deleteEdge(int u,int v)
{
        struct Vertex *locu;
        struct Edge *tmp,*q;

        locu = findVertex(u);

        if(locu == NULL )
        {
                printf("\nStart vertex not present\n");
                return;
        }
        if(locu->firstEdge == NULL)
        {
                printf("\nEdge not present\n");
                return;
        }

        if(locu->firstEdge->destVertex->info == v)
        {
                tmp = locu->firstEdge;
                locu->firstEdge = locu->firstEdge->nextEdge;
                free(tmp);
                return;
        }
        q = locu->firstEdge;
        while(q->nextEdge != NULL)
        {
                if(q->nextEdge->destVertex->info == v)
                {
                        tmp = q->nextEdge;
                        q->nextEdge = tmp->nextEdge;
                        free(tmp);
                        return;
                }
                q = q->nextEdge;
        }/*End of while*/

        printf("\nThis Edge not present in the graph\n");
}/*End of deleteEdge()*/

void display()
{
        struct Vertex *ptr;
        struct Edge *q;

        ptr = start;
        while(ptr!=NULL)
        {
                printf("%d ->",ptr->info);
                q = ptr->firstEdge;
                while(q!=NULL)
                {
                        printf(" %d",q->destVertex->info);
                        q = q->nextEdge;
                }
                printf("\n");
                ptr = ptr->nextVertex;
         }
}/*End of display()*/

OUTPUT : :


/* Insertion Deletion of Vertices and Edges in Graph using Adjacency list */

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 1

Enter a vertex to be inserted : 1

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 1

Enter a vertex to be inserted : 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 1 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 2
2 ->

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 1

Enter a vertex to be inserted : 3

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 1

Enter a vertex to be inserted : 4

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 1

Enter a vertex to be inserted : 5

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 2 3

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 3 4

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 4 5

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 2
2 -> 3
3 -> 4
4 -> 5
5 ->

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 1 5

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 5 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 2 5
2 -> 3
3 -> 4
4 -> 5
5 -> 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 2

Enter an Edge to be inserted : 1 4

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 2 5 4
2 -> 3
3 -> 4
4 -> 5
5 -> 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 3

Enter a vertex to be deleted : 4

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 2 5
2 -> 3
3 ->
5 -> 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 4

Enter an edge to be deleted : 1 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 5
1 -> 5
2 -> 3
3 ->
5 -> 2

1.Insert a Vertex
2.Insert an Edge
3.Delete a Vertex
4.Delete an Edge
5.Display
6.Exit

Enter your choice : 6

Process returned 1

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post…


Recommended Posts : :

5 2 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dinesh Gunaji Gawas

It would be great and very helpful if you implemented bfs and dfs traversals ….hoping you will take this into consideration