Table of contents | |
Introduction to Graphs in DSA C++ | |
What is a Graph? | |
Types of Graphs | |
Graph Representations | |
Graph Operations | |
Sample Code | |
Sample Problems |
Graphs are an essential data structure in computer science and are widely used to model relationships between objects. In this article, we will explore the basics of graphs and how they can be implemented in C++. We will cover the fundamental concepts of graphs, different types of graphs, graph representations, and basic operations on graphs.
A graph is a collection of vertices (also known as nodes) and edges. The vertices represent objects, and the edges represent the connections or relationships between those objects. Graphs can be used to represent a wide range of real-world scenarios, such as social networks, computer networks, transportation systems, and more.
There are two main types of graphs: directed graphs and undirected graphs.
There are multiple ways to represent a graph in memory. The two most common representations are the adjacency matrix and the adjacency list.
Adjacency Matrix: An adjacency matrix is a 2D matrix that represents a graph. The rows and columns of the matrix correspond to the vertices of the graph, and the entries of the matrix indicate whether there is an edge between two vertices. For an undirected graph, the matrix is symmetric.
Here's an example of an adjacency matrix for an undirected graph with four vertices:
0 1 2 3
0 0 1 1 0
1 1 0 1 1
2 1 1 0 1
3 0 1 1 0
Adjacency List: An adjacency list is a collection of linked lists or arrays, where each vertex has a list of its adjacent vertices. It is a more space-efficient representation compared to the adjacency matrix, especially for sparse graphs.
Here's an example of an adjacency list for the same undirected graph:
0 -> 1 -> 2
1 -> 0 -> 2 -> 3
2 -> 0 -> 1 -> 3
3 -> 1 -> 2
Graphs support various operations that allow us to manipulate and analyze the data they represent. Let's discuss some of the common operations:
Let's look at some C++ code snippets to illustrate the concepts discussed above.
#include <iostream>
#include <vector>
using namespace std;
// Graph representation using adjacency list
class Graph {
int V; // Number of vertices
vector<vector<int>> adjList;
public:
Graph(int numVertices) {
V = numVertices;
adjList.resize(V);
}
void addEdge(int src, int dest) {
adjList[src].push_back(dest);
adjList[dest].push_back(src);
}
void printGraph() {
for (int i = 0; i < V; i++) {
cout << i << " -> ";
for (int j : adjList[i]) {
cout << j << " ";
}
cout << endl;
}
}
};
int main() {
// Create a graph with 4 vertices
Graph graph(4);
// Add edges
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
// Print the graph
graph.printGraph();
return 0;
}
Output:
0 -> 1 2
1 -> 0 2 3
2 -> 0 1
3 -> 1
In this example, we create a graph using an adjacency list representation and add edges between vertices. Finally, we print the graph.
Solution to these problems can be found by implementing the appropriate algorithms for graph traversal and path finding.
In conclusion, graphs are a powerful data structure used to model relationships between objects. They have various applications in computer science and can be represented using different approaches. By understanding the basics of graphs and their operations, you can solve a wide range of problems efficiently.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|