Home | Projects | Notes > Data Structures & Algorithms > Graphs


| Adjacency Matrx | Adjacency List | |
|---|---|---|
| Add vertex | O(|V|2); Have to completely rebuild the 2D matrix | O(1) |
| Add edge | O(1) | O(1) |
| Remove vertex | O(|V|2); Have to completely rebuild the 2D matrix | O(|V|); Have to check every vertex |
| Remove edge | O(1) | O(1) |
Using an adjacency list for a graph consistently offers better time complexity compared to using an adjacency matrix.
| Adjacency Matrx | Adjacency List |
|---|---|
| O(|V|2) | O(|V| + |E|) |
One significant disadvantage of representing a graph using an adjacency matrix is that it requires storing information for all vertices and edges, even those that are not connected.
xxxxxxxxxx301//==============================================================================2// File : graph.h3// Brief : Interface for Graph using adjacency list4// Author : Kyungjae Lee5// Date : Aug 03, 20236//==============================================================================7
8910
11121314
15using namespace std;16
17// Class for Graph using adjacency list18class Graph19{20public:21 bool addVertex(string vertex); // Adds a vertex22 bool removeVertex(string vertex); // Removes a vertex23 bool addEdge(string vertex1, string vertex2); // Adds an edge24 bool removeEdge(string vertex1, string vertex2);// Removes an edge25 void printGraph(void); // Prints graph26private:27 unordered_map<string, unordered_set<string> > adjList; // Adjacency list28};29
30// GRAPH_Hxxxxxxxxxx901//==============================================================================2// File : graph.cpp3// Brief : Implementation of Graph using adjacency list4// Author : Kyungjae Lee5// Date : Aug 03, 20236//==============================================================================7
89
10using namespace std;11
12//------------------------------------------------------------------------------13// Implementation of Graph class interface14//------------------------------------------------------------------------------15
16// Adds a vertex17// T = O(1)18bool Graph::addVertex(string vertex)19{20 // Make sure that 'vertex' does not exist 21 if (adjList.count(vertex) == 0)22 {23 adjList[vertex];24 return true;25 }26
27 return false;28} // End of addVertex29
30// Removes a vertex31// T = O(|V|)32bool Graph::removeVertex(string vertex)33{34 // Make sure that 'vertex' does exist 35 if (adjList.count(vertex) == 0)36 return false;37
38 // Loop throuh unordered_set and remove all edges associated with 'vertex'39 for (auto otherVertex : adjList.at(vertex))40 adjList.at(otherVertex).erase(vertex);41
42 // Remove 'vertex'43 adjList.erase(vertex);44
45 return true;46} // End of removeVertex47
48// Adds an edge49// T = O(1)50bool Graph::addEdge(string vertex1, string vertex2)51{52 // Make sure that 'vertex1' and 'vertex2' both exist53 if (adjList.count(vertex1) && adjList.count(vertex2))54 {55 adjList.at(vertex1).insert(vertex2);56 adjList.at(vertex2).insert(vertex1);57 return true;58 }59
60 return false;61} // End of addEdge62
63// Removes an edge64// T = O(1)65bool Graph::removeEdge(string vertex1, string vertex2)66{67 // Make sure that 'vertex1' and 'vertex2' both exist68 if (adjList.count(vertex1) && adjList.count(vertex2))69 {70 adjList.at(vertex1).erase(vertex2);71 adjList.at(vertex2).erase(vertex1);72 return true;73 }74
75 return false;76} // End of removeEdge77
78// Prints graph79// T = O(|V|*|E|)80void Graph::printGraph(void)81{82 for (auto [vertex, edges] : adjList)83 {84 cout << vertex << ": [ ";85 for (auto edge: edges)86 cout << edge << " ";87
88 cout << "]" << endl;89 }90} // End of printGraphxxxxxxxxxx351//==============================================================================2// File : main.cpp3// Brief : Test driver for Graph using adjacency list4// Author : Kyungjae Lee5// Date : Jul 31, 20236//==============================================================================7
8910
11using namespace std;12
13int main(int argc, char *argv[])14{15 Graph *g = new Graph();16
17 g->addVertex("A");18 g->addVertex("B");19 g->addVertex("C");20
21 g->addEdge("A", "B");22 g->addEdge("B", "C");23 g->addEdge("A", "C");24
25 g->printGraph();26
27 cout << endl;28
29 //g->removeEdge("A", "C");30 g->removeVertex("A");31
32 g->printGraph();33
34 return 0;35}xxxxxxxxxx61B: [ C A ]2C: [ A B ]3A: [ C B ]4
5B: [ C ]6C: [ B ]