Question: THIS IS THE CODE WHERE WE SHOULD ADD CREATE PATHS AND EDGES (everything in the pictures below) #include #include // Define the maximum number of
THIS IS THE CODE WHERE WE SHOULD ADD CREATE PATHS AND EDGES
(everything in the pictures below)
#include
#include
// Define the maximum number of vertices in the graph
#define N 6
// Data structure to store a graph object
struct Graph
{
// An array of pointers to Node to represent an adjacency list
struct Node* head[N];
};
// Data structure to store adjacency list nodes of the graph
struct Node
{
int dest;
struct Node* next;
};
// Data structure to store a graph edge
struct Edge {
int src, dest;
};
// Function to create an adjacency list from specified edges
struct Graph* createGraph(struct Edge edges[], int n)
{
// allocate storage for the graph data structure
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
// initialize head pointer for all vertices
for (int i = 0; i
graph->head[i] = NULL;
}
// add edges to the directed graph one by one
for (int i = 0; i
Graph Representation in C Programming Language In the graph's adjacency list representation, each vertex in the graph is associated with collection of its neighboring vertices or edges, i.e. every vertex stores a list of adjacent vertices. For example, for the above graph, below is its adjacency list pictorial representation: 0 0 > 1 1 1 N 2 2 0 1. 3 2 A 5 5 Now, let's take a look at the C code, representing this graph. Output: (@-> 1) (1 -> 2) (2 -> 1) (2 -> 0) (3 -> 2) (4 -> 5) (5 -> 4) As evident from the code, in a directed graph, we only create an edge from src to dest in the adjacency list. Now, if the graph is undirected, we also need to create an edge from dest to src in the adjacency list. Try to add it to your code. This is what the output should look like: Output: (@=> 1) (1 -> 2) (2 -> 1) (1 -> 0) (2 -> ) (2 -> 1) (@=> 2) (1 -> 2) (2 -> 3) (3 -> 2) (4 -> 5) (5 -> 4) (4 -> 5) (5 -> 4) A In a weighted graph, each edge will have weight (or cost) associated with it, as shown below: 6 3 10 Try adding weight values to each one of the edges. This is what the output should look like for the weighted directed graph: Output: 2 -> (5) 0 -> 1 (6) 1 -> 2 (7) 2 -> 1 (4) 3 -> 2 (10) 4 -> 5 (1) 5 -> 4 (3) For weighted undirected graphs, you should create a path from dest to src as well in the adjacency list. 7 w {
// get the source and destination vertex
int src = edges[i].src;
int dest = edges[i].dest;
// allocate a new node of adjacency list from src to dest
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->dest = dest;
// point new node to the current head
newNode->next = graph->head[src];
// point head pointer to the new node
graph->head[src] = newNode;
}
return graph;
}
// Function to print adjacency list representation of a graph
void printGraph(struct Graph* graph)
{
for (int i = 0; i
{
// print current vertex and all its neighbors
struct Node* ptr = graph->head[i];
while (ptr != NULL)
{
printf("(%d > %d)\t", i, ptr->dest);
ptr = ptr->next;
}
printf(" ");
}
}
// Directed graph implementation in C
int main(void)
{
// input array containing edges of the graph (as per the above diagram)
// (x, y) pair in the array represents an edge from x to y
struct Edge edges[] =
{
{0, 1}, {1, 2}, {2, 0}, {2, 1}, {3, 2}, {4, 5}, {5, 4}
};
// calculate the total number of edges
int n = sizeof(edges)/sizeof(edges[0]);
// construct a graph from the given edges
struct Graph *graph = createGraph(edges, n);
// Function to print adjacency list representation of a graph
printGraph(graph);
return 0;
}


Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
