|
| 1 | +//Copyright (C) Suresh Kumar Srivastava - All Rights Reserved |
| 2 | +//DSA Masterclass courses are available on CourseGalaxy.com |
| 3 | + |
| 4 | +//DirectedGraph.cpp : Program for directed graph using adjacency matrix. |
| 5 | + |
| 6 | +#include<iostream> |
| 7 | +#include<string> |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +static const int maxSize = 30; |
| 11 | + |
| 12 | +class Vertex |
| 13 | +{ |
| 14 | + public: |
| 15 | + string name; |
| 16 | + |
| 17 | + public: |
| 18 | + Vertex(string name) |
| 19 | + { |
| 20 | + this->name = name; |
| 21 | + } |
| 22 | +};//End of class Vertex |
| 23 | + |
| 24 | +class DirectedGraph |
| 25 | +{ |
| 26 | + private: |
| 27 | + int nVertices; |
| 28 | + int nEdges; |
| 29 | + int adj[maxSize][maxSize]; |
| 30 | + Vertex *vertexList[maxSize]; |
| 31 | + |
| 32 | + private: |
| 33 | + int getIndex(string vertexName); |
| 34 | + bool isAdjacent(int u, int v); |
| 35 | + |
| 36 | + public: |
| 37 | + DirectedGraph(); |
| 38 | + ~DirectedGraph(); |
| 39 | + void insertVertex(string vertexName); |
| 40 | + void insertEdge(string source, string destination); |
| 41 | + void deleteEdge(string source, string destination); |
| 42 | + void display(); |
| 43 | + bool edgeExists(string source, string destination); |
| 44 | + int getOutdegree(string vertex); |
| 45 | + int getIndegree(string vertex); |
| 46 | + |
| 47 | +};//End of class DirectedGraph |
| 48 | + |
| 49 | +DirectedGraph::DirectedGraph() |
| 50 | +{ |
| 51 | + nVertices = 0; |
| 52 | + nEdges = 0; |
| 53 | + |
| 54 | + for(int i=0; i<maxSize; i++) |
| 55 | + { |
| 56 | + for(int j=0; j<maxSize; j++) |
| 57 | + { |
| 58 | + adj[i][j] = 0; |
| 59 | + } |
| 60 | + } |
| 61 | +}//End of DirectedGraph() |
| 62 | + |
| 63 | +DirectedGraph::~DirectedGraph() |
| 64 | +{ |
| 65 | + for(int i=0; i<nVertices; i++) |
| 66 | + { |
| 67 | + delete vertexList[i]; |
| 68 | + } |
| 69 | +}//End of ~DirectedGraph() |
| 70 | + |
| 71 | +void DirectedGraph::insertVertex(string vertexName) |
| 72 | +{ |
| 73 | + vertexList[nVertices++] = new Vertex(vertexName); |
| 74 | +}//End of insertVertex() |
| 75 | + |
| 76 | +int DirectedGraph::getIndex(string vertexName) |
| 77 | +{ |
| 78 | + for(int i=0; i<nVertices; i++ ) |
| 79 | + { |
| 80 | + if(vertexName == vertexList[i]->name) |
| 81 | + return i; |
| 82 | + } |
| 83 | + |
| 84 | + throw exception("Invalid Vertex"); |
| 85 | + |
| 86 | +}//End of getIndex() |
| 87 | + |
| 88 | +void DirectedGraph::insertEdge(string source, string destination) |
| 89 | +{ |
| 90 | + int u = getIndex(source); |
| 91 | + int v = getIndex(destination); |
| 92 | + |
| 93 | + if(u == v) |
| 94 | + cout << "Not a valid edge\n"; |
| 95 | + else if(adj[u][v] != 0) |
| 96 | + cout << "Edge already present\n"; |
| 97 | + else |
| 98 | + { |
| 99 | + adj[u][v] = 1; |
| 100 | + nEdges++; |
| 101 | + } |
| 102 | +}//End of insertEdge() |
| 103 | + |
| 104 | +void DirectedGraph::deleteEdge(string source, string destination) |
| 105 | +{ |
| 106 | + int u = getIndex(source); |
| 107 | + int v = getIndex(destination); |
| 108 | + |
| 109 | + if(adj[u][v] != 0) |
| 110 | + { |
| 111 | + adj[u][v] = 0; |
| 112 | + nEdges--; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + cout << "Edge doesn't exist\n"; |
| 117 | + } |
| 118 | +}//End of deleteEdge() |
| 119 | + |
| 120 | +void DirectedGraph::display() |
| 121 | +{ |
| 122 | + for(int i=0; i<nVertices; i++) |
| 123 | + { |
| 124 | + for(int j=0; j<nVertices; j++) |
| 125 | + cout << adj[i][j] << " "; |
| 126 | + cout <<"\n"; |
| 127 | + } |
| 128 | +}//End of display() |
| 129 | + |
| 130 | +bool DirectedGraph::isAdjacent(int u, int v) |
| 131 | +{ |
| 132 | + return (adj[u][v] != 0); |
| 133 | +}//End of isAdjacent() |
| 134 | + |
| 135 | +bool DirectedGraph::edgeExists(string source, string destination) |
| 136 | +{ |
| 137 | + return isAdjacent(getIndex(source), getIndex(destination)); |
| 138 | +}//End of edgeExists() |
| 139 | + |
| 140 | +//Returns number of edges going out from a vertex |
| 141 | +int DirectedGraph::getOutdegree(string vertex) |
| 142 | +{ |
| 143 | + int u = getIndex(vertex); |
| 144 | + |
| 145 | + int outdegree = 0; |
| 146 | + for(int v = 0; v<nVertices; v++) |
| 147 | + { |
| 148 | + if(adj[u][v] != 0) |
| 149 | + outdegree++; |
| 150 | + } |
| 151 | + |
| 152 | + return outdegree; |
| 153 | +}//End of getOutdegree() |
| 154 | + |
| 155 | +//Returns number of edges coming to a vertex |
| 156 | +int DirectedGraph::getIndegree(string vertex) |
| 157 | +{ |
| 158 | + int u = getIndex(vertex); |
| 159 | + |
| 160 | + int indegree = 0; |
| 161 | + for(int v=0; v<nVertices; v++) |
| 162 | + { |
| 163 | + if(adj[v][u] != 0) |
| 164 | + indegree++; |
| 165 | + } |
| 166 | + |
| 167 | + return indegree; |
| 168 | +}//End of getIndegree() |
| 169 | + |
| 170 | +int main() |
| 171 | +{ |
| 172 | + DirectedGraph dGraph; |
| 173 | + |
| 174 | + try |
| 175 | + { |
| 176 | + //Creating the graph, inserting the vertices and edges |
| 177 | + dGraph.insertVertex("0"); |
| 178 | + dGraph.insertVertex("1"); |
| 179 | + dGraph.insertVertex("2"); |
| 180 | + dGraph.insertVertex("3"); |
| 181 | + |
| 182 | + dGraph.insertEdge("0","3"); |
| 183 | + dGraph.insertEdge("1","2"); |
| 184 | + dGraph.insertEdge("2","3"); |
| 185 | + dGraph.insertEdge("3","1"); |
| 186 | + dGraph.insertEdge("0","2"); |
| 187 | + |
| 188 | + //Display the graph |
| 189 | + dGraph.display(); |
| 190 | + cout << "\n"; |
| 191 | + |
| 192 | + //Deleting an edge |
| 193 | + dGraph.deleteEdge("0","2"); |
| 194 | + |
| 195 | + //Display the graph |
| 196 | + dGraph.display(); |
| 197 | + |
| 198 | + //Check if there is an edge between two vertices |
| 199 | + cout << "Edge exist : " << (dGraph.edgeExists("2","3") ? "True" : "False") << "\n"; |
| 200 | + |
| 201 | + //Display Indegree and Outdegree of a vertex |
| 202 | + cout << "Outdegree : " << dGraph.getOutdegree("3") << "\n"; |
| 203 | + cout << "Indegree : " << dGraph.getIndegree("3") << "\n"; |
| 204 | + |
| 205 | + }//End of try |
| 206 | + catch(exception e) |
| 207 | + { |
| 208 | + cout << e.what() << "\n"; |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | + |
| 213 | +}//End of main() |
| 214 | + |
0 commit comments