Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Introduction to Graphs

Introduction to Graphs | DSA in C++ - Software Development PDF Download

Introduction to Graphs in DSA C++


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.

What is a Graph?

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.

Types of Graphs

There are two main types of graphs: directed graphs and undirected graphs.

  • Directed Graph: In a directed graph, the edges have a specific direction. This means that the connection between two vertices has a source vertex and a destination vertex. The edges are represented by arrows indicating the direction of the connection.
  • Undirected Graph: In an undirected graph, the edges do not have any direction. The connection between two vertices is bidirectional, meaning it can be traversed in both directions. The edges are represented by lines connecting the vertices.

Graph Representations


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

Graph Operations


Graphs support various operations that allow us to manipulate and analyze the data they represent. Let's discuss some of the common operations:

  • Adding a Vertex: To add a new vertex to a graph, we simply need to add a new entry to the graph's representation. The new vertex will have no edges initially.
  • Adding an Edge: To add an edge between two vertices, we need to update the graph's representation accordingly. In an adjacency matrix, we set the appropriate entry to indicate the existence of the edge. In an adjacency list, we add the edge to the respective adjacency lists of the vertices.
  • Removing a Vertex: To remove a vertex from a graph, we need to remove all its edges and update the graph's representation accordingly.
  • Removing an Edge: To remove an edge between two vertices, we update the graph's representation accordingly. In an adjacency matrix, we set the appropriate entry to indicate the absence of the edge. In an adjacency list, we remove the edge from the respective adjacency lists of the vertices.
  • Traversing the Graph: Graph traversal is the process of visiting all the vertices in a graph. There are two popular methods for graph traversal: breadth-first search (BFS) and depth-first search (DFS).

Sample Code

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.

Sample Problems

  • Given a graph, perform a depth-first search (DFS) traversal starting from vertex 0.
  • Given a graph, perform a breadth-first search (BFS) traversal starting from vertex 0.
  • Given a graph, find the shortest path between two vertices using Dijkstra's algorithm.
  • Given a directed acyclic graph (DAG), find the longest path between two vertices.
  • Given a graph, detect if it contains a cycle.

Solution to these problems can be found by implementing the appropriate algorithms for graph traversal and path finding.

Conclusion

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.

The document Introduction to Graphs | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Introduction to Graphs | DSA in C++ - Software Development

,

Introduction to Graphs | DSA in C++ - Software Development

,

Extra Questions

,

ppt

,

video lectures

,

Objective type Questions

,

Exam

,

Summary

,

shortcuts and tricks

,

Viva Questions

,

study material

,

Important questions

,

Sample Paper

,

mock tests for examination

,

Previous Year Questions with Solutions

,

MCQs

,

Introduction to Graphs | DSA in C++ - Software Development

,

Semester Notes

,

pdf

,

past year papers

,

Free

,

practice quizzes

;