#include <stdio.h> // Use printf
#define MaxNode 6 // Define Max Node of Graph
int graph[MaxNode][MaxNode] = {
{0,1,1,1,0,0},
{1,0,1,0,1,0},
{1,1,0,0,0,0},
{1,0,0,0,1,1},
{0,1,0,1,0,0},
{0,0,0,1,0,0}
}; // Declare array and keep data of graph
char NodeName[MaxNode] = {'A','B','C','D','E','F'};
void DispArray2D() // Display value in 2D Array
{
int i, j; // i = Row, j = Column
printf(" ");
for (j = 0; j < MaxNode; j++) // Display column names
printf("%c ", NodeName[j]);
printf("\n"); // Line feed
for (i = 0; i < MaxNode; i++) // Row loop
{
printf("%c ", NodeName[i]); // Display row name
for (j = 0; j < MaxNode; j++) // Column loop
printf("%d ", graph[i][j]); // Display adjacency values
printf("\n");
}
}
void DispSetOfVertex() // Display set of Vertex
{
int i;
printf("\nSet of Vertex = {");
for (i = 0; i < MaxNode; i++)
{
printf("%c", NodeName[i]); // Display each node name
if(i != MaxNode - 1)
printf(", ");
}
printf("}\n");
}
void DispSetOfEdge() // Display set of Edge
{
int i, j;
printf("\nSet of Edge = {");
for (i = 0; i < MaxNode; i++) // Row loop
{
for (j = i + 1; j < MaxNode; j++) // Only consider i < j to avoid duplicate edges
{
if (graph[i][j] == 1)
printf("(%c,%c), ", NodeName[i], NodeName[j]);
}
}
printf("\b\b }\n"); // Remove last comma and space
}
int main()
{
printf("GRAPH (ADJACENCY MATRIX REPRESENTATION METHOD)\n");
printf("===============================================\n");
DispArray2D();
DispSetOfVertex();
DispSetOfEdge();
getchar(); // Use getchar() instead of getch() for compatibility
return 0;
} // End Main