0% found this document useful (0 votes)
11 views

Program No10

Uploaded by

Mohd Kaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Program No10

Uploaded by

Mohd Kaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Mohd Kaleem (22DCS067)

Program No. – 10

➢ Write a program in C check whether a given graph is directed or not.


Algorithm:
1. Input:
• adjacencyMatrix: A 2D array representing the adjacency matrix of the graph.
This matrix stores 1 if there's an edge from row (source) to column
(destination), and 0 otherwise.
• numVertices: The number of vertices in the graph.
2. Check for self-loops:
• Iterate through the diagonal elements of the adjacency matrix
(adjacencyMatrix[i][i]).
• If any diagonal element is 1, it indicates a self-loop (edge from a node to
itself). In this case, the graph is directed, and the algorithm returns 1.
3. Check for directed edges:
• Iterate through each row (i) of the adjacency matrix (representing source
nodes).
• For each row, iterate through each column (j) of the matrix (representing
destination nodes).
• Check if there's an edge from source i to destination j by checking
adjacencyMatrix[i][j].
o If there's an edge (adjacencyMatrix[i][j] == 1), but there's no
corresponding edge in the opposite direction (adjacencyMatrix[j][i] !=
1), it indicates a directed edge.
o If a directed edge is found, the algorithm returns 1 (directed graph).
4. Undirected graph:
• If the loop completes without finding self-loops or directed edges, it means all
edges (if any) exist in both directions. This indicates an undirected graph, and
the algorithm returns 0

Program:
//This program is belonging to Mohd Kaleem
#include <stdio.h>

#define MAX_VERTICES 50

int checkGraphDirection(int
adjacencyMatrix[MAX_VERTICES][MAX_VERTICES], int numVertices) {
Mohd Kaleem (22DCS067)

int i, j;

// Check for self-loops


for (i = 0; i < numVertices; i++) {
if (adjacencyMatrix[i][i] == 1) {
return 1; // Directed graph
}
}

// Check for directed edges


for (i = 0; i < numVertices; i++) {
for (j = 0; j < numVertices; j++) {
if (adjacencyMatrix[i][j] && !adjacencyMatrix[j][i]) {
return 1; // Directed graph
}
}
}

return 0; // Undirected graph


}

int main() {
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices, i, j;

printf("Enter the number of vertices : ");


if (scanf("%d", &numVertices) != 1 || numVertices <= 0 || numVertices >
MAX_VERTICES) {
fprintf(stderr, "Error: Invalid number of vertices.\n");
return 1;
}

printf("Enter the adjacency matrix:\n");


for (i = 0; i < numVertices; i++) {
for (j = 0; j < numVertices; j++) {
if (scanf("%d", &adjacencyMatrix[i][j]) != 1 || (adjacencyMatrix[i][j] != 0 &&
adjacencyMatrix[i][j] != 1)) {
fprintf(stderr, "Error: Invalid input value. Please enter only 0 or 1.\n");
return 1;
}
}
}

if (checkGraphDirection(adjacencyMatrix, numVertices)) {
printf("The graph is directed.\n");
} else {
Mohd Kaleem (22DCS067)

printf("The graph is undirected.\n");


}

return 0;
}

Output:
Enter the number of vertices : 3
Enter the adjacency matrix:

011
101
110
The graph is undirected.

Test Case:
Test
Case Description Expected Behavior

ERROR!
1. Enter the number of vertices : 56 Error: Invalid number of vertices.

Enter the number of vertices : 3


Enter the adjacency matrix:
Error: Invalid input value. Please enter only 0 or
2. 121 1.

ERROR!
3. Enter the number of vertices : a Error: Invalid number of vertices.

ERROR!
Enter the adjacency matrix:
Error: Invalid input value. Please enter only 0 or
4. Enter the number of vertices : 1.2 1.

You might also like