What is a Graph and How is it Represented

πŸ’‘ Concept Name

Graph – A non-linear data structure that consists of vertices (nodes) and edges (connections) used to represent networks, relationships, or paths.

πŸ“˜ Quick Intro

Graphs model pairwise relations between objects. Common examples include social networks, road maps, or dependency trees. Graphs can be directed or undirected, weighted or unweighted.

🧠 Analogy / Short Story

Think of a city map: each location is a node (vertex), and the roads between them are edges. Some roads are one-way (directed), others go both ways (undirected). We use graphs to model such structures.

πŸ”§ Technical Explanation

  • πŸ”Ή A graph G = (V, E) where V = vertices, E = edges
  • 🎯 Types:
    • Directed vs Undirected
    • Weighted vs Unweighted
    • Cyclic vs Acyclic
  • πŸ—‚οΈ Representations:
    • Adjacency Matrix: 2D array of size VΓ—V
    • Adjacency List: Array of lists
  • βš™οΈ Used in BFS, DFS, Dijkstra, Prim, Kruskal, and other algorithms

🎯 Purpose & Use Case

  • βœ… Model relationships in social networks
  • βœ… Network routing (routers, switches)
  • βœ… Game AI and pathfinding (like A*)
  • βœ… Scheduling and dependency resolution
  • βœ… Web page ranking and crawl (e.g., Google’s PageRank)

πŸ’» Real Code Example

// Adjacency List Representation
class Graph
{
    int V;
    List<int>[] adj;

    public Graph(int vertices)
    {
        V = vertices;
        adj = new List<int>[V];
        for (int i = 0; i < V; i++)
            adj[i] = new List<int>();
    }

    public void AddEdge(int src, int dest)
    {
        adj[src].Add(dest);
        adj[dest].Add(src); // remove for directed graph
    }

    public void PrintGraph()
    {
        for (int i = 0; i < V; i++)
        {
            Console.Write($""Vertex {i}: "");
            foreach (var edge in adj[i])
                Console.Write(edge + "" "");
            Console.WriteLine();
        }
    }
}

// Usage
var g = new Graph(4);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(1, 2);
g.AddEdge(2, 3);
g.PrintGraph();

❓ Interview Q&A

Q1: What is a graph in data structures?
A: A collection of nodes (vertices) connected by edges.

Q2: What are the types of graphs?
A: Directed, undirected, weighted, unweighted, cyclic, and acyclic graphs.

Q3: What is a directed graph?
A: A graph where edges have a direction from one vertex to another.

Q4: What is an undirected graph?
A: A graph where edges have no direction, connecting two vertices bidirectionally.

Q5: What is a weighted graph?
A: A graph where edges carry weights or costs.

Q6: What is a cycle in a graph?
A: A path where the start and end vertices are the same.

Q7: What is an acyclic graph?
A: A graph with no cycles.

Q8: How are graphs represented in memory?
A: Using adjacency lists or adjacency matrices.

Q9: What are common applications of graphs?
A: Social networks, routing algorithms, dependency resolution, and recommendation systems.

Q10: Can graphs be disconnected?
A: Yes, if not all vertices are reachable from each other.

πŸ“ MCQs

Q1. What is a graph?

  • Array
  • Nodes connected by edges
  • Tree
  • List

Q2. Which is a directed graph?

  • Edges have no direction
  • Edges have direction
  • No edges
  • Undirected edges

Q3. What is an undirected graph?

  • Edges have direction
  • Edges connect vertices bidirectionally
  • No edges
  • Single vertex

Q4. What is a weighted graph?

  • Edges have no weights
  • Edges have weights
  • Vertices have weights
  • No weights

Q5. What defines a cycle in a graph?

  • No repeated vertices
  • Path starting and ending at same vertex
  • Path with no edges
  • Disconnected graph

Q6. What is an acyclic graph?

  • Graph with cycles
  • Graph without cycles
  • Graph with weights
  • Disconnected graph

Q7. How are graphs represented?

  • Array only
  • Adjacency list or matrix
  • Linked list
  • Queue

Q8. Where are graphs used?

  • Sorting
  • Social networks and routing
  • Math only
  • Text processing

Q9. Can graphs be disconnected?

  • No
  • Yes
  • Sometimes
  • Depends on edges

Q10. What is a vertex in a graph?

  • An edge
  • A node
  • A path
  • A cycle

πŸ’‘ Bonus Insight

Graphs are everywhereβ€”from social networks to compiler optimizations. Knowing how to model a problem as a graph often leads to elegant and efficient algorithmic solutions.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Data Structures πŸ“š

What is a Data Structure and Why is it Important? πŸ‘‰ Learn More
Linear vs Non-linear Data Structures πŸ‘‰ Learn More
Arrays vs Linked Lists πŸ‘‰ Learn More
Advantages & Disadvantages of Arrays πŸ‘‰ Learn More
Singly Linked List Explained πŸ‘‰ Learn More
Singly vs Doubly Linked List πŸ‘‰ Learn More
Circular Linked List & Its Use Cases πŸ‘‰ Learn More
Reversing a Linked List πŸ‘‰ Learn More
Stack vs Queue πŸ‘‰ Learn More
Circular Queue Explained πŸ‘‰ Learn More
Queue Overflow & Underflow πŸ‘‰ Learn More
Applications of Stacks πŸ‘‰ Learn More
Priority Queue Explained πŸ‘‰ Learn More
Deque vs Queue πŸ‘‰ Learn More
Implement Stack Using Queues πŸ‘‰ Learn More
Implement Queue Using Stacks πŸ‘‰ Learn More
Trees in Data Structures πŸ‘‰ Learn More
Binary Trees Explained πŸ‘‰ Learn More
Binary Search Tree vs Binary Tree πŸ‘‰ Learn More
Insert & Delete in BST πŸ‘‰ Learn More
Tree Traversals (Inorder, Preorder, Postorder) πŸ‘‰ Learn More
Level Order Traversal πŸ‘‰ Learn More
Balanced Binary Tree πŸ‘‰ Learn More
AVL Trees Explained πŸ‘‰ Learn More
Red-Black Trees πŸ‘‰ Learn More
B-tree vs BST πŸ‘‰ Learn More
Heaps in Data Structures πŸ‘‰ Learn More
Min-Heap vs Max-Heap πŸ‘‰ Learn More
Implementing Heap Using Arrays πŸ‘‰ Learn More
Applications of Heaps πŸ‘‰ Learn More
Tries & Word Searching πŸ‘‰ Learn More
Graphs Explained πŸ‘‰ Learn More
Adjacency Matrix vs List πŸ‘‰ Learn More
BFS vs DFS πŸ‘‰ Learn More
Detecting Cycle in Graph πŸ‘‰ Learn More
Directed Acyclic Graph (DAG) πŸ‘‰ Learn More
Topological Sorting πŸ‘‰ Learn More
Dijkstra’s Algorithm πŸ‘‰ Learn More
Prim’s vs Kruskal’s Algorithm πŸ‘‰ Learn More
Union-Find / Disjoint Set πŸ‘‰ Learn More
Hash Tables Explained πŸ‘‰ Learn More
Hash Collision Handling πŸ‘‰ Learn More
Open Addressing vs Chaining πŸ‘‰ Learn More
Load Factor in Hashing πŸ‘‰ Learn More
Static vs Dynamic Data Structures πŸ‘‰ Learn More
Segment Trees πŸ‘‰ Learn More
Share:

Tags:


Feedback Modal Popup