fork download
  1. #include <stdio.h> // Use printf
  2. #define MaxNode 6 // Define Max Node of Graph
  3. int graph[MaxNode][MaxNode] = {
  4. {0,1,1,1,0,0},
  5. {1,0,1,0,1,0},
  6. {1,1,0,0,0,0},
  7. {1,0,0,0,1,1},
  8. {0,1,0,1,0,0},
  9. {0,0,0,1,0,0}
  10. }; // Declare array and keep data of graph
  11. char NodeName[MaxNode] = {'A','B','C','D','E','F'};
  12. void DispArray2D() // Display value in 2D Array
  13. {
  14. int i, j; // i = Row, j = Column
  15. printf(" ");
  16. for (j = 0; j < MaxNode; j++) // Display column names
  17. printf("%c ", NodeName[j]);
  18. printf("\n"); // Line feed
  19. for (i = 0; i < MaxNode; i++) // Row loop
  20. {
  21. printf("%c ", NodeName[i]); // Display row name
  22. for (j = 0; j < MaxNode; j++) // Column loop
  23. printf("%d ", graph[i][j]); // Display adjacency values
  24. printf("\n");
  25. }
  26. }
  27. void DispSetOfVertex() // Display set of Vertex
  28. {
  29. int i;
  30. printf("\nSet of Vertex = {");
  31. for (i = 0; i < MaxNode; i++)
  32. {
  33. printf("%c", NodeName[i]); // Display each node name
  34. if(i != MaxNode - 1)
  35. printf(", ");
  36. }
  37. printf("}\n");
  38. }
  39. void DispSetOfEdge() // Display set of Edge
  40. {
  41. int i, j;
  42. printf("\nSet of Edge = {");
  43. for (i = 0; i < MaxNode; i++) // Row loop
  44. {
  45. for (j = i + 1; j < MaxNode; j++) // Only consider i < j to avoid duplicate edges
  46. {
  47. if (graph[i][j] == 1)
  48. printf("(%c,%c), ", NodeName[i], NodeName[j]);
  49. }
  50. }
  51. printf("\b\b }\n"); // Remove last comma and space
  52. }
  53. int main()
  54. {
  55. printf("GRAPH (ADJACENCY MATRIX REPRESENTATION METHOD)\n");
  56. printf("===============================================\n");
  57. DispArray2D();
  58. DispSetOfVertex();
  59. DispSetOfEdge();
  60. getchar(); // Use getchar() instead of getch() for compatibility
  61. return 0;
  62. } // End Main
Success #stdin #stdout 0.02s 25708KB
stdin
Standard input is empty
stdout
#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