|
| 1 | +/* ADJACENCY LIST FOR REPRESENTING DIRECTED GRAPH |
| 2 | + It is a method of representing a digraph using 2 linked lists |
| 3 | + 1. Node list |
| 4 | + 2. Edge list corresponding to each node |
| 5 | + Edges arising from a node are stored in edge list of each node |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <bits/stdc++.h> |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +/* Declare a structure for representing digraph with data and 2 links |
| 12 | + * link represents the edge between the parent node and next node (edge list) |
| 13 | + * pnext point to the next node from which new edges arise(node list) |
| 14 | + * g points to the graph |
| 15 | +*/ |
| 16 | +typedef struct AdjacencyList |
| 17 | +{ |
| 18 | + int data; |
| 19 | + struct AdjacencyList *link, *pnext; |
| 20 | +} AdjacencyList; |
| 21 | +AdjacencyList *g; |
| 22 | + |
| 23 | +//to insert into the graph |
| 24 | +void insert_to_graph(AdjacencyList *g, int parent, int value) |
| 25 | +{ |
| 26 | + //ptr points to the first node in the graph |
| 27 | + AdjacencyList *ptr = g->link, *p; |
| 28 | + //Check if the graph is empty |
| 29 | + if (g->link == NULL) |
| 30 | + { |
| 31 | + // ptr stores the value of parent node |
| 32 | + ptr = new AdjacencyList; |
| 33 | + ptr->data = parent; |
| 34 | + ptr->pnext = NULL; |
| 35 | + ptr->link = NULL; |
| 36 | + g->link = ptr; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + //Search for parent in the node list |
| 41 | + while (ptr != NULL && ptr->data != parent) |
| 42 | + { |
| 43 | + p = ptr; |
| 44 | + ptr = ptr->pnext; |
| 45 | + } |
| 46 | + //If parent is not found in the node list insert as last node |
| 47 | + if (ptr == NULL) |
| 48 | + { |
| 49 | + ptr = new AdjacencyList; |
| 50 | + ptr->data = parent; |
| 51 | + ptr->pnext = NULL; |
| 52 | + ptr->link = NULL; |
| 53 | + p->pnext = ptr; |
| 54 | + } |
| 55 | + } |
| 56 | + //insert value in the edge list of parent node |
| 57 | + AdjacencyList *node; |
| 58 | + node = new AdjacencyList; |
| 59 | + node->data = value; |
| 60 | + node->link = NULL; |
| 61 | + node->pnext = NULL; |
| 62 | + //Traverse till the end of the edge list |
| 63 | + while (ptr->link != NULL) |
| 64 | + ptr = ptr->link; |
| 65 | + //Insert value as the last node in the edge list |
| 66 | + ptr->link = node; |
| 67 | +} |
| 68 | + |
| 69 | +//To delete a node from the graph |
| 70 | +void delete_from_graph(AdjacencyList *g, int value) |
| 71 | +{ |
| 72 | + //Set flag as 0 assuming value is not present in the graph |
| 73 | + int flag = 0; |
| 74 | + //If the graph is not empty |
| 75 | + if (g->link != NULL) |
| 76 | + { |
| 77 | + //ptr points to first node |
| 78 | + AdjacencyList *ptr = g->link, *p, *pre; |
| 79 | + //If the first node in the node list is value |
| 80 | + if (ptr->data == value) |
| 81 | + { |
| 82 | + g->link = ptr->pnext; |
| 83 | + flag = 1; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + //Traverse the node list till value is found |
| 88 | + while (ptr->pnext != NULL && ptr->data != value) |
| 89 | + { |
| 90 | + p = ptr; |
| 91 | + ptr = ptr->pnext; |
| 92 | + } |
| 93 | + //If value is found delete node and entire edge list of the node |
| 94 | + if (ptr->data == value) |
| 95 | + { |
| 96 | + p->pnext = ptr->pnext; |
| 97 | + flag = 1; |
| 98 | + } |
| 99 | + } |
| 100 | + ptr = g->link; |
| 101 | + //Traverse till the end of the node list |
| 102 | + while (ptr != NULL) |
| 103 | + { |
| 104 | + p = ptr; |
| 105 | + //traverse the edge list of p till value is found |
| 106 | + while (p->link && p->data != value) |
| 107 | + { |
| 108 | + pre = p; |
| 109 | + p = p->link; |
| 110 | + } |
| 111 | + // If value is found in the edge list of p delete edge |
| 112 | + if (p->data == value) |
| 113 | + { |
| 114 | + pre->link = p->link; |
| 115 | + flag = 1; |
| 116 | + } |
| 117 | + ptr = ptr->pnext; |
| 118 | + } |
| 119 | + } |
| 120 | + //If flag is 0 value in not present in the graph |
| 121 | + if (flag == 0) |
| 122 | + printf("Node %d does not exist !\n", value); |
| 123 | +} |
| 124 | + |
| 125 | +//to print the graph |
| 126 | +void print_graph(AdjacencyList *g) |
| 127 | +{ |
| 128 | + //Check if graph is empty |
| 129 | + if (g->link == NULL) |
| 130 | + printf("Graph Empty !\n"); |
| 131 | + else |
| 132 | + { |
| 133 | + AdjacencyList *ptr = g->link, *p; |
| 134 | + //Traverse the node list |
| 135 | + while (ptr) |
| 136 | + { |
| 137 | + p = ptr; |
| 138 | + printf("%d ->", ptr->data); |
| 139 | + //traverse the edge list of p and print all the nodes |
| 140 | + while (p->link) |
| 141 | + { |
| 142 | + printf(" %d", p->link->data); |
| 143 | + p = p->link; |
| 144 | + } |
| 145 | + printf("\n"); |
| 146 | + ptr = ptr->pnext; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +int main() |
| 152 | +{ |
| 153 | + AdjacencyList *g = (AdjacencyList *)malloc(sizeof(AdjacencyList) * 10); |
| 154 | + g->link = NULL; |
| 155 | + //menu driven program to insert, delete and print graph according to choice |
| 156 | + printf("1. Insert into graph\n2. Delete from graph\n3. Display\n4. Exit\n\n"); |
| 157 | + while (1) |
| 158 | + { |
| 159 | + int choice; |
| 160 | + printf("Enter choice: "); |
| 161 | + scanf("%d", &choice); |
| 162 | + switch (choice) |
| 163 | + { |
| 164 | + case 1: |
| 165 | + { |
| 166 | + int parent, value; |
| 167 | + printf("Enter parent and value: "); |
| 168 | + scanf("%d %d", &parent, &value); |
| 169 | + insert_to_graph(g, parent, value); |
| 170 | + } |
| 171 | + break; |
| 172 | + |
| 173 | + case 2: |
| 174 | + { |
| 175 | + int value; |
| 176 | + printf("Enter value: "); |
| 177 | + scanf("%d", &value); |
| 178 | + delete_from_graph(g, value); |
| 179 | + } |
| 180 | + break; |
| 181 | + |
| 182 | + case 3: |
| 183 | + { |
| 184 | + print_graph(g); |
| 185 | + } |
| 186 | + break; |
| 187 | + |
| 188 | + case 4: |
| 189 | + { |
| 190 | + return 0; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return 0; |
| 196 | +} |
| 197 | +/* |
| 198 | + Sample I/O: |
| 199 | + 1. Insert into graph |
| 200 | + 2. Delete from graph |
| 201 | + 3. Display |
| 202 | + 4. Exit |
| 203 | +
|
| 204 | + Enter choice: 1 |
| 205 | + Enter parent and value: 2 3 |
| 206 | + Enter choice: 1 |
| 207 | + Enter parent and value: 3 4 |
| 208 | + Enter choice: 1 |
| 209 | + Enter parent and value: 1 5 |
| 210 | + Enter choice: 1 |
| 211 | + Enter parent and value: 2 4 |
| 212 | + Enter choice: 3 |
| 213 | + 2 -> 3 4 |
| 214 | + 3 -> 4 |
| 215 | + 1 -> 5 |
| 216 | + Enter choice: 4 |
| 217 | +*/ |
| 218 | +/* |
| 219 | + TIME COMPLEXITY |
| 220 | + Insertion - O(n) |
| 221 | + Deletion - O(n^2) |
| 222 | + Print graph - O(n^2) |
| 223 | + Total time complexity of the program - O(n^2) |
| 224 | + SPACE COMPLEXITY- O(n) |
| 225 | +*/ |
0 commit comments