# What Is Graph in Data Structure Using C?

//

Larry Thompson

What Is Graph in Data Structure Using C?

In the field of data structures, a graph is a non-linear data structure that consists of a set of nodes (also known as vertices) and a set of edges connecting these nodes. Graphs are widely used to represent connections or relationships between different entities, making them an essential tool in various applications such as social networks, network routing, and data visualization.

## Graph Terminology

Before diving into the implementation of graphs in C, let’s familiarize ourselves with some common graph terminology:

• Node: A node, also known as a vertex, represents an entity in a graph.
• Edge: An edge is a connection between two nodes that signifies their relationship.
• Directed Graph: In a directed graph, edges have a specific direction and can only be traversed in that direction.
• Undirected Graph: In an undirected graph, edges have no specific direction and can be traversed in both directions.
• Cyclic Graph: A cyclic graph contains at least one path that forms a cycle (a sequence of nodes that starts and ends on the same node).
• Acyclic Graph: An acyclic graph does not contain any cycles.

## Graph Representation in C

There are several ways to represent graphs in C. Two commonly used methods are the adjacency matrix representation and the adjacency list representation.

The adjacency matrix representation uses a 2D array to store the connections between nodes. The rows and columns of the matrix represent the nodes, and the values in the matrix indicate whether there is an edge between two nodes or not.

Here’s an example of how an adjacency matrix can be represented in C:

```#define MAX_NODES 100

typedef struct {
int matrix[MAX_NODES][MAX_NODES];
int numNodes;
} Graph;

void initializeGraph(Graph* graph, int numNodes) {
int i, j;

graph->numNodes = numNodes;

for (i = 0; i < numNodes; i++) {
for (j = 0; j < numNodes; j++) {
graph->matrix[i][j] = 0;
}
}
}

void addEdge(Graph* graph, int source, int destination) {
if (source >= 0 && source < graph->numNodes && destination >= 0 && destination < graph->numNodes) {
graph->matrix[source][destination] = 1;
// For undirected graphs, uncomment the following line
// graph->matrix[destination][source] = 1;
}
}
```

The adjacency list representation uses an array of linked lists to store the connections between nodes. Each node has a list associated with it that contains all its adjacent nodes.

Here’s an example of how an adjacency list can be represented in C:

```typedef struct Node {
int value;
struct Node* next;
} Node;

typedef struct {
Node* array[MAX_NODES];
int numNodes;
} Graph;

void initializeGraph(Graph* graph, int numNodes) {
int i;

graph->numNodes = numNodes;

for (i = 0; i < numNodes; i++) {
graph->array[i] = NULL;
}
}

void addEdge(Graph* graph, int source, int destination) {
if (source >= 0 && source < graph->numNodes && destination >= 0 && destination < graph->numNodes) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = destination;
newNode->next = graph->array[source];
graph->array[source] = newNode;

// For undirected graphs, uncomment the following lines
// newNode = (Node*)malloc(sizeof(Node));
// newNode->value = source;
// newNode->next = graph->array[destination];
// graph->array[destination] = newNode;
}
}
```

## Conclusion

Graphs are a fundamental data structure that allows us to represent and analyze relationships between entities. In this article, we learned about the basics of graphs and explored two common ways to represent them in C: the adjacency matrix representation and the adjacency list representation.

Understanding graphs and their implementations is essential for solving various problems efficiently.

By incorporating these HTML styling elements such as bold text, underlined text,

unordered lists