delete node in graph in c

Solutions on MaxInterview for delete node in graph in c by the best coders in the world

showing results for - "delete node in graph in c"
Lennard
14 Apr 2017
1#include<stdio.h>
2#define max 20
3int adj[ max ][ max ];
4int n;
5main()
6{
7    int choice;
8    int node, origin, destin;
9    create_graph();
10    while ( 1 )
11        {
12            printf( "1.Insert a node\n" );
13            printf( "2.Insert an edge\n" );
14            printf( "3.Delete a node\n" );
15            printf( "4.Delete an edge\n" );
16            printf( "5.Dispaly\n" );
17            printf( "6.Exit\n" );
18            printf( "Enter your choice : " );
19            scanf( "%d", &choice );
20            switch ( choice )
21                {
22                case 1:
23                    insert_node();
24                    break;
25                case 2:
26                    printf( "Enter an edge to be inserted : " );
27                    fflush( stdin );
28                    scanf( "%d %d", &origin, &destin );
29                    insert_edge( origin, destin );
30                    break;
31                case 3:
32                    printf( "Enter a node to be deleted : " );
33                    fflush( stdin );
34                    scanf( "%d", &node );
35                    delete_node( node );
36                    break;
37                case 4:
38                    printf( "Enter an edge to be deleted : " );
39                    fflush( stdin );
40                    scanf( "%d %d", &origin, &destin );
41                    del_edge( origin, destin );
42                    break;
43                case 5:
44                    display();
45                    break;
46                case 6:
47                    exit();
48                default:
49                    printf( "Wrong choice\n" );
50                    break;
51                } /*End of switch*/
52        } /*End of while*/
53} /*End of main()*/
54create_graph()
55{
56    int i, max_edges, origin, destin;
57    printf( "Enter number of nodes : " );
58    scanf( "%d", &n );
59    max_edges = n * ( n – 1 ); /* Taking directed graph */
60    for ( i = 1;i <= max_edges;i++ )
61        {
62            printf( "Enter edge %d( 0 0 ) to quit : ", i );
63            scanf( "%d %d", &origin, &destin );
64            if ( ( origin == 0 ) && ( destin == 0 ) )
65                break;
66            if ( origin > n || destin > n || origin <= 0 || destin <= 0 )
67                {
68                    printf( "Invalid edge!\n" );
69                    i–;
70                }
71            else
72                adj[ origin ][ destin ] = 1;
73        } /*End of for*/
74} /*End of create_graph()*/
75display()
76{
77    int i, j;
78    for ( i = 1;i <= n;i++ )
79        {
80            for ( j = 1;j <= n;j++ )
81                printf( "%4d", adj[ i ][ j ] );
82            printf( "\n" );
83        }
84} /*End of display()*/
85insert_node()
86{
87    int i;
88    n++; /*Increase number of nodes in the graph*/
89    printf( "The inserted node is %d \n", n );
90    for ( i = 1;i <= n;i++ )
91        {
92            adj[ i ][ n ] = 0;
93            adj[ n ][ i ] = 0;
94        }
95} /*End of insert_node()*/
96delete_node( char u )
97{
98    int i, j;
99    if ( n == 0 )
100        {
101            printf( "Graph is empty\n" );
102            return ;
103        }
104    if ( u > n )
105        {
106            printf( "This node is not present in the graph\n" );
107            return ;
108        }
109    for ( i = u;i <= n – 1;i++ )
110        for ( j = 1;j <= n;j++ )
111            {
112                adj[ j ][ i ] = adj[ j ][ i + 1 ]; /* Shift columns left */
113                adj[ i ][ j ] = adj[ i + 1 ][ j ]; /* Shift rows up */
114            }
115    n–; /*Decrease the number of nodes in the graph */
116} /*End of delete_node*/
117insert_edge( char u, char v )
118{
119    if ( u > n )
120        {
121            printf( "Source node does not exist\n" );
122            return ;
123        }
124    if ( v > n )
125        {
126            printf( "Destination node does not exist\n" );
127            return ;
128        }
129    adj[ u ][ v ] = 1;
130} /*End of insert_edge()*/
131del_edge( char u, char v )
132{
133    if ( u > n || v > n || adj[ u ][ v ] == 0 )
134        {
135            printf( "This edge does not exist\n" );
136            return ;
137        }
138    adj[ u ][ v ] = 0;
139} /*End of del_edge()*/
similar questions
queries leading to this page
delete node in graph in c