Open In App

C# Data Structures

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Data Structures are an important part of programming language. When we are creating solutions for real-world problems, choosing the right data structure is critical because it can impact the performance of algorithms. If we know the fundamentals of data structures and know how to use them effectively, then we can build the optimal solutions, and we can also create effective applications. In C#, data structures can be built using arrays, lists, stacks, queues, linked lists, trees, graphs, hash tables, and more. C# provides built-in data structures through the System.Collections namespace.

DataStructures

In this article, we will discuss the Data Structures in C# Programming Language and their relationship with specific C# Data Types. We will discuss all the built-in data structures such as arrays, lists, dictionaries, and more. Additionally, we will also cover some of the advanced data structures like trees, graphs, and others.

1. Linear Data Structure

  • Array
  • List
  • LinkedList
  • Stack
  • Queue
  • PriorityQueue

Arrays

In C# arrays are an important data structure which is used to store collection of elements of the same type. It is used to store the multiple variables in a single variable as shown in the below example, we create a single variable arr and store multiple values in it. The elements in an array are stored in contiguous memory locations, and each element can be accessed using an index starting from 0. For example, if we want to access the value of arr[2], we get the value 6 from the array shown in the below image.

Array


Types of Arrays in C#

List

List in C# is a generic collection used to store the elements or objects in the form of a list defined under System.Collection.Generic namespace. It is a strongly typed list of objects. Lists in C# dynamically adjust the size dynamically means we don't need to define the size like arrays. It increases it sizes as we added the elements in the list.

Example:

C#
// Creating and printing a List
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        List<string> l = new List<string> { "C#", "Java", "Javascript" };
        
        foreach (string name in l)
        {
            Console.WriteLine(name);
        }
    }
}

Output
C#
Java
Javascript


LinkedList

Linked List is a linear data structure and in this elements are not stored at a contiguous location. Linked List is basically a collection of data(Nodes) where each connected to the other and forms a chain structure of nodes, Each node stores the data and the addresses of the next and previous nodes. In C#, LinkedList is the generic type of collection that is defined in the System.Collections.Generic namespace. By default in C# the LinkedList from generics collection is a double linkedList which contains both next and previous node.

  • It offers enumerators for straightforward traversal.
  • Every node in a LinkedList<T> object is of the type LinkedListNode<T>.
  • We can easily store duplicate elements of the same type in LinkedList
CSharp-LinkedList


Example:

C#
// C# program to Add elements to a LinkedList
using System;
using System.Collections.Generic;

class Geeks 
{
    static void Main()
    {
        // Create a new LinkedList of strings
        LinkedList<int> l = new LinkedList<int>();

        // Add elements to the LinkedList

        // Adds at the end
        l.AddLast(3);
        // Adds at the beginning
        l.AddFirst(5);
        // Adds at the end
        l.AddLast(7);
        // Adds at the end
        l.AddLast(0);

        // Display the elements in the LinkedList
        Console.WriteLine("Elements in the LinkedList:");
        foreach(var i in l)
        { 
          Console.WriteLine(i); 
        }
    }
}

Output
Elements in the LinkedList:
5
3
7
0


Stack

Stack data structure works on the concept LIFO (Last In First Out). In stack the element which we add in stack is out first This means both insertion and deletion operations happen at one end only. In C#, the Stack<T> class is present in the System.Collections.Generic namespace. We justsimplye use this class and perform different stack-related operation like pop( remove element), push (adding element) and peek ( top element). There is multiple application of stack-like method calling and recursion is based on the stack data structure.

  • Pop and push operations done on constant O(n) time
  • Add duplicate and null values inthe stack
  • Elements are added and remove from one end.
Stack-representation-in-Data-Structures-(1)


Example:

C#
// C# program Implementing Stack class
using System;
using System.Collections.Generic;

public class Geeks
 {
    public static void Main(string[] args)
    {
        // Create a new stack
        Stack<int> s = new Stack<int>();

        // Push elements onto the stack
        s.Push(1);
        s.Push(2);
        s.Push(3);
        s.Push(4);
        
        // Peek element
        Console.WriteLine("Peek element of stack: "+ s.Peek());
        
        // Pop elements from the stack
        while (s.Count > 0) {
            Console.WriteLine("Elements of Stack: "+ s.Pop());
        }
    }
}

Output
Peek element of stack: 4
Elements of Stack: 4
Elements of Stack: 3
Elements of Stack: 2
Elements of Stack: 1


Queue

A Queue Data is an important data structure which works as "First in, First out(FIFO), where the first element added to the queue is the first one to be removed. In C# we can use the queue from the generic class by using the Queue<T> class by using the System.Collections.Generic namespace. When we add an item to the list, it is called enqueue, and when we remove an item, it is called deque.

  • Enqueue adds an element to the end of the Queue.
  • deque removes the oldest element from the start of the Queue.
  • Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.
  • Queue is also like a list we don't have to put the size initially it grows as we added the elements in it and it also take null as a valid value.


Example:

C#
// C# program to demonstrates the working of queue
using System;
using System.Collections;

public class Geeks 
{
    static public void Main()
    {
        // Create a queue
        // Using Queue class
        Queue q = new Queue();

        // Adding elements in Queue
        // Using Enqueue() method
        q.Enqueue("GFG");
        q.Enqueue(10);
        q.Enqueue(null);
        q.Enqueue(3.5);
        q.Enqueue("Geeks123");

      // Display the first element deque in the queue
      Console.WriteLine(q.Dequeue());

        // Accessing the elements
        // of q Queue
        // Using foreach loop
        foreach(var i in q) 
        { 
          Console.WriteLine("Elements of Queue: "+ i); 
        }
    }
}

Output
GFG
Elements of Queue: 10
Elements of Queue: 
Elements of Queue: 3.5
Elements of Queue: Geeks123


Priority Queue

In C#, the PriorityQueue is a predefined class which is sent in the System.Collections.Generic namespace. It is used to store elements according to their priority. Elements with greater priority are removed from the queue before those with lesser priority. It can be implemented using SortedList or SortedDictionary. There are multiple use cases of priority queue for example finding the second largest element.

  • When we add an elements in priority queue it inserted with an associated priority.
  • The elements which are removed based on their priority, lower numerical priority values are dequed first (by defualt PriorityQueue in C# is a min-heap).
  • The queue ensures that the elements with the lowest priority value are dequed first.
  • Elements are sorted internally according to their priority values.

Example:

C#
using System;
using System.Collections.Generic;

class Geeks
{
    static void Main()
    {
        // Create a priority queue with string elements and int priorities
        PriorityQueue<string, int> pq = new PriorityQueue<string, int>();

        // Enqueue elements with their priorities
        pq.Enqueue("Low priority task", 3);
        pq.Enqueue("Medium priority task", 5);
        pq.Enqueue("High priority task", 7);
        pq.Enqueue("Highest priority task", 10);

        // Dequeue elements and print them
        while (pq.Count > 0)
        {
            // Dequeue the highest priority element
            string item = pq.Dequeue();
            Console.WriteLine("Processing: " + item);
        }
    }
}

Output:

PriorityQueue

Note: the PrirorityQueue is introduced in .NET 6 so if we are using earlier versions so we have to implemet the custom priority queue.


2. Associated Data Structure (Key-Value Pair Collections)

  • Dictionary
  • SortedList
  • HashTable

Dictionary

Dictionary in C# is a generic collection that stores key-value pairs. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic means the size of the dictionary is growing according to the need.

  • Key-Value Pair: The value is stored in the Key-Value pair.
  • Efficient Lookup: It provides fast lookups for values based on keys.
  • Unique Keys: Stored keys uniquely and adding duplicate keys results in a runtime exception.

Example:

C#
// C# program to demonstrate how to 
// create and display a dictionary
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating a dictionary
        Dictionary<int, string> sub = new Dictionary<int, string>();

        // Adding elements
        sub.Add(1, "One");
        sub.Add(2, "Two");
        sub.Add(3, "Three");

        // Displaying dictionary
        foreach (var ele in sub)
        {
            Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
        }
    }
}

Output
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three


SortedList

In C#, SortedList is a collection of key-value pairs sorted according to keys. By default, this collection sorts ascendingly It is of both generic and non-generic type of collection. The elements are sorted by key in ascending order and the keys are defined uniquely but we can store duplicate values.

Example:

C#
// Creating and adding key, values to the sorted list
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating a SortedList
        SortedList<int, string> sl = new SortedList<int, string>();

        // Adding key-value pairs
        sl.Add(3, "Three");
        sl.Add(1, "One");
        sl.Add(2, "Two");

        // Displaying elements in sorted by key
        foreach (var item in sl)
        {
            Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
        }
    }
}

Output
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three


HashTable

In C#, Hashtable is a special data structure uses to store key-value pairs. Hashtable uses the hash code to organize the keys for efficient data retrieval. It performs the insertion and deletion in constant time. The key can be any object, and each key is associated with a corresponding value. It is a part of the System.Collections namespace and is non-generic.

  • In Hashtable we can also store the values as null.
  • In Hashtable the key value must be immutable.
  • It stores values in the object type so we can store different type of values in it.
  • In Hasthtable we can store the duplicate values but the key must be unique.
  • The elements of Hashtable that are key-value pair is stored as DictionaryEntry objects.

Example:

C#
// C# program to add elements to the hashtable
using System;
using System.Collections;

class Geeks 
{
    static void Main()
    {
        // Create a new Hashtable
        Hashtable ht = new Hashtable();

        // Add key-value pairs to the Hashtable
        ht.Add("One", 1);
        ht.Add("Two", 2);
        ht.Add("Three", 3);

        Console.WriteLine("Hashtable elements:");
        foreach(DictionaryEntry e in ht)
        {
            Console.WriteLine($"{e.Key}: {e.Value}");
        }
    }
}

Output
Hashtable elements:
Two: 2
Three: 3
One: 1


3. Set-Based Data Structure

  • HashSet
  • SortedSet

HashSet

In C#, a HashSet<T> class is an unordered collection of unique elements. It comes under System.Collections.Generic namespace. It is used to prevent duplicates from being inserted into the collection. In terms of performance, it is generally better than a list. It does not maintain the order of elements like a list does.

Characteristics of HashSet Class:

  • The main characteristic of HashSet<T> is it enables the efficient set operations, and store the unique elements ( no duplicate allowed ) and their order not be same.
  • We don't need to define the capacity like arrays, a HashSet<T> object's size increases as we added the element in the Hashset.
  • HashSet<T> has different default mathematical set operations for example we can use union and intersections of two Hashsets.

Example:

C#
// C# Program to demonstrate the use of HashSet
using System;
using System.Collections.Generic;

class Geeks 
{
    public static void Main(string[] args)
    {
        // Instantiate an object of HashSet
        HashSet<int> hs = new HashSet<int>();

        // Adding elements
        hs.Add(1);
        hs.Add(2);
        hs.Add(3);
      
        // Duplicate element, will not be added
        hs.Add(1);

        // Printing the Size and Element of HashSet
        Console.WriteLine("HashSet Size: " + hs.Count);
        Console.WriteLine("Elements in HashSet: "
        + string.Join(", ", hs));
    }
}

Output
HashSet Size: 3
Elements in HashSet: 1, 2, 3


SortedSet

SortedSet in C# is a collection of objects that stores elements uniquely in sorted order. It is the generic type collection defined under System.Collections.Generic namespace. It is a dynamic collection means the size of the SortedSet is automatically increased when new elements are added. There are some key features mentioned below:

  • Unique and Sorted: SortedSet is used to store elements uniquely in sorted order.
  • Mathematical Operations: provides many mathematical set operations, such as intersection, union, and difference.
  • Optimal: Perform insert, delete, and search operations with O(log n) time complexity.

Example:

C#
// C# program to demonstrate the use of SortedSet
using System;
using System.Collections.Generic;

class Geeks 
{
    public static void Main() {
      
        // Creating a SortedSet
        SortedSet<int> num = 
          new SortedSet<int> { 7, 1, 2, 8, 1, 4 };

        // Adding elements
        num.Add(6);
      
        // Adding duplicate (will not be added)
        num.Add(2);

        // Displaying elements
        Console.WriteLine("SortedSet elements:");
        foreach (int ele in num)
            Console.Write(ele + " ");
    }
}

Output
SortedSet elements:
1 2 4 6 7 8 


4. Specialized Data Structure

  • Tuple
  • ValueTuple
  • BitArray

Tuple

A tuple is a data structure which consists of multiple parts. It is the easiest way to represent a data set which has multiple values of different types. It was introduced in .NET Framework 4.0. In a tuple, we can add elements from 1 to 8. If try to add elements greater than eight, Tuples are generally used when we want to create a data structure which contains objects with their properties and don’t want to create a separate type for that.

Features of Tuples:

  • It allows us to represent multiple data into a single data set.
  • It allows us to create, manipulate, and access data sets.
  • It returns multiple values from a method without using out parameter.
  • It can also store duplicate elements.
  • It allows us to pass multiple values to a method with the help of single parameters.

Example:

C#
// Example of Tuple
using System;

public class Geeks
{
      // Main Method 
    static public void Main()
    {
          // Creating a Tuple
        var tuple = (123, "Hello", true);
        
          // Accessing the Elements
        Console.WriteLine(tuple.Item1); 
        Console.WriteLine(tuple.Item2); 
        Console.WriteLine(tuple.Item3); 
    }
}

Output
123
Hello
True


ValueTuple

ValueTuple is a structure introduced in C# 7.0 which represents the value type. Already included in .NET Framework 4.7 or higher version. It allows us to store a data set that contains multiple values that may or may not be related to each other. It can store elements starting from 0 to 8 and can store elements of different types. We can also store duplicate elements in the value tuple.Tuple is of reference type, but ValueTuple is of the value type.

  • Tuple does not provide naming conventions, but ValueTuple provides strong naming conventions.
  • Tuples can’t have zero components, but ValueTuple can have zero elements.
  • ValueTuple provides a lightweight mechanism for returning multiple values from the existing methods.
  • The syntax of ValueTuple is more optimized than Tuples.
  • In ValueTuple fields are mutable. But in Tuple, fields are read-only.

Example:

C#
// Example of Tuple
using System;

public class Geeks
{
    // Main Method 
    static public void Main()
    {
        // ValueTuple with three elements
        ValueTuple<string, string, int> vt = new 
        ValueTuple<string, string, int>("C#", "Java", 357);

        // Accessing the Elements
        Console.WriteLine("First Element: " + vt.Item1);
        Console.WriteLine("Second Element: " + vt.Item2);
        Console.WriteLine("Third Element: " + vt.Item3);
    }
}

Output
First Element: C#
Second Element: Java
Third Element: 357


Bit Array

BitArray is an important data structure in C#.It is present in the System.Collections namespace. It manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e. 1, and false indicates the bit is off i.e. 0. 

  • When we add the element in the bit array it automatically increased using the Length property
  • When we deleted the element in the bit array it automatically decreased using the Length property.
  • We can be accessed the elements using an integer index. Indexes in this collection are zero-based.

Example: 

C#
using System;
using System.Collections;

class Geeks 
{
    static void Main(string[] args)
    {
        // Creating a BitArray with 5 bits (initialized to
        // false by default)
        BitArray b = new BitArray(5);

        // Setting individual bits
        b[0] = true;
        b[1] = true;
        b[3] = true;

        // Printing the BitArray (will display true or false
        // for each bit)
        for (int i = 0; i < b.Count; i++) 
        {
            Console.WriteLine(
                $"Bit at index {i}: {b[i]}");
        }
    }
}

Output
Bit at index 0: True
Bit at index 1: True
Bit at index 2: False
Bit at index 3: True
Bit at index 4: False


5. Hierarchical Data Sturctures

Tree

Tree real-world is a real world heirarichal data structure which shows as parent-child relationships. Tree data structure are used in multiple applications such as showing the file structure and Document object model in websites. where each node is connected to each other. the child and parent both are used to define as node(object). In the below image we can see the important part of a tree data structure.

treeTerminologies


Implementation:

C#
using System;
using System.Collections.Generic;

class Geeks
{
    static void PrintParents(int node, List<List<int>> adj, int parent)
    {
        if (parent == 0)
        {
            Console.WriteLine($"{node} -> Root");
        }
        else
        {
            Console.WriteLine($"{node} -> {parent}");
        }

        foreach (int cur in adj[node])
        {
            if (cur != parent)
            {
                PrintParents(cur, adj, node);
            }
        }
    }

    static void PrintChildren(int Root, List<List<int>> adj)
    {
        Queue<int> q = new Queue<int>();
        q.Enqueue(Root);
        bool[] vis = new bool[adj.Count];

        while (q.Count > 0)
        {
            int node = q.Dequeue();
            vis[node] = true;
            Console.Write($"{node} -> ");

            foreach (int cur in adj[node])
            {
                if (!vis[cur])
                {
                    Console.Write($"{cur} ");
                    q.Enqueue(cur);
                }
            }
            Console.WriteLine();
        }
    }

    static void PrintLeafNodes(int Root, List<List<int>> adj)
    {
        for (int i = 0; i < adj.Count; i++)
        {
            if (adj[i].Count == 1 && i != Root)
            {
                Console.Write($"{i} ");
            }
        }
        Console.WriteLine();
    }

    static void PrintDegrees(int Root, List<List<int>> adj)
    {
        for (int i = 1; i < adj.Count; i++)
        {
            Console.Write($"{i}: ");

            if (i == Root)
            {
                Console.WriteLine(adj[i].Count);
            }
            else
            {
                Console.WriteLine(adj[i].Count - 1);
            }
        }
    }

    static void Main(string[] args)
    {
        int N = 7;
        int Root = 1;
        List<List<int>> adj = new List<List<int>>();

        for (int i = 0; i <= N; i++)
        {
            adj.Add(new List<int>());
        }

        adj[1].AddRange(new int[] { 2, 3, 4 });
        adj[2].AddRange(new int[] { 1, 5, 6 });
        adj[4].Add(7);

        Console.WriteLine("The parents of each node are:");
        PrintParents(Root, adj, 0);

        Console.WriteLine("The children of each node are:");
        PrintChildren(Root, adj);

        Console.WriteLine("The leaf nodes of the tree are:");
        PrintLeafNodes(Root, adj);

        Console.WriteLine("The degrees of each node are:");
        PrintDegrees(Root, adj);
    }
}


Output:

DsaOutput


Heap

The Heap is a complete binary tree data structure that satisfies the heap property for every node, the value of its children either greater than or equal to its own value. There are multiple application of heap such as implementation of priority queues, where the smallest (or largest) element is always at the root of the tree.

Types of Heap

  • Min Heap
  • Max Heap

Max-Heap: In max heap the value of the root node must be the greatest among all its descendant(children's) nodes and the same thing must be done for its left and right sub-tree also.



Code Implementation:

C#
using System;
using System.Collections.Generic;

public class MaxHeap<T> where T : IComparable<T>
{
    private List<T> elements = new List<T>();

    public int Size => elements.Count;

    public bool IsEmpty => elements.Count == 0;

    public void Add(T item)
    {
        elements.Add(item);
        HeapifyUp(elements.Count - 1);
    }

    public T Peek()
    {
        if (elements.Count == 0)
            throw new InvalidOperationException("Heap is empty");
        return elements[0];
    }

    public T RemoveMax()
    {
        if (elements.Count == 0)
            throw new InvalidOperationException("Heap is empty");

        T result = elements[0];
        elements[0] = elements[elements.Count - 1];
        elements.RemoveAt(elements.Count - 1);
        HeapifyDown(0);

        return result;
    }

// HeapifyUp and HeapifyDown Oprations
    private void HeapifyUp(int index)
    {
        while (index > 0)
        {
            int parentIndex = (index - 1) / 2;
            if (elements[index].CompareTo(elements[parentIndex]) <= 0)
                break;

            Swap(index, parentIndex);
            index = parentIndex;
        }
    }

    private void HeapifyDown(int index)
    {
        while (index < elements.Count / 2)
        {
            int leftChildIndex = 2 * index + 1;
            int rightChildIndex = 2 * index + 2;
            int largerChildIndex = leftChildIndex;

            if (rightChildIndex < elements.Count && elements[rightChildIndex].CompareTo(elements[leftChildIndex]) > 0)
            {
                largerChildIndex = rightChildIndex;
            }

            if (elements[index].CompareTo(elements[largerChildIndex]) >= 0)
                break;

            Swap(index, largerChildIndex);
            index = largerChildIndex;
        }
    }
    
    // Swap two elements in the heap
    private void Swap(int index1, int index2)
    {
        T temp = elements[index1];
        elements[index1] = elements[index2];
        elements[index2] = temp;
    }
}

// Main class
class Geeks
{
    static void Main()
    {
        MaxHeap<int> maxHeap = new MaxHeap<int>();
        maxHeap.Add(3);
        maxHeap.Add(5);
        maxHeap.Add(7);
        maxHeap.Add(10);

        // Max element in the heap
        Console.WriteLine("Max element: " + maxHeap.Peek());

       // Traverse in the heap
        while (!maxHeap.IsEmpty)
        {  
            // Remove max element one by one
            Console.WriteLine(maxHeap.RemoveMax());
        }
    }
}

Output
Max element: 10
10
7
5
3


Min-Heap: In min heap the value of the root node must be the smallest among all its descendant(children's) nodes and the same for both the sides. Its left and right sub-tree also.



Code Implementation:

C#
using System;
using System.Collections.Generic;

public class MinHeap<T> where T : IComparable<T>
{
    private List<T> elements = new List<T>();

    public int Size => elements.Count;

    public bool IsEmpty => elements.Count == 0;

    public void Add(T item)
    {
        elements.Add(item);
        HeapifyUp(elements.Count - 1);
    }

    public T Peek()
    {
        if (elements.Count == 0)
            throw new InvalidOperationException("Heap is empty");
        return elements[0];
    }

    public T RemoveMin()
    {
        if (elements.Count == 0)
            throw new InvalidOperationException("Heap is empty");

        T result = elements[0];
        elements[0] = elements[elements.Count - 1];
        elements.RemoveAt(elements.Count - 1);
        HeapifyDown(0);

        return result;
    }

    private void HeapifyUp(int index)
    {
        while (index > 0)
        {
            int parentIndex = (index - 1) / 2;
            if (elements[index].CompareTo(elements[parentIndex]) >= 0)
                break;

            Swap(index, parentIndex);
            index = parentIndex;
        }
    }

    private void HeapifyDown(int index)
    {
        while (index < elements.Count / 2)
        {
            int leftChildIndex = 2 * index + 1;
            int rightChildIndex = 2 * index + 2;
            int smallerChildIndex = leftChildIndex;

            if (rightChildIndex < elements.Count && elements[rightChildIndex].CompareTo(elements[leftChildIndex]) < 0)
            {
                smallerChildIndex = rightChildIndex;
            }

            if (elements[index].CompareTo(elements[smallerChildIndex]) <= 0)
                break;

            Swap(index, smallerChildIndex);
            index = smallerChildIndex;
        }
    }

    private void Swap(int index1, int index2)
    {
        T temp = elements[index1];
        elements[index1] = elements[index2];
        elements[index2] = temp;
    }
}

class Geeks
{
    static void Main()
    {
        MinHeap<int> minHeap = new MinHeap<int>();
        minHeap.Add(10);
        minHeap.Add(5);
        minHeap.Add(20);
        minHeap.Add(1);

        // Min element in the heap
        Console.WriteLine("Min element: " + minHeap.Peek());

        // Traverse in the heap
        while (!minHeap.IsEmpty)
        {
            // Remove min element one by one
            Console.WriteLine(minHeap.RemoveMin());
        }
    }
}

Output
Min element: 1
1
5
10
20


Graph

Graph is a non-linear data structure and it contains two constraints, that are vertices and edges. There are multiple applications of graph like it is used in maps, connecting data on the social media sites and machine learning algorithms. We define the graph using in the form of nodes and then for showing the connection we use either the matrix or list.

  • Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertices or nodes. Every node/vertex can be labelled or unlabelled.
  • Edges: Edges are drawn or used to connect two nodes of the graph. It can bean ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be labelled/unlabelled.
Introduction-to-Graphs

Types of Graph Representation

  • Adjacency Matrix
  • Adjacency List

Adjacency Matrix

An adjacency matrix is used to represents a graph in the form of a matrix where we use the boolean (0’s and 1’s) values. If there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having dimension n x n.

  • If there is an edge from vertex i to j, mark adjMat[i][j] as 1.
  • If there is no edge from vertex i to j, mark adjMat[i][j] as 0.
GraphMatrixRepresentation


Code Implementation

C#
using System;

public class Geeks
{
    // Add an edge between two vertices
    public static void AddEdge(int[,] mat, int i, int j)
    {
        mat[i, j] = 1; // Since the graph is 
        mat[j, i] = 1; // undirected
    }

    // Display the adjacency matrix
    public static void DisplayMatrix(int[,] mat)
    {
        int V = mat.GetLength(0); 
        for (int i = 0; i < V; i++)
        {
            for (int j = 0; j < V; j++)
            {
                Console.Write(mat[i, j] + " ");
            }
            Console.WriteLine(); 
        }
    }

    // Main method to run the program
    public static void Main(string[] args)
    {
        int V = 4; // Number of vertices
        int[,] mat = new int[V, V]; // Initialize matrix

        // Add edges to the graph
        AddEdge(mat, 0, 1);
        AddEdge(mat, 0, 2);
        AddEdge(mat, 1, 2);
        AddEdge(mat, 2, 3);

        // Optionally, initialize matrix directly
        /*
        int[,] mat = new int[,]
        {
            { 0, 1, 0, 0 },
            { 1, 0, 1, 0 },
            { 0, 1, 0, 1 },
            { 0, 0, 1, 0 }
        };
        */

        // Display adjacency matrix
        Console.WriteLine("Adjacency Matrix:");
        DisplayMatrix(mat);
    }
}

Output
Adjacency Matrix:
0 1 1 0 
1 0 1 0 
1 1 0 1 
0 0 1 0 


Adjacency List

An array of Lists is used to store edges between two vertices. The size of the array is equal to the number of vertices (i.e. lists, n). Each index in this array represents a specific vertex in the graph. The entry at the index i of the array contains a linked list containing the vertices that are adjacent to vertex i.

Let’s assume there are n vertices in the graph So, create an array of lists of size n as adjList[n].

  • adjList[0] will have all the nodes which are connected (neighbour) to vertex 0.
  • adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and so on.
GraphMatrixRepresentation


Code Implementation

C#
using System;
using System.Collections.Generic;

public class Geeks
{
    // Method to add an edge between two vertices
    public static void AddEdge(List<List<int>> adj, int i, int j)
    {
         // Undirected
        adj[i].Add(j);
        adj[j].Add(i);
    }

    // Method to display the adjacency list
    public static void DisplayAdjList(List<List<int>> adj)
    {
        for (int i = 0; i < adj.Count; i++)
        {
            Console.Write($"{i}: "); // Print the vertex
            foreach (int j in adj[i])
            {
                Console.Write($"{j} "); // Print its adjacent
            }
            Console.WriteLine(); 
        }
    }

    // Main method
    public static void Main(string[] args)
    {
        // Create a graph with 4 vertices and no edges
        int V = 4;
        List<List<int>> adj = new List<List<int>>(V); 
        for (int i = 0; i < V; i++)
            adj.Add(new List<int>());

        // Now add edges one by one
        AddEdge(adj, 0, 1);
        AddEdge(adj, 0, 2);
        AddEdge(adj, 1, 2);
        AddEdge(adj, 2, 3);

        Console.WriteLine("Adjacency List Representation:");
        DisplayAdjList(adj);
    }
}

Output
Adjacency List Representation:
0: 1 2 
1: 0 2 
2: 0 1 3 
3: 2 

Next Article

Similar Reads