|
1 | 1 | {
|
2 | | - "cells": [], |
3 | | - "metadata": {}, |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "839ca9fa-309e-4dbf-a062-3f6a66615873", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Directed Graphs\n", |
| 9 | + "\n", |
| 10 | + "Directed graphs, or digraphs, are a type of graph where edges have a direction associated with them. In other words, each edge in a directed graph is an ordered pair of vertices, indicating a one-way relationship between them. This directional nature distinguishes directed graphs from undirected graphs.\n", |
| 11 | + "\n", |
| 12 | + "In a directed graph, vertices (or nodes) are connected by directed edges, which represent the flow or progression from one vertex to another. This directed relationship implies that an edge from vertex A to vertex B does not necessarily imply the existence of an edge from B to A.\n", |
| 13 | + "\n", |
| 14 | + "Directed graphs are used to model various real-world scenarios, such as dependencies between tasks, network flows, web page links, and social relationships with a sense of direction.\n" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "id": "5d14c672-55f8-4fb1-a5ce-d4222195e5bf", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 70, |
| 28 | + "id": "483639ea-dabd-4753-836b-3f9b7dd35fa6", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "class DirectedGraph:\n", |
| 33 | + " def __init__(self, no_of_vertices):\n", |
| 34 | + " self.no_of_vertices = no_of_vertices\n", |
| 35 | + " self.adj_matrix = [[0] * no_of_vertices for _ in range(no_of_vertices)]\n", |
| 36 | + "\n", |
| 37 | + " def add_edge(self, start, end, weight=1):\n", |
| 38 | + " self.adj_matrix[start][end] = weight\n", |
| 39 | + "\n", |
| 40 | + " def remove_edge(self, start, end):\n", |
| 41 | + " self.adj_matrix[start][end] = 0\n", |
| 42 | + "\n", |
| 43 | + " def display(self):\n", |
| 44 | + " for row in self.adj_matrix:\n", |
| 45 | + " print(row)\n", |
| 46 | + "\n", |
| 47 | + " def add_vertex(self):\n", |
| 48 | + " self.no_of_vertices += 1\n", |
| 49 | + " # Add a new row for the new vertex\n", |
| 50 | + " self.adj_matrix.append([0] * self.no_of_vertices)\n", |
| 51 | + " # Add a new column for the new vertex in existing rows\n", |
| 52 | + " for row in self.adj_matrix:\n", |
| 53 | + " row.append(0)\n", |
| 54 | + "\n", |
| 55 | + " def remove_vertex(self, vertex):\n", |
| 56 | + " if vertex < self.no_of_vertices:\n", |
| 57 | + " del self.adj_matrix[vertex]\n", |
| 58 | + " self.no_of_vertices -= 1\n", |
| 59 | + "\n", |
| 60 | + " for row in self.adj_matrix:\n", |
| 61 | + " del row[vertex]\n", |
| 62 | + "\n", |
| 63 | + " def get_neighbors(self, vertex):\n", |
| 64 | + " if vertex < self.no_of_vertices:\n", |
| 65 | + " return [index for index, weight in enumerate(self.adj_matrix[vertex]) if weight > 0]\n", |
| 66 | + " else:\n", |
| 67 | + " return []\n", |
| 68 | + "\n", |
| 69 | + " def has_edge(self, start, end):\n", |
| 70 | + " return self.adj_matrix[start][end] > 0\n", |
| 71 | + "\n", |
| 72 | + " def graph_size(self):\n", |
| 73 | + " return self.no_of_vertices, sum(row.count(1) for row in self.adj_matrix)\n", |
| 74 | + "\n", |
| 75 | + " def clear_graph(self):\n", |
| 76 | + " self.no_of_vertices = 0\n", |
| 77 | + " self.adj_matrix = []\n", |
| 78 | + "\n", |
| 79 | + " def in_degree(self, vertex):\n", |
| 80 | + " in_degree_count = 0\n", |
| 81 | + " for i in range(self.no_of_vertices):\n", |
| 82 | + " in_degree_count += self.adj_matrix[i][vertex]\n", |
| 83 | + " return in_degree_count\n", |
| 84 | + "\n", |
| 85 | + " def out_degree(self, vertex):\n", |
| 86 | + " out_degree_count = 0\n", |
| 87 | + " for i in range(self.no_of_vertices):\n", |
| 88 | + " out_degree_count += self.adj_matrix[vertex][i]\n", |
| 89 | + " return out_degree_count\n" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 71, |
| 95 | + "id": "58100cfd-be2a-4d7d-8128-79b42c0b7893", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "graph = DirectedGraph(5)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 72, |
| 105 | + "id": "4eb0c220-439f-4c5a-aeed-832a663e418e", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "name": "stdout", |
| 110 | + "output_type": "stream", |
| 111 | + "text": [ |
| 112 | + "Directed Graph:\n", |
| 113 | + "[0, 1, 1, 0, 0]\n", |
| 114 | + "[1, 0, 0, 1, 0]\n", |
| 115 | + "[0, 1, 0, 1, 0]\n", |
| 116 | + "[0, 0, 1, 0, 1]\n", |
| 117 | + "[1, 0, 1, 0, 0]\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "graph.add_edge(0, 1)\n", |
| 123 | + "graph.add_edge(0, 2)\n", |
| 124 | + "graph.add_edge(1, 3)\n", |
| 125 | + "graph.add_edge(2, 3)\n", |
| 126 | + "graph.add_edge(3, 4)\n", |
| 127 | + "\n", |
| 128 | + "# Adding more edges\n", |
| 129 | + "graph.add_edge(4, 0)\n", |
| 130 | + "graph.add_edge(2, 1)\n", |
| 131 | + "graph.add_edge(4, 2)\n", |
| 132 | + "graph.add_edge(1, 0)\n", |
| 133 | + "graph.add_edge(3, 2)\n", |
| 134 | + "\n", |
| 135 | + "print(\"Directed Graph:\")\n", |
| 136 | + "graph.display()" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 73, |
| 142 | + "id": "002d4290-251f-46ac-b0f6-9502925d0d0d", |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "In-Degree of Vertex 0: 2\n", |
| 150 | + "Out-Degree of Vertex 0: 2\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "vertex = 0\n", |
| 156 | + "in_degree = graph.in_degree(vertex)\n", |
| 157 | + "out_degree = graph.out_degree(vertex)\n", |
| 158 | + "\n", |
| 159 | + "print(f'In-Degree of Vertex {vertex}: {in_degree}')\n", |
| 160 | + "print(f'Out-Degree of Vertex {vertex}: {out_degree}')" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 74, |
| 166 | + "id": "211b51e8-34d1-444b-b434-5d9a50ff23ba", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "name": "stdout", |
| 171 | + "output_type": "stream", |
| 172 | + "text": [ |
| 173 | + "\n", |
| 174 | + "Directed Graph after removing edge (1, 3):\n", |
| 175 | + "[0, 1, 1, 0, 0]\n", |
| 176 | + "[1, 0, 0, 0, 0]\n", |
| 177 | + "[0, 1, 0, 1, 0]\n", |
| 178 | + "[0, 0, 1, 0, 1]\n", |
| 179 | + "[1, 0, 1, 0, 0]\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "graph.remove_edge(1, 3)\n", |
| 185 | + "\n", |
| 186 | + "print(\"\\nDirected Graph after removing edge (1, 3):\")\n", |
| 187 | + "graph.display()\n" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": 75, |
| 193 | + "id": "42924b20-1dae-4820-aee1-bc9896d197af", |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [ |
| 196 | + { |
| 197 | + "name": "stdout", |
| 198 | + "output_type": "stream", |
| 199 | + "text": [ |
| 200 | + "\n", |
| 201 | + "Directed Graph after adding a vertex:\n", |
| 202 | + "[0, 1, 1, 0, 0, 0]\n", |
| 203 | + "[1, 0, 0, 0, 0, 0]\n", |
| 204 | + "[0, 1, 0, 1, 0, 0]\n", |
| 205 | + "[0, 0, 1, 0, 1, 0]\n", |
| 206 | + "[1, 0, 1, 0, 0, 0]\n", |
| 207 | + "[0, 0, 0, 0, 0, 0, 0]\n" |
| 208 | + ] |
| 209 | + } |
| 210 | + ], |
| 211 | + "source": [ |
| 212 | + "graph.add_vertex()\n", |
| 213 | + "\n", |
| 214 | + "print(\"\\nDirected Graph after adding a vertex:\")\n", |
| 215 | + "graph.display()" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": 76, |
| 221 | + "id": "6ec08462-1653-4475-aab3-77aae9210643", |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [ |
| 224 | + { |
| 225 | + "name": "stdout", |
| 226 | + "output_type": "stream", |
| 227 | + "text": [ |
| 228 | + "\n", |
| 229 | + "Directed Graph after removing vertex 1:\n", |
| 230 | + "[0, 1, 0, 0, 0]\n", |
| 231 | + "[0, 0, 1, 0, 0]\n", |
| 232 | + "[0, 1, 0, 1, 0]\n", |
| 233 | + "[1, 1, 0, 0, 0]\n", |
| 234 | + "[0, 0, 0, 0, 0, 0]\n" |
| 235 | + ] |
| 236 | + } |
| 237 | + ], |
| 238 | + "source": [ |
| 239 | + "graph.remove_vertex(1)\n", |
| 240 | + "\n", |
| 241 | + "print(\"\\nDirected Graph after removing vertex 1:\")\n", |
| 242 | + "graph.display()\n" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "code", |
| 247 | + "execution_count": 77, |
| 248 | + "id": "8d7ae565-d5d4-4143-9faa-f880ed1e7b70", |
| 249 | + "metadata": {}, |
| 250 | + "outputs": [ |
| 251 | + { |
| 252 | + "name": "stdout", |
| 253 | + "output_type": "stream", |
| 254 | + "text": [ |
| 255 | + "\n", |
| 256 | + "Neighbors of vertex 0: [1]\n" |
| 257 | + ] |
| 258 | + } |
| 259 | + ], |
| 260 | + "source": [ |
| 261 | + "neighbors = graph.get_neighbors(0)\n", |
| 262 | + "print(\"\\nNeighbors of vertex 0:\", neighbors)" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "cell_type": "code", |
| 267 | + "execution_count": 78, |
| 268 | + "id": "1c20d315-b99b-48e5-984c-f12b65347f9d", |
| 269 | + "metadata": {}, |
| 270 | + "outputs": [ |
| 271 | + { |
| 272 | + "name": "stdout", |
| 273 | + "output_type": "stream", |
| 274 | + "text": [ |
| 275 | + "\n", |
| 276 | + "Does edge (0, 2) exist? False\n" |
| 277 | + ] |
| 278 | + } |
| 279 | + ], |
| 280 | + "source": [ |
| 281 | + "has_edge = graph.has_edge(0, 2)\n", |
| 282 | + "print(\"\\nDoes edge (0, 2) exist?\", has_edge)\n" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "code", |
| 287 | + "execution_count": 79, |
| 288 | + "id": "54a286fa-e389-472f-a489-74a08906c45d", |
| 289 | + "metadata": {}, |
| 290 | + "outputs": [ |
| 291 | + { |
| 292 | + "name": "stdout", |
| 293 | + "output_type": "stream", |
| 294 | + "text": [ |
| 295 | + "\n", |
| 296 | + "Graph Size (Vertices, Edges): (5, 6)\n" |
| 297 | + ] |
| 298 | + } |
| 299 | + ], |
| 300 | + "source": [ |
| 301 | + "size = graph.graph_size()\n", |
| 302 | + "print(\"\\nGraph Size (Vertices, Edges):\", size)\n", |
| 303 | + "\n", |
| 304 | + "graph.clear_graph()" |
| 305 | + ] |
| 306 | + }, |
| 307 | + { |
| 308 | + "cell_type": "code", |
| 309 | + "execution_count": null, |
| 310 | + "id": "6b654b5a-d8bd-433c-8455-54dab63cc151", |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [], |
| 313 | + "source": [] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": 80, |
| 318 | + "id": "949100d1-c70a-43f9-b88b-3ec4340ab86c", |
| 319 | + "metadata": {}, |
| 320 | + "outputs": [ |
| 321 | + { |
| 322 | + "name": "stdout", |
| 323 | + "output_type": "stream", |
| 324 | + "text": [ |
| 325 | + "\n", |
| 326 | + "Directed Graph after clearing:\n" |
| 327 | + ] |
| 328 | + } |
| 329 | + ], |
| 330 | + "source": [ |
| 331 | + "print(\"\\nDirected Graph after clearing:\")\n", |
| 332 | + "graph.display()\n" |
| 333 | + ] |
| 334 | + }, |
| 335 | + { |
| 336 | + "cell_type": "code", |
| 337 | + "execution_count": null, |
| 338 | + "id": "ab15b137-e360-4fef-9445-f5dac0b69bbb", |
| 339 | + "metadata": {}, |
| 340 | + "outputs": [], |
| 341 | + "source": [] |
| 342 | + } |
| 343 | + ], |
| 344 | + "metadata": { |
| 345 | + "kernelspec": { |
| 346 | + "display_name": "Python 3 (ipykernel)", |
| 347 | + "language": "python", |
| 348 | + "name": "python3" |
| 349 | + }, |
| 350 | + "language_info": { |
| 351 | + "codemirror_mode": { |
| 352 | + "name": "ipython", |
| 353 | + "version": 3 |
| 354 | + }, |
| 355 | + "file_extension": ".py", |
| 356 | + "mimetype": "text/x-python", |
| 357 | + "name": "python", |
| 358 | + "nbconvert_exporter": "python", |
| 359 | + "pygments_lexer": "ipython3", |
| 360 | + "version": "3.10.6" |
| 361 | + } |
| 362 | + }, |
4 | 363 | "nbformat": 4,
|
5 | 364 | "nbformat_minor": 5
|
6 | 365 | }
|
0 commit comments