Shivam Chaturvedi@portfolio:~$

All About Graphs In Data Structures

By Shivam Chaturvedi15 min read
Last Updated: 2024-08-17
Graphs Blog

Introduction

Graphs are fundamental structures used to model pairwise relations between objects. They consist of nodes (vertices) and edges (connections between nodes). Graphs are used in various applications, from computer networks and social networks to recommendation systems and route planning.

Types of Graphs

Graphs can be categorized into various types based on their properties:

  • Directed Graph (Digraph): A graph where edges have a direction, indicating a one-way relationship between nodes.
  • Undirected Graph: A graph where edges do not have a direction, representing a bidirectional relationship between nodes.
  • Weighted Graph: A graph where edges have weights, representing the cost or distance between nodes.
  • Unweighted Graph: A graph where all edges are equal, without weights.
  • Cyclic Graph: A graph that contains at least one cycle, where a cycle is a path that starts and ends at the same vertex.
  • Acyclic Graph: A graph with no cycles.

What are Cycles in Graphs?

A cycle in a graph is a path that begins and ends at the same node without traversing any edge more than once. Detecting cycles is crucial in various algorithms and applications, including network analysis and scheduling problems. Cyclic graphs are those that contain at least one cycle, while acyclic graphs do not.

Learn More About Graphs

Breadth-First Search (BFS)

Breadth-First Search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the root (or an arbitrary node) and explores all the neighbor nodes at the present depth prior to moving on to nodes at the next depth level.

BFS Algorithm

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void BFS(int start, vector>& adj, vector& visited) {
    queue q;
    q.push(start);
    visited[start] = true;

    while (!q.empty()) {
        int node = q.front();
        q.pop();
        cout << node << " ";

        for (int neighbor : adj[node]) {
            if (!visited[neighbor]) {
                q.push(neighbor);
                visited[neighbor] = true;
            }
        }
    }
}

int main() {
    int V = 5;
    vector> adj(V);
    vector visited(V, false);

    adj[0].push_back(1);
    adj[0].push_back(4);
    adj[1].push_back(2);
    adj[1].push_back(3);
    adj[1].push_back(4);
    adj[2].push_back(3);
    adj[3].push_back(4);

    cout << "BFS starting from node 0:" << endl;
    BFS(0, adj, visited);
    return 0;
}
      

Depth-First Search (DFS)

Depth-First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the root (or an arbitrary node) and explores as far as possible along each branch before backtracking.

DFS Algorithm

#include <iostream>
#include <vector>

using namespace std;

void DFS(int node, vector>& adj, vector& visited) {
    visited[node] = true;
    cout << node << " ";

    for (int neighbor : adj[node]) {
        if (!visited[neighbor]) {
            DFS(neighbor, adj, visited);
        }
    }
}

int main() {
    int V = 5;
    vector> adj(V);
    vector visited(V, false);

    adj[0].push_back(1);
    adj[0].push_back(4);
    adj[1].push_back(2);
    adj[1].push_back(3);
    adj[1].push_back(4);
    adj[2].push_back(3);
    adj[3].push_back(4);

    cout << "DFS starting from node 0:" << endl;
    DFS(0, adj, visited);
    return 0;
}
      

Key Points

  • Graphs are essential for modeling and solving problems involving pairwise relationships.
  • Different types of graphs serve different purposes based on their properties.
  • Understanding cycles is important for detecting and managing dependencies and constraints in various systems.
  • Graphs can be directed or undirected, weighted or unweighted, and cyclic or acyclic.
  • BFS explores nodes level by level, while DFS explores as far as possible along each branch.

Frequently Asked Questions

  • Q: What are the common applications of graphs?
    A: Graphs are used in various fields such as network routing, social network analysis, recommendation systems, and more.
  • Q: How do you detect cycles in a graph?
    A: Cycle detection can be performed using Depth-First Search (DFS) or algorithms like Tarjan's or Floyd-Warshall.
  • Q: What is the difference between a directed and an undirected graph?
    A: In a directed graph, edges have a direction, while in an undirected graph, edges represent bidirectional relationships.
  • Q: What is the difference between BFS and DFS?
    A: BFS explores nodes level by level using a queue, while DFS explores nodes as far as possible along each branch using a stack or recursion.

Comments