w3resource

C Program to add directed edge in Graph


3. Directed Edge Insertion Challenges

Write a C function to add a directed edge between two vertices in a graph.

Sample Solution:

C Code:

#include <stdio.h>

#define MAX_VERTICES 100

// Function to add a directed edge from 'start' to 'end'
void addDirectedEdge(int graph[MAX_VERTICES][MAX_VERTICES], int start, int end, int vertices) {
    if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
        printf("Invalid vertices.\n");
        return;
    }

    graph[start][end] = 1;
}

// Function to display the adjacency matrix
void displayGraph(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
    printf("Adjacency Matrix:\n");
    for (int i = 0; i < vertices; i++) {
        for (int j = 0; j < vertices; j++) {
            printf("%d ", graph[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int vertices, edges;

    // Input the number of vertices
    printf("Enter the number of vertices: ");
    scanf("%d", &vertices);

    if (vertices <= 0 || vertices > MAX_VERTICES) {
        printf("Invalid number of vertices. Exiting...\n");
        return 1;
    }

    int graph[MAX_VERTICES][MAX_VERTICES] = {0}; // Initialize the adjacency matrix with zeros

    // Input the number of edges
    printf("Enter the number of edges: ");
    scanf("%d", &edges);

    if (edges < 0 || edges > vertices * (vertices - 1)) {
        printf("Invalid number of edges. Exiting...\n");
        return 1;
    }

    // Input edges and construct the adjacency matrix
    for (int i = 0; i < edges; i++) {
        int start, end;
        printf("Enter edge %d (start end): ", i + 1);
        scanf("%d %d", &start, &end);

        // Validate input vertices
        if (start < 0 || start >= vertices || end < 0 || end >= vertices) {
            printf("Invalid vertices. Try again.\n");
            i--;
            continue;
        }

        addDirectedEdge(graph, start, end, vertices);
    }

    // Display the adjacency matrix
    displayGraph(graph, vertices);

    return 0;
}

Output:

Enter the number of vertices: 5
Enter the number of edges: 6
Enter edge 1 (start end): 0 1
Enter edge 2 (start end): 1 2
Enter edge 3 (start end): 2 3
Enter edge 4 (start end): 3 4
Enter edge 5 (start end): 4 0
Enter edge 6 (start end): 2 4
Adjacency Matrix:
0 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 1
1 0 0 0 0

Explanation:

The above C program represents a directed graph using an adjacency matrix. Here's a brief explanation:

  • Function addDirectedEdge:
    • Take a graph represented by an adjacency matrix, the start and end vertices of a directed edge, and the total number of vertices.
    • Checks if the provided vertices are valid.
    • If valid, it adds a directed edge from the 'start' vertex to the 'end' vertex by updating the corresponding entry in the adjacency matrix.
  • Function displayGraph:
    • Takes a graph represented by an adjacency matrix and the total number of vertices.
    • Displays the adjacency matrix on the console.
  • Main Function:
    • Reads the number of vertices and edges from the user.
    • Creates an adjacency matrix initialized with zeros.
    • Takes input for directed edges, validates the vertices, and adds them to the graph using the "addDirectedEdge()" function.
    • Displays the resulting adjacency matrix.

Flowchart:

Flowchart: C Program to add directed edge in Graph.


Flowchart: C Program to add directed edge in Graph.


For more Practice: Solve these Related Problems:

  • Write a C program to add a directed edge between two vertices in an adjacency matrix and check for duplicate edge insertions.
  • Write a C program to add a directed edge while preventing self-loops, updating the matrix accordingly.
  • Write a C program to implement a function that adds a directed edge with a specified weight and validates the weight range.
  • Write a C program to add a directed edge between two vertices and update the corresponding in-degree and out-degree counters.

Go to:


PREV : Add Vertex to Graph Extended Challenges.
NEXT : Depth-First Search (DFS) Extended Challenges.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.