C Adjacency List 🎯

beginner
14 min

C Adjacency List 🎯

Welcome to our deep dive into C Adjacency List! This tutorial is perfect for both beginners and intermediates looking to understand and apply this essential data structure in their programming journey.

What is an Adjacency List? 📝

An adjacency list is a collection of data structures used to represent a finite graph. Each graph vertex (node) links to a list of its neighboring vertices, making it easy to traverse and understand the graph's structure.

In C, we use arrays and linked lists to implement an adjacency list. Let's get started with understanding the basics!

Adjacency List Representation 💡

We will represent the adjacency list using an array of linked lists, where each element in the array corresponds to a vertex. If there's an edge between vertices i and j, we store j in the ith element's linked list.

Let's consider a simple example with the following graph:

A -> B A -> C B -> D C -> D

The adjacency list representation for this graph would look like:

c
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; int main() { Node* vertex_A = malloc(sizeof(Node)); Node* vertex_B = malloc(sizeof(Node)); Node* vertex_C = malloc(sizeof(Node)); Node* vertex_D = malloc(sizeof(Node)); vertex_A->data = 'A'; vertex_B->data = 'B'; vertex_C->data = 'C'; vertex_D->data = 'D'; vertex_A->next = vertex_B; vertex_A->next->next = vertex_C; vertex_C->next = vertex_D; vertex_B->next = vertex_D; // Print the adjacency list Node* current = vertex_A; while (current != NULL) { printf("%c -> ", current->data); current = current->next; } return 0; }

When you run this code, it will output:

A -> B -> C -> D

This output represents the adjacency list for our simple graph.

Traversing the Adjacency List 💡

To traverse the adjacency list, we'll start from a vertex and visit its neighbors one by one, following the links in the linked list. Let's modify the previous example to output the neighbors of each vertex:

c
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; void printNeighbors(Node* vertex) { printf("%c's neighbors are: ", vertex->data); Node* current = vertex; while (current != NULL) { printf("%c ", current->data); current = current->next; } printf("\n"); } int main() { Node* vertex_A = malloc(sizeof(Node)); Node* vertex_B = malloc(sizeof(Node)); Node* vertex_C = malloc(sizeof(Node)); Node* vertex_D = malloc(sizeof(Node)); vertex_A->data = 'A'; vertex_B->data = 'B'; vertex_C->data = 'C'; vertex_D->data = 'D'; vertex_A->next = vertex_B; vertex_A->next->next = vertex_C; vertex_C->next = vertex_D; vertex_B->next = vertex_D; printNeighbors(vertex_A); printNeighbors(vertex_B); printNeighbors(vertex_C); printNeighbors(vertex_D); return 0; }

When you run this code, it will output:

A's neighbors are: B C B's neighbors are: A D C's neighbors are: A D D's neighbors are: B C

This output shows the neighbors of each vertex in our graph.

Quiz Time! 💡

Quick Quiz
Question 1 of 1

What is an adjacency list in the context of graphs?

Quick Quiz
Question 1 of 1

How do we represent an adjacency list in C?

Wrapping Up ✅

You now have a solid understanding of what an adjacency list is, how to represent it in C, and how to traverse it. This data structure is essential for representing graphs and is used extensively in various real-world applications.

Practice making your own graphs and implementing their adjacency lists in C to reinforce your understanding. Happy coding! 💻

Stay tuned for more in-depth lessons on C programming at CodeYourCraft! 🎉