|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Author: OMKAR PATHAK" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "# Graph\n" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "* Graph is a data structure that consists of following two components:\n", |
| 22 | + " 1. A finite set of vertices also called as **nodes.**\n", |
| 23 | + " 2. A finite set of ordered pair of the form (u, v) called as **edge**. The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.\n", |
| 24 | + "* Graphs can be represented by two ways:\n", |
| 25 | + " 1. **Adjacency Matrix**: A two-dimensional matrix, in which the rows represent source vertices and columns represent destination vertices. Data on edges and vertices must be stored externally. Only the cost for one edge can be stored between each pair of vertices.\n", |
| 26 | + " 2. **Adjacency List**: Vertices are stored as records or objects, and every vertex stores a list of adjacent vertices. This data structure allows the storage of additional data on the vertices. Additional data can be stored if edges are also stored as objects, in which case each vertex stores its incident edges and each edge stores its incident vertices." |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "### Time Complexities:" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "_NOTE: AL = Adjacency List, AM = Adjacency Matrix, V = Vertex, E = Edge_\n", |
| 41 | + "* Store Graph: AL: O(|V| + |E|), AM: O(|V| * |V|)\n", |
| 42 | + "* Add Vertex: AL: O(1), AM: O(|V| * |V|)\n", |
| 43 | + "* Add Edge: AL: O(1), AM: O(1)\n", |
| 44 | + "* Remove Vertex: AL: O(|E|), AM: O(|V| * |V|)\n", |
| 45 | + "* Remove Edge: AL: O(|V|), AM: O(1)\n", |
| 46 | + "<br><br>\n", |
| 47 | + "* AL: Slow to remove vertices and edges, because it needs to find all vertices or edges\n", |
| 48 | + "* AM: Slow to add or remove vertices, because matrix must be resized/copied" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "### Implementation for Adjacency List" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 1, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "name": "stdout", |
| 65 | + "output_type": "stream", |
| 66 | + "text": [ |
| 67 | + "0 -> 1 -> 4\n", |
| 68 | + "1 -> 0 -> 4 -> 3 -> 2\n", |
| 69 | + "2 -> 3\n", |
| 70 | + "3 -> 4\n", |
| 71 | + "4 -> 1 -> 3\n" |
| 72 | + ] |
| 73 | + } |
| 74 | + ], |
| 75 | + "source": [ |
| 76 | + "class AdjacencyList(object):\n", |
| 77 | + " def __init__(self):\n", |
| 78 | + " self.List = {}\n", |
| 79 | + " \n", |
| 80 | + " def addEdge(self, fromVertex, toVertex):\n", |
| 81 | + " # check if vertex is already present\n", |
| 82 | + " if fromVertex in self.List.keys():\n", |
| 83 | + " self.List[fromVertex].append(toVertex)\n", |
| 84 | + " else:\n", |
| 85 | + " self.List[fromVertex] = [toVertex]\n", |
| 86 | + " \n", |
| 87 | + " def printList(self):\n", |
| 88 | + " for i in self.List:\n", |
| 89 | + " print(i,'->',' -> '.join([str(j) for j in self.List[i]]))\n", |
| 90 | + " \n", |
| 91 | + "al = AdjacencyList()\n", |
| 92 | + "al.addEdge(0, 1)\n", |
| 93 | + "al.addEdge(0, 4)\n", |
| 94 | + "al.addEdge(4, 1)\n", |
| 95 | + "al.addEdge(4, 3)\n", |
| 96 | + "al.addEdge(1, 0)\n", |
| 97 | + "al.addEdge(1, 4)\n", |
| 98 | + "al.addEdge(1, 3)\n", |
| 99 | + "al.addEdge(1, 2)\n", |
| 100 | + "al.addEdge(2, 3)\n", |
| 101 | + "al.addEdge(3, 4)\n", |
| 102 | + "\n", |
| 103 | + "al.printList()" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "### Application of Graphs:" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "* In computer science, graphs are used to represent networks of communication, data organization, computational devices.\n", |
| 118 | + "* Graph theory is also used to study molecules in chemistry and physics.\n", |
| 119 | + "* In mathematics, graphs are useful in geometry.\n", |
| 120 | + "* Weighted graphs, are used to represent structures in which pairwise connections have some numerical values. Ex: Road Network.\n", |
| 121 | + "* Graph algorithms are useful for calculating the shortest path in Routing .\n", |
| 122 | + "* Maps – finding the shortest/cheapest path for a car from one city to another, by using given roads." |
| 123 | + ] |
| 124 | + } |
| 125 | + ], |
| 126 | + "metadata": { |
| 127 | + "kernelspec": { |
| 128 | + "display_name": "Python 3", |
| 129 | + "language": "python", |
| 130 | + "name": "python3" |
| 131 | + }, |
| 132 | + "language_info": { |
| 133 | + "codemirror_mode": { |
| 134 | + "name": "ipython", |
| 135 | + "version": 3 |
| 136 | + }, |
| 137 | + "file_extension": ".py", |
| 138 | + "mimetype": "text/x-python", |
| 139 | + "name": "python", |
| 140 | + "nbconvert_exporter": "python", |
| 141 | + "pygments_lexer": "ipython3", |
| 142 | + "version": "3.5.2" |
| 143 | + } |
| 144 | + }, |
| 145 | + "nbformat": 4, |
| 146 | + "nbformat_minor": 2 |
| 147 | +} |
0 commit comments